diff --git a/src/oca/java/test/VirtualMachineTest.java b/src/oca/java/test/VirtualMachineTest.java index 84f8f60a86..8e9f6d100f 100644 --- a/src/oca/java/test/VirtualMachineTest.java +++ b/src/oca/java/test/VirtualMachineTest.java @@ -1,12 +1,12 @@ /******************************************************************************* * Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) - * + * * Licensed 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. @@ -23,6 +23,7 @@ import org.junit.Test; import org.opennebula.client.Client; import org.opennebula.client.OneResponse; import org.opennebula.client.host.Host; +import org.opennebula.client.image.Image; import org.opennebula.client.vm.VirtualMachine; import org.opennebula.client.vm.VirtualMachinePool; @@ -36,10 +37,39 @@ public class VirtualMachineTest private static Client client; private static int hid_A, hid_B; - + private static OneResponse res; private static String name = "new_test_machine"; + + /** + * Wait until the VM changes to the specified state. + * There is a time-out of 10 seconds. + */ + void waitAssert(VirtualMachine vm, String state, String lcmState) + { + int n_steps = 100; + int step = 100; + + int i = 0; + + vm.info(); + + while( !( (vm.stateStr().equals(state) && (!state.equals("ACTIVE") || vm.lcmStateStr().equals(lcmState) ))|| i > n_steps )) + { + try{ Thread.sleep(step); } catch (Exception e){} + + vm.info(); + i++; + } + + assertTrue( vm.stateStr().equals(state) ); + if(state.equals("ACTIVE")) + { + assertTrue( vm.lcmStateStr().equals(lcmState) ); + } + } + /** * @throws java.lang.Exception */ @@ -90,19 +120,14 @@ public class VirtualMachineTest public void tearDown() throws Exception { vm.finalizeVM(); + waitAssert(vm, "DONE", "-"); + + vm = null; } @Test public void allocate() { -// String template = "NAME = " + name + "\n"+ -// "MEMORY = 512\n" + -// "CONTEXT = [DNS = 192.169.1.4]"; -// -// res = VirtualMachine.allocate(client, template); -// assertTrue( !res.isError() ); -// assertTrue( res.getMessage().equals("0") ); - vmPool.info(); boolean found = false; @@ -120,8 +145,6 @@ public class VirtualMachineTest res = vm.info(); assertTrue( !res.isError() ); -// assertTrue( vm.getId().equals("0") ); -// assertTrue( vm.id() == 0 ); assertTrue( vm.getName().equals(name) ); } @@ -130,6 +153,7 @@ public class VirtualMachineTest { res = vm.hold(); assertTrue( !res.isError() ); + waitAssert(vm, "HOLD", "-"); } @Test @@ -139,6 +163,7 @@ public class VirtualMachineTest res = vm.release(); assertTrue( !res.isError() ); + waitAssert(vm, "PENDING", "-"); } @Test @@ -146,85 +171,98 @@ public class VirtualMachineTest { res = vm.deploy(hid_A); assertTrue( !res.isError() ); + waitAssert(vm, "ACTIVE", "RUNNING"); } @Test public void migrate() { vm.deploy(hid_A); - try{ Thread.sleep(5000); } catch (Exception e){} + waitAssert(vm, "ACTIVE", "RUNNING"); res = vm.migrate(hid_B); assertTrue( !res.isError() ); + waitAssert(vm, "ACTIVE", "RUNNING"); } @Test public void liveMigrate() { vm.deploy(hid_A); - try{ Thread.sleep(5000); } catch (Exception e){} + waitAssert(vm, "ACTIVE", "RUNNING"); res = vm.liveMigrate(hid_B); assertTrue( !res.isError() ); + waitAssert(vm, "ACTIVE", "RUNNING"); } @Test public void shutdown() { vm.deploy(hid_A); - try{ Thread.sleep(5000); } catch (Exception e){} + waitAssert(vm, "ACTIVE", "RUNNING"); res = vm.shutdown(); - assertTrue( !res.isError() ); + assertTrue( !res.isError() ); + waitAssert(vm, "DONE", "-"); } @Test public void cancel() { vm.deploy(hid_A); - try{ Thread.sleep(5000); } catch (Exception e){} + waitAssert(vm, "ACTIVE", "RUNNING"); res = vm.cancel(); assertTrue( !res.isError() ); + waitAssert(vm, "DONE", "-"); } @Test public void stop() { vm.deploy(hid_A); - try{ Thread.sleep(5000); } catch (Exception e){} + waitAssert(vm, "ACTIVE", "RUNNING"); res = vm.stop(); assertTrue( !res.isError() ); + waitAssert(vm, "STOPPED", "-"); } @Test public void suspend() { vm.deploy(hid_A); - try{ Thread.sleep(5000); } catch (Exception e){} + waitAssert(vm, "ACTIVE", "RUNNING"); res = vm.suspend(); assertTrue( !res.isError() ); + waitAssert(vm, "SUSPENDED", "-"); } @Test public void resume() { vm.deploy(hid_A); - try{ Thread.sleep(5000); } catch (Exception e){} + waitAssert(vm, "ACTIVE", "RUNNING"); + vm.suspend(); - try{ Thread.sleep(5000); } catch (Exception e){} + waitAssert(vm, "SUSPENDED", "-"); res = vm.resume(); assertTrue( !res.isError() ); + waitAssert(vm, "ACTIVE", "RUNNING"); } @Test - public void finalize() + public void finalizeVM() { + vm.deploy(hid_A); + waitAssert(vm, "ACTIVE", "RUNNING"); res = vm.finalizeVM(); + assertTrue( !res.isError() ); + waitAssert(vm, "DONE", "-"); } @Test @@ -245,4 +283,36 @@ public class VirtualMachineTest assertTrue( vm.xpath("TEMPLATE/MEMORY").equals("512") ); assertTrue( vm.xpath("TEMPLATE/CONTEXT/DNS").equals("192.169.1.4") ); } + + @Test + public void savedisk() + { + String template = "NAME = savedisk_vm\n"+ + "MEMORY = 512\n" + + "CONTEXT = [DNS = 192.169.1.4]\n" + + "DISK = [ TYPE = fs, SIZE = 4, " + + "FORMAT = ext3, TARGET = sdg ]"; + + res = VirtualMachine.allocate(client, template); + int vmid = !res.isError() ? Integer.parseInt(res.getMessage()) : -1; + + vm = new VirtualMachine(vmid, client); + + String imgTemplate = + "NAME = \"image\"\n" + + "ATT1 = \"val1\""; + + res = Image.allocate(client, imgTemplate); + + int imgid = res.isError() ? -1 : Integer.parseInt(res.getMessage()); + + res = vm.savedisk(0, imgid); + assertTrue( !res.isError() ); + + res = vm.info(); + assertTrue( !res.isError() ); + + assertTrue( vm.xpath("TEMPLATE/DISK/SAVE_AS").equals(Integer.toString(imgid)) ); + + } } diff --git a/src/oca/java/test/test.sh b/src/oca/java/test/test.sh index 22f418cad0..86c11a35e7 100755 --- a/src/oca/java/test/test.sh +++ b/src/oca/java/test/test.sh @@ -3,7 +3,18 @@ # For instance: test.sh ImageTest JUNIT_JAR="/usr/share/java/junit4.jar" -ONEDB="$ONE_LOCATION/var/one.db" + +if [ -z $ONE_LOCATION ]; then + echo "ONE_LOCATION not defined." + exit -1 +fi + +VAR_LOCATION="$ONE_LOCATION/var" + +if [ "$(ls -A $VAR_LOCATION)" ]; then + echo "$VAR_LOCATION is not empty." + exit -1 +fi PID=$$ @@ -18,6 +29,6 @@ CODE=$? pkill -P $PID oned sleep 4s; pkill -9 -P $PID oned -rm $ONEDB +rm -rf $VAR_LOCATION/* exit $CODE \ No newline at end of file