use of com.google.api.services.compute.Compute.Instances.StartWithEncryptionKey in project cloudbreak by hortonworks.
the class GcpInstanceResourceBuilderTest method doTestCustomEncryption.
public void doTestCustomEncryption(Map<String, Object> params, CustomerEncryptionKey encryptionKey) throws IOException {
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
CloudInstance cloudInstance = newCloudInstance(params, instanceAuthentication);
when(compute.instances()).thenReturn(instances);
ArgumentCaptor<InstancesStartWithEncryptionKeyRequest> requestCaptor = ArgumentCaptor.forClass(InstancesStartWithEncryptionKeyRequest.class);
Get get = Mockito.mock(Get.class);
when(instances.get(anyString(), anyString(), anyString())).thenReturn(get);
StartWithEncryptionKey start = Mockito.mock(StartWithEncryptionKey.class);
when(instances.startWithEncryptionKey(anyString(), anyString(), anyString(), requestCaptor.capture())).thenReturn(start);
String expectedSource = "google.disk";
AttachedDisk disk = new AttachedDisk();
disk.setSource(expectedSource);
Instance instance = new Instance();
instance.setDisks(List.of(disk));
instance.setStatus("TERMINATED");
when(get.execute()).thenReturn(instance);
when(start.setPrettyPrint(true)).thenReturn(start);
when(start.execute()).thenReturn(operation);
when(customGcpDiskEncryptionService.hasCustomEncryptionRequested(any(InstanceTemplate.class))).thenReturn(true);
when(customGcpDiskEncryptionCreatorService.createCustomerEncryptionKey(any(InstanceTemplate.class))).thenReturn(encryptionKey);
CloudVmInstanceStatus vmInstanceStatus = builder.start(context, authenticatedContext, cloudInstance);
assertEquals(InstanceStatus.IN_PROGRESS, vmInstanceStatus.getStatus());
verify(customGcpDiskEncryptionCreatorService, times(1)).createCustomerEncryptionKey(any(InstanceTemplate.class));
verify(instances, times(0)).start(anyString(), anyString(), anyString());
InstancesStartWithEncryptionKeyRequest keyRequest = requestCaptor.getValue();
assertNotNull(keyRequest.getDisks());
assertEquals(1, keyRequest.getDisks().size());
CustomerEncryptionKeyProtectedDisk protectedDisk = keyRequest.getDisks().iterator().next();
assertEquals(encryptionKey, protectedDisk.getDiskEncryptionKey());
assertEquals(expectedSource, protectedDisk.getSource());
}
Aggregations