VmGetIpStepCmd extends StepCommand { @Override protected void execute() throws ApiFeException, InterruptedException, RpcException { String vmId = (String) step.getTransientResource(ResourceReserveStepCmd.VM_ID); checkNotNull(vmId, "VM id is not available"); Map<String, VmService.NetworkInfo> networkInfoMap = getVmNetworkInfo(vmId); logger.info("VM {} requested to be connected to {} network(s)", vmId, networkInfoMap.size()); if (networkInfoMap.size() > 0) { for (VmService.NetworkInfo networkInfo : networkInfoMap.values()) { networkInfo.privateIpAddress = allocateIpToMac(networkInfo.id, vmId, networkInfo.macAddress); taskCommand.getPhotonControllerXenonRestClient().post( SubnetIPLeaseSyncTriggerService.SELF_LINK , SubnetIPLeaseSyncUtils.buildSubnetIPLeaseSyncState(networkInfo.id)); } updateVmNetworkInfo(vmId, networkInfoMap); } } VmGetIpStepCmd(TaskCommand taskCommand, StepBackend stepBackend, StepEntity step);   }
@Test public void testVmIdMissing() throws Throwable { VmGetIpStepCmd command = getVmGetIpStepCmd(); try { command.execute(); fail("Should have failed due to missing vm id"); } catch (NullPointerException e) { assertThat(e.getMessage(), containsString("VM id is not available")); } }
@Test public void testSuccess() throws Throwable { VmGetIpStepCmd command = getVmGetIpStepCmd(); step.createOrUpdateTransientResource(ResourceReserveStepCmd.VM_ID, "vm-id"); Operation vmServiceStateGetOp = mock(Operation.class); Operation vmServiceStateUpdateOp = new Operation().setStatusCode(Operation.STATUS_CODE_OK); VmService.State state = createVmServiceState(); DhcpSubnetService.IpOperationPatch ipOperationPatchResult = new DhcpSubnetService.IpOperationPatch( DhcpSubnetService.IpOperationPatch.Kind.AllocateIp, "vm-id", "macAddress", "10.0.0.1"); Operation allocateIpResultOp = new Operation().setBody(ipOperationPatchResult); DhcpSubnetService.IpOperationPatch ipOperationPatchResult2 = new DhcpSubnetService.IpOperationPatch( DhcpSubnetService.IpOperationPatch.Kind.AllocateIp, "vm-id", "macAddress2", "10.0.0.2"); Operation allocateIpResultOp2 = new Operation().setBody(ipOperationPatchResult2); doReturn(state).when(vmServiceStateGetOp).getBody(any()); doReturn(vmServiceStateGetOp).when(photonControllerXenonRestClient).get(VmServiceFactory.SELF_LINK + "/" + "vm-id"); doReturn(allocateIpResultOp).when(photonControllerXenonRestClient) .patch(eq(DhcpSubnetService.FACTORY_LINK + "/" + "network-id"), any(DhcpSubnetService.IpOperationPatch.class)); doReturn(allocateIpResultOp2).when(photonControllerXenonRestClient) .patch(eq(DhcpSubnetService.FACTORY_LINK + "/" + "network-id2"), any(DhcpSubnetService.IpOperationPatch.class)); doReturn(vmServiceStateUpdateOp).when(photonControllerXenonRestClient) .patch(eq(VmServiceFactory.SELF_LINK + "/" + "vm-id"), any(VmService.State.class)); command.execute(); verify(photonControllerXenonRestClient).get(eq(VmServiceFactory.SELF_LINK + "/vm-id")); verify(photonControllerXenonRestClient, times(3)).patch(anyString(), any(DhcpSubnetService.IpOperationPatch.class)); }
@Test public void testSubnetIdMissing() throws Throwable { VmGetIpStepCmd command = getVmGetIpStepCmd(); step.createOrUpdateTransientResource(ResourceReserveStepCmd.VM_ID, "vm-id"); Operation operation = mock(Operation.class); VmService.State state = createVmServiceState(); for (VmService.NetworkInfo networkInfo : state.networkInfo.values()) { networkInfo.id = null; } doReturn(state).when(operation).getBody(any()); doReturn(operation).when(photonControllerXenonRestClient).get(anyString()); try { command.execute(); fail("Should have failed due to missing network id"); } catch (NullPointerException e) { assertThat(e.getMessage(), containsString("subnetId is not available")); } }
@Test public void testMacAddressMissing() throws Throwable { VmGetIpStepCmd command = getVmGetIpStepCmd(); step.createOrUpdateTransientResource(ResourceReserveStepCmd.VM_ID, "vm-id"); Operation operation = mock(Operation.class); VmService.State state = createVmServiceState(); for (VmService.NetworkInfo networkInfo : state.networkInfo.values()) { networkInfo.macAddress = null; } doReturn(state).when(operation).getBody(any()); doReturn(operation).when(photonControllerXenonRestClient).get(anyString()); try { command.execute(); fail("Should have failed due to missing macAddress"); } catch (NullPointerException e) { assertThat(e.getMessage(), containsString("macAddress is not available")); } }
@Test public void testUpdateAddressFailed() throws Throwable { VmGetIpStepCmd command = getVmGetIpStepCmd(); step.createOrUpdateTransientResource(ResourceReserveStepCmd.VM_ID, "vm-id"); Operation vmServiceStateGetOp = mock(Operation.class); Operation vmServiceStateUpdateOp = new Operation().setStatusCode(Operation.STATUS_CODE_TIMEOUT); VmService.State state = createVmServiceState(); DhcpSubnetService.IpOperationPatch ipOperationPatchResult = new DhcpSubnetService.IpOperationPatch( DhcpSubnetService.IpOperationPatch.Kind.AllocateIp, "vm-id", "macAddress", "10.0.0.1"); Operation allocateIpResultOp = new Operation().setBody(ipOperationPatchResult); DhcpSubnetService.IpOperationPatch ipOperationPatchResult2 = new DhcpSubnetService.IpOperationPatch( DhcpSubnetService.IpOperationPatch.Kind.AllocateIp, "vm-id", "macAddress2", "10.0.0.2"); Operation allocateIpResultOp2 = new Operation().setBody(ipOperationPatchResult2); doReturn(state).when(vmServiceStateGetOp).getBody(any()); doReturn(vmServiceStateGetOp).when(photonControllerXenonRestClient).get(VmServiceFactory.SELF_LINK + "/" + "vm-id"); doReturn(allocateIpResultOp).when(photonControllerXenonRestClient) .patch(eq(DhcpSubnetService.FACTORY_LINK + "/" + "network-id"), any(DhcpSubnetService.IpOperationPatch.class)); doReturn(allocateIpResultOp2).when(photonControllerXenonRestClient) .patch(eq(DhcpSubnetService.FACTORY_LINK + "/" + "network-id2"), any(DhcpSubnetService.IpOperationPatch.class)); doReturn(vmServiceStateUpdateOp).when(photonControllerXenonRestClient) .patch(eq(VmServiceFactory.SELF_LINK + "/" + "vm-id"), any(VmService.State.class)); try { command.execute(); fail("Should have failed due to update issue."); } catch (IllegalStateException e) { assertThat(e.getMessage(), is("Failed to update VM's NetworkInfo. StatusCode: 408")); } }