diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java index a8e1d57995..9bb431f149 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java @@ -20,17 +20,27 @@ package org.jclouds.virtualbox; import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest; +import java.net.URI; import java.util.Properties; +import org.eclipse.jetty.server.Server; import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContextFactory; +import org.jclouds.domain.Credentials; import org.jclouds.logging.log4j.config.Log4JLoggingModule; +import org.jclouds.predicates.InetSocketAddressConnect; import org.jclouds.sshj.config.SshjSshClientModule; +import org.jclouds.virtualbox.config.VirtualBoxConstants; +import org.jclouds.virtualbox.functions.admin.StartJettyIfNotAlreadyRunning; +import org.jclouds.virtualbox.functions.admin.StartVBoxIfNotAlreadyRunning; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; +import org.virtualbox_4_1.SessionState; +import org.virtualbox_4_1.VirtualBoxManager; import com.google.common.collect.ImmutableSet; import com.google.inject.Module; @@ -58,6 +68,8 @@ public class BaseVirtualBoxClientLiveTest { } protected ComputeServiceContext context; + protected VirtualBoxManager manager; + protected Server jetty; @BeforeGroups(groups = { "live" }) public void setupClient() { @@ -66,12 +78,34 @@ public class BaseVirtualBoxClientLiveTest { properties.setProperty(provider + ".apiversion", apiversion); context = new ComputeServiceContextFactory().createContext(provider, identity, credential, ImmutableSet. of(new Log4JLoggingModule(), new SshjSshClientModule())); + jetty = new StartJettyIfNotAlreadyRunning(port).apply(basebaseResource); + startVboxIfNotAlreadyRunning(); } @AfterGroups(groups = "live") - protected void tearDown() { + protected void tearDown() throws Exception { if (context != null) context.close(); + if (jetty != null) + jetty.stop(); + // TODO: should we stop the vbox manager? } + private String basebaseResource = "."; + // TODO: I'd not use 8080, maybe something like 28080 + // also update pom.xml so that this passes through + private int port = Integer.parseInt(System.getProperty(VirtualBoxConstants.VIRTUALBOX_JETTY_PORT, "8080")); + + protected void startVboxIfNotAlreadyRunning() { + Credentials localhostCredentials = new Credentials("toor", "password"); + ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest("hostId", "localhost", "guestId", + "localhost", localhostCredentials); + + manager = new StartVBoxIfNotAlreadyRunning(localHostContext.getComputeService(), + VirtualBoxManager.createInstance("hostId"), new InetSocketAddressConnect(), "hostId", localhostCredentials) + .apply(URI.create(endpoint)); + + assert manager.getSessionObject().getState() == SessionState.Unlocked : "manager needs to be in unlocked state or all tests will fail!!: " + + manager; + } } diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartJettyIfNotAlreadyRunningLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartJettyIfNotAlreadyRunningTest.java similarity index 56% rename from sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartJettyIfNotAlreadyRunningLiveTest.java rename to sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartJettyIfNotAlreadyRunningTest.java index d9b3f8d08f..bcca15eaab 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartJettyIfNotAlreadyRunningLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartJettyIfNotAlreadyRunningTest.java @@ -19,34 +19,37 @@ package org.jclouds.virtualbox.functions.admin; +import static org.easymock.EasyMock.expect; +import static org.easymock.classextension.EasyMock.createMock; +import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.classextension.EasyMock.verify; import static org.testng.Assert.assertEquals; import org.eclipse.jetty.server.Server; -import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.testng.annotations.Test; /** * @author Andrea Turli */ -@Test(groups = "live", singleThreaded = true, testName = "StartJettyIfNotAlreadyRunningLiveTest") -public class StartJettyIfNotAlreadyRunningLiveTest extends BaseVirtualBoxClientLiveTest { +@Test(groups = "unit", singleThreaded = true, testName = "StartJettyIfNotAlreadyRunningTest") +public class StartJettyIfNotAlreadyRunningTest { private String basebaseResource = "."; - private String port = "8080"; + private int port = 8080; @Test - public void testLaunchJettyServer() throws Exception { - Server server = new StartJettyIfNotAlreadyRunning(port).apply(basebaseResource); - server.stop(); - assertEquals(server.getState(), server.STOPPED); + public void testLaunchJettyServerWhenAlreadyRunningDoesntLaunchAgain() { + Server jetty = createMock(Server.class); + expect(jetty.getState()).andReturn(Server.STARTED); + replay(jetty); + + assertEquals(new StartJettyIfNotAlreadyRunning(jetty, port).apply(basebaseResource), jetty); + verify(jetty); + } @Test - public void testLaunchingSameJettyServer() throws Exception { - Server server = new StartJettyIfNotAlreadyRunning(port).apply(basebaseResource); - assertEquals(server.getState(), server.STARTED); - Server sameServer = new StartJettyIfNotAlreadyRunning(port).apply(basebaseResource); - sameServer.stop(); + public void testLaunchJettyServerWhenNotRunningStartsJettyOnCorrectHostPortAndBasedir() { + // TODO: all yours! } - } diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartVBoxIfNotAlreadyRunningLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartVBoxIfNotAlreadyRunningLiveTest.java deleted file mode 100644 index d87dad2aec..0000000000 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartVBoxIfNotAlreadyRunningLiveTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.jclouds.virtualbox.functions.admin; - -import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest; -import static org.testng.Assert.assertEquals; - -import java.net.URI; - -import org.jclouds.compute.ComputeServiceContext; -import org.jclouds.domain.Credentials; -import org.testng.annotations.Test; -import org.virtualbox_4_1.SessionState; -import org.virtualbox_4_1.VirtualBoxManager; - -@Test(groups = "live", singleThreaded = true, testName = "startVBoxIfNotAlreadyRunningLiveTest") -public class StartVBoxIfNotAlreadyRunningLiveTest { - - @Test - public void testStartVbox() throws Exception { - Credentials localhostCredentials = new Credentials("toor", "password"); - ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest("hostId", "localhost", "guestId", - "localhost", localhostCredentials); - - URI endpointUri = URI.create("http://localhost:18083/"); - - VirtualBoxManager manager = new StartVBoxIfNotAlreadyRunning(localHostContext, "hostId", localhostCredentials) - .apply(endpointUri); - - assertEquals(manager.getSessionObject().getState(), SessionState.Unlocked); - } - -} diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartVBoxIfNotAlreadyRunningTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartVBoxIfNotAlreadyRunningTest.java new file mode 100644 index 0000000000..00506d22b1 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartVBoxIfNotAlreadyRunningTest.java @@ -0,0 +1,167 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.jclouds.virtualbox.functions.admin; + +import static org.easymock.EasyMock.expect; +import static org.easymock.classextension.EasyMock.createMock; +import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.classextension.EasyMock.verify; +import static org.testng.Assert.assertEquals; + +import java.net.URI; + +import org.jclouds.compute.ComputeService; +import org.jclouds.compute.ComputeServiceContextFactory; +import org.jclouds.compute.domain.ExecResponse; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.domain.Credentials; +import org.jclouds.net.IPSocket; +import org.jclouds.ssh.ConfiguresSshClient; +import org.jclouds.ssh.SshClient; +import org.testng.annotations.Test; +import org.virtualbox_4_1.VirtualBoxManager; + +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.inject.AbstractModule; +import com.google.inject.Module; + +@Test(groups = "unit", singleThreaded = true, testName = "StartVBoxIfNotAlreadyRunningTest") +public class StartVBoxIfNotAlreadyRunningTest { + + @Test(expectedExceptions = IllegalStateException.class) + public void testStartVboxThrowsIllegalStateExceptionIfTheNodeIdConfiguredIsntAround() throws Exception { + + ComputeService compute = new ComputeServiceContextFactory().createContext("stub", "foo", "bar") + .getComputeService(); + + VirtualBoxManager manager = createMock(VirtualBoxManager.class); + @SuppressWarnings("unchecked") + Predicate socketTester = createMock(Predicate.class); + String hostId = "hostId"; + URI endpointUri = URI.create("http://localhost:18083/"); + Credentials localhostCredentials = new Credentials("toor", "password"); + + manager.connect(endpointUri.toASCIIString(), localhostCredentials.identity, localhostCredentials.credential); + + replay(socketTester); + replay(manager); + + new StartVBoxIfNotAlreadyRunning(compute, manager, socketTester, hostId, localhostCredentials).apply(endpointUri); + + } + + @Test + public void testStartVboxConnectsToManagerWhenPortAlreadyListening() throws Exception { + + ComputeService compute = new ComputeServiceContextFactory().createContext("stub", "foo", "bar") + .getComputeService(); + + // TODO: possibly better to use a defined name as opposed to an id, since + // most compute services the id is not predictable. + NodeMetadata node = Iterables.getOnlyElement(compute.createNodesInGroup("foo", 1)); + String hostId = node.getId(); + + VirtualBoxManager manager = createMock(VirtualBoxManager.class); + @SuppressWarnings("unchecked") + Predicate socketTester = createMock(Predicate.class); + URI endpointUri = URI.create("http://localhost:18083/"); + Credentials localhostCredentials = new Credentials("toor", "password"); + + expect(socketTester.apply(new IPSocket(endpointUri.getHost(), endpointUri.getPort()))).andReturn(true); + manager.connect(endpointUri.toASCIIString(), localhostCredentials.identity, localhostCredentials.credential); + + replay(socketTester); + replay(manager); + + assertEquals( + new StartVBoxIfNotAlreadyRunning(compute, manager, socketTester, hostId, localhostCredentials) + .apply(endpointUri), + manager); + + verify(socketTester); + verify(manager); + } + + @ConfiguresSshClient + static class StartingVBoxWhenNotRunningModule extends AbstractModule { + + @Override + protected void configure() { + SshClient.Factory factory = createMock(SshClient.Factory.class); + SshClient client = createMock(SshClient.class); + // NOTE we may want to switch to a node supplier so that we can predict + // these values. Right now, it is node 2 since the above test made node + // 1. + IPSocket expectedSshSockectFor2ndCreatedNode = new IPSocket("144.175.1.2", 22); + Credentials expectedCredentialsFor2ndCreatedNode = new Credentials("root", "password2"); + expect(factory.create(expectedSshSockectFor2ndCreatedNode, expectedCredentialsFor2ndCreatedNode)).andReturn( + client).times(2); + + expect(client.getUsername()).andReturn(expectedCredentialsFor2ndCreatedNode.identity).times(2); + expect(client.getHostAddress()).andReturn(expectedSshSockectFor2ndCreatedNode.getAddress()).times(2); + + client.disconnect(); + client.connect(); + expect(client.exec("VBoxManage setproperty websrvauthlibrary null\n")).andReturn(new ExecResponse("", "", 0)); + + client.disconnect(); + client.connect(); + expect(client.exec("vboxwebsrv -t 10000 -v -b\n")).andReturn(new ExecResponse("", "", 0)); + + replay(factory); + replay(client); + bind(SshClient.Factory.class).toInstance(factory); + + } + + } + + @Test + public void testStartVboxDisablesPasswordAccessOnWebsrvauthlibraryStartsVboxwebsrvInBackgroundAndConnectsManagerWhenPortIsNotListening() + throws Exception { + ComputeService compute = new ComputeServiceContextFactory().createContext("stub", "foo", "bar", + ImmutableSet. of(new StartingVBoxWhenNotRunningModule())).getComputeService(); + NodeMetadata node = Iterables.getOnlyElement(compute.createNodesInGroup("foo", 1)); + String hostId = node.getId(); + + VirtualBoxManager manager = createMock(VirtualBoxManager.class); + @SuppressWarnings("unchecked") + Predicate socketTester = createMock(Predicate.class); + Credentials localhostCredentials = new Credentials("toor", "password"); + URI endpointUri = URI.create("http://localhost:18083/"); + + expect(socketTester.apply(new IPSocket(endpointUri.getHost(), endpointUri.getPort()))).andReturn(false); + + manager.connect(endpointUri.toASCIIString(), localhostCredentials.identity, localhostCredentials.credential); + + replay(socketTester); + replay(manager); + + assertEquals( + new StartVBoxIfNotAlreadyRunning(compute, manager, socketTester, hostId, localhostCredentials) + .apply(endpointUri), + manager); + + verify(socketTester); + verify(manager); + } +}