Skip to content

Commit

Permalink
Add handwritten integration test for Compute GAPIC (googleapis#3660)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamlin authored Sep 11, 2018
1 parent 0e58b7d commit a2f7111
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
5 changes: 5 additions & 0 deletions google-cloud-clients/google-cloud-compute/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,10 @@
<classifier>testlib</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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<DiskType> diskPage = diskTypeClient.listDiskTypes(ProjectZoneName.of(DEFAULT_PROJECT, ZONE)).getPage();
Iterator<DiskType> 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<DiskType> diskPage = diskTypeClient.listDiskTypes(request).getPage();
Iterator<DiskType> 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<DiskTypesScopedList> diskTypeScopedListIterator = Lists.newArrayList(pagedListResponse.iterateAll());
List<DiskType> 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/", "");
}
}

0 comments on commit a2f7111

Please sign in to comment.