Skip to content
This repository was archived by the owner on Jul 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions src/test/java/com/google/cloud/compute/v1/it/BaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2020 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 org.junit.Assert.fail;

import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.compute.v1.DiskTypeSettings;
import com.google.cloud.compute.v1.GlobalOperationClient;
import com.google.cloud.compute.v1.GlobalOperationSettings;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.ProjectGlobalOperationName;
import com.google.cloud.compute.v1.ProjectName;
import com.google.cloud.compute.v1.ProjectRegionName;
import com.google.cloud.compute.v1.ProjectRegionOperationName;
import com.google.cloud.compute.v1.ProjectZoneName;
import com.google.cloud.compute.v1.ProjectZoneOperationName;
import com.google.cloud.compute.v1.RegionOperationClient;
import com.google.cloud.compute.v1.RegionOperationSettings;
import com.google.cloud.compute.v1.ZoneOperationClient;
import com.google.cloud.compute.v1.ZoneOperationSettings;
import java.io.IOException;
import org.junit.AfterClass;
import org.junit.BeforeClass;

public class BaseTest {

protected static final String DEFAULT_PROJECT = ServiceOptions.getDefaultProjectId();
protected static final ProjectName PROJECT_NAME = ProjectName.of(DEFAULT_PROJECT);
protected static final String PROJECT_LINK =
String.format("https://www.googleapis.com/compute/v1/projects/%s", DEFAULT_PROJECT);
protected static final String ZONE = "us-central1-a";
protected static final String ZONE_SELF_LINK = String.format("%s/zones/%s", PROJECT_LINK, ZONE);

protected static final String REGION = "us-central1";
protected static final String REGION_LINK = String.format("%s/regions/%s", PROJECT_LINK, REGION);
protected static final ProjectRegionName PROJECT_REGION_NAME =
ProjectRegionName.of(DEFAULT_PROJECT, REGION);
protected static final String IP_PROTOCOL = "TCP";
protected static final ProjectZoneName PROJECT_ZONE_NAME =
ProjectZoneName.of(DEFAULT_PROJECT, ZONE);

private static GlobalOperationClient globalOperationClient;
private static RegionOperationClient regionOperationClient;
private static ZoneOperationClient zoneOperationClient;
protected static CredentialsProvider credentialsProvider;

@BeforeClass
public static void setUpOperationClients() throws IOException {
Credentials credentials =
GoogleCredentials.getApplicationDefault()
.createScoped(DiskTypeSettings.getDefaultServiceScopes());
credentialsProvider = FixedCredentialsProvider.create(credentials);

GlobalOperationSettings globalOperationSettings =
GlobalOperationSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
globalOperationClient = GlobalOperationClient.create(globalOperationSettings);

RegionOperationSettings regionOperationSettings =
RegionOperationSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
regionOperationClient = RegionOperationClient.create(regionOperationSettings);

ZoneOperationSettings zoneOperationSettings =
ZoneOperationSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
zoneOperationClient = ZoneOperationClient.create(zoneOperationSettings);
}

@AfterClass
public static void tearDownOperationClients() {
globalOperationClient.close();
regionOperationClient.close();
zoneOperationClient.close();
}

protected static Operation waitForOperation(Operation operation) {
Operation completedOperation;
if (operation.getZone() != null) {
completedOperation =
zoneOperationClient.waitZoneOperation(
ProjectZoneOperationName.of(operation.getName(), DEFAULT_PROJECT, ZONE));
} else if (operation.getRegion() != null) {
completedOperation =
regionOperationClient.waitRegionOperation(
ProjectRegionOperationName.of(operation.getName(), DEFAULT_PROJECT, REGION));
} else {
completedOperation =
globalOperationClient.waitGlobalOperation(
ProjectGlobalOperationName.of(operation.getName(), DEFAULT_PROJECT));
}
if (completedOperation.getError() != null) {
fail("Operation failed: " + completedOperation.getError().toString());
}
return completedOperation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2020 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.cloud.compute.v1.AcceleratorType;
import com.google.cloud.compute.v1.AcceleratorTypeClient;
import com.google.cloud.compute.v1.AcceleratorTypeSettings;
import com.google.cloud.compute.v1.AcceleratorTypesScopedList;
import com.google.cloud.compute.v1.ProjectZoneName;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;

public class ITAcceleratorTypeTest extends BaseTest {

private static AcceleratorTypeClient acceleratorTypeClient;

private static final ProjectZoneName PROJECT_ZONE_NAME =
ProjectZoneName.of(DEFAULT_PROJECT, ZONE);

@BeforeClass
public static void setUp() throws IOException {
AcceleratorTypeSettings acceleratorTypeSettings =
AcceleratorTypeSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
acceleratorTypeClient = AcceleratorTypeClient.create(acceleratorTypeSettings);
}

public static void tearDown() {
acceleratorTypeClient.close();
}

@Test
public void listAcceleratorTypesTest() {
List<AcceleratorType> acceleratorTypes =
Lists.newArrayList(
acceleratorTypeClient.listAcceleratorTypes(PROJECT_ZONE_NAME).iterateAll());
assertThat(acceleratorTypes).isNotNull();
assertThat(acceleratorTypes.size()).isGreaterThan(0);
assertThat(acceleratorTypes.contains(null)).isFalse();
}

@Test
public void aggregatedListAcceleratorTypesTest() {
List<AcceleratorTypesScopedList> typesScopedLists =
Lists.newArrayList(
acceleratorTypeClient.aggregatedListAcceleratorTypes(true, PROJECT_NAME).iterateAll());
assertThat(typesScopedLists).isNotNull();
assertThat(typesScopedLists.size()).isGreaterThan(0);
assertThat(typesScopedLists.contains(null)).isFalse();
}
}
159 changes: 159 additions & 0 deletions src/test/java/com/google/cloud/compute/v1/it/ITAddressTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright 2020 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.cloud.compute.v1.Address;
import com.google.cloud.compute.v1.AddressClient;
import com.google.cloud.compute.v1.AddressSettings;
import com.google.cloud.compute.v1.AddressesScopedList;
import com.google.cloud.compute.v1.GlobalAddressClient;
import com.google.cloud.compute.v1.GlobalAddressSettings;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.ProjectRegionAddressName;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class ITAddressTest extends BaseTest {

private static AddressClient addressClient;
private static GlobalAddressClient globalAddressClient;

private static final String ADDRESS_NAME = TestHelper.getTestUniqueName("address");
private static final String NETWORK_TIER = "PREMIUM";
private static final Address ADDRESS =
Address.newBuilder()
.setName(ADDRESS_NAME)
.setNetworkTier(NETWORK_TIER)
.setRegion(REGION_LINK)
.build();
private static final String ADDRESS_TYPE = "EXTERNAL";
private static final String ADDRESS_OPERATION_STATUS = "RESERVED";
private static final String ADDRESS_SELF_LINK =
String.format("%s/addresses/%s", REGION_LINK, ADDRESS_NAME);
private static final String GLOBAL_ADDRESS_LINK =
String.format("%s/global/addresses/%s", PROJECT_LINK, ADDRESS_NAME);

private static ListMultimap<String, String> resourcesToCleanUp = ArrayListMultimap.create();

@BeforeClass
public static void setUp() throws IOException {
AddressSettings addressSettings =
AddressSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
addressClient = AddressClient.create(addressSettings);

Operation completedOperation =
waitForOperation(addressClient.insertAddress(PROJECT_REGION_NAME, ADDRESS));
resourcesToCleanUp.put("address", completedOperation.getTargetLink());

GlobalAddressSettings globalAddressSettings =
GlobalAddressSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
globalAddressClient = GlobalAddressClient.create(globalAddressSettings);
Address address =
Address.newBuilder()
.setName(ADDRESS_NAME)
.setAddressType(ADDRESS_TYPE)
.setRegion(PROJECT_REGION_NAME.toString())
.build();
completedOperation =
waitForOperation(globalAddressClient.insertGlobalAddress(PROJECT_NAME, address));
resourcesToCleanUp.put("global-address", completedOperation.getTargetLink());
}

@AfterClass
public static void tearDown() {
for (String address : resourcesToCleanUp.get("address")) {
waitForOperation(addressClient.deleteAddress(address));
}
for (String address : resourcesToCleanUp.get("global-address")) {
waitForOperation(globalAddressClient.deleteGlobalAddress(address));
}
globalAddressClient.close();
addressClient.close();
}

@Test
public void getAddressesTest() {
ProjectRegionAddressName projectRegionAddressName =
ProjectRegionAddressName.of(ADDRESS_NAME, DEFAULT_PROJECT, REGION);
Address address = addressClient.getAddress(projectRegionAddressName);
assertThat(address).isNotNull();
assertThat(address.getAddressType()).isEqualTo(ADDRESS_TYPE);
assertThat(address.getName()).isEqualTo(ADDRESS_NAME);
assertThat(address.getNetworkTier()).isEqualTo(NETWORK_TIER);
assertThat(address.getRegion()).isEqualTo(REGION_LINK);
assertThat(address.getStatus()).isEqualTo(ADDRESS_OPERATION_STATUS);
assertThat(address.getSelfLink()).isEqualTo(ADDRESS_SELF_LINK);
}

@Test
public void listAddressesTest() {
List<Address> addresses =
Lists.newArrayList(addressClient.listAddresses(PROJECT_REGION_NAME).iterateAll());
assertThat(addresses.size()).isGreaterThan(0);
for (Address address : addresses) {
if (ADDRESS_NAME.equals(address.getName())) {
assertThat(address.getAddressType()).isEqualTo(ADDRESS_TYPE);
assertThat(address.getName()).isEqualTo(ADDRESS_NAME);
assertThat(address.getNetworkTier()).isEqualTo(NETWORK_TIER);
assertThat(address.getRegion()).isEqualTo(REGION_LINK);
assertThat(address.getStatus()).isEqualTo(ADDRESS_OPERATION_STATUS);
assertThat(address.getSelfLink()).isEqualTo(ADDRESS_SELF_LINK);
}
}
}

@Test
public void aggregatedListAddressesTest() {
List<AddressesScopedList> addressesScopedLists =
Lists.newArrayList(addressClient.aggregatedListAddresses(true, PROJECT_NAME).iterateAll());
for (AddressesScopedList addressesScopedList : addressesScopedLists) {
List<Address> addresses = addressesScopedList.getAddressesList();
if (addresses != null) {
for (Address address : addresses) {
if (ADDRESS_NAME.equals(address.getName())) {
assertThat(address.getAddressType()).isEqualTo(ADDRESS_TYPE);
assertThat(address.getName()).isEqualTo(ADDRESS_NAME);
assertThat(address.getNetworkTier()).isEqualTo(NETWORK_TIER);
assertThat(address.getStatus()).isEqualTo(ADDRESS_OPERATION_STATUS);
}
}
}
}
}

@Test
public void listGlobalAddressesTest() {
List<Address> addresses =
Lists.newArrayList(globalAddressClient.listGlobalAddresses(PROJECT_NAME).iterateAll());
for (Address address : addresses) {
if (ADDRESS_NAME.equals(address.getName())) {
assertThat(address.getAddressType()).isEqualTo(ADDRESS_TYPE);
assertThat(address.getName()).isEqualTo(ADDRESS_NAME);
assertThat(address.getNetworkTier()).isEqualTo(NETWORK_TIER);
assertThat(address.getStatus()).isEqualTo(ADDRESS_OPERATION_STATUS);
assertThat(address.getSelfLink()).isEqualTo(GLOBAL_ADDRESS_LINK);
}
}
}
}
Loading