changed setup function tests to be mocks so they don't conflict with test fixture setup

This commit is contained in:
Adrian Cole 2011-10-29 12:29:16 +02:00
parent 00c47eeab2
commit fe4a971d55
4 changed files with 219 additions and 65 deletions

View File

@ -20,17 +20,27 @@
package org.jclouds.virtualbox; package org.jclouds.virtualbox;
import static com.google.common.base.Preconditions.checkNotNull; 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 java.util.Properties;
import org.eclipse.jetty.server.Server;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory; import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.domain.Credentials;
import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.predicates.InetSocketAddressConnect;
import org.jclouds.sshj.config.SshjSshClientModule; 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.AfterGroups;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; 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.common.collect.ImmutableSet;
import com.google.inject.Module; import com.google.inject.Module;
@ -58,6 +68,8 @@ public class BaseVirtualBoxClientLiveTest {
} }
protected ComputeServiceContext context; protected ComputeServiceContext context;
protected VirtualBoxManager manager;
protected Server jetty;
@BeforeGroups(groups = { "live" }) @BeforeGroups(groups = { "live" })
public void setupClient() { public void setupClient() {
@ -66,12 +78,34 @@ public class BaseVirtualBoxClientLiveTest {
properties.setProperty(provider + ".apiversion", apiversion); properties.setProperty(provider + ".apiversion", apiversion);
context = new ComputeServiceContextFactory().createContext(provider, identity, credential, context = new ComputeServiceContextFactory().createContext(provider, identity, credential,
ImmutableSet.<Module> of(new Log4JLoggingModule(), new SshjSshClientModule())); ImmutableSet.<Module> of(new Log4JLoggingModule(), new SshjSshClientModule()));
jetty = new StartJettyIfNotAlreadyRunning(port).apply(basebaseResource);
startVboxIfNotAlreadyRunning();
} }
@AfterGroups(groups = "live") @AfterGroups(groups = "live")
protected void tearDown() { protected void tearDown() throws Exception {
if (context != null) if (context != null)
context.close(); 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;
}
} }

View File

@ -19,34 +19,37 @@
package org.jclouds.virtualbox.functions.admin; 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 static org.testng.Assert.assertEquals;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
/** /**
* @author Andrea Turli * @author Andrea Turli
*/ */
@Test(groups = "live", singleThreaded = true, testName = "StartJettyIfNotAlreadyRunningLiveTest") @Test(groups = "unit", singleThreaded = true, testName = "StartJettyIfNotAlreadyRunningTest")
public class StartJettyIfNotAlreadyRunningLiveTest extends BaseVirtualBoxClientLiveTest { public class StartJettyIfNotAlreadyRunningTest {
private String basebaseResource = "."; private String basebaseResource = ".";
private String port = "8080"; private int port = 8080;
@Test @Test
public void testLaunchJettyServer() throws Exception { public void testLaunchJettyServerWhenAlreadyRunningDoesntLaunchAgain() {
Server server = new StartJettyIfNotAlreadyRunning(port).apply(basebaseResource); Server jetty = createMock(Server.class);
server.stop(); expect(jetty.getState()).andReturn(Server.STARTED);
assertEquals(server.getState(), server.STOPPED); replay(jetty);
assertEquals(new StartJettyIfNotAlreadyRunning(jetty, port).apply(basebaseResource), jetty);
verify(jetty);
} }
@Test @Test
public void testLaunchingSameJettyServer() throws Exception { public void testLaunchJettyServerWhenNotRunningStartsJettyOnCorrectHostPortAndBasedir() {
Server server = new StartJettyIfNotAlreadyRunning(port).apply(basebaseResource); // TODO: all yours!
assertEquals(server.getState(), server.STARTED);
Server sameServer = new StartJettyIfNotAlreadyRunning(port).apply(basebaseResource);
sameServer.stop();
} }
} }

View File

@ -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);
}
}

View File

@ -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<IPSocket> 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<IPSocket> 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.<Module> of(new StartingVBoxWhenNotRunningModule())).getComputeService();
NodeMetadata node = Iterables.getOnlyElement(compute.createNodesInGroup("foo", 1));
String hostId = node.getId();
VirtualBoxManager manager = createMock(VirtualBoxManager.class);
@SuppressWarnings("unchecked")
Predicate<IPSocket> 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);
}
}