/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.abiquo.domain.cloud;
import static com.google.common.collect.Iterables.find;
import static com.google.common.collect.Iterables.size;
import static org.jclouds.abiquo.reference.AbiquoTestConstants.PREFIX;
import static org.jclouds.abiquo.util.Assert.assertHasError;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import javax.ws.rs.core.Response.Status;
import org.jclouds.abiquo.domain.exception.AbiquoException;
import org.jclouds.abiquo.domain.task.AsyncTask;
import org.jclouds.abiquo.domain.task.VirtualMachineTask;
import org.jclouds.abiquo.internal.BaseAbiquoApiLiveApiTest;
import org.testng.annotations.Test;
import com.abiquo.server.core.cloud.VirtualMachineDto;
import com.abiquo.server.core.cloud.VirtualMachineState;
import com.google.common.base.Predicate;
/**
* Live integration tests for the {@link VirtualMachine} domain class.
*
* @author Francesc Montserrat
*/
@Test(groups = "api", testName = "VirtualMachineLiveApiTest")
public class VirtualMachineLiveApiTest extends BaseAbiquoApiLiveApiTest {
public void testListAllVirtualMachines() {
Iterable<VirtualMachine> vms = env.context.getCloudService().listVirtualMachines();
assertTrue(size(vms) > 1);
// Verify that the VM created in the setup exists. This call will throw
// a NoSuchElementException if no macthing VM is found
find(vms, new Predicate<VirtualMachine>() {
@Override
public boolean apply(VirtualMachine input) {
return input.getId().equals(env.virtualMachine.getId());
}
});
}
public void testHasDataFromNode() {
assertNotNull(env.virtualMachine.getNameLabel());
assertNotNull(env.virtualMachine.getInternalName());
assertNotNull(env.virtualMachine.getOwnerName());
}
public void testUpdateInfoFromNode() {
env.virtualMachine.setNameLabel(PREFIX + "-label-updated");
env.virtualMachine.update();
env.virtualMachine.refresh();
assertEquals(env.virtualMachine.getNameLabel(), PREFIX + "-label-updated");
}
public void testGetTasks() {
Iterable<AsyncTask<?, ?>> tasks = env.virtualMachine.listTasks();
assertNotNull(tasks);
}
public void testGetState() {
VirtualMachineState state = env.virtualMachine.getState();
assertEquals(state, VirtualMachineState.NOT_ALLOCATED);
}
public void testIsPersistent() {
assertFalse(env.virtualMachine.isPersistent());
}
public void testGetVirtualAppliance() {
VirtualAppliance vapp = env.virtualMachine.getVirtualAppliance();
assertNotNull(vapp);
assertEquals(vapp.getId(), env.virtualAppliance.getId());
}
public void testRebootVirtualMachineFailsWhenNotAllocated() {
// Since the virtual machine is not deployed, this should not generate a
// task
try {
env.virtualMachine.reboot();
fail("Reboot should have failed for the NOT_ALLOCATED virtual machine");
} catch (AbiquoException ex) {
assertHasError(ex, Status.CONFLICT, "VM-11");
}
}
public void testUpdateForcingLimits() {
int originalHard = env.virtualDatacenter.getCpuCountHardLimit();
int originalSoft = env.virtualDatacenter.getCpuCountSoftLimit();
env.virtualDatacenter.setCpuCountHardLimit(10);
env.virtualDatacenter.setCpuCountSoftLimit(5);
env.virtualDatacenter.update();
try {
VirtualMachine vm = env.virtualAppliance.getVirtualMachine(env.virtualMachine.getId());
vm.setCpu(7);
VirtualMachineTask task = vm.update(true);
assertNull(task);
assertEquals(vm.getCpu().intValue(), 7);
} finally {
env.virtualDatacenter.setCpuCountHardLimit(originalHard);
env.virtualDatacenter.setCpuCountSoftLimit(originalSoft);
env.virtualDatacenter.update();
}
}
public void testAttachDvd() {
VirtualMachine vm = VirtualMachine.Builder.fromVirtualMachine(env.virtualMachine).dvd(true).build();
vm.save();
VirtualMachineDto updated = env.cloudApi.getVirtualMachine(env.virtualAppliance.unwrap(), vm.getId());
assertNotNull(updated.getDvd());
}
}