Repository: oozie
Updated Branches:
  refs/heads/master 567ce9fd2 -> 9e8598ea4


http://git-wip-us.apache.org/repos/asf/oozie/blob/9e8598ea/tools/src/test/java/org/apache/oozie/tools/diag/TestArgParser.java
----------------------------------------------------------------------
diff --git a/tools/src/test/java/org/apache/oozie/tools/diag/TestArgParser.java 
b/tools/src/test/java/org/apache/oozie/tools/diag/TestArgParser.java
new file mode 100644
index 0000000..ba48dbb
--- /dev/null
+++ b/tools/src/test/java/org/apache/oozie/tools/diag/TestArgParser.java
@@ -0,0 +1,112 @@
+/**
+ * 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.oozie.tools.diag;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.oozie.tools.LauncherSecurityManager;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+public class TestArgParser {
+    private static SecurityManager SECURITY_MANAGER;
+    private CommandLine mockCommandLine = mock(CommandLine.class);
+    private final ArgParser argParser =  new ArgParser();
+
+    @BeforeClass
+    public static void setUp() throws Exception {
+        SECURITY_MANAGER = System.getSecurityManager();
+        interceptSecurityManager();
+    }
+
+    private static void interceptSecurityManager() {
+        new LauncherSecurityManager();
+    }
+
+    @AfterClass
+    public static void tearDown() throws Exception {
+        System.setSecurityManager(SECURITY_MANAGER);
+    }
+
+    @Before
+    public void initTestCase() {
+        doReturn("-1").when(mockCommandLine).getOptionValue(anyString(), 
anyString());
+        argParser.setCommandLine(mockCommandLine);
+    }
+
+    @Test
+    public void testUsageDisplayedWithoutUserInput() throws Exception {
+        final String[] args = {""};
+        final OutputStream interceptedStdout = new ByteArrayOutputStream();
+        final OutputStream interceptedStderr = new ByteArrayOutputStream();
+
+        interceptSystemStreams(interceptedStdout, interceptedStderr);
+
+        try {
+            DiagBundleCollectorDriver.main(args);
+        }
+        catch (final SecurityException securityException){
+            assertTrue(interceptedStdout.toString().contains("usage: "));
+            assertTrue(interceptedStderr.toString().contains("Missing required 
options: oozie, output"));
+        }
+        finally {
+            restoreSystemStreams();
+        }
+    }
+
+    private void restoreSystemStreams() {
+        System.setErr(System.err);
+        System.setErr(System.out);
+    }
+
+    private void interceptSystemStreams(OutputStream interceptedStdout, 
OutputStream interceptedStderr) {
+        System.setOut(new PrintStream(interceptedStdout));
+        System.setErr(new PrintStream(interceptedStderr));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testExceptionThrownWithInvalidNumWorkflows() throws Exception {
+        argParser.getNumWorkflows();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testExceptionThrownWithInvalidNumCoordinators() throws 
Exception {
+        argParser.getNumCoordinators();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testExceptionThrownWithInvalidNumBundles() throws Exception {
+        argParser.getNumBundles();
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testExceptionThrownWithInvalidMaxChildActions() throws 
Exception {
+        argParser.getMaxChildActions();
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/oozie/blob/9e8598ea/tools/src/test/java/org/apache/oozie/tools/diag/TestMetricsCollector.java
----------------------------------------------------------------------
diff --git 
a/tools/src/test/java/org/apache/oozie/tools/diag/TestMetricsCollector.java 
b/tools/src/test/java/org/apache/oozie/tools/diag/TestMetricsCollector.java
new file mode 100644
index 0000000..53a9dcd
--- /dev/null
+++ b/tools/src/test/java/org/apache/oozie/tools/diag/TestMetricsCollector.java
@@ -0,0 +1,228 @@
+/**
+ * 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.oozie.tools.diag;
+
+import org.apache.oozie.client.OozieClient;
+import org.apache.oozie.client.OozieClientException;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+
+import static junit.framework.TestCase.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doReturn;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestMetricsCollector {
+    final DiagOozieClient client = new DiagOozieClient("testClient", null);
+    private File testOut;
+
+    @Rule public TemporaryFolder folder= new TemporaryFolder();
+    @Mock private OozieClient mockOozieClient;
+
+    @Before
+    public void setup() throws IOException {
+        testOut = folder.newFolder();
+    }
+
+    @Test
+    public void testMetricsCanBeRetrieved() throws OozieClientException, 
IOException {
+        String exampleMetrics = "{\n" +
+                "   \"gauges\" : {\n" +
+                "        \"jvm.memory.non-heap.committed\" : {\n" +
+                "          \"value\" : 62590976\n" +
+                "        },\n" +
+                "        \"oozie.mode\" : {\n" +
+                "          \"value\" : \"NORMAL\"\n" +
+                "        }\n" +
+                "    },\n" +
+                "    \"timers\" : {\n" +
+                "        \"commands.action.end.call.timer\" : {\n" +
+                "          \"mean\" : 108.5,\n" +
+                "          \"p50\" : 111.5,\n" +
+                "          \"p75\" : 165.5,\n" +
+                "          \"p999\" : 169,\n" +
+                "          \"count\" : 4,\n" +
+                "          \"p95\" : 169,\n" +
+                "          \"max\" : 169,\n" +
+                "          \"mean_rate\" : 0,\n" +
+                "          \"duration_units\" : \"milliseconds\",\n" +
+                "          \"p98\" : 169,\n" +
+                "          \"m1_rate\" : 0,\n" +
+                "          \"rate_units\" : \"calls/millisecond\",\n" +
+                "          \"m15_rate\" : 0,\n" +
+                "          \"stddev\" : 62.9417720330995,\n" +
+                "          \"m5_rate\" : 0,\n" +
+                "          \"p99\" : 169,\n" +
+                "          \"min\" : 42\n" +
+                "        }\n" +
+                "    },\n" +
+                "    \"histograms\" : {\n" +
+                "        \"callablequeue.threads.active.histogram\" : {\n" +
+                "          \"p999\" : 1,\n" +
+                "          \"mean\" : 0.0625,\n" +
+                "          \"min\" : 0,\n" +
+                "          \"p75\" : 0,\n" +
+                "          \"p95\" : 1,\n" +
+                "          \"count\" : 48,\n" +
+                "          \"p98\" : 1,\n" +
+                "          \"stddev\" : 0.24462302739504083,\n" +
+                "          \"max\" : 1,\n" +
+                "          \"p99\" : 1,\n" +
+                "          \"p50\" : 0\n" +
+                "        },\n" +
+                "    },\n" +
+                "    \"counters\" : {\n" +
+                "        \"commands.job.info.executions\" : {\n" +
+                "          \"count\" : 9\n" +
+                "        },\n" +
+                "        \"jpa.CoordJobsGetPendingJPAExecutor\" : {\n" +
+                "          \"count\" : 1\n" +
+                "        },\n" +
+                "        \"jpa.GET_WORKFLOW\" : {\n" +
+                "          \"count\" : 10\n" +
+                "        }\n" +
+                "    }\n" +
+                "}";
+        final JSONObject testMetricsJSON = getJsonObject(exampleMetrics);
+
+        final DiagOozieClient.Metrics metrics = client.new 
Metrics(testMetricsJSON);
+        doReturn(metrics).when(mockOozieClient).getMetrics();
+        final MetricsCollector metricsCollector = new 
MetricsCollector(mockOozieClient);
+
+        metricsCollector.storeMetrics(testOut);
+
+        final File metricsOut = new File(testOut, "metrics.txt");
+        final String str = new String(Files.readAllBytes(metricsOut.toPath()), 
StandardCharsets.UTF_8.toString());
+
+        assertTrue(str.contains("CoordJobsGetPendingJPAExecutor"));
+    }
+
+    @Test
+    public void testInstrumentionCanBeRetrieved() throws OozieClientException, 
IOException {
+        final String exampleInstrumentation = "{\n" +
+                "  \"counters\": [\n" +
+                "    {\n" +
+                "      \"group\": \"db\",\n" +
+                "      \"data\": [\n" +
+                "        {\n" +
+                "          \"name\": \"test\",\n" +
+                "          \"value\": \"42\"\n" +
+                "        }\n" +
+                "      ]\n" +
+                "    }\n" +
+                "  ],\n" +
+                "  \"timers\": [\n" +
+                "    {\n" +
+                "      \"group\": \"db\",\n" +
+                "      \"data\": [\n" +
+                "        {\n" +
+                "          \"ownMinTime\": 2,\n" +
+                "          \"ownTimeStdDev\": 0,\n" +
+                "          \"totalTimeStdDev\": 0,\n" +
+                "          \"ownTimeAvg\": 3,\n" +
+                "          \"ticks\": 117,\n" +
+                "          \"name\": \"update-workflow\",\n" +
+                "          \"ownMaxTime\": 32,\n" +
+                "          \"totalMinTime\": 2,\n" +
+                "          \"totalMaxTime\": 32,\n" +
+                "          \"totalTimeAvg\": 3\n" +
+                "        }\n" +
+                "      ]\n" +
+                "    }\n" +
+                "  ],\n" +
+                "  \"samplers\": [\n" +
+                "    {\n" +
+                "      \"group\": \"callablequeue\",\n" +
+                "      \"data\": [\n" +
+                "        {\n" +
+                "          \"name\": \"threads.active\",\n" +
+                "          \"value\": 1.8333333333333333\n" +
+                "        },\n" +
+                "        {\n" +
+                "          \"name\": \"delayed.queue.size\",\n" +
+                "          \"value\": 0\n" +
+                "        },\n" +
+                "        {\n" +
+                "          \"name\": \"queue.size\",\n" +
+                "          \"value\": 0\n" +
+                "        }\n" +
+                "      ]\n" +
+                "    }\n" +
+                "  ],\n" +
+                "  \"variables\": [\n" +
+                "    {\n" +
+                "      \"group\": \"jvm\",\n" +
+                "      \"data\": [\n" +
+                "        {\n" +
+                "          \"name\": \"max.memory\",\n" +
+                "          \"value\": 506920960\n" +
+                "        },\n" +
+                "        {\n" +
+                "          \"name\": \"total.memory\",\n" +
+                "          \"value\": 56492032\n" +
+                "        },\n" +
+                "        {\n" +
+                "          \"name\": \"free.memory\",\n" +
+                "          \"value\": 45776800\n" +
+                "        }\n" +
+                "      ]\n" +
+                "    }\n" +
+                "  ]\n" +
+                "}";
+
+        final JSONObject testInstrumentationJSON = 
getJsonObject(exampleInstrumentation);
+        final DiagOozieClient.Instrumentation metrics = client.new 
Instrumentation(testInstrumentationJSON);
+
+        doReturn(metrics).when(mockOozieClient).getInstrumentation();
+        final MetricsCollector metricsCollector = new 
MetricsCollector(mockOozieClient);
+
+        metricsCollector.storeInstrumentationInfo(testOut);
+
+        final File instrumentationFile = new File(testOut, 
"instrumentation.txt");
+        assertTrue(instrumentationFile.exists());
+
+        final String str = new 
String(Files.readAllBytes(instrumentationFile.toPath()), 
StandardCharsets.UTF_8.toString());
+        assertTrue(str.contains("45776800"));
+    }
+
+    private JSONObject getJsonObject(final String exampleInstrumentation) {
+        final JSONParser parser = new JSONParser();
+        JSONObject testInstrumentationJSON = null;
+        try {
+            testInstrumentationJSON = (JSONObject) 
parser.parse(exampleInstrumentation);
+        } catch (ParseException e) {
+            fail();
+        }
+        return testInstrumentationJSON;
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/oozie/blob/9e8598ea/tools/src/test/java/org/apache/oozie/tools/diag/TestServerInfoCollector.java
----------------------------------------------------------------------
diff --git 
a/tools/src/test/java/org/apache/oozie/tools/diag/TestServerInfoCollector.java 
b/tools/src/test/java/org/apache/oozie/tools/diag/TestServerInfoCollector.java
new file mode 100644
index 0000000..a8225f7
--- /dev/null
+++ 
b/tools/src/test/java/org/apache/oozie/tools/diag/TestServerInfoCollector.java
@@ -0,0 +1,125 @@
+/**
+ * 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.oozie.tools.diag;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static junit.framework.TestCase.assertTrue;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doReturn;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestServerInfoCollector {
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder();
+
+    @Mock
+    private DiagOozieClient mockDiagClient;
+    private File testTempFolder;
+    private ServerInfoCollector serverInfoCollector;
+
+    @Before
+    public void setup() throws IOException {
+        testTempFolder = folder.newFolder();
+        serverInfoCollector = new ServerInfoCollector(mockDiagClient);
+    }
+
+    @Test
+    public void testGetShareLibInfo() throws Exception {
+        
doReturn("share1\nshare2").when(mockDiagClient).listShareLib(anyString());
+
+        serverInfoCollector.storeShareLibInfo(testTempFolder);
+
+        final File shareLibOut = new File(testTempFolder, "sharelib.txt");
+        assertTrue(shareLibOut.exists());
+        assertFileContains(shareLibOut, "share1");
+    }
+
+    @Test
+    public void testGetJavaSystemProperties() throws Exception {
+        Map<String, String> testSysProps = new HashMap<>();
+        testSysProps.put("javax.xml.parsers.DocumentBuilderFactory", 
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
+        doReturn(testSysProps).when(mockDiagClient).getJavaSystemProperties();
+
+        serverInfoCollector.storeJavaSystemProperties(testTempFolder);
+
+        final File shareLibOut = new File(testTempFolder, 
"java-sys-props.txt");
+        assertTrue(shareLibOut.exists());
+        assertFileContains(shareLibOut, "DocumentBuilderFactory");
+    }
+
+    @Test
+    public void testGetOsEnv() throws Exception {
+        Map<String, String> testSysProps = new HashMap<>();
+        testSysProps.put("JETTY_OUT", "jetty.out");
+        doReturn(testSysProps).when(mockDiagClient).getOSEnv();
+
+        serverInfoCollector.storeOsEnv(testTempFolder);
+
+        final File shareLibOut = new File(testTempFolder, "os-env-vars.txt");
+        assertTrue(shareLibOut.exists());
+        assertFileContains(shareLibOut, "JETTY_OUT");
+    }
+
+    @Test
+    public void testGetServerConfiguration() throws Exception {
+        final Map<String, String> testSysProps = new HashMap<>();
+        testSysProps.put("oozie.services", 
"org.apache.oozie.service.SchedulerService");
+        doReturn(testSysProps).when(mockDiagClient).getServerConfiguration();
+
+        serverInfoCollector.storeServerConfiguration(testTempFolder);
+
+        final File shareLibOut = new File(testTempFolder, 
"effective-oozie-site.xml");
+        assertTrue(shareLibOut.exists());
+        assertFileContains(shareLibOut, "SchedulerService");
+    }
+
+    @Test
+    public void testGetCallableQueueDump() throws Exception {
+        final List<String> testDump = 
Arrays.asList("(coord_action_start,1)","(coord_action_start,1)");
+        doReturn(testDump).when(mockDiagClient).getQueueDump();
+
+        serverInfoCollector.storeCallableQueueDump(testTempFolder);
+
+        final File shareLibOut = new File(testTempFolder, "queue-dump.txt");
+        assertTrue(shareLibOut.exists());
+        assertFileContains(shareLibOut, "coord_action_start");
+    }
+
+    static void assertFileContains(final File testFile, final String testData) 
throws IOException {
+        final String str = new String(Files.readAllBytes(testFile.toPath()), 
StandardCharsets.UTF_8.toString());
+        assertTrue(str.contains(testData));
+    }
+}
\ No newline at end of file

Reply via email to