Skip to content

Commit

Permalink
KEYCLOAK-2400 - changed adapter installation to be able to install it…
Browse files Browse the repository at this point in the history
… on windows.
  • Loading branch information
pdrozd committed Jan 27, 2016
1 parent 81569ce commit bf276a8
Showing 1 changed file with 36 additions and 25 deletions.
@@ -1,5 +1,9 @@
package org.keycloak.testsuite.arquillian;

import static org.keycloak.testsuite.auth.page.AuthRealm.ADMIN;
import static org.keycloak.testsuite.auth.page.AuthRealm.MASTER;
import static org.keycloak.testsuite.util.WaitUtils.pause;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -9,11 +13,13 @@
import java.net.URL;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.jboss.arquillian.container.spi.Container;
import org.jboss.arquillian.container.spi.ContainerRegistry;
import org.jboss.arquillian.container.spi.event.StartSuiteContainers;
import org.jboss.arquillian.container.spi.event.StopSuiteContainers;
import org.jboss.arquillian.container.spi.event.container.AfterStart;
import org.jboss.arquillian.container.test.api.ContainerController;
import org.jboss.arquillian.core.api.Event;
import org.jboss.arquillian.core.api.Instance;
Expand All @@ -22,7 +28,6 @@
import org.jboss.arquillian.core.api.annotation.Observes;
import org.jboss.arquillian.test.spi.annotation.ClassScoped;
import org.jboss.arquillian.test.spi.annotation.SuiteScoped;
import org.jboss.arquillian.container.spi.event.container.AfterStart;
import org.jboss.arquillian.test.spi.event.suite.BeforeClass;
import org.jboss.arquillian.test.spi.event.suite.BeforeSuite;
import org.jboss.logging.Logger;
Expand All @@ -32,10 +37,6 @@
import org.keycloak.testsuite.arquillian.annotation.AppServerContainer;
import org.keycloak.testsuite.util.OAuthClient;

import static org.keycloak.testsuite.auth.page.AuthRealm.ADMIN;
import static org.keycloak.testsuite.auth.page.AuthRealm.MASTER;
import static org.keycloak.testsuite.util.WaitUtils.pause;

/**
*
* @author tkyjovsk
Expand Down Expand Up @@ -126,7 +127,6 @@ public void afterStart(@Observes AfterStart event) throws IOException, Interrupt
log.info("\n\n### Starting keycloak current version ###\n");
alreadyStopped = true;
}

installAdapters(container);
}

Expand Down Expand Up @@ -275,50 +275,61 @@ public static String getAppServerContextRootFromSystemProperty() {

private void installAdapters(Container container) throws InterruptedException, IOException {
if (!alreadyInstalled && !skipInstallAdapters && isJBossBased(container)) {
String jbossCliPath = jbossHomePath + "/bin/jboss-cli.sh";
String adapterScriptPathArg = "--file=" + jbossHomePath + "/bin/adapter-install.cli";
String samlAdapterScriptPathArg = "--file=" + jbossHomePath + "/bin/adapter-install-saml.cli";
File bin = new File(jbossHomePath + "/bin");
String command = "java -jar " + jbossHomePath + "/bin/client/jboss-cli-client.jar";
String adapterScript = "adapter-install.cli";
String samlAdapterScript = "adapter-install-saml.cli";
String managementPort = container.getContainerConfiguration().getContainerProperties().get("managementPort");
String controllerArg = "--controller=localhost:" + managementPort;

log.info("Installing adapter to app server via cli script");
execCommand(new String[]{"/bin/sh", jbossCliPath, "--connect", adapterScriptPathArg, controllerArg});
log.info("Installing saml adapter to app server via cli script");
execCommand(new String[]{"/bin/sh", jbossCliPath, "--connect", samlAdapterScriptPathArg, controllerArg});
String controllerArg = " --controller=localhost:" + managementPort;
if (new File(bin, adapterScript).exists()) {
log.info("Installing adapter to app server via cli script");
execCommand(command + " --connect --file=" + adapterScript + controllerArg, bin);
}
if (new File(bin, samlAdapterScript).exists()) {
log.info("Installing saml adapter to app server via cli script");
execCommand(command + " --connect --file=" + samlAdapterScript + controllerArg, bin);
}
log.info("Restarting container");
execCommand(new String[]{"/bin/sh", jbossCliPath, "--connect", "--command=:reload", controllerArg});
pause(5000);
execCommand(command + " --connect --command=reload" + controllerArg, bin);
log.info("Container restarted");
pause(5000);
checkServerLog(jbossHomePath);
if (container.getName().startsWith("app-server")) {
alreadyInstalled = true;
}
}
}

private void execCommand(String... command) throws IOException, InterruptedException {
Process process = Runtime.getRuntime().exec(command);

private void execCommand(String command, File dir) throws IOException, InterruptedException {
Process process = Runtime.getRuntime().exec(command, null, dir);
if (process.waitFor(10, TimeUnit.SECONDS)) {
if (process.exitValue() != 0) {
getOutput("ERROR", process.getErrorStream());
throw new RuntimeException("Adapter installation failed. Process exitValue: "
+ process.exitValue() + "; <error output>\n" + getOutput(process.getErrorStream())
+ "</error output>");
+ process.exitValue());
}
getOutput("OUTPUT", process.getInputStream());
log.debug("process.isAlive(): " + process.isAlive());
} else {
process.destroyForcibly();
if (process.isAlive()) {
process.destroyForcibly();
}
throw new RuntimeException("Timeout after 10 seconds.");
}
}

private String getOutput(InputStream is) throws IOException {
private void getOutput(String type, InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
builder.append("<").append(type).append(">");
System.out.println(builder);
builder = new StringBuilder();
while (reader.ready()) {
builder.append(reader.readLine());
System.out.println(reader.readLine());
}
return builder.toString();
builder.append("</").append(type).append(">");
System.out.println(builder);
}

private boolean isJBossBased(Container container) {
Expand Down

0 comments on commit bf276a8

Please sign in to comment.