Renamed VmSpecification to VmSpec and added equals() test.

This commit is contained in:
Mattias Holmqvist 2011-12-15 22:39:00 +01:00
parent 5cd866d384
commit 41a6bb4355
9 changed files with 104 additions and 28 deletions

View File

@ -26,7 +26,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* A description of a Virtual Machine in VirtualBox. * A description of a Virtual Machine in VirtualBox.
*/ */
public class VmSpecification { public class VmSpec {
private final String vmName; private final String vmName;
private final String osTypeId; private final String osTypeId;
@ -35,7 +35,7 @@ public class VmSpecification {
private final Map<Long, NatAdapter> natNetworkAdapters; private final Map<Long, NatAdapter> natNetworkAdapters;
private final Set<StorageController> controllers; private final Set<StorageController> controllers;
public VmSpecification(String vmId, String vmName, String osTypeId, boolean forceOverwrite, Set<StorageController> controllers, Map<Long, NatAdapter> natNetworkAdapters) { public VmSpec(String vmId, String vmName, String osTypeId, boolean forceOverwrite, Set<StorageController> controllers, Map<Long, NatAdapter> natNetworkAdapters) {
this.vmId = vmId; this.vmId = vmId;
this.vmName = vmName; this.vmName = vmName;
this.osTypeId = osTypeId; this.osTypeId = osTypeId;
@ -88,10 +88,10 @@ public class VmSpecification {
} }
public VmSpecification build() { public VmSpec build() {
checkNotNull(name, "name"); checkNotNull(name, "name");
checkNotNull(id, "id"); checkNotNull(id, "id");
return new VmSpecification(id, name, osTypeId, forceOverwrite, controllers, natNetworkAdapters); return new VmSpec(id, name, osTypeId, forceOverwrite, controllers, natNetworkAdapters);
} }
} }
@ -124,7 +124,7 @@ public class VmSpecification {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
VmSpecification that = (VmSpecification) o; VmSpec that = (VmSpec) o;
if (forceOverwrite != that.forceOverwrite) return false; if (forceOverwrite != that.forceOverwrite) return false;
if (controllers != null ? !controllers.equals(that.controllers) : that.controllers != null) return false; if (controllers != null ? !controllers.equals(that.controllers) : that.controllers != null) return false;

View File

@ -20,7 +20,7 @@
package org.jclouds.virtualbox.functions; package org.jclouds.virtualbox.functions;
import com.google.common.base.Function; import com.google.common.base.Function;
import org.jclouds.virtualbox.domain.VmSpecification; import org.jclouds.virtualbox.domain.VmSpec;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IVirtualBox; import org.virtualbox_4_1.IVirtualBox;
import org.virtualbox_4_1.VBoxException; import org.virtualbox_4_1.VBoxException;
@ -31,7 +31,7 @@ import javax.annotation.Nullable;
/** /**
* @author Mattias Holmqvist * @author Mattias Holmqvist
*/ */
public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Function<VmSpecification, IMachine> { public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Function<VmSpec, IMachine> {
private VirtualBoxManager manager; private VirtualBoxManager manager;
@ -40,7 +40,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Functi
} }
@Override @Override
public IMachine apply(@Nullable VmSpecification launchSpecification) { public IMachine apply(@Nullable VmSpec launchSpecification) {
final IVirtualBox vBox = manager.getVBox(); final IVirtualBox vBox = manager.getVBox();
String vmName = launchSpecification.getVmName(); String vmName = launchSpecification.getVmName();
try { try {
@ -58,7 +58,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Functi
return e.getMessage().contains("VirtualBox error: Could not find a registered machine named "); return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ");
} }
private IMachine createMachine(IVirtualBox vBox, VmSpecification launchSpecification) { private IMachine createMachine(IVirtualBox vBox, VmSpec launchSpecification) {
// TODO: add support for settingsfile // TODO: add support for settingsfile
String settingsFile1 = null; String settingsFile1 = null;
IMachine newMachine = vBox.createMachine(settingsFile1, launchSpecification.getVmName(), IMachine newMachine = vBox.createMachine(settingsFile1, launchSpecification.getVmName(),

View File

@ -26,7 +26,6 @@ import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ExecResponse; import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.options.RunScriptOptions; import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.ssh.SshException; import org.jclouds.ssh.SshException;
@ -48,7 +47,7 @@ import static org.jclouds.virtualbox.util.MachineUtils.*;
import static org.virtualbox_4_1.LockType.Shared; import static org.virtualbox_4_1.LockType.Shared;
import static org.virtualbox_4_1.LockType.Write; import static org.virtualbox_4_1.LockType.Write;
public class IsoToIMachine implements Function<VmSpecification, IMachine> { public class IsoToIMachine implements Function<VmSpec, IMachine> {
@Resource @Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER) @Named(ComputeServiceConstants.COMPUTE_LOGGER)
@ -78,7 +77,7 @@ public class IsoToIMachine implements Function<VmSpecification, IMachine> {
} }
@Override @Override
public IMachine apply(VmSpecification vmSpecification) { public IMachine apply(VmSpec vmSpecification) {
ensureWebServerIsRunning(); ensureWebServerIsRunning();
@ -138,7 +137,7 @@ public class IsoToIMachine implements Function<VmSpecification, IMachine> {
return vm; return vm;
} }
private void setupDvdsForController(VmSpecification vmSpecification, String vmName, StorageController controller) { private void setupDvdsForController(VmSpec vmSpecification, String vmName, StorageController controller) {
Set<IsoImage> dvds = controller.getIsoImages(); Set<IsoImage> dvds = controller.getIsoImages();
for (IsoImage dvd : dvds) { for (IsoImage dvd : dvds) {
String dvdSource = dvd.getSourcePath(); String dvdSource = dvd.getSourcePath();
@ -163,7 +162,7 @@ public class IsoToIMachine implements Function<VmSpecification, IMachine> {
} }
} }
private String missingIDEControllersMessage(VmSpecification vmSpecification) { private String missingIDEControllersMessage(VmSpec vmSpecification) {
return String.format("First controller is not an IDE controller. Please verify that the VM spec is a correct master node: %s", vmSpecification); return String.format("First controller is not an IDE controller. Please verify that the VM spec is a correct master node: %s", vmSpecification);
} }

View File

@ -0,0 +1,79 @@
/**
* 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.domain;
import org.testng.annotations.Test;
import org.virtualbox_4_1.StorageBus;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
public class VmSpecTest {
@Test
public void testEqualsSuccessful() throws Exception {
VmSpec vmSpec = defaultVm().build();
VmSpec sameVmSpec = defaultVm().build();
assertEquals(vmSpec, sameVmSpec);
}
@Test
public void testEqualsWrongId() throws Exception {
VmSpec vmSpec = defaultVm().build();
VmSpec other = defaultVm().id("OtherVMId").build();
assertFalse(vmSpec.equals(other));
}
@Test
public void testEqualsWrongName() throws Exception {
VmSpec vmSpec = defaultVm().build();
VmSpec other = defaultVm().name("OtherName").build();
assertFalse(vmSpec.equals(other));
}
@Test
public void testEqualsWrongOsType() throws Exception {
VmSpec vmSpec = defaultVm().build();
VmSpec other = defaultVm().osTypeId("OtherOS").build();
assertFalse(vmSpec.equals(other));
}
@Test
public void testEqualsWrongForceOverwriteRule() throws Exception {
VmSpec vmSpec = defaultVm().build();
VmSpec other = defaultVm().forceOverwrite(false).build();
assertFalse(vmSpec.equals(other));
}
private VmSpec.Builder defaultVm() {
return VmSpec.builder()
.id("MyVmId")
.name("My VM")
.osTypeId("Ubuntu")
.natNetworkAdapter(
0,
NatAdapter.builder().tcpRedirectRule("localhost", 2222, "", 22).build())
.forceOverwrite(true)
.controller(
StorageController.builder().name("Controller")
.bus(StorageBus.IDE)
.attachHardDisk(0, 0, "/tmp/tempdisk.vdi").build());
}
}

View File

@ -26,9 +26,8 @@ import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect; import org.jclouds.predicates.InetSocketAddressConnect;
import org.jclouds.predicates.RetryablePredicate; import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.StorageController; import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpecification; import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.util.PropertyUtils; import org.jclouds.virtualbox.util.PropertyUtils;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.virtualbox_4_1.*; import org.virtualbox_4_1.*;
@ -89,7 +88,7 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
.attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso") .attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
.attachHardDisk(0, 1, workingDir + "/testadmin.vdi") .attachHardDisk(0, 1, workingDir + "/testadmin.vdi")
.attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso").build(); .attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso").build();
VmSpecification vmSpecification = VmSpecification.builder().id(vmId).name(vmName).osTypeId(osTypeId) VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).osTypeId(osTypeId)
.controller(ideController) .controller(ideController)
.forceOverwrite(true).build(); .forceOverwrite(true).build();
return new IsoToIMachine(manager, guestId, localHostContext, hostId, socketTester, return new IsoToIMachine(manager, guestId, localHostContext, hostId, socketTester,

View File

@ -21,7 +21,7 @@ package org.jclouds.virtualbox.functions;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.ErrorCode; import org.jclouds.virtualbox.domain.ErrorCode;
import org.jclouds.virtualbox.domain.VmSpecification; import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExists; import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExists;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.CleanupMode;
@ -40,7 +40,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B
public void testCreateNewMachine() throws Exception { public void testCreateNewMachine() throws Exception {
String vmName = "jclouds-test-create-1-node"; String vmName = "jclouds-test-create-1-node";
new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName); new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName);
VmSpecification launchSpecification = VmSpecification.builder().id(vmName).name(vmName) VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName)
.osTypeId("Debian").forceOverwrite(true).build(); .osTypeId("Debian").forceOverwrite(true).build();
IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification); IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);
IMachine machine = manager.getVBox().findMachine(vmName); IMachine machine = manager.getVBox().findMachine(vmName);
@ -52,7 +52,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B
public void testCreateNewMachineWithBadOsType() throws Exception { public void testCreateNewMachineWithBadOsType() throws Exception {
String vmName = "jclouds-test-create-2-node"; String vmName = "jclouds-test-create-2-node";
new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName); new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName);
VmSpecification launchSpecification = VmSpecification.builder().id(vmName).name(vmName) VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName)
.osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build(); .osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
try { try {
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification); new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);

View File

@ -29,7 +29,7 @@ import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify; import static org.easymock.classextension.EasyMock.verify;
import org.easymock.EasyMock; import org.easymock.EasyMock;
import org.jclouds.virtualbox.domain.VmSpecification; import org.jclouds.virtualbox.domain.VmSpec;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IVirtualBox; import org.virtualbox_4_1.IVirtualBox;
@ -49,7 +49,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
IVirtualBox vBox = createMock(IVirtualBox.class); IVirtualBox vBox = createMock(IVirtualBox.class);
String vmName = "jclouds-image-my-ubuntu-image"; String vmName = "jclouds-image-my-ubuntu-image";
VmSpecification launchSpecification = VmSpecification.builder().id(vmName).name(vmName).osTypeId("").build(); VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).osTypeId("").build();
IMachine createdMachine = createMock(IMachine.class); IMachine createdMachine = createMock(IMachine.class);
@ -90,7 +90,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
replay(manager, vBox); replay(manager, vBox);
VmSpecification launchSpecification = VmSpecification.builder().id("").name(vmName).osTypeId("").build(); VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").build();
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification); new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);
} }
@ -111,7 +111,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
replay(manager, vBox); replay(manager, vBox);
VmSpecification launchSpecification = VmSpecification.builder().id("").name(vmName).osTypeId("").build(); VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").build();
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification); new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);
} }

View File

@ -49,7 +49,6 @@ import java.util.concurrent.TimeUnit;
import static com.google.common.base.Predicates.equalTo; import static com.google.common.base.Predicates.equalTo;
import static com.google.common.collect.Iterables.any; import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Iterables.transform; import static com.google.common.collect.Iterables.transform;
import static org.jclouds.virtualbox.domain.ExecutionType.GUI;
import static org.jclouds.virtualbox.domain.ExecutionType.HEADLESS; import static org.jclouds.virtualbox.domain.ExecutionType.HEADLESS;
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest; import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
@ -91,7 +90,7 @@ public class IsoToIMachineLiveTest extends BaseVirtualBoxClientLiveTest {
.attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso") .attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
.attachHardDisk(0, 1, workingDir + "/testadmin.vdi") .attachHardDisk(0, 1, workingDir + "/testadmin.vdi")
.attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso").build(); .attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso").build();
VmSpecification vmSpecification = VmSpecification.builder().id(vmId).name(vmName).osTypeId(osTypeId) VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).osTypeId(osTypeId)
.controller(ideController) .controller(ideController)
.forceOverwrite(true) .forceOverwrite(true)
.natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build(); .natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build();

View File

@ -10,7 +10,7 @@ import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.ExecutionType; import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.domain.StorageController; import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpecification; import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.functions.IsoToIMachine; import org.jclouds.virtualbox.functions.IsoToIMachine;
import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning; import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning;
import org.jclouds.virtualbox.util.PropertyUtils; import org.jclouds.virtualbox.util.PropertyUtils;
@ -70,7 +70,7 @@ public class SshAvailableLiveTest extends BaseVirtualBoxClientLiveTest {
StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE) StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE)
.attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso") .attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
.attachHardDisk(0, 1, workingDir + "/testadmin.vdi").build(); .attachHardDisk(0, 1, workingDir + "/testadmin.vdi").build();
VmSpecification vmSpecification = VmSpecification.builder().id(vmId).name(vmName).osTypeId("") VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).osTypeId("")
.controller(ideController) .controller(ideController)
.forceOverwrite(true).build(); .forceOverwrite(true).build();