From 9044efd0a6e161b4ab0af6f7d93af2ce474baa92 Mon Sep 17 00:00:00 2001 From: Burke Davison <40617934+burkedavison@users.noreply.github.com> Date: Tue, 30 Jul 2024 14:47:46 -0400 Subject: [PATCH] ci: reduce compute integration tests (#11038) --- java-compute/google-cloud-compute/pom.xml | 12 +- .../v1/integration/ITAddressesTest.java | 125 ------- .../compute/v1/integration/ITHeadersTest.java | 90 ----- .../v1/integration/ITSmokeInstancesTest.java | 332 ------------------ 4 files changed, 3 insertions(+), 556 deletions(-) delete mode 100644 java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITAddressesTest.java delete mode 100644 java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITHeadersTest.java delete mode 100644 java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITSmokeInstancesTest.java diff --git a/java-compute/google-cloud-compute/pom.xml b/java-compute/google-cloud-compute/pom.xml index 36036c9940b5..b969b9f74e9f 100644 --- a/java-compute/google-cloud-compute/pom.xml +++ b/java-compute/google-cloud-compute/pom.xml @@ -1,5 +1,7 @@ - + 4.0.0 com.google.cloud google-cloud-compute @@ -77,12 +79,4 @@ test - - - native - - com.google.cloud.compute.v1.integration.ITAddressesTest - - - diff --git a/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITAddressesTest.java b/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITAddressesTest.java deleted file mode 100644 index 9eb0c494bf83..000000000000 --- a/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITAddressesTest.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright 2021 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.integration; - -import com.google.cloud.compute.v1.Address; -import com.google.cloud.compute.v1.AddressesClient; -import com.google.cloud.compute.v1.AddressesScopedList; -import com.google.cloud.compute.v1.AddressesSettings; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -public class ITAddressesTest extends BaseTest { - - private static List
addresses; - private static AddressesClient addressesClient; - private static String name; - - @BeforeClass - public static void setUp() throws IOException { - addresses = new ArrayList<>(); - AddressesSettings addressesSettings = AddressesSettings.newBuilder().build(); - addressesClient = AddressesClient.create(addressesSettings); - Util.cleanUpComputeAddresses(addressesClient, DEFAULT_PROJECT, DEFAULT_REGION, COMPUTE_PREFIX); - } - - @Before - public void setUpMethod() { - name = generateRandomName("address"); - } - - @AfterClass - public static void tearDown() throws ExecutionException, InterruptedException { - for (Address address : addresses) { - addressesClient.deleteAsync(DEFAULT_PROJECT, DEFAULT_REGION, address.getName()); - } - addressesClient.close(); - } - - @Test - public void testCRD() { - insertAddress(); - Address address = addressesClient.get(DEFAULT_PROJECT, DEFAULT_REGION, name); - Assert.assertEquals(name, address.getName()); - Assert.assertEquals("test", address.getDescription()); - } - - @Test - public void testList() { - insertAddress(); - AddressesClient.ListPagedResponse response = - addressesClient.list(DEFAULT_PROJECT, DEFAULT_REGION); - boolean presented = false; - for (Address element : response.iterateAll()) { - if (element.getName().equals(name)) { - presented = true; - } - } - Assert.assertTrue(presented); - } - - @Test - public void testAggregatedList() { - insertAddress(); - AddressesClient.AggregatedListPagedResponse response = - addressesClient.aggregatedList(DEFAULT_PROJECT); - boolean presented = false; - for (Map.Entry entry : response.iterateAll()) { - if (entry.getKey().equals("regions/" + DEFAULT_REGION)) { - for (Address elem : entry.getValue().getAddressesList()) { - if (elem.getName().equals(name)) { - presented = true; - } - } - } - } - Assert.assertTrue(presented); - } - - @Test - public void testNonAscii() { - insertAddress("тест"); - Address address = addressesClient.get(DEFAULT_PROJECT, DEFAULT_REGION, name); - Assert.assertEquals(name, address.getName()); - Assert.assertEquals("тест", address.getDescription()); - } - - private void insertAddress() { - insertAddress("test"); - } - - private void insertAddress(String description) { - Address address = Address.newBuilder().setName(name).setDescription(description).build(); - try { - addressesClient - .insertAsync(DEFAULT_PROJECT, DEFAULT_REGION, address) - .get(60, TimeUnit.SECONDS); - } catch (InterruptedException | ExecutionException | TimeoutException e) { - throw new RuntimeException("Insert operation failed.", e); - } - addresses.add(address); - } -} diff --git a/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITHeadersTest.java b/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITHeadersTest.java deleted file mode 100644 index 07e7ce8618d6..000000000000 --- a/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITHeadersTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2021 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.integration; - -import com.google.api.gax.longrunning.OperationFuture; -import com.google.cloud.compute.v1.Address; -import com.google.cloud.compute.v1.AddressesClient; -import com.google.cloud.compute.v1.AddressesSettings; -import com.google.cloud.compute.v1.Operation; -import com.sun.net.httpserver.Headers; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import com.sun.net.httpserver.HttpServer; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -public class ITHeadersTest { - - private static HttpServer server; - private static AddressesClient addressesClient; - private static Headers headers; - - @BeforeClass - public static void setUp() throws IOException { - server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); - server.createContext("/", new RequestHandler()); - server.setExecutor(null); - server.start(); - String address = server.getAddress().toString().replace("/", "http://"); - AddressesSettings addressesSettings = - AddressesSettings.newBuilder().setEndpoint(address).build(); - addressesClient = AddressesClient.create(addressesSettings); - } - - static class RequestHandler implements HttpHandler { - @Override - public void handle(HttpExchange t) throws IOException { - if (t.getRequestMethod() - .equals("POST")) { // we are only interested in insert() headers, skip GET() LRO - headers = t.getRequestHeaders(); - } - byte[] response = "{\"id\":2000}".getBytes(); - t.sendResponseHeaders(200, response.length); - t.getResponseBody().write(response); - t.close(); - } - } - - @AfterClass - public static void tearDown() { - server.stop(5); - } - - @Test - public void testHeaders() { - OperationFuture future = - addressesClient.insertAsync( - "testProject", "testRegion", Address.newBuilder().setName("testName").build()); - try { - future.get(5, TimeUnit.SECONDS); - } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); - } catch (TimeoutException e) {; // expected - } - future.cancel(true); - - Assert.assertTrue(headers.get("X-goog-api-client").get(0).contains("rest/")); - Assert.assertTrue(headers.get("Content-type").get(0).contains("application/json")); - } -} diff --git a/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITSmokeInstancesTest.java b/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITSmokeInstancesTest.java deleted file mode 100644 index 0ca38af926b4..000000000000 --- a/java-compute/google-cloud-compute/src/test/java/com/google/cloud/compute/v1/integration/ITSmokeInstancesTest.java +++ /dev/null @@ -1,332 +0,0 @@ -/* - * Copyright 2021 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.integration; - -import static junit.framework.TestCase.fail; - -import com.google.api.gax.rpc.NotFoundException; -import com.google.cloud.compute.v1.Allowed; -import com.google.cloud.compute.v1.AttachedDisk; -import com.google.cloud.compute.v1.AttachedDiskInitializeParams; -import com.google.cloud.compute.v1.Firewall; -import com.google.cloud.compute.v1.FirewallsClient; -import com.google.cloud.compute.v1.FirewallsSettings; -import com.google.cloud.compute.v1.GetInstanceRequest; -import com.google.cloud.compute.v1.Instance; -import com.google.cloud.compute.v1.InstanceGroupManager; -import com.google.cloud.compute.v1.InstanceGroupManagersClient; -import com.google.cloud.compute.v1.InstanceTemplate; -import com.google.cloud.compute.v1.InstanceTemplatesClient; -import com.google.cloud.compute.v1.InstancesClient; -import com.google.cloud.compute.v1.InstancesScopedList; -import com.google.cloud.compute.v1.NetworkInterface; -import com.google.cloud.compute.v1.Operation; -import com.google.cloud.compute.v1.ShieldedInstanceConfig; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -public class ITSmokeInstancesTest extends BaseTest { - private static final int DEFAULT_AWAIT_TERMINATION_DURATION = 10; - private static InstancesClient instancesClient; - private static FirewallsClient firewallsClient; - private static InstanceTemplatesClient instanceTemplatesClient; - private static InstanceGroupManagersClient instanceGroupManagersClient; - private static List instances; - private static final String DEFAULT_IMAGE = - "projects/debian-cloud/global/images/family/debian-12"; - private static final AttachedDisk DISK = - AttachedDisk.newBuilder() - .setBoot(true) - .setAutoDelete(true) - .setType(AttachedDisk.Type.PERSISTENT.toString()) - .setInitializeParams( - AttachedDiskInitializeParams.newBuilder().setSourceImage(DEFAULT_IMAGE).build()) - .build(); - private static final String MACHINE_TYPE = - "https://www.googleapis.com/compute/v1/projects/" - + DEFAULT_PROJECT - + "/zones/us-central1-a/machineTypes/n1-standard-1"; - private static final NetworkInterface NETWORK_INTERFACE = - NetworkInterface.newBuilder().setName("default").build(); - private static String INSTANCE; - - @BeforeClass - public static void setUp() throws IOException { - instances = new ArrayList<>(); - instancesClient = InstancesClient.create(); - Util.cleanUpComputeInstances(instancesClient, DEFAULT_PROJECT, DEFAULT_ZONE, COMPUTE_PREFIX); - - FirewallsSettings.Builder firewallsSettingsBuilder = FirewallsSettings.newBuilder(); - firewallsSettingsBuilder - .getSettings() - .setRetrySettings( - firewallsSettingsBuilder - .getSettings() - .getRetrySettings() - .toBuilder() - .setMaxAttempts(5) - .setInitialRetryDelay(org.threeten.bp.Duration.ofSeconds(5)) - .setRetryDelayMultiplier(1.0) - .build()); - FirewallsSettings firewallsSettings = firewallsSettingsBuilder.build(); - firewallsClient = FirewallsClient.create(firewallsSettings); - Util.cleanUpFirewalls(firewallsClient, DEFAULT_PROJECT, COMPUTE_PREFIX); - - instanceTemplatesClient = InstanceTemplatesClient.create(); - Util.cleanUpInstanceTemplates(instanceTemplatesClient, DEFAULT_PROJECT, COMPUTE_PREFIX); - - instanceGroupManagersClient = InstanceGroupManagersClient.create(); - Util.cleanUpGroupManagers( - instanceGroupManagersClient, DEFAULT_PROJECT, DEFAULT_ZONE, COMPUTE_PREFIX); - } - - @Before - public void setUpMethod() { - INSTANCE = generateRandomName("instance"); - } - - @AfterClass - public static void tearDown() throws ExecutionException, InterruptedException { - for (Instance instance : instances) { - instancesClient.deleteAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instance.getName()); - } - instancesClient.close(); - firewallsClient.close(); - instanceTemplatesClient.close(); - instanceGroupManagersClient.close(); - - instancesClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_DURATION, TimeUnit.SECONDS); - firewallsClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_DURATION, TimeUnit.SECONDS); - instanceTemplatesClient.awaitTermination(DEFAULT_AWAIT_TERMINATION_DURATION, TimeUnit.SECONDS); - instanceGroupManagersClient.awaitTermination( - DEFAULT_AWAIT_TERMINATION_DURATION, TimeUnit.SECONDS); - } - - @Test - public void testInsertInstance() throws ExecutionException, InterruptedException { - Instance resultInstance = insertInstance(); - assertInstanceDetails(resultInstance); - } - - @Test - public void testUpdateInstanceDescToEmpty() throws ExecutionException, InterruptedException { - // We test here: 1)set body field to an empty string - // 2)unset body field - Instance resultInstance = insertInstance(); - Assert.assertEquals("test", resultInstance.getDescription()); - Assert.assertEquals(0, resultInstance.getScheduling().getMinNodeCpus()); - Instance descInstance = resultInstance.toBuilder().setDescription("").build(); - instancesClient.updateAsync(DEFAULT_PROJECT, DEFAULT_ZONE, INSTANCE, descInstance).get(); - Instance updated = getInstance(); - assertInstanceDetails(updated); - Assert.assertEquals("", updated.getDescription()); - Assert.assertEquals(0, resultInstance.getScheduling().getMinNodeCpus()); - } - - @Test - public void testResizeGroupToZero() throws ExecutionException, InterruptedException { - // We test here: 1)set body field to zero - // 2)set query param to zero - List instanceGroupManagersToClean = new ArrayList<>(); - List instanceTemplatesToClean = new ArrayList<>(); - String templateName = generateRandomName("template"); - String instanceGroupManagerName = generateRandomName("igm"); - Instance instance = insertInstance(); - InstanceTemplate instanceTemplate = - InstanceTemplate.newBuilder() - .setSourceInstance(instance.getSelfLink()) - .setName(templateName) - .build(); - Operation insertOperation = - instanceTemplatesClient.insertAsync(DEFAULT_PROJECT, instanceTemplate).get(); - instanceTemplatesToClean.add(templateName); - try { - InstanceGroupManager instanceGroupManager = - InstanceGroupManager.newBuilder() - .setName(instanceGroupManagerName) - .setBaseInstanceName("java-gapic") - .setInstanceTemplate(insertOperation.getTargetLink()) - .setTargetSize(0) - .build(); - instanceGroupManagersClient - .insertAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManager) - .get(); - instanceGroupManagersToClean.add(instanceGroupManagerName); - InstanceGroupManager fetched = - instanceGroupManagersClient.get(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName); - Assert.assertEquals(0, fetched.getTargetSize()); - - instanceGroupManagersClient - .resizeAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName, 1) - .get(); - - InstanceGroupManager resizedIGM = - instanceGroupManagersClient.get(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName); - Assert.assertEquals(1, resizedIGM.getTargetSize()); - - instanceGroupManagersClient - .resizeAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName, 0) - .get(); - - InstanceGroupManager instanceGroupManagerResized = - instanceGroupManagersClient.get(DEFAULT_PROJECT, DEFAULT_ZONE, instanceGroupManagerName); - Assert.assertEquals(0, instanceGroupManagerResized.getTargetSize()); - - } finally { - for (String name : instanceGroupManagersToClean) { - instanceGroupManagersClient.deleteAsync(DEFAULT_PROJECT, DEFAULT_ZONE, name); - } - for (String name : instanceTemplatesToClean) { - instanceTemplatesClient.deleteAsync(DEFAULT_PROJECT, name); - } - } - } - - @Test - public void testAggregatedList() throws ExecutionException, InterruptedException { - insertInstance(); - boolean presented = false; - InstancesClient.AggregatedListPagedResponse response = - instancesClient.aggregatedList(DEFAULT_PROJECT); - for (Map.Entry entry : response.iterateAll()) { - if (entry.getKey().equals("zones/" + DEFAULT_ZONE)) { - for (Instance instance : entry.getValue().getInstancesList()) { - if (instance.getName().equals(INSTANCE)) { - presented = true; - assertInstanceDetails(instance); - } - } - } - } - Assert.assertTrue(presented); - } - - @Test - public void testDefaultClient() throws ExecutionException, InterruptedException { - Instance instanceResource = - Instance.newBuilder() - .setName(INSTANCE) - .setMachineType(MACHINE_TYPE) - .addDisks(DISK) - .addNetworkInterfaces(NETWORK_INTERFACE) - .build(); - instancesClient.insertAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceResource).get(); - instances.add(instanceResource); - assertInstanceDetails(getInstance()); - } - - @Test - public void testDefaultResource() throws InterruptedException { - Instance instanceResource = Instance.newBuilder().build(); - try { - instancesClient.insertAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceResource).get(); - fail("Did not catch the exception"); - } catch (ExecutionException ex) { - String message = "com.google.api.gax.rpc.InvalidArgumentException: Bad Request"; - Assert.assertEquals(message, ex.getMessage()); - } - } - - @Test - public void testApiError() { - try { - getInstance(); - fail("Did not catch the exception"); - } catch (NotFoundException ex) { - String message = "Not Found"; - Assert.assertEquals(message, ex.getMessage()); - } - } - - @Test - public void testCapitalLetterField() throws ExecutionException, InterruptedException { - // we want to test a field like "IPProtocol" - String name = generateRandomName("fw-rule"); - Firewall firewall = - Firewall.newBuilder() - .setName(name) - .addAllowed(Allowed.newBuilder().setIPProtocol("tcp").addPorts("80").build()) - .addSourceRanges("0.0.0.0/0") - .build(); - try { - firewallsClient.insertAsync(DEFAULT_PROJECT, firewall).get(); - Firewall fetched = firewallsClient.get(DEFAULT_PROJECT, name); - Assert.assertEquals(name, fetched.getName()); - Assert.assertEquals("tcp", fetched.getAllowed(0).getIPProtocol()); - } finally { - firewallsClient.deleteAsync(DEFAULT_PROJECT, name); - } - } - - @Test - public void testPatch() throws ExecutionException, InterruptedException { - Instance resultInstance = insertInstance(); - Assert.assertFalse(resultInstance.getShieldedInstanceConfig().getEnableSecureBoot()); - instancesClient.stopAsync(DEFAULT_PROJECT, DEFAULT_ZONE, INSTANCE).get(); - ShieldedInstanceConfig shieldedInstanceConfigResource = - ShieldedInstanceConfig.newBuilder().setEnableSecureBoot(true).build(); - instancesClient - .updateShieldedInstanceConfigAsync( - DEFAULT_PROJECT, DEFAULT_ZONE, INSTANCE, shieldedInstanceConfigResource) - .get(); - Instance updInstance = getInstance(); - Assert.assertTrue(updInstance.getShieldedInstanceConfig().getEnableSecureBoot()); - } - - private Instance insertInstance() throws ExecutionException, InterruptedException { - Instance instanceResource = - Instance.newBuilder() - .setName(INSTANCE) - .setMachineType(MACHINE_TYPE) - .addDisks(DISK) - .addNetworkInterfaces(NETWORK_INTERFACE) - .setDescription("test") - .build(); - instancesClient.insertAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceResource).get(); - instances.add(instanceResource); - return getInstance(); - } - - private Instance getInstance() { - GetInstanceRequest request = - GetInstanceRequest.newBuilder() - .setProject(DEFAULT_PROJECT) - .setZone(DEFAULT_ZONE) - .setInstance(INSTANCE) - .build(); - return instancesClient.get(request); - } - - private void assertInstanceDetails(Instance instance) { - Assert.assertNotNull(instance); - Assert.assertEquals(instance.getName(), INSTANCE); - Assert.assertNotNull(instance.getFingerprint()); - Assert.assertEquals(instance.getMachineType(), MACHINE_TYPE); - Assert.assertEquals(instance.getDisksCount(), 1); - Assert.assertEquals( - instance.getDisksList().get(0).getType(), AttachedDisk.Type.PERSISTENT.toString()); - Assert.assertEquals(instance.getNetworkInterfacesCount(), 1); - } -}