@Test public void testPrepareVmDiskOperation() throws Throwable { com.vmware.photon.controller.api.Operation operation = com.vmware.photon.controller.api.Operation.DETACH_DISK; DiskService.State diskState = new DiskService.State(); diskState.name = "test-vm-disk-1"; diskState.projectId = projectId; diskState.flavorId = flavorDcpBackend.getEntityByNameAndKind("core-100", PersistentDisk.KIND).getId(); diskState.capacityGb = 64; diskState.diskType = DiskType.PERSISTENT; diskState.state = DiskState.ATTACHED; Operation result = dcpClient.post(DiskServiceFactory.SELF_LINK, diskState); DiskService.State createdDiskState = result.getBody(DiskService.State.class); String diskId = ServiceUtils.getIDFromDocumentSelfLink(createdDiskState.documentSelfLink); List<String> disks = new ArrayList<>(); disks.add(diskId); TaskEntity task = vmDcpBackend.prepareVmDiskOperation(vmId, disks, operation); assertThat(task, is(notNullValue())); assertThat(task.getState(), is(TaskEntity.State.QUEUED)); assertThat(task.getSteps().size(), is(1)); assertThat(task.getSteps().get(0).getOperation(), is(operation)); try { vmDcpBackend.prepareVmDiskOperation( vmId, disks, com.vmware.photon.controller.api.Operation.MOCK_OP); fail("vmDcpBackend.prepareVmOperation with invalid operation should have failed"); } catch (NotImplementedException e) { // do nothing } }
@Test public void testUpdateState() throws Throwable { assertThat(vm.getState(), is(not(VmState.ERROR))); vmDcpBackend.updateState(vm, VmState.ERROR); assertThat(vm.getState(), is(VmState.ERROR)); vm = vmDcpBackend.findById(vmId); assertThat(vm.getState(), is(VmState.ERROR)); String agent = UUID.randomUUID().toString(); String agentIp = UUID.randomUUID().toString(); String datastoreId = UUID.randomUUID().toString(); String datastoreName = UUID.randomUUID().toString(); vmDcpBackend.updateState(vm, VmState.STARTED, agent, agentIp, datastoreId, datastoreName); assertThat(vm.getState(), is(VmState.STARTED)); assertThat(vm.getAgent(), is(agent)); assertThat(vm.getHost(), is(agentIp)); assertThat(vm.getDatastore(), is(datastoreId)); assertThat(vm.getDatastoreName(), is(datastoreName)); vm = vmDcpBackend.findById(vmId); assertThat(vm.getState(), is(VmState.STARTED)); assertThat(vm.getAgent(), is(agent)); assertThat(vm.getHost(), is(agentIp)); assertThat(vm.getDatastore(), is(datastoreId)); assertThat(vm.getDatastoreName(), is(datastoreName)); }
@Test public void testDetachIso() throws Throwable { VmService.State vmState = new VmService.State(); Iso iso = new Iso(); iso.setName(UUID.randomUUID().toString()); vmState.isos = new ArrayList<>(); vmState.isos.add(iso); dcpClient.patch(VmServiceFactory.SELF_LINK + "/" + vm.getId(), vmState); assertThat(vmDcpBackend.isosAttached(vm).isEmpty(), is(false)); vmDcpBackend.detachIso(vm); assertThat(vmDcpBackend.isosAttached(vm).isEmpty(), is(true)); }
@Test public void testFilter() throws Throwable { List<Vm> foundVms = vmDcpBackend.filter(vm.projectId, Optional.<String>absent()); assertThat(foundVms, is(notNullValue())); assertThat(foundVms.size(), is(1)); assertThat(foundVms.get(0).getName(), is(vm.name)); foundVms = vmDcpBackend.filter(vm.projectId, Optional.of(vm.name)); assertThat(foundVms, is(notNullValue())); assertThat(foundVms.size(), is(1)); assertThat(foundVms.get(0).getName(), is(vm.name)); }
@Test public void testPrepareVmGetMksTicket() throws Throwable { vmDcpBackend.updateState(vmDcpBackend.findById(vmId), VmState.STARTED); TaskEntity task = vmDcpBackend.prepareVmGetMksTicket(vmId); assertThat(task, is(notNullValue())); assertThat(task.getState(), is(TaskEntity.State.QUEUED)); assertThat(task.getSteps().size(), is(1)); assertThat( task.getSteps().get(0).getOperation(), is(com.vmware.photon.controller.api.Operation.GET_MKS_TICKET)); }
@Test public void testAddtag() throws Throwable { vm = vmDcpBackend.findById(vm.getId()); int originalSize = vm.getTags().size(); Tag tag = new Tag("namespace:predicate=value"); vmDcpBackend.addTag(vm.getId(), tag); vm = vmDcpBackend.findById(vm.getId()); assertThat(vm.getTags().size(), is(originalSize + 1)); List<String> tagValues = vm.getTags().stream().map(t -> t.getValue()).collect(Collectors.toList()); assertTrue(tagValues.contains(tag.getValue())); }
@Test public void testAddIso() throws Throwable { IsoEntity isoEntity = new IsoEntity(); isoEntity.setId("iso-id"); isoEntity.setName("iso-name"); isoEntity.setSize(100L); vmDcpBackend.addIso(isoEntity, vm); VmEntity updatedVm = vmDcpBackend.findById(vmId); assertThat(updatedVm, CoreMatchers.notNullValue()); assertThat(updatedVm.getIsos().size(), is(1)); assertThat(updatedVm.getIsos().get(0), is(isoEntity)); }
@Test public void testFindById() throws Throwable { VmEntity foundVmEntity = vmDcpBackend.findById(vmId); assertThat(foundVmEntity.getName(), is(vm.name)); assertThat(foundVmEntity.getFlavorId(), is(vm.flavorId)); assertThat(foundVmEntity.getImageId(), is(vm.imageId)); assertThat(foundVmEntity.getProjectId(), is(vm.projectId)); assertThat(foundVmEntity.getState(), is(vm.vmState)); assertThat( foundVmEntity.getAffinities().get(0).getResourceId(), is(vm.affinities.get(0).getId())); assertThat( foundVmEntity.getAffinities().get(0).getKind(), is(vm.affinities.get(0).getKind())); assertThat(foundVmEntity.getIsos().get(0).getName(), is(vm.isos.get(0).getName())); assertThat(foundVmEntity.getIsos().get(0).getSize(), is(vm.isos.get(0).getSize())); assertThat(foundVmEntity.getMetadata().get("key1"), is(vm.metadata.get("key1"))); assertThat(foundVmEntity.getNetworks().get(0), is(vm.networks.get(0))); assertThat(vm.tags.contains(foundVmEntity.getTags().iterator().next().getValue()), is(true)); assertThat(foundVmEntity.getAgent(), is(vm.agent)); assertThat(foundVmEntity.getHost(), is(vm.host)); assertThat(foundVmEntity.getDatastore(), is(vm.datastore)); assertThat(foundVmEntity.getDatastoreName(), is(vm.datastoreName)); }
@Test public void testGetAllVmsOnHost() throws Throwable { List<Vm> foundVms = vmDcpBackend.getAllVmsOnHost(hostId); assertThat(foundVms, is(notNullValue())); assertThat(foundVms.size(), is(1)); assertThat(foundVms.get(0).getName(), is(vm.name)); }
@Test public void testFindByProjectId() throws Throwable { List<Vm> foundVms = vmDcpBackend.filterByProject(vm.projectId); assertThat(foundVms, is(notNullValue())); assertThat(foundVms.size(), is(1)); assertThat(foundVms.get(0).getName(), is(vm.name)); }
@Test public void testFilterByNetwork() throws Throwable { List<Vm> foundVms = vmDcpBackend.filterByNetwork(vm.networks.get(0)); assertThat(foundVms, is(notNullValue())); assertThat(foundVms.size(), is(1)); assertThat(foundVms.get(0).getName(), is(vm.name)); }
@Test public void testCountVmsOnHost() throws Throwable { HostEntity hostEntity = new HostEntity(); hostEntity.setAddress(vm.host); hostEntity.setId(hostId); int countOfVmsOnHost = vmDcpBackend.countVmsOnHost(hostEntity); assertThat(countOfVmsOnHost, is(1)); }
@Test( expectedExceptions = VmNotFoundException.class, expectedExceptionsMessageRegExp = "VM nonExistingVm not found") public void testAddIsoFailed() throws Throwable { VmEntity vmEntity = new VmEntity(); vmEntity.setId("nonExistingVm"); vmDcpBackend.addIso(new IsoEntity(), vmEntity); }
@Test public void testFilterByTagNoMatch() throws Throwable { List<Vm> foundVms = vmDcpBackend .filterByTag(vm.projectId, new Tag("tag1"), Optional.<Integer>absent()) .getItems(); assertThat(foundVms, is(notNullValue())); assertThat(foundVms.size(), is(0)); }
@Test public void testPrepareVmOperation() throws Throwable { com.vmware.photon.controller.api.Operation operation = VmOperation.VALID_OPERATIONS.iterator().next(); TaskEntity task = vmDcpBackend.prepareVmOperation(vmId, operation); assertThat(task, is(notNullValue())); assertThat(task.getState(), is(TaskEntity.State.QUEUED)); assertThat(task.getSteps().size(), is(1)); assertThat(task.getSteps().get(0).getOperation(), is(operation)); try { vmDcpBackend.prepareVmOperation(vmId, com.vmware.photon.controller.api.Operation.MOCK_OP); fail("vmDcpBackend.prepareVmOperation with invalid operation should have failed"); } catch (NotImplementedException e) { // do nothing } }
@Test public void testFilterByTagPagination() throws Throwable { ResourceList<Vm> foundVms = vmDcpBackend.filterByTag( vm.projectId, new Tag(vm.tags.iterator().next()), Optional.of(1)); assertThat(foundVms.getItems(), is(notNullValue())); assertThat(foundVms.getItems().size(), is(1)); assertThat(foundVms.getItems().get(0).getName(), is(vm.name)); }
@Test public void testAddTagWhenOriginalTagIsNull() throws Throwable { // overwriting the vmCreateSpec tags to empty from non-empty vmCreateSpec.setTags(null); createdVmTaskEntity = vmDcpBackend.prepareVmCreate(projectId, vmCreateSpec); vmId = createdVmTaskEntity.getEntityId(); entityLockDcpBackend.clearTaskLocks(createdVmTaskEntity); vm = vmDcpBackend.findById(vmId); int originalSize = vm.getTags().size(); Tag tag = new Tag("namespace:predicate=value"); vmDcpBackend.addTag(vm.getId(), tag); vm = vmDcpBackend.findById(vm.getId()); assertThat(vm.getTags().size(), is(1)); List<String> tagValues = vm.getTags().stream().map(t -> t.getValue()).collect(Collectors.toList()); assertTrue(tagValues.contains(tag.getValue())); }
@Test public void testPrepareVmGetMksTicketInvalidVmState() throws Throwable { try { vmDcpBackend.prepareVmGetMksTicket(vmId); } catch (InvalidVmStateException e) { assertThat( e.getMessage(), is("Get Mks Ticket is not allowed on vm that is not powered on.")); } }
@Test(expectedExceptions = InvalidImageStateException.class) public void testNullImageSizeThrowsInvalidImageStateException() throws Throwable { AttachedDiskCreateSpec disk = new AttachedDiskCreateSpec(); disk.setBootDisk(true); ImageEntity image = new ImageEntity(); image.setSize(null); List<Throwable> warnings = new ArrayList<>(); VmDcpBackend.updateBootDiskCapacity(Arrays.asList(disk), image, warnings); }
@Test public void testWithNonExistingProjectId() throws Throwable { String id = UUID.randomUUID().toString(); try { vmDcpBackend.filterByProject(id); fail("vmDcpBackend.filterByProject for a non existing projectId should have failed"); } catch (ProjectNotFoundException e) { assertThat(e.getMessage(), containsString(id)); } }
@Test public void testFindByIdWithNonExistingId() throws Throwable { String id = UUID.randomUUID().toString(); try { vmDcpBackend.findById(id); fail("vmDcpBackend.findById for a non existing id should have failed"); } catch (VmNotFoundException e) { assertThat(e.getMessage(), containsString(id)); } }
@Test public void testPrepareSetMetadata() throws Throwable { Map<String, String> metadata = new HashMap<>(); metadata.put("key", "value"); TaskEntity task = vmDcpBackend.prepareSetMetadata(vmId, metadata); assertThat(task, is(notNullValue())); assertThat(task.getState(), is(TaskEntity.State.COMPLETED)); assertThat(task.getSteps().size(), is(0)); // check that metadata was saved VmEntity updatedVm = vmDcpBackend.findById(vmId); assertThat(updatedVm, notNullValue()); assertThat(updatedVm.getMetadata(), is(metadata)); // make sure that no other fields have changed updatedVm.setMetadata(this.vm.getMetadata()); assertThat(this.vm, is(updatedVm)); }
@Test public void testPrepareVmGetNetworks() throws Throwable { TaskEntity task = vmDcpBackend.prepareVmGetNetworks(vmId); assertThat(task, is(notNullValue())); assertThat(task.getState(), is(TaskEntity.State.QUEUED)); assertThat(task.getSteps().size(), is(1)); assertThat( task.getSteps().get(0).getOperation(), is(com.vmware.photon.controller.api.Operation.GET_NETWORKS)); }
@Test public void testFilterByTag() throws Throwable { List<Vm> foundVms = vmDcpBackend .filterByTag( vm.projectId, new Tag(vm.tags.iterator().next()), Optional.<Integer>absent()) .getItems(); assertThat(foundVms, is(notNullValue())); assertThat(foundVms.size(), is(1)); assertThat(foundVms.get(0).getName(), is(vm.name)); }
@Test public void testPrepareVmDetachIso() throws Throwable { TaskEntity task = vmDcpBackend.prepareVmDetachIso(vmId); assertThat(task, is(notNullValue())); assertThat(task.getState(), is(TaskEntity.State.QUEUED)); assertThat(task.getSteps().size(), is(1)); assertThat( task.getSteps().get(0).getOperation(), is(com.vmware.photon.controller.api.Operation.DETACH_ISO)); assertThat(task.getSteps().get(0).getTransientResourceEntities().get(0), is(vm)); }
@Test public void testPrepareVmDelete() throws Throwable { TaskEntity task = vmDcpBackend.prepareVmDelete(vmId); assertThat(task, is(notNullValue())); assertThat(task.getState(), is(TaskEntity.State.QUEUED)); assertThat(task.getSteps().size(), is(1)); assertThat( task.getSteps().get(0).getOperation(), is(com.vmware.photon.controller.api.Operation.DELETE_VM)); assertThat(task.getToBeLockedEntityIds().size(), is(1)); assertThat(task.getToBeLockedEntityIds().get(0), is(vmId)); }
@Test public void testTombstone() throws Throwable { TombstoneEntity tombstone = tombstoneDcpBackend.getByEntityId(vm.getId()); assertThat(tombstone, nullValue()); assertThat(getUsage("vm.cost"), is(1.0)); vmDcpBackend.tombstone(vm); tombstone = tombstoneDcpBackend.getByEntityId(vm.getId()); assertThat(tombstone.getEntityId(), is(vm.getId())); assertThat(tombstone.getEntityKind(), is(Vm.KIND)); assertThat(getUsage("vm.cost"), is(0.0)); }
@Test public void testPrepareVmCreate() throws Throwable { String vmId = createdVmTaskEntity.getEntityId(); assertThat(createdVmTaskEntity.getSteps().size(), is(2)); assertThat( createdVmTaskEntity.getSteps().get(0).getOperation(), is(com.vmware.photon.controller.api.Operation.RESERVE_RESOURCE)); assertThat( createdVmTaskEntity .getSteps() .get(0) .getTransientResourceEntities(ProjectEntity.KIND) .size(), is(1)); assertThat( createdVmTaskEntity .getSteps() .get(0) .getTransientResourceEntities(ProjectEntity.KIND) .get(0) .getId(), is(projectId)); assertThat( createdVmTaskEntity.getSteps().get(1).getOperation(), is(com.vmware.photon.controller.api.Operation.CREATE_VM)); assertThat(createdVmTaskEntity.getToBeLockedEntityIds().size(), is(1)); assertThat(createdVmTaskEntity.getToBeLockedEntityIds().get(0), is(vmId)); VmEntity vm = vmDcpBackend.findById(vmId); assertThat(vm, is(notNullValue())); assertThat(getUsage("vm.cost"), is(1.0)); assertThat(vm.getImageId(), is(imageId)); assertThat(vm.getAffinities().get(0).getResourceId(), is("disk-id1")); assertThat(vm.getAffinities().get(0).getKind(), is("disk")); assertThat(vm.getAffinities().get(1).getResourceId(), is("disk-id2")); assertThat(vm.getAffinities().get(1).getKind(), is("disk")); Set<TagEntity> tags = vm.getTags(); assertThat(tags.size(), is(2)); TagEntity tag1 = new TagEntity(); tag1.setValue("value1"); TagEntity tag2 = new TagEntity(); tag2.setValue("value2"); assertTrue(tags.contains(tag1)); assertTrue(tags.contains(tag2)); assertThat(vmCreateSpec.getNetworks().equals(vm.getNetworks()), is(true)); }
@BeforeMethod public void setUp() throws Throwable { commonHostAndClientSetup(basicServiceHost, apiFeDcpRestClient); commonDataSetup( tenantDcpBackend, resourceTicketDcpBackend, projectDcpBackend, flavorDcpBackend, flavorLoader); commonVmAndImageSetup(vmDcpBackend, networkDcpBackend); vmId = createdVmTaskEntity.getEntityId(); entityLockDcpBackend.clearTaskLocks(createdVmTaskEntity); vm = vmDcpBackend.findById(vmId); }
@Test(dataProvider = "IsoFileNames") public void testPrepareVmAttachIso(String isoFileName) throws Throwable { TaskEntity task = vmDcpBackend.prepareVmAttachIso(vmId, inputStream, isoFileName); assertThat(task, is(notNullValue())); assertThat(task.getState(), is(TaskEntity.State.QUEUED)); assertThat(task.getSteps().size(), is(2)); assertThat( task.getSteps().get(0).getOperation(), is(com.vmware.photon.controller.api.Operation.UPLOAD_ISO)); IsoEntity iso = (IsoEntity) task.getSteps().get(0).getTransientResourceEntities().get(1); assertThat(iso.getName(), is(isoName)); assertThat(task.getToBeLockedEntityIds().size(), is(2)); assertThat(task.getToBeLockedEntityIds().get(0), is(iso.getId())); assertThat(task.getToBeLockedEntityIds().get(1), is(vmId)); }