Search in sources :

Example 91 with Operation

use of com.google.api.services.compute.model.Operation in project pipeline5 by hartwigmedical.

the class GoogleComputeEngineTest method setUp.

@Before
public void setUp() throws Exception {
    images = mock(Compute.Images.class);
    imagesFromFamily = mock(Compute.Images.GetFromFamily.class);
    when(imagesFromFamily.execute()).thenReturn(new Image());
    when(images.getFromFamily(ARGUMENTS.project(), VirtualMachineJobDefinition.STANDARD_IMAGE)).thenReturn(imagesFromFamily);
    final ArgumentCaptor<Instance> instanceArgumentCaptor = ArgumentCaptor.forClass(Instance.class);
    Operation insertOperation = mock(Operation.class);
    when(insertOperation.getName()).thenReturn("insert");
    final Compute.Instances instances = mock(Compute.Instances.class);
    lifecycleManager = mock(InstanceLifecycleManager.class);
    instance = mock(Instance.class);
    when(lifecycleManager.newInstance()).thenReturn(instance);
    when(lifecycleManager.deleteOldInstancesAndStart(instanceArgumentCaptor.capture(), any(), any())).thenReturn(insertOperation);
    when(instance.getName()).thenReturn(INSTANCE_NAME);
    Compute.Instances.Stop stop = mock(Compute.Instances.Stop.class);
    Operation stopOperation = mock(Operation.class);
    when(stopOperation.getName()).thenReturn("stop");
    when(stopOperation.getStatus()).thenReturn(DONE);
    when(stop.execute()).thenReturn(stopOperation);
    when(instances.stop(ARGUMENTS.project(), FIRST_ZONE_NAME, INSTANCE_NAME)).thenReturn(stop);
    Compute.Instances.Delete delete = mock(Compute.Instances.Delete.class);
    Operation deleteOperation = mock(Operation.class);
    when(deleteOperation.getName()).thenReturn("delete");
    when(deleteOperation.getStatus()).thenReturn(DONE);
    when(delete.execute()).thenReturn(stopOperation);
    when(instances.delete(ARGUMENTS.project(), FIRST_ZONE_NAME, INSTANCE_NAME)).thenReturn(delete);
    Compute.Instances.List list = mock(Compute.Instances.List.class);
    InstanceList instanceList = mock(InstanceList.class);
    Instance one = mock(Instance.class);
    Instance two = mock(Instance.class);
    Instance three = mock(Instance.class);
    when(one.getName()).thenReturn("vm-1");
    when(two.getName()).thenReturn("vm-2");
    when(three.getName()).thenReturn("vm-3");
    List<Instance> existingInstances = Arrays.asList(one, two, three);
    when(instances.list(any(), any())).thenReturn(list);
    when(list.execute()).thenReturn(instanceList);
    when(instanceList.getItems()).thenReturn(existingInstances);
    final Compute.ZoneOperations zoneOperations = mock(Compute.ZoneOperations.class);
    final Compute.ZoneOperations.Get zoneOpGet = mock(Compute.ZoneOperations.Get.class);
    Operation zoneOpGetOperation = mock(Operation.class);
    when(zoneOpGetOperation.getStatus()).thenReturn(DONE);
    when(zoneOpGet.execute()).thenReturn(zoneOpGetOperation);
    when(zoneOperations.get(ARGUMENTS.project(), FIRST_ZONE_NAME, "insert")).thenReturn(zoneOpGet);
    when(zoneOperations.get(ARGUMENTS.project(), FIRST_ZONE_NAME, "stop")).thenReturn(zoneOpGet);
    compute = mock(Compute.class);
    when(compute.images()).thenReturn(images);
    when(compute.instances()).thenReturn(instances);
    when(compute.zoneOperations()).thenReturn(zoneOperations);
    Compute.Zones zones = mock(Compute.Zones.class);
    Compute.Zones.List zonesList = mock(Compute.Zones.List.class);
    when(zonesList.execute()).thenReturn(new ZoneList().setItems(Lists.newArrayList(zone(FIRST_ZONE_NAME), zone(SECOND_ZONE_NAME))));
    when(zones.list(ARGUMENTS.project())).thenReturn(zonesList);
    when(compute.zones()).thenReturn(zones);
    bucketWatcher = mock(BucketCompletionWatcher.class);
    victim = new GoogleComputeEngine(ARGUMENTS, compute, z -> {
    }, lifecycleManager, bucketWatcher, Labels.of(Arguments.testDefaults(), TestInputs.defaultSomaticRunMetadata()));
    runtimeBucket = MockRuntimeBucket.test();
    jobDefinition = VirtualMachineJobDefinition.builder().name("test").namespacedResults(RESULTS_DIRECTORY).startupCommand(BashStartupScript.of(runtimeBucket.getRuntimeBucket().name())).build();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) Instance(com.google.api.services.compute.model.Instance) InstanceList(com.google.api.services.compute.model.InstanceList) Tags(com.google.api.services.compute.model.Tags) Arguments(com.hartwig.pipeline.Arguments) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MockRuntimeBucket(com.hartwig.pipeline.testsupport.MockRuntimeBucket) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Zone(com.google.api.services.compute.model.Zone) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) Lists(com.google.common.collect.Lists) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) TestInputs(com.hartwig.pipeline.testsupport.TestInputs) Map(java.util.Map) PipelineStatus(com.hartwig.pipeline.execution.PipelineStatus) Before(org.junit.Before) ZoneList(com.google.api.services.compute.model.ZoneList) State(com.hartwig.pipeline.execution.vm.BucketCompletionWatcher.State) ResultsDirectory(com.hartwig.pipeline.ResultsDirectory) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) Scheduling(com.google.api.services.compute.model.Scheduling) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Operation(com.google.api.services.compute.model.Operation) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) List(java.util.List) Image(com.google.api.services.compute.model.Image) Labels(com.hartwig.pipeline.labels.Labels) Collections(java.util.Collections) Compute(com.google.api.services.compute.Compute) Mockito.mock(org.mockito.Mockito.mock) Instance(com.google.api.services.compute.model.Instance) Operation(com.google.api.services.compute.model.Operation) Image(com.google.api.services.compute.model.Image) ZoneList(com.google.api.services.compute.model.ZoneList) Compute(com.google.api.services.compute.Compute) InstanceList(com.google.api.services.compute.model.InstanceList) Before(org.junit.Before)

Example 92 with Operation

use of com.google.api.services.compute.model.Operation in project pipeline5 by hartwigmedical.

the class GoogleComputeEngineTest method triesMultipleZonesWhenResourcesExhausted.

@Test
public void triesMultipleZonesWhenResourcesExhausted() {
    Operation resourcesExhausted = new Operation().setStatus("DONE").setName("insert").setError(new Operation.Error().setErrors(Collections.singletonList(new Operation.Error.Errors().setCode(GoogleComputeEngine.ZONE_EXHAUSTED_ERROR_CODE))));
    when(lifecycleManager.deleteOldInstancesAndStart(instance, FIRST_ZONE_NAME, INSTANCE_NAME)).thenReturn(resourcesExhausted, mock(Operation.class));
    when(bucketWatcher.currentState(any(), any())).thenReturn(State.STILL_WAITING, State.STILL_WAITING, State.SUCCESS);
    victim.submit(runtimeBucket.getRuntimeBucket(), jobDefinition);
    verify(lifecycleManager).deleteOldInstancesAndStart(instance, SECOND_ZONE_NAME, INSTANCE_NAME);
}
Also used : Operation(com.google.api.services.compute.model.Operation) Test(org.junit.Test)

Example 93 with Operation

use of com.google.api.services.compute.model.Operation in project pipeline5 by hartwigmedical.

the class GoogleComputeEngineTest method triesMultipleZonesWhenUnsupportedOperation.

@Test
public void triesMultipleZonesWhenUnsupportedOperation() {
    Operation resourcesExhausted = new Operation().setStatus("DONE").setName("insert").setError(new Operation.Error().setErrors(Collections.singletonList(new Operation.Error.Errors().setCode(GoogleComputeEngine.UNSUPPORTED_OPERATION_ERROR_CODE))));
    when(lifecycleManager.deleteOldInstancesAndStart(instance, FIRST_ZONE_NAME, INSTANCE_NAME)).thenReturn(resourcesExhausted, mock(Operation.class));
    when(bucketWatcher.currentState(any(), any())).thenReturn(State.STILL_WAITING, State.STILL_WAITING, State.SUCCESS);
    victim.submit(runtimeBucket.getRuntimeBucket(), jobDefinition);
    verify(lifecycleManager).deleteOldInstancesAndStart(instance, SECOND_ZONE_NAME, INSTANCE_NAME);
}
Also used : Operation(com.google.api.services.compute.model.Operation) Test(org.junit.Test)

Example 94 with Operation

use of com.google.api.services.compute.model.Operation in project pipeline5 by hartwigmedical.

the class InstanceLifecycleManagerTest method shouldStop.

@Test
public void shouldStop() throws Exception {
    Compute.Instances.Stop stop = mock(Compute.Instances.Stop.class);
    Operation stopOperation = mock(Operation.class);
    String stopOperationName = "stoperation";
    when(instances.stop(ARGUMENTS.project(), zoneOne, vmName)).thenReturn(stop);
    when(stop.execute()).thenReturn(stopOperation);
    when(stopOperation.getName()).thenReturn(stopOperationName);
    when(stopOperation.getStatus()).thenReturn(DONE_STATUS);
    Compute.ZoneOperations zoneOperations = mock(Compute.ZoneOperations.class);
    Compute.ZoneOperations.Get zoneOpsGet = mock(Compute.ZoneOperations.Get.class);
    Operation statusOperation = mock(Operation.class);
    when(compute.zoneOperations()).thenReturn(zoneOperations);
    when(zoneOperations.get(ARGUMENTS.project(), zoneOne, stopOperationName)).thenReturn(zoneOpsGet);
    when(zoneOpsGet.execute()).thenReturn(statusOperation);
    when(statusOperation.getStatus()).thenReturn(DONE_STATUS);
    victim.stop(zoneOne, vmName);
    verify(stop).execute();
}
Also used : Compute(com.google.api.services.compute.Compute) Operation(com.google.api.services.compute.model.Operation) Test(org.junit.Test)

Example 95 with Operation

use of com.google.api.services.compute.model.Operation in project terra-resource-buffer by DataBiosphere.

the class CreateNetworkStep method doStep.

@Override
public StepResult doStep(FlightContext flightContext) throws RetryException {
    String projectId = flightContext.getWorkingMap().get(GOOGLE_PROJECT_ID, String.class);
    try {
        Network network = new Network().setName(GoogleUtils.NETWORK_NAME).setAutoCreateSubnetworks(false);
        // Skip this steps if network already exists. This may happen when previous step's polling
        // times out, while network is created before next retry.
        Optional<Operation> insertOperation = createResourceAndIgnoreConflict(() -> computeCow.networks().insert(projectId, network).execute());
        if (insertOperation.isPresent()) {
            OperationCow<?> operation = computeCow.globalOperations().operationCow(projectId, insertOperation.get());
            pollUntilSuccess(operation, Duration.ofSeconds(3), Duration.ofMinutes(5));
        }
    } catch (IOException | InterruptedException e) {
        logger.info("Error when creating network", e);
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_RETRY, e);
    }
    return StepResult.getStepResultSuccess();
}
Also used : Network(com.google.api.services.compute.model.Network) Operation(com.google.api.services.compute.model.Operation) IOException(java.io.IOException) StepResult(bio.terra.stairway.StepResult)

Aggregations

Operation (com.google.api.services.compute.model.Operation)96 Compute (com.google.api.services.compute.Compute)40 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)37 IOException (java.io.IOException)37 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)23 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)21 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)17 Instance (com.google.api.services.compute.model.Instance)15 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)15 AutoScalingData (org.apache.druid.indexing.overlord.autoscaling.AutoScalingData)14 GcpContext (com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext)12 InstanceGroupManagersListManagedInstancesResponse (com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse)10 Location (com.sequenceiq.cloudbreak.cloud.model.Location)10 HashMap (java.util.HashMap)10 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)9 Disk (com.google.api.services.compute.model.Disk)8 InstanceGroupManagersDeleteInstancesRequest (com.google.api.services.compute.model.InstanceGroupManagersDeleteInstancesRequest)8 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)8 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)8