fix #79 with testcase

This commit is contained in:
Peter Hormanns 2012-01-04 12:27:05 +00:00
parent 9d9ea1a18c
commit 98477776a8
10 changed files with 160 additions and 204 deletions

View File

@ -154,7 +154,7 @@
<pathelement path="/usr/share/java/junit4.jar"/> <pathelement path="/usr/share/java/junit4.jar"/>
</classpath> </classpath>
<formatter type="xml"/> <formatter type="xml"/>
<test name="de.hsadmin.remote.RemoteTest" todir="${build.home}/junit" /> <test name="de.hsadmin.remote.ContinuousIntegrationTest" todir="${build.home}/junit" />
</junit> </junit>
</target> </target>

View File

@ -7,151 +7,69 @@ import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
public class CommandShell public class CommandShell {
{
private static boolean bExecute = true; // really execute or just store command and stdin?
private static String executedCommands; // stored command and stdin
private static String[] aEnvironment; // stored environment
/** Set mode of real execution or just storing the command and stdin. public static String execute(String command) throws ShellException {
* return execute(command, null);
* @param bExec
* specifies whether shell commands should really be executed (true) or not (false)
*/
public static void setExecute( boolean bExec )
{
bExecute = bExec;
} }
/** Returns and clears the last command which should have been executed. public static String execute(String command, String stdInput) throws ShellException {
*
* @return
* Last command, plus "<<EOF\n" + stdin + "EOF" if stdin was given.
*/
public static String popLastCommand()
{
String aLastCommand = executedCommands;
executedCommands = null;
return aLastCommand != null ? aLastCommand.trim() : null;
}
/** Returns and clears the environment which ist currently used for commands.
*
* @return
* Most recent command environment.
*/
public static String[] popEnvironment()
{
String[] aLastEnvironment = aEnvironment;
aEnvironment = null;
return aLastEnvironment;
}
public static void setEnvironment( String[] env )
{
aEnvironment = env;
}
/** Execute the given command by a shell.
*
* @throws ShellException
* if an execution error has occured.
*/
public static String execute( String command )
throws ShellException
{
return execute( command, null );
}
/** Execute the given command by a shell including stdin.
*
* @throws ShellException
* if an execution error has occured.
*/
public static String execute( String command, String stdInput )
throws ShellException
{
Process backend = null; Process backend = null;
String callOutput = null; String callOutput = null;
int exitCode = 0; int exitCode = 0;
try try {
{
String logCommand = command; String logCommand = command;
if ( stdInput != null ) //&& stdInput.length() > 0 ) if (stdInput != null) {
logCommand += "<<EOF\n" + stdInput + "EOF"; logCommand += "<<EOF\n" + stdInput + "EOF";
logCommand += "\n";
// add to command buffer (used in tests)
if ( executedCommands == null )
executedCommands = logCommand;
else
executedCommands += logCommand;
if ( bExecute )
{
// TODO logging
System.out.println("--- shell command: ---" );
System.out.println( logCommand );
String[] cmdArray = { "/bin/bash", "-c", command };
backend = Runtime.getRuntime().exec(cmdArray, aEnvironment);
if ( stdInput != null )
{
OutputStream stdInputStream = backend.getOutputStream();
PrintWriter stdInputWriter = new PrintWriter(stdInputStream);
stdInputWriter.print(stdInput);
stdInputWriter.close();
stdInputStream.close();
}
callOutput = readProcessStream(backend.getInputStream());
exitCode = backend.waitFor();
// TODO logging
System.out.println( "RET: " + exitCode );
if (exitCode != 0)
{
String aErr = readProcessStream(backend.getErrorStream());
// TODO logging
System.out.println( "ERR: " + aErr );
System.out.println("--- done. ---" );
throw new ShellException( exitCode, aErr );
}
// TODO logging
System.out.println("--- done. ---" );
} }
} logCommand += "\n";
catch (IOException e) // TODO logging
{ System.out.println("--- shell command: ---");
throw new ShellException(); System.out.println(logCommand);
}
catch (InterruptedException e)
{
throw new ShellException();
}
if ( callOutput != null ) String[] cmdArray = { "/bin/bash", "-c", command };
{ backend = Runtime.getRuntime().exec(cmdArray);
// int nLen = callOutput.length(); if (stdInput != null) {
// if ( nLen > 0 && callOutput.charAt(nLen-1) == '\n' ) OutputStream stdInputStream = backend.getOutputStream();
// callOutput = callOutput.substring(0, nLen-1); PrintWriter stdInputWriter = new PrintWriter(stdInputStream);
stdInputWriter.print(stdInput);
stdInputWriter.close();
stdInputStream.close();
}
callOutput = readProcessStream(backend.getInputStream());
exitCode = backend.waitFor();
// TODO logging
System.out.println("RET: " + exitCode);
if (exitCode != 0) {
String aErr = readProcessStream(backend.getErrorStream());
// TODO logging
System.out.println("ERR: " + aErr);
System.out.println("--- done. ---");
throw new ShellException(aErr);
}
// TODO logging
System.out.println("--- done. ---");
} catch (IOException e) {
throw new ShellException(e);
} catch (InterruptedException e) {
throw new ShellException(e);
}
if (callOutput != null) {
return callOutput.trim(); return callOutput.trim();
} }
return null; return null;
} }
private static String readProcessStream(InputStream stream) throws IOException private static String readProcessStream(InputStream stream) throws IOException {
{ BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
BufferedReader reader = new BufferedReader(
new InputStreamReader(stream));
StringBuffer textBuff = new StringBuffer(); StringBuffer textBuff = new StringBuffer();
String textLine = reader.readLine(); String textLine = reader.readLine();
while (textLine != null) while (textLine != null) {
{
textBuff.append(textLine); textBuff.append(textLine);
textBuff.append('\n'); textBuff.append('\n');
textLine = reader.readLine(); textLine = reader.readLine();

View File

@ -1,19 +1,14 @@
package de.hsadmin.core.qserv; package de.hsadmin.core.qserv;
public class ShellException public class ShellException extends Exception {
extends Exception
{
private static final long serialVersionUID = 8335020360721047849L;
int nExitCode; private static final long serialVersionUID = 5499293305075489652L;
public ShellException() public ShellException(String message) {
{ super(message);
} }
public ShellException( int exitCode, String message ) public ShellException(Throwable e) {
{ super(e);
super( message );
nExitCode = exitCode;
} }
} }

View File

@ -13,47 +13,26 @@ public class ShellProcessor extends AbstractProcessor {
private static final long serialVersionUID = -649045174380048818L; private static final long serialVersionUID = -649045174380048818L;
private String aSystemCall; private String aSystemCall;
private String[] aEnv;
private String aInput; private String aInput;
private String aOutput; private String aOutput;
private String aErrors; private String aErrors;
/**
* Constructor for a queue entry which executes a system call.
*
* @param aSystemCall
* the system call to be executed
*/
public ShellProcessor(String aSystemCall) {
this(aSystemCall, null, null);
}
/**
* Constructor for a queue entry which executes a system call with stdin
* data.
*
* @param aSystemCall
* the system call to be executed
* @param aInput
* data for stdin of the system call
*/
public ShellProcessor(String aSystemCall, String aInput) { public ShellProcessor(String aSystemCall, String aInput) {
this.aSystemCall = aSystemCall; this.aSystemCall = aSystemCall;
this.aInput = aInput; this.aInput = aInput;
} }
public ShellProcessor(String aSystemCall, String[] aEnv, String aInput) { public ShellProcessor(String aSystemCall) {
this.aSystemCall = aSystemCall; this.aSystemCall = aSystemCall;
this.aEnv = aEnv; this.aInput = null;
this.aInput = aInput;
} }
public Object process() throws ProcessorException { public Object process() throws ProcessorException {
try { try {
CommandShell.setEnvironment(aEnv);
aOutput = CommandShell.execute(aSystemCall, aInput); aOutput = CommandShell.execute(aSystemCall, aInput);
return aOutput; return aOutput;
} catch (ShellException aExc) { } catch (ShellException aExc) {
aErrors = aExc.getMessage();
aExc.printStackTrace(System.err); // Logging aExc.printStackTrace(System.err); // Logging
throw new ProcessorException(aExc); throw new ProcessorException(aExc);
} }

View File

@ -249,9 +249,14 @@ public class DomainProcessorFactory implements EntityProcessorFactory {
private Processor createApacheVHostSetupProcessor(Domain dom, Map<String, String> templateVars) private Processor createApacheVHostSetupProcessor(Domain dom, Map<String, String> templateVars)
throws ProcessorException { throws ProcessorException {
String domName = dom.getName(); String domName = dom.getName();
String pac = dom.getUser().getPac().getName();
Processor domSetupProcessor = new CompoundProcessor( Processor domSetupProcessor = new CompoundProcessor(
createDomainDirectoriesProcessor(dom, templateVars), createDomainDirectoriesProcessor(dom, templateVars),
new CreateFileProcessor(selectVHostTemplate(dom), templateVars, "/etc/apache2/sites-generated/" + domName, "root", "root", "644"), new CreateFileProcessor(selectVHostTemplate(dom), templateVars, "/etc/apache2/sites-generated/" + domName, "root", "root", "644"),
new ShellProcessor("ls /etc/apache2/pem/" + pac + ".pem >/dev/null 2>&1 " +
"&& sed -i '/SSLCertificate.*default/d' " + "/etc/apache2/sites-generated/" + domName +
" && (ls /etc/apache2/pem/" + pac + ".chain.pem >/dev/null 2>&1 || sed -i '/SSLCertificateChain.*" + pac + "/d' " + "/etc/apache2/sites-generated/" + domName + ")" +
" || sed -i '/SSLCertificate.*" + pac + "/d' " + "/etc/apache2/sites-generated/" + domName),
new ShellProcessor( new ShellProcessor(
"ln -sf /etc/apache2/sites-generated/" + domName + " /etc/apache2/sites-enabled/010-" + domName + "ln -sf /etc/apache2/sites-generated/" + domName + " /etc/apache2/sites-enabled/010-" + domName +
" && invoke-rc.d apache2 reload >/dev/null 2>&1") " && invoke-rc.d apache2 reload >/dev/null 2>&1")

View File

@ -62,6 +62,8 @@ NameVirtualHost {DOM_IPNUMBEREX}:443
SSLEngine On SSLEngine On
SSLCertificateFile /etc/apache2/pems/default.pem SSLCertificateFile /etc/apache2/pems/default.pem
SSLCertificateChainFile /etc/apache2/pems/default.chain.pem SSLCertificateChainFile /etc/apache2/pems/default.chain.pem
SSLCertificateFile /etc/apache2/pems/{PAC}.pem
SSLCertificateChainFile /etc/apache2/pems/{PAC}.chain.pem
DocumentRoot /home/doms/{DOM_HOSTNAME}/htdocs-ssl DocumentRoot /home/doms/{DOM_HOSTNAME}/htdocs-ssl

View File

@ -52,6 +52,8 @@ NameVirtualHost {DOM_IPNUMBEREX}:443
SSLEngine On SSLEngine On
SSLCertificateFile /etc/apache2/pems/default.pem SSLCertificateFile /etc/apache2/pems/default.pem
SSLCertificateChainFile /etc/apache2/pems/default.chain.pem SSLCertificateChainFile /etc/apache2/pems/default.chain.pem
SSLCertificateFile /etc/apache2/pems/{PAC}.pem
SSLCertificateChainFile /etc/apache2/pems/{PAC}.chain.pem
DocumentRoot /home/doms/{DOM_HOSTNAME}/htdocs-ssl DocumentRoot /home/doms/{DOM_HOSTNAME}/htdocs-ssl

View File

@ -0,0 +1,26 @@
package de.hsadmin.remote;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
InitDataTest.class,
RoleTest.class,
MysqlUserTest.class,
MysqlDbTest.class,
PgsqlUserTest.class,
PgsqlDbTest.class,
PacTest.class,
UnixUserTest.class,
EMailAliasTest.class,
DomainTest.class,
EMailAddressTest.class,
SSLCertDomainTest.class
// CustomerTest.class,
// QueueTaskTest.class
})
public class ContinuousIntegrationTest {
}

View File

@ -62,9 +62,6 @@ public class InitDataTest {
setParams }; setParams };
try { try {
client.execute(CUST_MODULE + ".add", params); client.execute(CUST_MODULE + ".add", params);
// Object execute = client.execute(CUST_MODULE + ".add", params);
// Map<?, ?> result = (Map<?, ?>) execute;
// System.out.println(result);
assertEquals(membersCount + 1, getMembersCount()); assertEquals(membersCount + 1, getMembersCount());
} catch (XmlRpcException e) { } catch (XmlRpcException e) {
fail(e.getMessage()); fail(e.getMessage());
@ -167,42 +164,6 @@ public class InitDataTest {
assertEquals(count + 1, getUsersCount()); assertEquals(count + 1, getUsersCount());
} }
// @Test
public void testDelPac() {
int count = getPacsCount();
String user = "ad";
String grantingTicketURL = cas.getGrantingTicketURL(user);
Map<String, String> whereParams = new HashMap<String, String>();
whereParams.put("name", "aaa00");
Object[] params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
whereParams };
try {
client.execute(PAC_MODULE + ".delete", params);
} catch (XmlRpcException e) {
fail(e.getMessage());
}
assertEquals(count - 1, getPacsCount());
}
// @Test
public void testDelMember() {
int count = getMembersCount();
String user = "ad";
String grantingTicketURL = cas.getGrantingTicketURL(user);
Map<String, String> whereParams = new HashMap<String, String>();
whereParams.put("membercode", "hsh00-aaa");
Object[] params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
whereParams };
try {
client.execute(CUST_MODULE + ".delete", params);
} catch (XmlRpcException e) {
fail(e.getMessage());
}
assertEquals(count - 1, getMembersCount());
}
private int getMembersCount() { private int getMembersCount() {
int count = 0; int count = 0;
String user = "ad"; String user = "ad";

View File

@ -0,0 +1,68 @@
package de.hsadmin.remote;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.HashMap;
import java.util.Map;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import de.hsadmin.core.qserv.CommandShell;
import de.hsadmin.core.qserv.ShellException;
import de.hsadmin.core.util.Config;
public class SSLCertDomainTest {
private static final String MODULE = "domain";
private XmlRpcClient client;
private RemoteCASHelper cas;
private Config config;
@Before
public void setUp() throws Exception {
client = RemoteTestHelper.getClient();
cas = new RemoteCASHelper();
config = Config.getInstance();
}
@After
public void tearDown() throws Exception {
client = null;
cas = null;
config = null;
}
@Test
public void testSSLCertWithoutChain() {
String user = "ad";
String grantingTicketURL = cas.getGrantingTicketURL(user);
Map<String, String> setParams = new HashMap<String, String>();
setParams.put("name", "aaa02");
setParams.put("hive", "h81");
setParams.put("customer", config.getProperty("accountprefix.customer") + "-aaa");
setParams.put("basepac", "DW/B");
setParams.put("curinetaddr", "176.9.242.75");
Object[] params = new Object[] { user,
cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
setParams };
try {
Object execute = client.execute(MODULE + ".add", params);
assertTrue(execute instanceof Map<?, ?>);
Thread.sleep(5000L);
CommandShell.execute("grep 'SSLCertificateChainFile' /var/local/lxc/hive/etc/apache2/sites-generated/aaa02.hostsharing.net");
fail("ShellException expected");
} catch (XmlRpcException e) {
fail(e.getMessage());
} catch (ShellException e) {
} catch (InterruptedException e) {
fail(e.getMessage());
}
}
}