[Pki-devel] [PATCH] 666 Updated CLI to run individual selftests.

Endi Sukma Dewata edewata at redhat.com
Tue Jan 5 00:55:51 UTC 2016


The pki selftest-run command has been modified to execute the
specified selftests, or all selftests if nothing is specified.
The command will also display the status of each test and the
stack trace if it fails.

https://fedorahosted.org/pki/ticket/1502

-- 
Endi S. Dewata
-------------- next part --------------
From 0050f2fd4811806b9205e60ece6d68b61b8e6f84 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata at redhat.com>
Date: Tue, 5 Jan 2016 01:39:06 +0100
Subject: [PATCH] Updated CLI to run individual selftests.

The pki selftest-run command has been modified to execute the
specified selftests, or all selftests if nothing is specified.
The command will also display the status of each test and the
stack trace if it fails.

https://fedorahosted.org/pki/ticket/1502
---
 .../certsrv/selftests/ISelfTestSubsystem.java      |   7 +
 .../netscape/certsrv/selftests/SelfTestClient.java |  10 ++
 .../certsrv/selftests/SelfTestResource.java        |  12 ++
 .../netscape/certsrv/selftests/SelfTestResult.java | 150 +++++++++++++++++++++
 .../certsrv/selftests/SelfTestResults.java         |  38 ++++++
 .../cmstools/selftests/SelfTestRunCLI.java         |  48 ++++++-
 .../org/dogtagpki/server/rest/SelfTestService.java |  58 +++++++-
 .../cmscore/selftests/SelfTestSubsystem.java       |  75 ++++++-----
 8 files changed, 353 insertions(+), 45 deletions(-)
 create mode 100644 base/common/src/com/netscape/certsrv/selftests/SelfTestResult.java
 create mode 100644 base/common/src/com/netscape/certsrv/selftests/SelfTestResults.java

diff --git a/base/common/src/com/netscape/certsrv/selftests/ISelfTestSubsystem.java b/base/common/src/com/netscape/certsrv/selftests/ISelfTestSubsystem.java
index 29adde0828332c600a325c4aa9e4418d718b8ea4..c07b96acb8f31095143b5288b238ee051dd23626 100644
--- a/base/common/src/com/netscape/certsrv/selftests/ISelfTestSubsystem.java
+++ b/base/common/src/com/netscape/certsrv/selftests/ISelfTestSubsystem.java
@@ -139,6 +139,13 @@ public interface ISelfTestSubsystem
     public void runSelfTestsOnDemand()
             throws EMissingSelfTestException, ESelfTestException;
 
+    /**
+     * Execute a self test.
+     *
+     * @exception Exception self test exception
+     */
+    public void runSelfTest(String instanceName) throws Exception;
+
     //
     // methods associated with the list of startup self tests
     //
diff --git a/base/common/src/com/netscape/certsrv/selftests/SelfTestClient.java b/base/common/src/com/netscape/certsrv/selftests/SelfTestClient.java
index 108cc1f8f01689d846f0a45dc51b1b2c5f59d56a..7dce1db15aed5dc73657ab4f5cac5fd62c5037a2 100644
--- a/base/common/src/com/netscape/certsrv/selftests/SelfTestClient.java
+++ b/base/common/src/com/netscape/certsrv/selftests/SelfTestClient.java
@@ -50,6 +50,16 @@ public class SelfTestClient extends Client {
         client.getEntity(response, Void.class);
     }
 
+    public SelfTestResults runSelfTests() {
+        Response response = resource.runSelfTests();
+        return client.getEntity(response, SelfTestResults.class);
+    }
+
+    public SelfTestResult runSelfTest(String selfTestID) {
+        Response response = resource.runSelfTest(selfTestID);
+        return client.getEntity(response, SelfTestResult.class);
+    }
+
     public SelfTestData getSelfTest(String selfTestID) {
         Response response = resource.getSelfTest(selfTestID);
         return client.getEntity(response, SelfTestData.class);
diff --git a/base/common/src/com/netscape/certsrv/selftests/SelfTestResource.java b/base/common/src/com/netscape/certsrv/selftests/SelfTestResource.java
index 04f41999d6b830e5ec6782c24ad0464c5cc80146..2238beb11ffdff4b5f39fe50616595aa5e5b63db 100644
--- a/base/common/src/com/netscape/certsrv/selftests/SelfTestResource.java
+++ b/base/common/src/com/netscape/certsrv/selftests/SelfTestResource.java
@@ -50,8 +50,20 @@ public interface SelfTestResource {
     @ACLMapping("selftests.execute")
     public Response executeSelfTests(@QueryParam("action") String action);
 
+    @POST
+    @Path("run")
+    @ClientResponseType(entityType=SelfTestResults.class)
+    @ACLMapping("selftests.execute")
+    public Response runSelfTests();
+
     @GET
     @Path("{selfTestID}")
     @ClientResponseType(entityType=SelfTestData.class)
     public Response getSelfTest(@PathParam("selfTestID") String selfTestID);
+
+    @POST
+    @Path("{selfTestID}/run")
+    @ClientResponseType(entityType=SelfTestResult.class)
+    @ACLMapping("selftests.execute")
+    public Response runSelfTest(@PathParam("selfTestID") String selfTestID);
 }
diff --git a/base/common/src/com/netscape/certsrv/selftests/SelfTestResult.java b/base/common/src/com/netscape/certsrv/selftests/SelfTestResult.java
new file mode 100644
index 0000000000000000000000000000000000000000..1bb36192874253cfa7eb2f1ba37eb0ff65d8dc8c
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/selftests/SelfTestResult.java
@@ -0,0 +1,150 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2016 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.certsrv.selftests;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+/**
+ * @author Endi S. Dewata
+ */
+ at XmlRootElement(name="SelfTestResult")
+public class SelfTestResult {
+
+    public static Marshaller marshaller;
+    public static Unmarshaller unmarshaller;
+
+    static {
+        try {
+            marshaller = JAXBContext.newInstance(SelfTestResult.class).createMarshaller();
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+            unmarshaller = JAXBContext.newInstance(SelfTestResult.class).createUnmarshaller();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    String id;
+    String status;
+    String output;
+
+    @XmlAttribute(name="id")
+    public String getID() {
+        return id;
+    }
+
+    public void setID(String id) {
+        this.id = id;
+    }
+
+    @XmlElement(name="Status")
+    public String getStatus() {
+        return status;
+    }
+
+    public void setStatus(String status) {
+        this.status = status;
+    }
+
+    @XmlElement(name="Output")
+    public String getOutput() {
+        return output;
+    }
+
+    public void setOutput(String output) {
+        this.output = output;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((id == null) ? 0 : id.hashCode());
+        result = prime * result + ((output == null) ? 0 : output.hashCode());
+        result = prime * result + ((status == null) ? 0 : status.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        SelfTestResult other = (SelfTestResult) obj;
+        if (id == null) {
+            if (other.id != null)
+                return false;
+        } else if (!id.equals(other.id))
+            return false;
+        if (output == null) {
+            if (other.output != null)
+                return false;
+        } else if (!output.equals(other.output))
+            return false;
+        if (status == null) {
+            if (other.status != null)
+                return false;
+        } else if (!status.equals(other.status))
+            return false;
+        return true;
+    }
+
+    public String toString() {
+        try {
+            StringWriter sw = new StringWriter();
+            marshaller.marshal(this, sw);
+            return sw.toString();
+
+        } catch (Exception e) {
+            return super.toString();
+        }
+    }
+
+    public static SelfTestResult valueOf(String string) throws Exception {
+        try {
+            return (SelfTestResult)unmarshaller.unmarshal(new StringReader(string));
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    public static void main(String args[]) throws Exception {
+
+        SelfTestResult before = new SelfTestResult();
+        before.setID("selftest1");
+        before.setStatus("PASSED");
+        before.setOutput(null);
+
+        String string = before.toString();
+        System.out.println(string);
+
+        SelfTestResult after = SelfTestResult.valueOf(string);
+        System.out.println(before.equals(after));
+    }
+}
diff --git a/base/common/src/com/netscape/certsrv/selftests/SelfTestResults.java b/base/common/src/com/netscape/certsrv/selftests/SelfTestResults.java
new file mode 100644
index 0000000000000000000000000000000000000000..e4f2d951d537f81ec6d6652f23bbfe42eb3efc92
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/selftests/SelfTestResults.java
@@ -0,0 +1,38 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2016 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.certsrv.selftests;
+
+import java.util.Collection;
+
+import javax.xml.bind.annotation.XmlElementRef;
+import javax.xml.bind.annotation.XmlRootElement;
+
+import com.netscape.certsrv.base.DataCollection;
+
+/**
+ * @author Endi S. Dewata
+ */
+ at XmlRootElement(name="SelfTestResults")
+public class SelfTestResults extends DataCollection<SelfTestResult> {
+
+    @XmlElementRef
+    public Collection<SelfTestResult> getEntries() {
+        return super.getEntries();
+    }
+}
diff --git a/base/java-tools/src/com/netscape/cmstools/selftests/SelfTestRunCLI.java b/base/java-tools/src/com/netscape/cmstools/selftests/SelfTestRunCLI.java
index a3827178f1083294607f6dd5f92b9c8bd9bcc71e..2e40ee4f885c3e48bf5d22847807517ddc626586 100644
--- a/base/java-tools/src/com/netscape/cmstools/selftests/SelfTestRunCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/selftests/SelfTestRunCLI.java
@@ -21,7 +21,10 @@ package com.netscape.cmstools.selftests;
 import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
+import org.apache.commons.lang.StringUtils;
 
+import com.netscape.certsrv.selftests.SelfTestResult;
+import com.netscape.certsrv.selftests.SelfTestResults;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -38,7 +41,20 @@ public class SelfTestRunCLI extends CLI {
     }
 
     public void printHelp() {
-        formatter.printHelp(getFullName() + " [OPTIONS...]", options);
+        formatter.printHelp(getFullName() + " [selftests...] [OPTIONS...]", options);
+    }
+
+    public static void printSelfTestResult(SelfTestResult result) {
+        System.out.println("  Selftest ID: " + result.getID());
+
+        String status = result.getStatus();
+        System.out.println("  Status: " + status);
+
+        String output = result.getOutput();
+        if (StringUtils.isNotEmpty(output)) {
+            System.out.println("  Output:");
+            System.out.println(output);
+        }
     }
 
     public void execute(String[] args) throws Exception {
@@ -62,13 +78,33 @@ public class SelfTestRunCLI extends CLI {
 
         String[] cmdArgs = cmd.getArgs();
 
-        if (cmdArgs.length != 0) {
-            System.err.println("Error: Too many arguments specified.");
-            printHelp();
-            System.exit(-1);
+        SelfTestResults results;
+
+        if (cmdArgs.length == 0) {
+            results = selfTestCLI.selfTestClient.runSelfTests();
+
+        } else {
+
+            results = new SelfTestResults();
+
+            for (String selfTestID : cmdArgs) {
+                SelfTestResult result = selfTestCLI.selfTestClient.runSelfTest(selfTestID);
+                results.addEntry(result);;
+            }
         }
 
-        selfTestCLI.selfTestClient.executeSelfTests("run");
+        boolean first = true;
+
+        for (SelfTestResult result : results.getEntries()) {
+
+            if (first) {
+                first = false;
+            } else {
+                System.out.println();
+            }
+
+            printSelfTestResult(result);
+        }
 
         MainCLI.printMessage("Selftests completed");
     }
diff --git a/base/server/cms/src/org/dogtagpki/server/rest/SelfTestService.java b/base/server/cms/src/org/dogtagpki/server/rest/SelfTestService.java
index de9bd04ad21d8f387c270e3a4ff20274d1951915..e662ba9e76706c425c97e0f788315a55a6985542 100644
--- a/base/server/cms/src/org/dogtagpki/server/rest/SelfTestService.java
+++ b/base/server/cms/src/org/dogtagpki/server/rest/SelfTestService.java
@@ -18,6 +18,8 @@
 
 package org.dogtagpki.server.rest;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URLEncoder;
@@ -42,6 +44,8 @@ import com.netscape.certsrv.selftests.ISelfTestSubsystem;
 import com.netscape.certsrv.selftests.SelfTestCollection;
 import com.netscape.certsrv.selftests.SelfTestData;
 import com.netscape.certsrv.selftests.SelfTestResource;
+import com.netscape.certsrv.selftests.SelfTestResult;
+import com.netscape.certsrv.selftests.SelfTestResults;
 import com.netscape.cms.servlet.base.PKIService;
 
 /**
@@ -144,7 +148,7 @@ public class SelfTestService extends PKIService implements SelfTestResource {
             return createOKResponse(response);
 
         } catch (Exception e) {
-            e.printStackTrace();
+            CMS.debug(e);
             throw new PKIException(e.getMessage());
         }
     }
@@ -161,7 +165,7 @@ public class SelfTestService extends PKIService implements SelfTestResource {
             return createOKResponse(createSelfTestData(subsystem, selfTestID));
 
         } catch (Exception e) {
-            e.printStackTrace();
+            CMS.debug(e);
             throw new PKIException(e.getMessage());
         }
     }
@@ -182,10 +186,58 @@ public class SelfTestService extends PKIService implements SelfTestResource {
             subsystem.runSelfTestsOnDemand();
 
         } catch (Exception e) {
-            e.printStackTrace();
+            CMS.debug(e);
             throw new PKIException(e.getMessage());
         }
 
         return createNoContentResponse();
     }
+
+    @Override
+    public Response runSelfTests() {
+
+        CMS.debug("SelfTestService.runSelfTests()");
+
+        SelfTestResults results = new SelfTestResults();
+
+        try {
+            ISelfTestSubsystem subsystem = (ISelfTestSubsystem)CMS.getSubsystem(ISelfTestSubsystem.ID);
+            for (String selfTestID : subsystem.listSelfTestsEnabledOnDemand()) {
+                Response response = runSelfTest(selfTestID);
+                SelfTestResult result = (SelfTestResult)response.getEntity();
+                results.addEntry(result);
+            }
+
+        } catch (Exception e) {
+            CMS.debug(e);
+            throw new PKIException(e.getMessage());
+        }
+
+        return createOKResponse(results);
+    }
+
+    @Override
+    public Response runSelfTest(String selfTestID) {
+
+        CMS.debug("SelfTestService.runSelfTest(" + selfTestID + ")");
+
+        SelfTestResult result = new SelfTestResult();
+        result.setID(selfTestID);
+
+        try {
+            ISelfTestSubsystem subsystem = (ISelfTestSubsystem)CMS.getSubsystem(ISelfTestSubsystem.ID);
+            subsystem.runSelfTest(selfTestID);
+            result.setStatus("PASSED");
+
+        } catch (Exception e) {
+            result.setStatus("FAILED");
+
+            StringWriter sw = new StringWriter();
+            PrintWriter out = new PrintWriter(sw);
+            e.printStackTrace(out);
+            result.setOutput(sw.toString());
+        }
+
+        return createOKResponse(result);
+    }
 }
diff --git a/base/server/cmscore/src/com/netscape/cmscore/selftests/SelfTestSubsystem.java b/base/server/cmscore/src/com/netscape/cmscore/selftests/SelfTestSubsystem.java
index 14fab26e4d3f9ffdfc305acbd94b742be6141604..4b89aa49efbaaeb1b1039b899cf77c24db49ac32 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/selftests/SelfTestSubsystem.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/selftests/SelfTestSubsystem.java
@@ -501,16 +501,11 @@ public class SelfTestSubsystem
         Enumeration<SelfTestOrderedInstance> instances = mOnDemandOrder.elements();
 
         while (instances.hasMoreElements()) {
+
             SelfTestOrderedInstance instance = instances.nextElement();
-
-            String instanceFullName = null;
             String instanceName = instance.getSelfTestName();
 
-            if (instanceName != null) {
-                instanceName = instanceName.trim();
-                instanceFullName = getFullName(mPrefix,
-                            instanceName);
-            } else {
+            if (instanceName == null) {
                 log(mLogger,
                         CMS.getLogMessage(
                                 "CMSCORE_SELFTESTS_PROPERTY_NAME_IS_NULL"));
@@ -518,37 +513,11 @@ public class SelfTestSubsystem
                 throw new EMissingSelfTestException();
             }
 
-            if (mSelfTestInstances.containsKey(instanceName)) {
-                ISelfTest test = mSelfTestInstances.get(instanceName);
+            instanceName = instanceName.trim();
 
-                try {
-                    if (CMS.debugOn()) {
-                        CMS.debug("SelfTestSubsystem::runSelfTestsOnDemand():"
-                                + "    running \""
-                                + test.getSelfTestName()
-                                + "\"");
-                    }
+            String instanceFullName = getFullName(mPrefix, instanceName);
 
-                    test.runSelfTest(mLogger);
-
-                } catch (Exception e) {
-
-                    CMS.debug(e);
-
-                    // Check to see if the self test was critical:
-                    if (isSelfTestCriticalOnDemand(instanceName)) {
-                        log(mLogger,
-                                CMS.getLogMessage(
-                                        "CMSCORE_SELFTESTS_RUN_ON_DEMAND_FAILED",
-                                        instanceFullName));
-
-                        // shutdown the system gracefully
-                        CMS.shutdown();
-
-                        return;
-                    }
-                }
-            } else {
+            if (!mSelfTestInstances.containsKey(instanceName)) {
                 // self test plugin instance property name is not present
                 log(mLogger,
                         CMS.getLogMessage(
@@ -557,6 +526,27 @@ public class SelfTestSubsystem
 
                 throw new EMissingSelfTestException(instanceFullName);
             }
+
+            try {
+                runSelfTest(instanceName);
+
+            } catch (Exception e) {
+
+                CMS.debug(e);
+
+                // Check to see if the self test was critical:
+                if (isSelfTestCriticalOnDemand(instanceName)) {
+                    log(mLogger,
+                            CMS.getLogMessage(
+                                    "CMSCORE_SELFTESTS_RUN_ON_DEMAND_FAILED",
+                                    instanceFullName));
+
+                    // shutdown the system gracefully
+                    CMS.shutdown();
+
+                    return;
+                }
+            }
         }
 
         if (CMS.debugOn()) {
@@ -565,6 +555,19 @@ public class SelfTestSubsystem
         }
     }
 
+    public void runSelfTest(String instanceName) throws Exception {
+
+        CMS.debug("SelfTestSubsystem.runSelfTest(" + instanceName + ")");
+
+        ISelfTest test = mSelfTestInstances.get(instanceName);
+
+        if (test == null) {
+            throw new EMissingSelfTestException(instanceName);
+        }
+
+        test.runSelfTest(mLogger);
+    }
+
     //
     // methods associated with the list of startup self tests
     //
-- 
2.4.3



More information about the Pki-devel mailing list