From 4412aaf7265e640b3a51e24ba23b255c18aaf5c2 Mon Sep 17 00:00:00 2001 From: David Ribeiro Alves Date: Sat, 10 Mar 2012 01:45:10 +0000 Subject: [PATCH 1/2] working cluster + live test using compute service + nat + host-only --- .../VirtualBoxComputeServiceAdapter.java | 17 ++++- .../virtualbox/domain/NetworkAdapter.java | 17 ++++- .../domain/NetworkInterfaceCard.java | 4 +- .../virtualbox/domain/NetworkSpec.java | 7 +- .../functions/AttachHostOnlyAdapter.java | 55 ++++++++++++++ .../functions/AttachNicToMachine.java | 6 ++ .../virtualbox/functions/MastersCache.java | 28 ++++--- .../virtualbox/functions/NodeCreator.java | 43 +++++++++-- .../virtualbox/statements/SetIpAddress.java | 76 +++++++++++++++++++ .../BaseVirtualBoxClientLiveTest.java | 13 +++- ...rtualBoxComputeServiceAdapterLiveTest.java | 28 +++---- .../compute/VirtualBoxExperimentLiveTest.java | 43 ++++++++++- ...romIMachineIfNotAlreadyExistsLiveTest.java | 2 +- .../functions/CreateAndInstallVmLiveTest.java | 2 +- ...hineFromIsoIfNotAlreadyExistsLiveTest.java | 2 +- .../GuestAdditionsInstallerLiveTest.java | 2 +- .../IMachinePredicatesLiveTest.java | 2 +- 17 files changed, 294 insertions(+), 53 deletions(-) create mode 100644 labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachHostOnlyAdapter.java create mode 100644 labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetIpAddress.java diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java index 9c1d5628a2..37d684f107 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java @@ -26,13 +26,17 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_ import java.util.Map; +import javax.annotation.Resource; import javax.inject.Inject; +import javax.inject.Named; import org.jclouds.compute.ComputeServiceAdapter; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Template; +import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.domain.Location; import org.jclouds.javax.annotation.Nullable; +import org.jclouds.logging.Logger; import org.jclouds.virtualbox.domain.Master; import org.jclouds.virtualbox.domain.NodeSpec; import org.jclouds.virtualbox.domain.YamlImage; @@ -40,6 +44,7 @@ import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IProgress; import org.virtualbox_4_1.ISession; +import org.virtualbox_4_1.MachineState; import org.virtualbox_4_1.SessionState; import org.virtualbox_4_1.VirtualBoxManager; @@ -61,6 +66,10 @@ import com.google.inject.Singleton; @Singleton public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter { + @Resource + @Named(ComputeServiceConstants.COMPUTE_LOGGER) + protected Logger logger = Logger.NULL; + private final Supplier manager; private final Map images; private final LoadingCache mastersLoader; @@ -81,6 +90,7 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter() { @Override public boolean apply(IMachine arg0) { - return !arg0.getName().startsWith(VIRTUALBOX_NODE_PREFIX); + return arg0.getName().startsWith(VIRTUALBOX_NODE_PREFIX); } }); } @@ -178,6 +188,11 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter redirectRules; + private final String staticIp; public NetworkAdapter(NetworkAttachmentType networkAttachmentType, - String macAddress, Set redirectRules) { + String macAddress, Set redirectRules, + String staticIp) { this.networkAttachmentType = checkNotNull(networkAttachmentType, "networkAttachmentType"); this.macAddress = macAddress; this.redirectRules = ImmutableSet.copyOf(redirectRules); + this.staticIp = staticIp; } public static Builder builder() { @@ -59,6 +62,7 @@ public class NetworkAdapter { private NetworkAttachmentType networkAttachmentType; private String macAddress; private Set redirectRules = Sets.newLinkedHashSet(); + private String staticIp; /** * @@ -112,10 +116,15 @@ public class NetworkAdapter { guest, guestPort)); return this; } + + public Builder staticIp(String staticIp) { + this.staticIp = staticIp; + return this; + } public NetworkAdapter build() { return new NetworkAdapter(networkAttachmentType, macAddress, - redirectRules); + redirectRules,staticIp); } } @@ -130,6 +139,10 @@ public class NetworkAdapter { public String getMacAddress() { return macAddress; } + + public String getStaticIp() { + return staticIp; + } @Override public boolean equals(Object o) { diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkInterfaceCard.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkInterfaceCard.java index 427ec8b096..8d338bd108 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkInterfaceCard.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkInterfaceCard.java @@ -18,6 +18,7 @@ */ package org.jclouds.virtualbox.domain; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.Objects; @@ -41,11 +42,12 @@ public class NetworkInterfaceCard { public static class Builder { - private long slot; + private long slot = 0L; private NetworkAdapter networkAdapter; private String hostInterfaceName; public Builder slot(long slot) { + checkArgument(slot >= 0 && slot < 4, "must be 0, 1, 2, 3: %s", slot); this.slot = slot; return this; } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkSpec.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkSpec.java index ea1745455b..62b2930422 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkSpec.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkSpec.java @@ -18,7 +18,6 @@ */ package org.jclouds.virtualbox.domain; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import java.util.ArrayList; @@ -47,10 +46,8 @@ public class NetworkSpec { private List networkInterfaceCards = new ArrayList(); - public Builder addNIC(long slot, NetworkInterfaceCard networkInterfaceCard) { - checkArgument(slot >= 0 && slot < 4, "must be 0, 1, 2, 3: %s", slot); - NetworkInterfaceCard nic = NetworkInterfaceCard.builder().slot(slot).addNetworkAdapter(networkInterfaceCard.getNetworkAdapter()).build(); - this.networkInterfaceCards.add(nic); + public Builder addNIC(NetworkInterfaceCard networkInterfaceCard) { + this.networkInterfaceCards.add(networkInterfaceCard); return this; } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachHostOnlyAdapter.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachHostOnlyAdapter.java new file mode 100644 index 0000000000..35ee8c9c78 --- /dev/null +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachHostOnlyAdapter.java @@ -0,0 +1,55 @@ +/** + * 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; + +import static org.virtualbox_4_1.NetworkAdapterType.Am79C973; +import static org.virtualbox_4_1.NetworkAttachmentType.HostOnly; + +import javax.annotation.Nullable; + +import org.jclouds.virtualbox.domain.NetworkInterfaceCard; +import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.INetworkAdapter; + +import com.google.common.base.Function; + +/** + * @author dralves + */ +public class AttachHostOnlyAdapter implements Function { + + private NetworkInterfaceCard networkInterfaceCard; + + public AttachHostOnlyAdapter(NetworkInterfaceCard networkInterfaceCard) { + this.networkInterfaceCard = networkInterfaceCard; + } + + @Override + public Void apply(@Nullable IMachine machine) { + INetworkAdapter iNetworkAdapter = machine.getNetworkAdapter(networkInterfaceCard.getSlot()); + iNetworkAdapter.setAttachmentType(HostOnly); + iNetworkAdapter.setAdapterType(Am79C973); + iNetworkAdapter.setMACAddress(networkInterfaceCard.getNetworkAdapter().getMacAddress()); + iNetworkAdapter.setHostOnlyInterface(networkInterfaceCard.getHostInterfaceName()); + iNetworkAdapter.setEnabled(true); + machine.saveSettings(); + return null; + } + +} diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNicToMachine.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNicToMachine.java index fd161ec8bc..daaa28c610 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNicToMachine.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNicToMachine.java @@ -49,6 +49,8 @@ public class AttachNicToMachine implements Function } else if (hasBridgedAdapter(nic)) { return machineUtils.writeLockMachineAndApply(vmName, new AttachBridgedAdapterToMachine(nic)); + } else if (hasHostOnlyAdapter(nic)) { + return machineUtils.writeLockMachineAndApply(vmName, new AttachHostOnlyAdapter(nic)); } else return null; } @@ -62,4 +64,8 @@ public class AttachNicToMachine implements Function return nic.getNetworkAdapter().getNetworkAttachmentType() .equals(NetworkAttachmentType.Bridged); } + + private boolean hasHostOnlyAdapter(NetworkInterfaceCard nic) { + return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.HostOnly); + } } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MastersCache.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MastersCache.java index e2869859a9..146123d86d 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MastersCache.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/MastersCache.java @@ -37,6 +37,7 @@ import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Singleton; import org.jclouds.Constants; import org.jclouds.compute.domain.Image; @@ -72,7 +73,10 @@ import com.google.common.collect.Maps; * @author dralves * */ +@Singleton public class MastersCache extends AbstractLoadingCache { + + public static final int MASTER_PORT = 2222; @Resource @Named(ComputeServiceConstants.COMPUTE_LOGGER) @@ -121,7 +125,7 @@ public class MastersCache extends AbstractLoadingCache { public synchronized Master get(Image key) throws ExecutionException { // check if we have loaded this machine before if (masters.containsKey(key.getId())) { - return masters.get(key); + return masters.get(key.getId()); } String guestAdditionsFileName = String.format("VBoxGuestAdditions_%s.iso", version); @@ -154,12 +158,12 @@ public class MastersCache extends AbstractLoadingCache { .controller(ideController).forceOverwrite(true).cleanUpMode(CleanupMode.Full).build(); NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT) - .tcpRedirectRule("127.0.0.1", 2222, "", 22).build(); + .tcpRedirectRule("127.0.0.1", MASTER_PORT, "", 22).build(); NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) - .build(); + .slot(0L).build(); - NetworkSpec networkSpec = NetworkSpec.builder().addNIC(0L, networkInterfaceCard).build(); + NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); MasterSpec masterSpec = MasterSpec .builder() @@ -189,6 +193,14 @@ public class MastersCache extends AbstractLoadingCache { return master; } + @Override + public synchronized Master getIfPresent(Image key) { + if (masters.containsKey(key.getId())) { + return masters.get(key.getId()); + } + return null; + } + private String getFilePathOrDownload(String httpUrl) throws ExecutionException { String fileName = httpUrl.substring(httpUrl.lastIndexOf('/') + 1, httpUrl.length()); File localFile = new File(isosDir, fileName); @@ -200,12 +212,4 @@ public class MastersCache extends AbstractLoadingCache { return localFile.getAbsolutePath(); } - @Override - public Master getIfPresent(Image key) { - if (masters.containsKey(key.getId())) { - return masters.get(key.getId()); - } - return null; - } - } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java index 3b01c2d595..b32137ce2c 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java @@ -19,13 +19,18 @@ mh * Licensed to jclouds, Inc. (jclouds) under one or more package org.jclouds.virtualbox.functions; +import static com.google.common.base.Preconditions.checkNotNull; import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX; import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX; +import java.util.concurrent.atomic.AtomicInteger; + import javax.inject.Inject; import javax.inject.Singleton; import org.jclouds.compute.ComputeServiceAdapter.NodeAndInitialCredentials; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.options.RunScriptOptions; import org.jclouds.domain.LoginCredentials; import org.jclouds.virtualbox.domain.CloneSpec; import org.jclouds.virtualbox.domain.ExecutionType; @@ -35,6 +40,7 @@ import org.jclouds.virtualbox.domain.NetworkInterfaceCard; import org.jclouds.virtualbox.domain.NetworkSpec; import org.jclouds.virtualbox.domain.NodeSpec; import org.jclouds.virtualbox.domain.VmSpec; +import org.jclouds.virtualbox.statements.SetIpAddress; import org.jclouds.virtualbox.util.MachineUtils; import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; @@ -48,20 +54,35 @@ import com.google.common.base.Supplier; @Singleton public class NodeCreator implements Function> { + public static final int NODE_PORT_INIT = 3000; + public static final String VMS_NETWORK = "33.33.33."; + public static final String HOST_ONLY_IFACE_NAME = "vboxnet0"; + private final Supplier manager; private final Function cloner; + private final AtomicInteger nodePorts; + private final AtomicInteger nodeIps; + private MachineUtils machineUtils; + private Function imachineToNodeMetadata; @Inject public NodeCreator(Supplier manager, Function cloner, - MachineUtils machineUtils) { + MachineUtils machineUtils, Function imachineToNodeMetadata) { this.manager = manager; this.cloner = cloner; + this.nodePorts = new AtomicInteger(3000); + this.nodeIps = new AtomicInteger(1); + this.machineUtils = machineUtils; + this.imachineToNodeMetadata = imachineToNodeMetadata; } @Override public NodeAndInitialCredentials apply(NodeSpec nodeSpec) { + checkNotNull(nodeSpec, "NodeSpec"); + Master master = nodeSpec.getMaster(); + checkNotNull(master, "Master"); if (master.getMachine().getCurrentSnapshot() != null) { ISession session; @@ -81,21 +102,29 @@ public class NodeCreator implements Function nodeAndInitialCredentials = new NodeAndInitialCredentials(cloned, cloneName, LoginCredentials.builder().user("toor").password("password").authenticateSudo(true).build()); diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetIpAddress.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetIpAddress.java new file mode 100644 index 0000000000..e71992e09e --- /dev/null +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetIpAddress.java @@ -0,0 +1,76 @@ +/** + * 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.statements; + +import static com.google.common.base.Preconditions.checkNotNull; + +import org.jclouds.scriptbuilder.domain.OsFamily; +import org.jclouds.scriptbuilder.domain.Statement; +import org.jclouds.virtualbox.domain.NetworkInterfaceCard; + +import com.google.common.collect.ImmutableList; + +/** + * Sets the ipaddress using ssh. Used for host-only networking + * + * @author dralves + * + */ +public class SetIpAddress implements Statement { + + private String script; + + public SetIpAddress(NetworkInterfaceCard networkInterfaceCard) { + String ipAddress = networkInterfaceCard.getNetworkAdapter().getStaticIp(); + checkNotNull(ipAddress, "ip address"); + int slot = (int) networkInterfaceCard.getSlot(); + String iface = null; + switch (slot) { + case 0: + iface = "eth0"; + break; + case 1: + iface = "eth1"; + break; + case 2: + iface = "eth2"; + break; + case 3: + iface = "eth3"; + break; + default: + throw new IllegalArgumentException("slot must be 0,1,2,3 (was: " + slot + ")"); + } + script = String.format("ifconfig %s %s;", iface, ipAddress); + } + + @Override + public Iterable functionDependencies(OsFamily family) { + return ImmutableList.of(); + } + + @Override + public String render(OsFamily family) { + if (checkNotNull(family, "family") == OsFamily.WINDOWS) + throw new UnsupportedOperationException("windows not yet implemented"); + return script; + } + +} diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java index c50c479408..5478773143 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java @@ -24,6 +24,8 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.io.File; import java.net.URI; import java.util.Properties; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import javax.inject.Inject; import javax.inject.Named; @@ -39,9 +41,9 @@ import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.OsFamily; import org.jclouds.compute.domain.Template; import org.jclouds.compute.strategy.PrioritizeCredentialsFromTemplate; +import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; import org.jclouds.sshj.config.SshjSshClientModule; -import org.jclouds.virtualbox.compute.VirtualBoxComputeServiceAdapter; import org.jclouds.virtualbox.config.VirtualBoxConstants; import org.jclouds.virtualbox.domain.IsoSpec; import org.jclouds.virtualbox.domain.Master; @@ -109,6 +111,8 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest { protected PrioritizeCredentialsFromTemplate prioritizeCredentialsFromTemplate; @Inject protected LoadingCache mastersCache; + + private final ExecutorService singleThreadExec = Executors.newSingleThreadExecutor(); @Override protected void setupCredentials() { @@ -140,8 +144,10 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest { .credentialUrl(URI.create("file://" + System.getProperty("user.home") + "/.ssh/id_rsa")) .build())); - context = new ComputeServiceContextFactory().createContext(provider, identity, credential, - ImmutableSet. of(new SLF4JLoggingModule(), new SshjSshClientModule(), hostModule), overrides); + context = new ComputeServiceContextFactory().createContext(provider, identity, credential, ImmutableSet + . of(new SLF4JLoggingModule(), new SshjSshClientModule(), hostModule, new ExecutorServiceModule( + singleThreadExec, singleThreadExec)), overrides); + context.utils().injector().injectMembers(this); imageId = "ubuntu-11.04-server-i386"; @@ -157,6 +163,7 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest { checkNotNull(mastersCache.apply(template.getImage())); } + protected void undoVm(VmSpec vmSpecification) { machineUtils.unlockMachineAndApplyOrReturnNullIfNotRegistered(vmSpecification.getVmId(), new UnregisterMachineIfExistsAndDeleteItsMedia(vmSpecification)); diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java index 4dc90bd45a..a427a05b0a 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java @@ -58,20 +58,6 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien assertEquals(machine.getNode().getName(), machineName); doConnectViaSsh(machine.getNode(), prioritizeCredentialsFromTemplate.apply(template, machine.getCredentials())); } - - @Test - public void testListHardwareProfiles() { - Iterable profiles = adapter.listHardwareProfiles(); - assertEquals(1, Iterables.size(profiles)); - //TODO: check state; - } - - @Test - public void testListImages() { - Iterable iMageIterable = adapter.listImages(); - assertEquals(1, Iterables.size(iMageIterable)); - //TODO: check state; - } protected void doConnectViaSsh(IMachine machine, LoginCredentials creds) { SshClient ssh = context.utils().injector().getInstance(IMachineToSshClient.class).apply(machine); @@ -88,6 +74,20 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien } } + @Test + public void testListHardwareProfiles() { + Iterable profiles = adapter.listHardwareProfiles(); + assertEquals(1, Iterables.size(profiles)); + //TODO: check state; + } + + @Test + public void testListImages() { + Iterable iMageIterable = adapter.listImages(); + assertEquals(1, Iterables.size(iMageIterable)); + //TODO: check state; + } + @AfterClass @Override protected void tearDown() throws Exception { diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxExperimentLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxExperimentLiveTest.java index 996dd31c3d..a4d330d48b 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxExperimentLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxExperimentLiveTest.java @@ -19,19 +19,56 @@ package org.jclouds.virtualbox.compute; +import static org.testng.Assert.assertEquals; + +import java.util.Set; + +import javax.annotation.Resource; +import javax.inject.Named; + +import org.jclouds.compute.RunNodesException; +import org.jclouds.compute.domain.ExecResponse; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.reference.ComputeServiceConstants; +import org.jclouds.logging.Logger; +import org.jclouds.ssh.SshClient; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.testng.annotations.Test; +import com.google.common.base.Predicate; + /** * * @author Adrian Cole */ -@Test(groups = "live", testName = "VirtualBoxExperimentLiveTest") +@Test(groups = "live", singleThreaded = true, testName = "VirtualBoxExperimentLiveTest") public class VirtualBoxExperimentLiveTest extends BaseVirtualBoxClientLiveTest { + + + @Resource + @Named(ComputeServiceConstants.COMPUTE_LOGGER) + protected Logger logger = Logger.NULL; @Test - public void testAndExperiment() { - context.getComputeService().listNodes(); + public void testLaunchCluster() throws RunNodesException { + int numNodes = 4; + final String clusterName = "test-launch-cluster"; + Set nodes = context.getComputeService().createNodesInGroup(clusterName, + numNodes); + assertEquals(numNodes, nodes.size(), "wrong number of nodes"); + for (NodeMetadata node : nodes) { + logger.debug("Created Node: %s", node); + SshClient client = context.utils().sshForNode().apply(node); + client.connect(); + ExecResponse hello = client.exec("echo hello"); + assertEquals(hello.getOutput().trim(), "hello"); + } + context.getComputeService().destroyNodesMatching(new Predicate() { + @Override + public boolean apply(NodeMetadata input) { + return input.getId().contains(clusterName); + } + }); } } \ No newline at end of file diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest.java index c72f0c98cf..8912476737 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest.java @@ -99,7 +99,7 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest exten NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) .build(); - this.cloneNetworkSpec = NetworkSpec.builder().addNIC(0L, networkInterfaceCard).build(); + this.cloneNetworkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); sourceMachineSpec = MasterSpec.builder().iso(isoSpec).vm(sourceVmSpec).network(cloneNetworkSpec).build(); diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java index 75d36b06a2..87db6eee34 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java @@ -105,7 +105,7 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) .build(); - NetworkSpec networkSpec = NetworkSpec.builder().addNIC(0L, networkInterfaceCard).build(); + NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); masterSpec = MasterSpec .builder() diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java index 204f758d10..982a40529f 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java @@ -80,7 +80,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends .builder().addNetworkAdapter(networkAdapter).build(); NetworkSpec networkSpec = NetworkSpec.builder() - .addNIC(0L, networkInterfaceCard).build(); + .addNIC(networkInterfaceCard).build(); MasterSpec machineSpec = MasterSpec .builder() diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java index eafbea88d7..ce0d1504eb 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java @@ -99,7 +99,7 @@ public class GuestAdditionsInstallerLiveTest extends .builder().addNetworkAdapter(networkAdapter).build(); NetworkSpec networkSpec = NetworkSpec.builder() - .addNIC(0L, networkInterfaceCard).build(); + .addNIC(networkInterfaceCard).build(); sourceMachineSpec = MasterSpec.builder().iso(isoSpec).vm(sourceVmSpec) .network(networkSpec).build(); diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IMachinePredicatesLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IMachinePredicatesLiveTest.java index 67d66227c7..26b1a6167f 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IMachinePredicatesLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IMachinePredicatesLiveTest.java @@ -90,7 +90,7 @@ public class IMachinePredicatesLiveTest extends BaseVirtualBoxClientLiveTest { NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) .build(); - this.networkSpec = NetworkSpec.builder().addNIC(0L, networkInterfaceCard).build(); + this.networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); } From a77e4fda7d4366e5c79306113d764c192b381c1d Mon Sep 17 00:00:00 2001 From: David Ribeiro Alves Date: Sat, 10 Mar 2012 15:58:43 +0000 Subject: [PATCH 2/2] addressed the issues raised in adrian's review --- .../VirtualBoxComputeServiceAdapter.java | 3 +- .../virtualbox/domain/NetworkAdapter.java | 4 +- .../virtualbox/functions/MastersCache.java | 1 + .../virtualbox/functions/NodeCreator.java | 14 +++- .../BaseVirtualBoxClientLiveTest.java | 4 +- .../statements/SetIpAddressTest.java | 67 +++++++++++++++++++ 6 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/SetIpAddressTest.java diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java index 37d684f107..dcf9f3f4d0 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java @@ -20,6 +20,7 @@ package org.jclouds.virtualbox.compute; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.Iterables.filter; import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX; import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX; @@ -90,7 +91,7 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter { + // TODO parameterize public static final int MASTER_PORT = 2222; @Resource diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java index b32137ce2c..a4ec912af4 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java @@ -1,5 +1,5 @@ /** -mh * Licensed to jclouds, Inc. (jclouds) under one or more + * 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 @@ -54,9 +54,17 @@ import com.google.common.base.Supplier; @Singleton public class NodeCreator implements Function> { + // TODO parameterize public static final int NODE_PORT_INIT = 3000; + + // TODO parameterize public static final String VMS_NETWORK = "33.33.33."; + + // TODO parameterize public static final String HOST_ONLY_IFACE_NAME = "vboxnet0"; + + // TODO parameterize + public static final boolean USE_LINKED = true; private final Supplier manager; private final Function cloner; @@ -70,7 +78,7 @@ public class NodeCreator implements Function imachineToNodeMetadata) { this.manager = manager; this.cloner = cloner; - this.nodePorts = new AtomicInteger(3000); + this.nodePorts = new AtomicInteger(NODE_PORT_INIT); this.nodeIps = new AtomicInteger(1); this.machineUtils = machineUtils; this.imachineToNodeMetadata = imachineToNodeMetadata; @@ -115,7 +123,7 @@ public class NodeCreator implements Function mastersCache; - private final ExecutorService singleThreadExec = Executors.newSingleThreadExecutor(); + private final ExecutorService singleThreadExec = MoreExecutors.sameThreadExecutor(); @Override protected void setupCredentials() { diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/SetIpAddressTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/SetIpAddressTest.java new file mode 100644 index 0000000000..f72ea3d1fe --- /dev/null +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/SetIpAddressTest.java @@ -0,0 +1,67 @@ +/** + * 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.statements; + +import static junit.framework.Assert.assertEquals; + +import org.jclouds.scriptbuilder.domain.OsFamily; +import org.jclouds.virtualbox.domain.NetworkAdapter; +import org.jclouds.virtualbox.domain.NetworkInterfaceCard; +import org.testng.annotations.Test; +import org.virtualbox_4_1.NetworkAttachmentType; + +public class SetIpAddressTest { + + @Test + public void testSetIpeth0() { + NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard + .builder() + .slot(0L) + .addNetworkAdapter( + NetworkAdapter.builder().staticIp("127.0.0.1").networkAttachmentType(NetworkAttachmentType.NAT) + .build()).build(); + SetIpAddress setIpAddressStmtm = new SetIpAddress(networkInterfaceCard); + assertEquals("ifconfig eth0 127.0.0.1;", setIpAddressStmtm.render(OsFamily.UNIX)); + } + + @Test + public void testSetIpeth3() { + NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard + .builder() + .slot(3L) + .addNetworkAdapter( + NetworkAdapter.builder().staticIp("localhost").networkAttachmentType(NetworkAttachmentType.NAT) + .build()).build(); + SetIpAddress setIpAddressStmtm = new SetIpAddress(networkInterfaceCard); + assertEquals("ifconfig eth3 localhost;", setIpAddressStmtm.render(OsFamily.UNIX)); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testThrowsIllegalArgumentExceptionOnWrongSlot() { + NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard + .builder() + .slot(4L) + .addNetworkAdapter( + NetworkAdapter.builder().staticIp("localhost").networkAttachmentType(NetworkAttachmentType.NAT) + .build()).build(); + SetIpAddress setIpAddressStmtm = new SetIpAddress(networkInterfaceCard); + } + +}