EdColeman commented on code in PR #2751:
URL: https://github.com/apache/accumulo/pull/2751#discussion_r925555167


##########
server/base/src/test/java/org/apache/accumulo/server/conf/util/ZooInfoViewerTest.java:
##########
@@ -0,0 +1,437 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.accumulo.server.conf.util;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.apache.accumulo.core.Constants.ZINSTANCES;
+import static org.apache.accumulo.core.Constants.ZNAMESPACES;
+import static org.apache.accumulo.core.Constants.ZNAMESPACE_NAME;
+import static org.apache.accumulo.core.Constants.ZROOT;
+import static org.apache.accumulo.core.Constants.ZTABLES;
+import static org.apache.accumulo.core.Constants.ZTABLE_NAME;
+import static org.apache.accumulo.core.Constants.ZTABLE_NAMESPACE;
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.accumulo.core.data.InstanceId;
+import org.apache.accumulo.core.data.NamespaceId;
+import org.apache.accumulo.core.data.TableId;
+import org.apache.accumulo.fate.zookeeper.ZooReader;
+import org.apache.accumulo.fate.zookeeper.ZooUtil;
+import org.apache.accumulo.server.conf.codec.VersionedPropCodec;
+import org.apache.accumulo.server.conf.codec.VersionedProperties;
+import org.apache.accumulo.server.conf.store.NamespacePropKey;
+import org.apache.accumulo.server.conf.store.SystemPropKey;
+import org.apache.accumulo.server.conf.store.TablePropKey;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.data.Stat;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+
+public class ZooInfoViewerTest {
+
+  private final Logger log = LoggerFactory.getLogger(ZooInfoViewerTest.class);
+
+  private final VersionedPropCodec propCodec = VersionedPropCodec.getDefault();
+
+  @Test
+  public void simpleOutput() {
+    // StringWriter writer = new StringWriter();
+  }
+
+  @Test
+  public void optionsAllDefault() {
+    ZooInfoViewer.Opts opts = new ZooInfoViewer.Opts();
+    assertTrue(opts.printAllProps());
+    assertTrue(opts.printSysProps());
+    assertTrue(opts.printNamespaceProps());
+    assertTrue(opts.printTableProps());
+  }
+
+  @Test
+  public void onlySys() {
+    ZooInfoViewer.Opts opts = new ZooInfoViewer.Opts();
+    opts.parseArgs(ZooInfoViewer.class.getName(), new String[] {"--system"});
+
+    assertFalse(opts.printAllProps());
+    assertTrue(opts.printSysProps());
+    assertFalse(opts.printNamespaceProps());
+    assertFalse(opts.printTableProps());
+  }
+
+  @Test
+  public void onlyNamespaces() {
+    ZooInfoViewer.Opts opts = new ZooInfoViewer.Opts();
+    opts.parseArgs(ZooInfoViewer.class.getName(), new String[] {"-ns", "ns1", 
"ns2"});
+
+    assertFalse(opts.printAllProps());
+    assertFalse(opts.printSysProps());
+    assertTrue(opts.printNamespaceProps());
+    assertEquals(2, opts.getNamespaces().size());
+    assertFalse(opts.printTableProps());
+    assertEquals(0, opts.getTables().size());
+  }
+
+  @Test
+  public void allLongOpts() {
+    ZooInfoViewer.Opts opts = new ZooInfoViewer.Opts();
+    opts.parseArgs(ZooInfoViewer.class.getName(),
+        new String[] {"--system", "--namespaces", "ns1", "ns2", "--tables", 
"tb1", "tbl2"});
+
+    log.debug("namespaces: {}", opts.getNamespaces());
+    log.debug("tables: {}", opts.getTables());
+
+    assertFalse(opts.printAllProps());
+    assertTrue(opts.printSysProps());
+    assertTrue(opts.printNamespaceProps());
+    assertTrue(opts.printTableProps());
+    assertEquals(2, opts.getNamespaces().size());
+    assertEquals(2, opts.getTables().size());
+  }
+
+  @Test
+  public void allOpts() {
+    ZooInfoViewer.Opts opts = new ZooInfoViewer.Opts();
+    opts.parseArgs(ZooInfoViewer.class.getName(), new String[] {"-t", "tb1", 
"tbl2"});
+
+    assertFalse(opts.printAllProps());
+    assertFalse(opts.printSysProps());
+    assertFalse(opts.printNamespaceProps());
+    assertEquals(0, opts.getNamespaces().size());
+    assertTrue(opts.printTableProps());
+    assertEquals(2, opts.getTables().size());
+  }
+
+  @Test
+  public void fetchInstancesFromZk() throws Exception {
+
+    String instAName = "INST_A";
+    InstanceId instA = InstanceId.of(UUID.randomUUID());
+    String instBName = "INST_B";
+    InstanceId instB = InstanceId.of(UUID.randomUUID());
+
+    ZooReader zooReader = createMock(ZooReader.class);
+    String namePath = ZROOT + ZINSTANCES;
+    expect(zooReader.getChildren(eq(namePath))).andReturn(List.of(instAName, 
instBName)).once();
+    expect(zooReader.getData(eq(namePath + "/" + instAName)))
+        .andReturn(instA.canonical().getBytes(UTF_8)).once();
+    expect(zooReader.getData(eq(namePath + "/" + instBName)))
+        .andReturn(instB.canonical().getBytes(UTF_8)).once();
+    replay(zooReader);
+
+    ZooInfoViewer viewer = new ZooInfoViewer();
+    Map<String,InstanceId> instanceMap = viewer.readInstancesFromZk(zooReader);
+
+    log.trace("id map returned: {}", instanceMap);
+    assertEquals(instA, instanceMap.get(instAName));
+    assertEquals(instB, instanceMap.get(instBName));
+    verify(zooReader);
+  }
+
+  /**
+   * Expect that instance id passed is returned, instance name and zooReader 
are ignored.
+   */
+  @Test
+  public void instanceIdOption() throws Exception {
+
+    String instAName = "INST_A";
+    InstanceId instA = InstanceId.of(UUID.randomUUID());
+    String instBName = "INST_B";
+    InstanceId instB = InstanceId.of(UUID.randomUUID());
+
+    ZooReader zooReader = createMock(ZooReader.class);
+    String namePath = ZROOT + ZINSTANCES;
+    expect(zooReader.getChildren(eq(namePath))).andReturn(List.of(instAName, 
instBName)).once();
+    expect(zooReader.getData(eq(namePath + "/" + instAName)))
+        .andReturn(instA.canonical().getBytes(UTF_8)).once();
+    expect(zooReader.getData(eq(namePath + "/" + instBName)))
+        .andReturn(instB.canonical().getBytes(UTF_8)).once();
+    replay(zooReader);
+
+    ZooInfoViewer.Opts opts = new ZooInfoViewer.Opts();
+    opts.parseArgs(ZooInfoViewer.class.getName(), new String[] 
{"--instanceName", instBName});
+
+    ZooInfoViewer viewer = new ZooInfoViewer();
+    InstanceId found = viewer.getInstanceId(zooReader, opts);
+
+    assertEquals(instB, found);
+
+    verify(zooReader);
+  }
+
+  /**
+   *
+   */
+  @Test
+  public void instanceNameTest() {
+    String uuid = UUID.randomUUID().toString();
+    ZooReader zooReader = createMock(ZooReader.class);
+    ZooInfoViewer.Opts opts = new ZooInfoViewer.Opts();
+    opts.parseArgs(ZooInfoViewer.class.getName(),
+        new String[] {"--instanceId", uuid, "--instanceName", "foo"});
+    replay(zooReader);
+
+    ZooInfoViewer viewer = new ZooInfoViewer();
+    InstanceId found = viewer.getInstanceId(zooReader, opts);
+
+    assertEquals(InstanceId.of(uuid), found);
+
+    verify(zooReader);
+  }
+
+  @Test
+  public void instanceIdOutputTest() throws Exception {
+    String uuid = UUID.randomUUID().toString();
+
+    ZooReader zooReader = createMock(ZooReader.class);
+    var instanceName = "test";
+    expect(zooReader.getChildren(eq(ZROOT + 
ZINSTANCES))).andReturn(List.of(instanceName)).once();
+    expect(zooReader.getData(eq(ZROOT + ZINSTANCES + "/" + instanceName)))
+        .andReturn(uuid.getBytes(UTF_8)).once();
+    replay(zooReader);
+
+    String testFileName = "./target/zoo-info-viewer-" + 
System.currentTimeMillis() + ".txt";
+
+    ZooInfoViewer.Opts opts = new ZooInfoViewer.Opts();
+    opts.parseArgs(ZooInfoViewer.class.getName(),
+        new String[] {"--instanceId", uuid, "--print-instances", "--outfile", 
testFileName});
+
+    ZooInfoViewer viewer = new ZooInfoViewer();
+    // InstanceId found = viewer.getInstanceId(zooReader, opts);
+    viewer.generateReport(InstanceId.of(uuid), opts, zooReader);
+    // assertEquals(InstanceId.of(uuid), found);
+
+    verify(zooReader);
+
+    String line;
+    try (BufferedReader in = new BufferedReader(new FileReader(testFileName, 
UTF_8))) {
+      boolean found = false;
+      while ((line = in.readLine()) != null) {
+        if (line.contains("=")) {
+          String trimmed = line.trim();
+          found = trimmed.startsWith(instanceName) && trimmed.endsWith(uuid);
+          break;
+        }
+      }
+      assertTrue(found, "expected instance name, instance id not found");
+
+    }
+  }
+
+  @Test
+  public void instanceNameOutputTest() throws Exception {
+    String uuid = UUID.randomUUID().toString();
+
+    ZooReader zooReader = createMock(ZooReader.class);
+    var instanceName = "test";
+    expect(zooReader.getChildren(eq(ZROOT + 
ZINSTANCES))).andReturn(List.of(instanceName)).once();
+    expect(zooReader.getData(eq(ZROOT + ZINSTANCES + "/" + instanceName)))
+        .andReturn(uuid.getBytes(UTF_8)).once();
+    replay(zooReader);
+
+    String testFileName = "./target/zoo-info-viewer-" + 
System.currentTimeMillis() + ".txt";
+
+    ZooInfoViewer.Opts opts = new ZooInfoViewer.Opts();
+    opts.parseArgs(ZooInfoViewer.class.getName(), new String[] 
{"--instanceName", instanceName,
+        "--print-instances", "--outfile", testFileName});
+
+    ZooInfoViewer viewer = new ZooInfoViewer();
+    // InstanceId found = viewer.getInstanceId(zooReader, opts);
+    viewer.generateReport(InstanceId.of(uuid), opts, zooReader);
+    // assertEquals(InstanceId.of(uuid), found);
+
+    verify(zooReader);
+
+    String line;
+    try (BufferedReader in = new BufferedReader(new FileReader(testFileName, 
UTF_8))) {
+      boolean found = false;
+      while ((line = in.readLine()) != null) {
+        if (line.contains("=")) {
+          String trimmed = line.trim();
+          found = trimmed.startsWith(instanceName) && trimmed.endsWith(uuid);
+          break;
+        }
+      }
+      assertTrue(found, "expected instance name, instance id not found");
+    }
+  }
+
+  @SuppressFBWarnings(value = "CRLF_INJECTION_LOGS",
+      justification = "test output of generated output")
+  @Test
+  public void propTest() throws Exception {
+    String uuid = UUID.randomUUID().toString();
+    InstanceId iid = InstanceId.of(uuid);
+
+    ZooReader zooReader = createMock(ZooReader.class);
+    var instanceName = "test";
+    expect(zooReader.getChildren(eq(ZROOT + 
ZINSTANCES))).andReturn(List.of(instanceName))
+        .anyTimes();
+    expect(zooReader.getData(eq(ZROOT + ZINSTANCES + "/" + instanceName)))
+        .andReturn(uuid.getBytes(UTF_8)).anyTimes();
+
+    var sysPropBytes = propCodec
+        .toBytes(new VersionedProperties(123, Instant.now(), Map.of("s1", 
"sv1", "s2", "sv2")));
+    expect(zooReader.getData(eq(SystemPropKey.of(iid).getNodePath()), 
anyObject(Watcher.class),
+        anyObject(Stat.class))).andReturn(sysPropBytes).anyTimes();
+
+    var nsBasePath = ZooUtil.getRoot(iid) + ZNAMESPACES;
+    
expect(zooReader.getChildren(nsBasePath)).andReturn(List.of("a")).anyTimes();
+    expect(zooReader.getData(eq(nsBasePath + "/a" + ZNAMESPACE_NAME)))
+        .andReturn("a_name".getBytes(UTF_8)).anyTimes();
+    var nsPropBytes =
+        propCodec.toBytes(new VersionedProperties(123, Instant.now(), 
Map.of("n1", "nv1")));
+    NamespaceId nsId = NamespaceId.of("a");
+    expect(zooReader.getData(eq(NamespacePropKey.of(iid, nsId).getNodePath()),
+        anyObject(Watcher.class), 
anyObject(Stat.class))).andReturn(nsPropBytes).anyTimes();
+
+    var tBasePath = ZooUtil.getRoot(iid) + ZTABLES;
+    
expect(zooReader.getChildren(tBasePath)).andReturn(List.of("t")).anyTimes();
+    expect(zooReader.getData(eq(tBasePath + "/t" + ZTABLE_NAME)))
+        .andReturn("t_table".getBytes(UTF_8)).anyTimes();
+    var tPropBytes =
+        propCodec.toBytes(new VersionedProperties(123, Instant.now(), 
Map.of("t1", "tv1")));
+    TableId tid = TableId.of("t");
+    expect(zooReader.getData(eq(TablePropKey.of(iid, tid).getNodePath()), 
anyObject(Watcher.class),
+        anyObject(Stat.class))).andReturn(tPropBytes).anyTimes();
+    expect(zooReader.getData(tBasePath + "/t" + ZTABLE_NAMESPACE))
+        .andReturn("+default".getBytes(UTF_8)).anyTimes();
+
+    replay(zooReader);
+
+    NamespacePropKey nsKey = NamespacePropKey.of(iid, nsId);
+    log.trace("namespace base path: {}", nsKey.getBasePath());
+
+    String testFileName = "./target/zoo-info-viewer-" + 
System.currentTimeMillis() + ".txt";
+
+    ZooInfoViewer.Opts opts = new ZooInfoViewer.Opts();
+    opts.parseArgs(ZooInfoViewer.class.getName(),
+        new String[] {"--instanceId", uuid, "--print-props", "--outfile", 
testFileName});
+
+    ZooInfoViewer viewer = new ZooInfoViewer();
+    // InstanceId found = viewer.getInstanceId(zooReader, opts);
+    viewer.generateReport(InstanceId.of(uuid), opts, zooReader);
+    // assertEquals(InstanceId.of(uuid), found);
+
+    verify(zooReader);
+
+    String line;
+    try (BufferedReader in = new BufferedReader(new FileReader(testFileName, 
UTF_8))) {

Review Comment:
   Used scanner



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to