diff options
8 files changed, 353 insertions, 45 deletions
diff --git a/base/common/src/com/netscape/certsrv/selftests/ISelfTestSubsystem.java b/base/common/src/com/netscape/certsrv/selftests/ISelfTestSubsystem.java index 29adde082..c07b96acb 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 108cc1f8f..7dce1db15 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 04f41999d..2238beb11 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 000000000..1bb361928 --- /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 + */ +@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 000000000..e4f2d951d --- /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 + */ +@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 a3827178f..2e40ee4f8 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 de9bd04ad..e662ba9e7 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 14fab26e4..4b89aa49e 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; + SelfTestOrderedInstance instance = instances.nextElement(); 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); - - try { - if (CMS.debugOn()) { - CMS.debug("SelfTestSubsystem::runSelfTestsOnDemand():" - + " running \"" - + test.getSelfTestName() - + "\""); - } - - 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)); + instanceName = instanceName.trim(); - // shutdown the system gracefully - CMS.shutdown(); + String instanceFullName = getFullName(mPrefix, instanceName); - 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 // |