LoginCredentials.builder().user("root").password("romeo").build())).andThrow(
new AuthorizationException("Auth fail", null));
// run script without backgrounding (via predicate)
client2.connect();
expect(client2.exec("hostname\n")).andReturn(new ExecResponse("stub-r\n", "", 0));
client2.disconnect();
// run script without backgrounding (via id)
client2.connect();
expect(client2.exec("hostname\n")).andReturn(new ExecResponse("stub-r\n", "", 0));
client2.disconnect();
client2.connect();
try {
runScript(client2, "runScriptWithCreds",
Strings2.toStringAndClose(StubComputeServiceIntegrationTest.class
.getResourceAsStream("/runscript.sh")), 2);
} catch (IOException e) {
Throwables.propagate(e);
}
client2.disconnect();
client2New.connect();
try {
runScript(client2New, "adminUpdate", Strings2.toStringAndClose(StubComputeServiceIntegrationTest.class
.getResourceAsStream("/runscript_adminUpdate.sh")), 2);
} catch (IOException e) {
Throwables.propagate(e);
}
client2New.disconnect();
// check id
client2Foo.connect();
expect(client2Foo.getUsername()).andReturn("foo").atLeastOnce();
expect(client2Foo.getHostAddress()).andReturn("foo").atLeastOnce();
expect(client2Foo.exec("echo $USER\n")).andReturn(new ExecResponse("foo\n", "", 0));
client2Foo.disconnect();
expect(
factory.create(HostAndPort.fromParts("144.175.1.3", 22),
LoginCredentials.builder().user("root").password("password3").build())).andReturn(client3)
.times(2);
expect(
factory.create(HostAndPort.fromParts("144.175.1.4", 22),
LoginCredentials.builder().user("root").password("password4").build())).andReturn(client4)
.times(2);
expect(
factory.create(HostAndPort.fromParts("144.175.1.5", 22),
LoginCredentials.builder().user("root").password("password5").build())).andReturn(client5)
.times(2);
expect(
factory.create(HostAndPort.fromParts("144.175.1.6", 22),
LoginCredentials.builder().user("root").password("password6").build())).andReturn(client6)
.times(2);
expect(
factory.create(HostAndPort.fromParts("144.175.1.7", 22),
LoginCredentials.builder().user("root").password("password7").build())).andReturn(client7)
.times(2);
runScriptAndInstallSsh(client3, "bootstrap", 3);
runScriptAndInstallSsh(client4, "bootstrap", 4);
runScriptAndInstallSsh(client5, "bootstrap", 5);
runScriptAndInstallSsh(client6, "bootstrap", 6);
runScriptAndInstallSsh(client7, "bootstrap", 7);
expect(
factory.create(eq(HostAndPort.fromParts("144.175.1.1", 22)),
eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
.andReturn(client1);
expect(
factory.create(eq(HostAndPort.fromParts("144.175.1.2", 22)),
eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
.andReturn(client2);
expect(
factory.create(eq(HostAndPort.fromParts("144.175.1.3", 22)),
eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
.andReturn(client3);
expect(
factory.create(eq(HostAndPort.fromParts("144.175.1.4", 22)),
eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
.andReturn(client4);
expect(
factory.create(eq(HostAndPort.fromParts("144.175.1.5", 22)),
eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
.andReturn(client5);
expect(
factory.create(eq(HostAndPort.fromParts("144.175.1.6", 22)),
eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
.andReturn(client6);
expect(
factory.create(eq(HostAndPort.fromParts("144.175.1.7", 22)),
eq(LoginCredentials.builder().user("defaultAdminUsername").privateKey(Pems.PRIVATE_PKCS1_MARKER).build())))
.andReturn(client7);
helloAndJava(client2);
helloAndJava(client3);
helloAndJava(client4);
helloAndJava(client5);
helloAndJava(client6);
helloAndJava(client7);
replay(factory);
replay(client1);
replay(client1New);
replay(client2);
replay(client2New);
replay(client2Foo);
replay(client3);
replay(client4);
replay(client5);
replay(client6);
replay(client7);
bind(SshClient.Factory.class).toInstance(factory);
}
private void runScriptAndService(SshClient client, SshClient clientNew) {
client.connect();
try {
String scriptName = "configure-jetty";
client.put("/tmp/init-" + scriptName, Strings2.toStringAndClose(StubComputeServiceIntegrationTest.class
.getResourceAsStream("/initscript_with_jetty.sh")));
expect(client.exec("chmod 755 /tmp/init-" + scriptName)).andReturn(EXEC_GOOD);
expect(client.exec("ln -fs /tmp/init-" + scriptName + " " + scriptName)).andReturn(EXEC_GOOD);
expect(client.getUsername()).andReturn("root").atLeastOnce();
expect(client.getHostAddress()).andReturn("localhost").atLeastOnce();
expect(client.exec("/tmp/init-" + scriptName + " init")).andReturn(EXEC_GOOD);
expect(client.exec("/tmp/init-" + scriptName + " start")).andReturn(EXEC_GOOD);
expect(client.exec("/tmp/init-" + scriptName + " status")).andReturn(EXEC_GOOD);
// next status says the script is done, since not found.
expect(client.exec("/tmp/init-" + scriptName + " status")).andReturn(EXEC_BAD);
expect(client.exec("/tmp/init-" + scriptName + " stdout")).andReturn(EXEC_GOOD);
expect(client.exec("/tmp/init-" + scriptName + " stderr")).andReturn(EXEC_GOOD);
expect(client.exec("/tmp/init-" + scriptName + " exitstatus")).andReturn(EXEC_RC_GOOD);
// note we have to reconnect here, as we updated the login user.
client.disconnect();
clientNew.connect();
expect(clientNew.getUsername()).andReturn("web").atLeastOnce();
expect(clientNew.getHostAddress()).andReturn("localhost").atLeastOnce();
expect(clientNew.exec("head -1 /usr/local/jetty/VERSION.txt | cut -f1 -d ' '\n")).andReturn(EXEC_GOOD);
clientNew.disconnect();
clientNew.connect();
expect(clientNew.exec("java -fullversion\n")).andReturn(EXEC_GOOD);
clientNew.disconnect();
String startJetty = new StringBuilder()
.append("cd /usr/local/jetty").append('\n')
.append("nohup java -jar start.jar jetty.port=8080 > start.out 2> start.err < /dev/null &").append('\n')
.append("test $? && sleep 1").append('\n').toString();
clientNew.connect();
expect(clientNew.exec(startJetty)).andReturn(EXEC_GOOD);
clientNew.disconnect();
clientNew.connect();
expect(clientNew.exec("cd /usr/local/jetty\n./bin/jetty.sh stop\n")).andReturn(EXEC_GOOD);
clientNew.disconnect();
clientNew.connect();
expect(clientNew.exec(startJetty)).andReturn(EXEC_GOOD);
clientNew.disconnect();
} catch (IOException e) {
Throwables.propagate(e);
}
clientNew.disconnect();
}
private void runScriptAndInstallSsh(SshClient client, String scriptName, int nodeId) {
client.connect();
try {
runScript(client, scriptName, Strings2.toStringAndClose(StubComputeServiceIntegrationTest.class
.getResourceAsStream("/initscript_with_java.sh")), nodeId);
} catch (IOException e) {
Throwables.propagate(e);
}
client.disconnect();
}
private void runScript(SshClient client, String scriptName, String script, int nodeId) {
client.put("/tmp/init-" + scriptName, script);
expect(client.exec("chmod 755 /tmp/init-" + scriptName)).andReturn(EXEC_GOOD);
expect(client.exec("ln -fs /tmp/init-" + scriptName + " " + scriptName)).andReturn(EXEC_GOOD);
expect(client.getUsername()).andReturn("root").atLeastOnce();
expect(client.getHostAddress()).andReturn(nodeId + "").atLeastOnce();
expect(client.exec("/tmp/init-" + scriptName + " init")).andReturn(EXEC_GOOD);
expect(client.exec("/tmp/init-" + scriptName + " start")).andReturn(EXEC_GOOD);
expect(client.exec("/tmp/init-" + scriptName + " status")).andReturn(EXEC_GOOD);
// next status says the script is done, since not found.
expect(client.exec("/tmp/init-" + scriptName + " status")).andReturn(EXEC_BAD);
expect(client.exec("/tmp/init-" + scriptName + " stdout")).andReturn(EXEC_GOOD);
expect(client.exec("/tmp/init-" + scriptName + " stderr")).andReturn(EXEC_GOOD);
expect(client.exec("/tmp/init-" + scriptName + " exitstatus")).andReturn(EXEC_RC_GOOD);
}
private void helloAndJava(SshClient client) {
client.connect();
expect(client.exec("echo hello")).andReturn(new ExecResponse("hello", "", 0));
expect(client.exec("java -version")).andReturn(new ExecResponse("", "OpenJDK", 0));
client.disconnect();
}
};