Merge pull request #418 from dralves/jclouds-vbox-nat-host-only-working-cluster-and-live-test

working multi node cluster with nat+host-only
This commit is contained in:
Adrian Cole 2012-03-10 08:07:14 -08:00
commit aa14bf583c
18 changed files with 374 additions and 54 deletions

View File

@ -20,19 +20,24 @@
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;
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 +45,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 +67,10 @@ import com.google.inject.Singleton;
@Singleton
public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IMachine, IMachine, Image, Location> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final Supplier<VirtualBoxManager> manager;
private final Map<Image, YamlImage> images;
private final LoadingCache<Image, Master> mastersLoader;
@ -81,6 +91,7 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
Template template) {
try {
Master master = mastersLoader.get(template.getImage());
checkState(master != null, "could not find a master for image: "+template.getClass());
NodeSpec nodeSpec = NodeSpec.builder().master(master).name(name).tag(tag).template(template).build();
return cloneCreator.apply(nodeSpec);
} catch (Exception e) {
@ -93,7 +104,7 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
return Iterables.filter(manager.get().getVBox().getMachines(), new Predicate<IMachine>() {
@Override
public boolean apply(IMachine arg0) {
return !arg0.getName().startsWith(VIRTUALBOX_NODE_PREFIX);
return arg0.getName().startsWith(VIRTUALBOX_NODE_PREFIX);
}
});
}
@ -178,6 +189,11 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
private void powerDownMachine(IMachine machine) {
try {
if (machine.getState() == MachineState.PoweredOff){
logger.debug("vm was already powered down: ", machine.getName());
return;
}
logger.debug("powering down vm: ", machine.getName());
ISession machineSession = manager.get().openMachineSession(machine);
IProgress progress = machineSession.getConsole().powerDown();
progress.waitForCompletion(-1);

View File

@ -24,6 +24,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collections;
import java.util.Set;
import javax.annotation.Nullable;
import org.virtualbox_4_1.NATProtocol;
import org.virtualbox_4_1.NetworkAttachmentType;
@ -41,13 +43,16 @@ public class NetworkAdapter {
private final NetworkAttachmentType networkAttachmentType;
private final String macAddress;
private final Set<RedirectRule> redirectRules;
private final String staticIp;
public NetworkAdapter(NetworkAttachmentType networkAttachmentType,
String macAddress, Set<RedirectRule> redirectRules) {
String macAddress, Set<RedirectRule> redirectRules,
String staticIp) {
this.networkAttachmentType = checkNotNull(networkAttachmentType,
"networkAttachmentType");
this.macAddress = macAddress;
this.redirectRules = ImmutableSet.<RedirectRule>copyOf(redirectRules);
this.staticIp = staticIp;
}
public static Builder builder() {
@ -59,6 +64,7 @@ public class NetworkAdapter {
private NetworkAttachmentType networkAttachmentType;
private String macAddress;
private Set<RedirectRule> redirectRules = Sets.newLinkedHashSet();
private String staticIp;
/**
*
@ -113,9 +119,14 @@ public class NetworkAdapter {
return this;
}
public Builder staticIp(@Nullable String staticIp) {
this.staticIp = staticIp;
return this;
}
public NetworkAdapter build() {
return new NetworkAdapter(networkAttachmentType, macAddress,
redirectRules);
redirectRules,staticIp);
}
}
@ -131,6 +142,10 @@ public class NetworkAdapter {
return macAddress;
}
public String getStaticIp() {
return staticIp;
}
@Override
public boolean equals(Object o) {
if (this == o)

View File

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

View File

@ -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<NetworkInterfaceCard> networkInterfaceCards = new ArrayList<NetworkInterfaceCard>();
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;
}

View File

@ -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<IMachine, Void> {
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;
}
}

View File

@ -49,6 +49,8 @@ public class AttachNicToMachine implements Function<NetworkInterfaceCard, Void>
} 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<NetworkInterfaceCard, Void>
return nic.getNetworkAdapter().getNetworkAttachmentType()
.equals(NetworkAttachmentType.Bridged);
}
private boolean hasHostOnlyAdapter(NetworkInterfaceCard nic) {
return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.HostOnly);
}
}

View File

@ -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,8 +73,12 @@ import com.google.common.collect.Maps;
* @author dralves
*
*/
@Singleton
public class MastersCache extends AbstractLoadingCache<Image, Master> {
// TODO parameterize
public static final int MASTER_PORT = 2222;
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
@ -121,7 +126,7 @@ public class MastersCache extends AbstractLoadingCache<Image, Master> {
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 +159,12 @@ public class MastersCache extends AbstractLoadingCache<Image, Master> {
.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 +194,14 @@ public class MastersCache extends AbstractLoadingCache<Image, Master> {
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 +213,4 @@ public class MastersCache extends AbstractLoadingCache<Image, Master> {
return localFile.getAbsolutePath();
}
@Override
public Master getIfPresent(Image key) {
if (masters.containsKey(key.getId())) {
return masters.get(key.getId());
}
return null;
}
}

View File

@ -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
@ -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,43 @@ import com.google.common.base.Supplier;
@Singleton
public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials<IMachine>> {
// 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<VirtualBoxManager> manager;
private final Function<CloneSpec, IMachine> cloner;
private final AtomicInteger nodePorts;
private final AtomicInteger nodeIps;
private MachineUtils machineUtils;
private Function<IMachine, NodeMetadata> imachineToNodeMetadata;
@Inject
public NodeCreator(Supplier<VirtualBoxManager> manager, Function<CloneSpec, IMachine> cloner,
MachineUtils machineUtils) {
MachineUtils machineUtils, Function<IMachine, NodeMetadata> imachineToNodeMetadata) {
this.manager = manager;
this.cloner = cloner;
this.nodePorts = new AtomicInteger(NODE_PORT_INIT);
this.nodeIps = new AtomicInteger(1);
this.machineUtils = machineUtils;
this.imachineToNodeMetadata = imachineToNodeMetadata;
}
@Override
public NodeAndInitialCredentials<IMachine> apply(NodeSpec nodeSpec) {
checkNotNull(nodeSpec, "NodeSpec");
Master master = nodeSpec.getMaster();
checkNotNull(master, "Master");
if (master.getMachine().getCurrentSnapshot() != null) {
ISession session;
@ -81,21 +110,29 @@ public class NodeCreator implements Function<NodeSpec, NodeAndInitialCredentials
VmSpec cloneVmSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(CleanupMode.Full)
.forceOverwrite(true).build();
NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
.tcpRedirectRule("127.0.0.1", 2222, "", 22).build();
NetworkAdapter natAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT)
.tcpRedirectRule("127.0.0.1", this.nodePorts.getAndIncrement(), "", 22).build();
NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter)
.build();
NetworkInterfaceCard natIfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(natAdapter).slot(0L).build();
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(0L, networkInterfaceCard).build();
NetworkAdapter hostOnlyAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.HostOnly)
.staticIp(VMS_NETWORK + this.nodeIps.getAndIncrement()).build();
CloneSpec cloneSpec = CloneSpec.builder().linked(false).master(master.getMachine()).network(networkSpec)
NetworkInterfaceCard hostOnlyIfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(hostOnlyAdapter)
.addHostInterfaceName(HOST_ONLY_IFACE_NAME).slot(1L).build();
NetworkSpec networkSpec = NetworkSpec.builder().addNIC(natIfaceCard).addNIC(hostOnlyIfaceCard).build();
CloneSpec cloneSpec = CloneSpec.builder().linked(USE_LINKED).master(master.getMachine()).network(networkSpec)
.vm(cloneVmSpec).build();
IMachine cloned = cloner.apply(cloneSpec);
new LaunchMachineIfNotAlreadyRunning(manager.get(), ExecutionType.GUI, "").apply(cloned);
machineUtils.runScriptOnNode(imachineToNodeMetadata.apply(cloned), new SetIpAddress(hostOnlyIfaceCard),
RunScriptOptions.NONE);
// TODO get credentials from somewhere else (they are also HC in IMachineToSshClient)
NodeAndInitialCredentials<IMachine> nodeAndInitialCredentials = new NodeAndInitialCredentials<IMachine>(cloned,
cloneName, LoginCredentials.builder().user("toor").password("password").authenticateSudo(true).build());

View File

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

View File

@ -24,6 +24,7 @@ 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 javax.inject.Inject;
import javax.inject.Named;
@ -39,9 +40,10 @@ 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.MoreExecutors;
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;
@ -110,6 +112,8 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest {
@Inject
protected LoadingCache<Image, Master> mastersCache;
private final ExecutorService singleThreadExec = MoreExecutors.sameThreadExecutor();
@Override
protected void setupCredentials() {
// default behavior is to bomb when no user is configured, but we know the
@ -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.<Module> of(new SLF4JLoggingModule(), new SshjSshClientModule(), hostModule), overrides);
context = new ComputeServiceContextFactory().createContext(provider, identity, credential, ImmutableSet
.<Module> 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));

View File

@ -59,20 +59,6 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien
doConnectViaSsh(machine.getNode(), prioritizeCredentialsFromTemplate.apply(template, machine.getCredentials()));
}
@Test
public void testListHardwareProfiles() {
Iterable<IMachine> profiles = adapter.listHardwareProfiles();
assertEquals(1, Iterables.size(profiles));
//TODO: check state;
}
@Test
public void testListImages() {
Iterable<Image> 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);
try {
@ -88,6 +74,20 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien
}
}
@Test
public void testListHardwareProfiles() {
Iterable<IMachine> profiles = adapter.listHardwareProfiles();
assertEquals(1, Iterables.size(profiles));
//TODO: check state;
}
@Test
public void testListImages() {
Iterable<Image> iMageIterable = adapter.listImages();
assertEquals(1, Iterables.size(iMageIterable));
//TODO: check state;
}
@AfterClass
@Override
protected void tearDown() throws Exception {

View File

@ -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<? extends NodeMetadata> 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<NodeMetadata>() {
@Override
public boolean apply(NodeMetadata input) {
return input.getId().contains(clusterName);
}
});
}
}

View File

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

View File

@ -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()

View File

@ -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()

View File

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

View File

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

View File

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