From 6726b04645f760a1cb27130f616c6c0f3d49f358 Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Tue, 15 Nov 2011 16:18:43 +0000 Subject: [PATCH 1/3] issue 384: TakeSnapshotIfNotAlreadyAttached and Test added as preparation step for IMachineToIpAddress feature --- .../TakeSnapshotIfNotAlreadyAttached.java | 78 +++++++++++++ .../TakeSnapshotIfNotAlreadyAttachedTest.java | 103 ++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttachedTest.java diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java new file mode 100644 index 0000000000..49144b3f11 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java @@ -0,0 +1,78 @@ +/** + * 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 javax.annotation.Nullable; +import javax.annotation.Resource; +import javax.inject.Named; + +import org.jclouds.compute.reference.ComputeServiceConstants; +import org.jclouds.logging.Logger; +import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.IProgress; +import org.virtualbox_4_1.ISession; +import org.virtualbox_4_1.ISnapshot; +import org.virtualbox_4_1.VirtualBoxManager; + +import com.google.common.base.Function; + +/** + * @author Andrea Turli + */ +public class TakeSnapshotIfNotAlreadyAttached implements Function { + + + @Resource + @Named(ComputeServiceConstants.COMPUTE_LOGGER) + protected Logger logger = Logger.NULL; + + private VirtualBoxManager manager; + private String snapshotName; + private String snapshotDesc; + + + public TakeSnapshotIfNotAlreadyAttached(VirtualBoxManager manager, String snapshotName, String snapshotDesc) { + this.manager = manager; + this.snapshotName = snapshotName; + this.snapshotDesc = snapshotDesc; + } + + @Override + public ISnapshot apply(@Nullable IMachine machine) { + // Snapshot a machine + ISession session = null; + if(machine.getCurrentSnapshot() == null ) { + try { + session = manager.openMachineSession(machine); + IProgress progress = session.getConsole().takeSnapshot(snapshotName, snapshotDesc); + if (progress.getCompleted()) + logger.debug("Clone done with snapshot name: %s and descripton: %s", snapshotName, snapshotDesc); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + session.unlockMachine(); + } + } + return machine.getCurrentSnapshot(); + } + + +} diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttachedTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttachedTest.java new file mode 100644 index 0000000000..f317e89cfa --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttachedTest.java @@ -0,0 +1,103 @@ +/** + * 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.easymock.EasyMock.expect; +import static org.easymock.classextension.EasyMock.createMock; +import static org.easymock.classextension.EasyMock.createNiceMock; +import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.classextension.EasyMock.verify; + +import org.testng.annotations.Test; +import org.virtualbox_4_1.IConsole; +import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.IProgress; +import org.virtualbox_4_1.ISession; +import org.virtualbox_4_1.ISnapshot; +import org.virtualbox_4_1.IVirtualBox; +import org.virtualbox_4_1.VirtualBoxManager; + +/** + * @author Andrea Turli + */ +@Test(groups = "unit", testName = "TakeSnapshotIfNotAlreadyAttachedTest") +public class TakeSnapshotIfNotAlreadyAttachedTest { + + @Test + public void testTakeSnapshotIfNotAlreadyAttached() throws Exception { + + String snapshotName = "snap"; + String snapshotDesc = "snapDesc"; + + VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class); + IMachine machine = createMock(IMachine.class); + IVirtualBox vBox = createMock(IVirtualBox.class); + ISession session = createMock(ISession.class); + IConsole console = createNiceMock(IConsole.class); + IProgress progress = createNiceMock(IProgress.class); + ISnapshot snapshot = createNiceMock(ISnapshot.class); + expect(machine.getCurrentSnapshot()).andReturn(snapshot).anyTimes(); + + expect(manager.openMachineSession(machine)).andReturn(session); + + expect(session.getConsole()).andReturn(console); + expect(console.takeSnapshot(snapshotName, snapshotDesc)).andReturn( + progress); + expect(progress.getCompleted()).andReturn(true); + + session.unlockMachine(); + replay(manager, machine, vBox, session, console, progress); + + new TakeSnapshotIfNotAlreadyAttached(manager, snapshotName, snapshotDesc) + .apply(machine); + + verify(machine); + } + + @Test + public void testDoNothingIfAlreadyTakenSnapshot() throws Exception { + String snapshotName = "snap"; + String snapshotDesc = "snapDesc"; + + VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class); + IMachine machine = createMock(IMachine.class); + IVirtualBox vBox = createMock(IVirtualBox.class); + ISession session = createMock(ISession.class); + IConsole console = createNiceMock(IConsole.class); + + IProgress progress = createNiceMock(IProgress.class); + expect(progress.getCompleted()).andReturn(true); + expect(machine.getCurrentSnapshot()).andReturn(null).anyTimes(); + expect(manager.openMachineSession(machine)).andReturn(session); + + expect(session.getConsole()).andReturn(console); + expect(console.takeSnapshot(snapshotName, snapshotDesc)).andReturn( + progress); + + session.unlockMachine(); + replay(manager, machine, vBox, session, console, progress); + + new TakeSnapshotIfNotAlreadyAttached(manager, snapshotName, snapshotDesc) + .apply(machine); + + verify(machine); + + } + +} From c1e01901d61208441bf5be6e763628cda9d79e56 Mon Sep 17 00:00:00 2001 From: andreaturli Date: Tue, 15 Nov 2011 19:52:28 +0000 Subject: [PATCH 2/3] issue 384: improved try/catch behaviour in TakeSnapshotIfNotAlreadyAttached --- .../functions/TakeSnapshotIfNotAlreadyAttached.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java index 49144b3f11..decc6c825b 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java @@ -32,6 +32,7 @@ import org.virtualbox_4_1.ISnapshot; import org.virtualbox_4_1.VirtualBoxManager; import com.google.common.base.Function; +import com.google.common.base.Throwables; /** * @author Andrea Turli @@ -65,8 +66,8 @@ public class TakeSnapshotIfNotAlreadyAttached implements Function T propogate(Exception e) { + Throwables.propagate(e); + assert false; + return null; + } } From 025948ae766825fd64df754d6256485eac6a1991 Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Wed, 16 Nov 2011 15:44:54 +0000 Subject: [PATCH 3/3] issue 384: improved log messages in TakeSnapshotIfNotAlreadyAttached --- .../functions/TakeSnapshotIfNotAlreadyAttached.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java index decc6c825b..d2608e8fca 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/TakeSnapshotIfNotAlreadyAttached.java @@ -64,9 +64,9 @@ public class TakeSnapshotIfNotAlreadyAttached implements Function