From a2f7111ce1d5197047efd5dc276d8d9325d9c9fa Mon Sep 17 00:00:00 2001 From: Andrea Lin Date: Tue, 11 Sep 2018 10:39:59 -0700 Subject: [PATCH] Add handwritten integration test for Compute GAPIC (#3660) --- .../google-cloud-compute/pom.xml | 5 + .../cloud/compute/v1/it/ITComputeTest.java | 149 ++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java diff --git a/google-cloud-clients/google-cloud-compute/pom.xml b/google-cloud-clients/google-cloud-compute/pom.xml index cf6a49bbb945..b7bbeb282127 100644 --- a/google-cloud-clients/google-cloud-compute/pom.xml +++ b/google-cloud-clients/google-cloud-compute/pom.xml @@ -70,5 +70,10 @@ testlib test + + com.google.truth + truth + test + diff --git a/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java new file mode 100644 index 000000000000..e5da6b092c1f --- /dev/null +++ b/google-cloud-clients/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/it/ITComputeTest.java @@ -0,0 +1,149 @@ +/* + * Copyright 2018 Google LLC + * + * 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 + * + * https://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.cloud.compute.v1.it; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.api.gax.core.FixedCredentialsProvider; +import com.google.api.gax.paging.Page; +import com.google.auth.Credentials; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.cloud.ServiceOptions; +import com.google.cloud.compute.v1.DiskType; +import com.google.cloud.compute.v1.DiskTypeClient; +import com.google.cloud.compute.v1.DiskTypeClient.AggregatedListDiskTypesPagedResponse; +import com.google.cloud.compute.v1.DiskTypeSettings; +import com.google.cloud.compute.v1.DiskTypesScopedList; +import com.google.cloud.compute.v1.ListDiskTypesHttpRequest; +import com.google.cloud.compute.v1.ProjectName; +import com.google.cloud.compute.v1.ProjectZoneDiskTypeName; +import com.google.cloud.compute.v1.ProjectZoneName; +import com.google.common.collect.Lists; +import java.io.IOException; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; + +public class ITComputeTest { + + private static final String ZONE = "us-central1-a"; + private static final String DISK_TYPE = "local-ssd"; + private static final String DEFAULT_PROJECT = ServiceOptions.getDefaultProjectId(); + + private static DiskTypeClient diskTypeClient; + private static DiskTypeSettings diskTypeSettings; + + + @Rule + public Timeout globalTimeout = Timeout.seconds(300); + + @BeforeClass + public static void beforeClass() throws IOException{ + Credentials credentials = GoogleCredentials.getApplicationDefault(); + + diskTypeSettings = + DiskTypeSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build(); + diskTypeClient = + DiskTypeClient.create(diskTypeSettings); + } + + @AfterClass + public static void tearDown() throws Exception { + diskTypeClient.close(); + } + + @Test + public void testGetDiskType() { + DiskType diskType = diskTypeClient.getDiskType(ProjectZoneDiskTypeName + .of(DISK_TYPE, DEFAULT_PROJECT, ZONE)); + ProjectZoneDiskTypeName returnDiskName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink())); + assertThat(returnDiskName.getZone()).isEqualTo(ZONE); + assertThat(returnDiskName.getDiskType()).isEqualTo(DISK_TYPE); + assertThat(diskType.getCreationTimestamp()).isNotNull(); + assertThat(diskType.getDescription()).isNotNull(); + assertThat(diskType.getValidDiskSize()).isNotNull(); + assertThat(diskType.getDefaultDiskSizeGb()).isNotNull(); + } + + @Test + public void testListDiskTypes() { + Page diskPage = diskTypeClient.listDiskTypes(ProjectZoneName.of(DEFAULT_PROJECT, ZONE)).getPage(); + Iterator diskTypeIterator = diskPage.iterateAll().iterator(); + assertThat(diskTypeIterator.hasNext()).isTrue(); + while (diskTypeIterator.hasNext()) { + DiskType diskType = diskTypeIterator.next(); + assertThat(diskType.getSelfLink()).isNotNull(); + ProjectZoneDiskTypeName returnDiskName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink())); + assertThat(returnDiskName.getZone()).isEqualTo(ZONE); + assertThat(diskType.getCreationTimestamp()).isNotNull(); + assertThat(diskType.getDescription()).isNotNull(); + assertThat(diskType.getValidDiskSize()).isNotNull(); + assertThat(diskType.getDefaultDiskSizeGb()).isNotNull(); + } + } + + @Test + public void testListDiskTypesWithFilter() { + ListDiskTypesHttpRequest request = ListDiskTypesHttpRequest.newBuilder() + .setZone(ProjectZoneName.of(DEFAULT_PROJECT, ZONE).toString()) + .setFilter("(defaultDiskSizeGb = 375)") + .build(); + Page diskPage = diskTypeClient.listDiskTypes(request).getPage(); + Iterator diskTypeIterator = diskPage.iterateAll().iterator(); + assertThat(diskTypeIterator.hasNext()).isTrue(); + while (diskTypeIterator.hasNext()) { + DiskType diskType = diskTypeIterator.next(); + assertThat(diskType.getZone()).isNotNull(); + ProjectZoneName zoneName = ProjectZoneName.parse(trimUrl(diskType.getZone())); + assertThat(zoneName.getZone()).isEqualTo(ZONE); + assertThat(diskType.getCreationTimestamp()).isNotNull(); + assertThat(diskType.getDescription()).isNotNull(); + assertThat(diskType.getValidDiskSize()).isNotNull(); + assertThat(diskType.getDefaultDiskSizeGb()).isNotNull(); + } + } + + @Test + public void testAggregatedListDiskTypes() { + AggregatedListDiskTypesPagedResponse pagedListResponse = diskTypeClient.aggregatedListDiskTypes(ProjectName.of(DEFAULT_PROJECT)); + List diskTypeScopedListIterator = Lists.newArrayList(pagedListResponse.iterateAll()); + List diskTypeIterator = new LinkedList<>(); + for (DiskTypesScopedList scopedList : diskTypeScopedListIterator) { + diskTypeIterator.addAll(scopedList.getDiskTypesList()); + } + assertThat(diskTypeIterator.size()).isGreaterThan(0); + for (DiskType diskType : diskTypeIterator) { + assertThat(diskType.getZone()).isNotNull(); + ProjectZoneDiskTypeName zoneName = ProjectZoneDiskTypeName.parse(trimUrl(diskType.getSelfLink())); + assertThat(zoneName.getDiskType()).isNotNull(); + assertThat(zoneName.getZone()).isNotNull(); + assertThat(diskType.getCreationTimestamp()).isNotNull(); + assertThat(diskType.getDescription()).isNotNull(); + assertThat(diskType.getValidDiskSize()).isNotNull(); + assertThat(diskType.getDefaultDiskSizeGb()).isNotNull(); + } + } + + /** For a given resource's URI, trim the path until it contains only the PathTemplate string. */ + private static String trimUrl(String url) { + return url.replaceFirst("^https://www.googleapis.com/compute/v1/", ""); + } +}