Added UnregisterMachineIfExists function to cleanup image machines before running IsoToIMachineLiveTest

This commit is contained in:
Mattias Holmqvist 2011-10-29 22:47:47 +02:00
parent 3404850aba
commit 8cdcfff261
3 changed files with 128 additions and 1 deletions

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.functions.admin;
import com.google.common.base.Function;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger;
import org.jclouds.virtualbox.domain.ErrorCode;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.VBoxException;
import org.virtualbox_4_1.VirtualBoxManager;
import javax.annotation.Nullable;
import javax.annotation.Resource;
import javax.inject.Named;
public class UnregisterMachineIfExists implements Function<String, Void> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private VirtualBoxManager manager;
private CleanupMode mode;
public UnregisterMachineIfExists(VirtualBoxManager manager, CleanupMode mode) {
this.manager = manager;
this.mode = mode;
}
@Override
public Void apply(@Nullable String vmName) {
try {
IMachine machine = manager.getVBox().findMachine(vmName);
machine.unregister(mode);
} catch (VBoxException e) {
ErrorCode errorCode = ErrorCode.valueOf(e);
switch (errorCode) {
case VBOX_E_OBJECT_NOT_FOUND:
logger.debug("Machine %s does not exists, cannot unregister", vmName);
break;
default:
throw e;
}
}
return null;
}
}

View File

@ -36,8 +36,12 @@ import org.jclouds.domain.Credentials;
import org.jclouds.json.Json; import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule; import org.jclouds.json.config.GsonModule;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExists;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.VirtualBoxManager; import org.virtualbox_4_1.VirtualBoxManager;
@ -69,9 +73,9 @@ public class IsoToIMachineLiveTest extends BaseVirtualBoxClientLiveTest {
public void setUp() throws Exception { public void setUp() throws Exception {
identity = "toor"; identity = "toor";
credential = "password"; credential = "password";
new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName);
} }
@Test
public void testCreateImageMachineFromIso() throws Exception { public void testCreateImageMachineFromIso() throws Exception {
VirtualBoxManager manager = (VirtualBoxManager) context.getProviderSpecificContext().getApi(); VirtualBoxManager manager = (VirtualBoxManager) context.getProviderSpecificContext().getApi();

View File

@ -0,0 +1,56 @@
/*
* 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 org.testng.annotations.Test;
import org.virtualbox_4_1.*;
import java.util.Collections;
import java.util.List;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.expect;
import static org.easymock.classextension.EasyMock.*;
@Test(groups = "unit", testName = "UnregisterMachineIfExistsTest")
public class UnregisterMachineIfExistsTest {
@Test
public void testUnregisterExistingMachine() throws Exception {
VirtualBoxManager manager = createMock(VirtualBoxManager.class);
IVirtualBox vBox = createMock(IVirtualBox.class);
IMachine registeredMachine = createMock(IMachine.class);
List<IMedium> mediums = Collections.emptyList();
CleanupMode mode = CleanupMode.Full;
String vmName = "jclouds-image-example-machine";
expect(manager.getVBox()).andReturn(vBox).anyTimes();
expect(vBox.findMachine(vmName)).andReturn(registeredMachine);
expect(registeredMachine.unregister(mode)).andReturn(mediums);
expectLastCall().anyTimes();
replay(manager, vBox, registeredMachine);
new UnregisterMachineIfExists(manager, mode).apply(vmName);
}
}