From 3af1497a07fb381ceee963676434c02f4212c252 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 1 Mar 2016 16:17:38 +0100 Subject: [PATCH] Add RemoteComputeHelper and integration tests --- .../compute/testing/RemoteComputeHelper.java | 117 ++++++ .../gcloud/compute/testing/package-info.java | 31 ++ .../gcloud/compute/it/ITComputeTest.java | 378 ++++++++++++++++++ 3 files changed, 526 insertions(+) create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/testing/RemoteComputeHelper.java create mode 100644 gcloud-java-compute/src/main/java/com/google/gcloud/compute/testing/package-info.java create mode 100644 gcloud-java-compute/src/test/java/com/google/gcloud/compute/it/ITComputeTest.java diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/testing/RemoteComputeHelper.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/testing/RemoteComputeHelper.java new file mode 100644 index 000000000000..266b31cd7c93 --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/testing/RemoteComputeHelper.java @@ -0,0 +1,117 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed 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 com.google.gcloud.compute.testing; + +import com.google.gcloud.AuthCredentials; +import com.google.gcloud.RetryParams; +import com.google.gcloud.compute.ComputeOptions; + +import java.io.IOException; +import java.io.InputStream; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Utility to create a remote Compute configuration for testing. Compute options can be obtained + * via the {@link #options()} method. Returned options have custom + * {@link ComputeOptions#retryParams()}: {@link RetryParams#retryMaxAttempts()} is {@code 10}, + * {@link RetryParams#retryMinAttempts()} is {@code 6}, {@link RetryParams#maxRetryDelayMillis()} is + * {@code 30000}, {@link RetryParams#totalRetryPeriodMillis()} is {@code 120000} and + * {@link RetryParams#initialRetryDelayMillis()} is {@code 250}. + * {@link ComputeOptions#connectTimeout()} and {@link ComputeOptions#readTimeout()} are both set to + * {@code 60000}. + */ +public class RemoteComputeHelper { + + private static final Logger log = Logger.getLogger(RemoteComputeHelper.class.getName()); + private final ComputeOptions options; + + private RemoteComputeHelper(ComputeOptions options) { + this.options = options; + } + + /** + * Returns a {@link ComputeOptions} object to be used for testing. + */ + public ComputeOptions options() { + return options; + } + + /** + * Creates a {@code RemoteComputeHelper} object for the given project id and JSON key input + * stream. + * + * @param projectId id of the project to be used for running the tests + * @param keyStream input stream for a JSON key + * @return A {@code RemoteComputeHelper} object for the provided options + * @throws ComputeHelperException if {@code keyStream} is not a valid JSON key stream + */ + public static RemoteComputeHelper create(String projectId, InputStream keyStream) + throws ComputeHelperException { + try { + ComputeOptions computeOptions = ComputeOptions.builder() + .authCredentials(AuthCredentials.createForJson(keyStream)) + .projectId(projectId) + .retryParams(retryParams()) + .connectTimeout(60000) + .readTimeout(60000) + .build(); + return new RemoteComputeHelper(computeOptions); + } catch (IOException ex) { + if (log.isLoggable(Level.WARNING)) { + log.log(Level.WARNING, ex.getMessage()); + } + throw ComputeHelperException.translate(ex); + } + } + + /** + * Creates a {@code RemoteComputeHelper} object using default project id and authentication + * credentials. + */ + public static RemoteComputeHelper create() { + ComputeOptions computeOptions = ComputeOptions.builder() + .retryParams(retryParams()) + .connectTimeout(60000) + .readTimeout(60000) + .build(); + return new RemoteComputeHelper(computeOptions); + } + + private static RetryParams retryParams() { + return RetryParams.builder() + .retryMaxAttempts(10) + .retryMinAttempts(6) + .maxRetryDelayMillis(30000) + .totalRetryPeriodMillis(120000) + .initialRetryDelayMillis(250) + .build(); + } + + public static class ComputeHelperException extends RuntimeException { + + private static final long serialVersionUID = -5747977015007639912L; + + public ComputeHelperException(String message, Throwable cause) { + super(message, cause); + } + + public static ComputeHelperException translate(Exception ex) { + return new ComputeHelperException(ex.getMessage(), ex); + } + } +} diff --git a/gcloud-java-compute/src/main/java/com/google/gcloud/compute/testing/package-info.java b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/testing/package-info.java new file mode 100644 index 000000000000..86f1c2428cde --- /dev/null +++ b/gcloud-java-compute/src/main/java/com/google/gcloud/compute/testing/package-info.java @@ -0,0 +1,31 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed 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. + */ + +/** + * A testing helper for Google Compute Engine. + * + *

A simple usage example: + * + *

Before the test: + *

 {@code
+ * RemoteComputeHelper computeHelper = RemoteComputeHelper.create();
+ * Compute compute = computeHelper.options().service();
+ * } 
+ * + * @see + * gcloud-java tools for testing + */ +package com.google.gcloud.compute.testing; diff --git a/gcloud-java-compute/src/test/java/com/google/gcloud/compute/it/ITComputeTest.java b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/it/ITComputeTest.java new file mode 100644 index 000000000000..15f3e1c05420 --- /dev/null +++ b/gcloud-java-compute/src/test/java/com/google/gcloud/compute/it/ITComputeTest.java @@ -0,0 +1,378 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed 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 com.google.gcloud.compute.it; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import com.google.gcloud.Page; +import com.google.gcloud.compute.Compute; +import com.google.gcloud.compute.DiskType; +import com.google.gcloud.compute.License; +import com.google.gcloud.compute.LicenseId; +import com.google.gcloud.compute.MachineType; +import com.google.gcloud.compute.Region; +import com.google.gcloud.compute.Zone; +import com.google.gcloud.compute.testing.RemoteComputeHelper; + +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; + +import java.util.Iterator; + +public class ITComputeTest { + + private static final String REGION = "us-central1"; + private static final String ZONE = "us-central1-a"; + private static final String DISK_TYPE = "local-ssd"; + private static final String MACHINE_TYPE = "f1-micro"; + private static final LicenseId LICENSE_ID = LicenseId.of("ubuntu-os-cloud", "ubuntu-1404-trusty"); + + private static Compute compute; + + @Rule + public Timeout globalTimeout = Timeout.seconds(300); + + @BeforeClass + public static void beforeClass() throws InterruptedException { + RemoteComputeHelper computeHelper = RemoteComputeHelper.create(); + compute = computeHelper.options().service(); + } + + @Test + public void testGetDiskType() { + DiskType diskType = compute.getDiskType(ZONE, DISK_TYPE); + // todo(mziccard): uncomment or remove once #695 is closed + // assertNotNull(diskType.id()); + assertEquals(ZONE, diskType.diskTypeId().zone()); + assertEquals(DISK_TYPE, diskType.diskTypeId().diskType()); + assertNotNull(diskType.creationTimestamp()); + assertNotNull(diskType.description()); + assertNotNull(diskType.validDiskSize()); + assertNotNull(diskType.defaultDiskSizeGb()); + } + + @Test + public void testGetDiskTypeWithSelectedFields() { + DiskType diskType = compute.getDiskType(ZONE, DISK_TYPE, + Compute.DiskTypeOption.fields(Compute.DiskTypeField.CREATION_TIMESTAMP)); + // todo(mziccard): uncomment or remove once #695 is closed + // assertNotNull(diskType.id()); + assertEquals(ZONE, diskType.diskTypeId().zone()); + assertEquals(DISK_TYPE, diskType.diskTypeId().diskType()); + assertNotNull(diskType.creationTimestamp()); + assertNull(diskType.description()); + assertNull(diskType.validDiskSize()); + assertNull(diskType.defaultDiskSizeGb()); + } + + @Test + public void testListDiskTypes() { + Page diskPage = compute.listDiskTypes(ZONE); + Iterator diskTypeIterator = diskPage.iterateAll(); + while(diskTypeIterator.hasNext()) { + DiskType diskType = diskTypeIterator.next(); + // todo(mziccard): uncomment or remove once #695 is closed + // assertNotNull(diskType.id()); + assertNotNull(diskType.diskTypeId()); + assertEquals(ZONE, diskType.diskTypeId().zone()); + assertNotNull(diskType.creationTimestamp()); + assertNotNull(diskType.description()); + assertNotNull(diskType.validDiskSize()); + assertNotNull(diskType.defaultDiskSizeGb()); + } + } + + @Test + public void testListDiskTypesWithSelectedFields() { + Page diskPage = compute.listDiskTypes(ZONE, + Compute.DiskTypeListOption.fields(Compute.DiskTypeField.CREATION_TIMESTAMP)); + Iterator diskTypeIterator = diskPage.iterateAll(); + while(diskTypeIterator.hasNext()) { + DiskType diskType = diskTypeIterator.next(); + assertNull(diskType.id()); + assertNotNull(diskType.diskTypeId()); + assertEquals(ZONE, diskType.diskTypeId().zone()); + assertNotNull(diskType.creationTimestamp()); + assertNull(diskType.description()); + assertNull(diskType.validDiskSize()); + assertNull(diskType.defaultDiskSizeGb()); + } + } + + @Test + public void testAggregatedListDiskTypes() { + Page diskPage = compute.listDiskTypes(); + Iterator diskTypeIterator = diskPage.iterateAll(); + while(diskTypeIterator.hasNext()) { + DiskType diskType = diskTypeIterator.next(); + // todo(mziccard): uncomment or remove once #695 is closed + // assertNotNull(diskType.id()); + assertNotNull(diskType.diskTypeId()); + assertNotNull(diskType.creationTimestamp()); + assertNotNull(diskType.description()); + assertNotNull(diskType.validDiskSize()); + assertNotNull(diskType.defaultDiskSizeGb()); + } + } + + @Test + public void testAggregatedListDiskTypesWithSelectedFields() { + Page diskPage = compute.listDiskTypes( + Compute.DiskTypeListOption.fields(Compute.DiskTypeField.CREATION_TIMESTAMP)); + Iterator diskTypeIterator = diskPage.iterateAll(); + while(diskTypeIterator.hasNext()) { + DiskType diskType = diskTypeIterator.next(); + assertNull(diskType.id()); + assertNotNull(diskType.diskTypeId()); + assertEquals(ZONE, diskType.diskTypeId().zone()); + assertNotNull(diskType.creationTimestamp()); + assertNull(diskType.description()); + assertNull(diskType.validDiskSize()); + assertNull(diskType.defaultDiskSizeGb()); + } + } + + @Test + public void testGetMachineType() { + MachineType machineType = compute.getMachineType(ZONE, MACHINE_TYPE); + assertEquals(ZONE, machineType.machineTypeId().zone()); + assertEquals(MACHINE_TYPE, machineType.machineTypeId().machineType()); + assertNotNull(machineType.id()); + assertNotNull(machineType.creationTimestamp()); + assertNotNull(machineType.description()); + assertNotNull(machineType.cpus()); + assertNotNull(machineType.memoryMb()); + assertNotNull(machineType.maximumPersistentDisks()); + assertNotNull(machineType.maximumPersistentDisksSizeGb()); + } + + @Test + public void testGetMachineTypeWithSelectedFields() { + MachineType machineType = compute.getMachineType(ZONE, MACHINE_TYPE, + Compute.MachineTypeOption.fields(Compute.MachineTypeField.ID)); + assertEquals(ZONE, machineType.machineTypeId().zone()); + assertEquals(MACHINE_TYPE, machineType.machineTypeId().machineType()); + assertNotNull(machineType.id()); + assertNull(machineType.creationTimestamp()); + assertNull(machineType.description()); + assertNull(machineType.cpus()); + assertNull(machineType.memoryMb()); + assertNull(machineType.maximumPersistentDisks()); + assertNull(machineType.maximumPersistentDisksSizeGb()); + } + + @Test + public void testListMachineTypes() { + Page machinePage = compute.listMachineTypes(ZONE); + Iterator machineTypeIterator = machinePage.iterateAll(); + while(machineTypeIterator.hasNext()) { + MachineType machineType = machineTypeIterator.next(); + assertNotNull(machineType.machineTypeId()); + assertEquals(ZONE, machineType.machineTypeId().zone()); + assertNotNull(machineType.id()); + assertNotNull(machineType.creationTimestamp()); + assertNotNull(machineType.description()); + assertNotNull(machineType.cpus()); + assertNotNull(machineType.memoryMb()); + assertNotNull(machineType.maximumPersistentDisks()); + assertNotNull(machineType.maximumPersistentDisksSizeGb()); + } + } + + @Test + public void testListMachineTypesWithSelectedFields() { + Page machinePage = compute.listMachineTypes(ZONE, + Compute.MachineTypeListOption.fields(Compute.MachineTypeField.CREATION_TIMESTAMP)); + Iterator machineTypeIterator = machinePage.iterateAll(); + while(machineTypeIterator.hasNext()) { + MachineType machineType = machineTypeIterator.next(); + assertNotNull(machineType.machineTypeId()); + assertEquals(ZONE, machineType.machineTypeId().zone()); + assertNull(machineType.id()); + assertNotNull(machineType.creationTimestamp()); + assertNull(machineType.description()); + assertNull(machineType.cpus()); + assertNull(machineType.memoryMb()); + assertNull(machineType.maximumPersistentDisks()); + assertNull(machineType.maximumPersistentDisksSizeGb()); + } + } + + @Test + public void testAggregatedListMachineTypes() { + Page machinePage = compute.listMachineTypes(); + Iterator machineTypeIterator = machinePage.iterateAll(); + while(machineTypeIterator.hasNext()) { + MachineType machineType = machineTypeIterator.next(); + assertNotNull(machineType.machineTypeId()); + assertNotNull(machineType.id()); + assertNotNull(machineType.creationTimestamp()); + assertNotNull(machineType.description()); + assertNotNull(machineType.cpus()); + assertNotNull(machineType.memoryMb()); + assertNotNull(machineType.maximumPersistentDisks()); + assertNotNull(machineType.maximumPersistentDisksSizeGb()); + } + } + + @Test + public void testAggregatedListMachineTypesWithSelectedFields() { + Page machinePage = compute.listMachineTypes( + Compute.MachineTypeListOption.fields(Compute.MachineTypeField.CREATION_TIMESTAMP)); + Iterator machineTypeIterator = machinePage.iterateAll(); + while(machineTypeIterator.hasNext()) { + MachineType machineType = machineTypeIterator.next(); + assertNotNull(machineType.machineTypeId()); + assertNull(machineType.id()); + assertNotNull(machineType.creationTimestamp()); + assertNull(machineType.description()); + assertNull(machineType.cpus()); + assertNull(machineType.memoryMb()); + assertNull(machineType.maximumPersistentDisks()); + assertNull(machineType.maximumPersistentDisksSizeGb()); + } + } + + @Test + public void testGetLicense() { + License license= compute.getLicense(LICENSE_ID); + assertEquals(LICENSE_ID, license.licenseId()); + assertNotNull(license.chargesUseFee()); + } + + @Test + public void testGetLicenseWithSelectedFields() { + License license = compute.getLicense(LICENSE_ID, Compute.LicenseOption.fields()); + assertEquals(LICENSE_ID, license.licenseId()); + assertNull(license.chargesUseFee()); + } + + @Test + public void testGetRegion() { + Region region = compute.getRegion(REGION); + assertEquals(REGION, region.regionId().region()); + assertNotNull(region.description()); + assertNotNull(region.creationTimestamp()); + assertNotNull(region.id()); + assertNotNull(region.quotas()); + assertNotNull(region.status()); + assertNotNull(region.zones()); + } + + @Test + public void testGetRegionWithSelectedFields() { + Region region = compute.getRegion(REGION, Compute.RegionOption.fields(Compute.RegionField.ID)); + assertEquals(REGION, region.regionId().region()); + assertNotNull(region.id()); + assertNull(region.description()); + assertNull(region.creationTimestamp()); + assertNull(region.quotas()); + assertNull(region.status()); + assertNull(region.zones()); + } + + @Test + public void testListRegions() { + Page regionPage = compute.listRegions(); + Iterator regionIterator = regionPage.iterateAll(); + while(regionIterator.hasNext()) { + Region region = regionIterator.next(); + assertNotNull(region.regionId()); + assertNotNull(region.description()); + assertNotNull(region.creationTimestamp()); + assertNotNull(region.id()); + assertNotNull(region.quotas()); + assertNotNull(region.status()); + assertNotNull(region.zones()); + } + } + + @Test + public void testListRegionsWithSelectedFields() { + Page regionPage = + compute.listRegions(Compute.RegionListOption.fields(Compute.RegionField.ID)); + Iterator regionIterator = regionPage.iterateAll(); + while(regionIterator.hasNext()) { + Region region = regionIterator.next(); + assertNotNull(region.regionId()); + assertNull(region.description()); + assertNull(region.creationTimestamp()); + assertNotNull(region.id()); + assertNull(region.quotas()); + assertNull(region.status()); + assertNull(region.zones()); + } + } + + @Test + public void testGetZone() { + Zone zone = compute.getZone(ZONE); + assertEquals(ZONE, zone.zoneId().zone()); + assertNotNull(zone.id()); + assertNotNull(zone.creationTimestamp()); + assertNotNull(zone.description()); + assertNotNull(zone.status()); + assertNotNull(zone.region()); + } + + @Test + public void testGetZoneWithSelectedFields() { + Zone zone = compute.getZone(ZONE, Compute.ZoneOption.fields(Compute.ZoneField.ID)); + assertEquals(ZONE, zone.zoneId().zone()); + assertNotNull(zone.id()); + assertNull(zone.creationTimestamp()); + assertNull(zone.description()); + assertNull(zone.status()); + assertNull(zone.maintenanceWindows()); + assertNull(zone.region()); + } + + @Test + public void testListZones() { + Page zonePage = compute.listZones(); + Iterator zoneIterator = zonePage.iterateAll(); + while(zoneIterator.hasNext()) { + Zone zone = zoneIterator.next(); + assertNotNull(zone.zoneId()); + assertNotNull(zone.id()); + assertNotNull(zone.creationTimestamp()); + assertNotNull(zone.description()); + assertNotNull(zone.status()); + assertNotNull(zone.region()); + } + } + + @Test + public void testListZonesWithSelectedFields() { + Page zonePage = compute.listZones( + Compute.ZoneListOption.fields(Compute.ZoneField.CREATION_TIMESTAMP)); + Iterator zoneIterator = zonePage.iterateAll(); + while(zoneIterator.hasNext()) { + Zone zone = zoneIterator.next(); + assertNotNull(zone.zoneId()); + assertNull(zone.id()); + assertNotNull(zone.creationTimestamp()); + assertNull(zone.description()); + assertNull(zone.status()); + assertNull(zone.region()); + } + } +}