|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package compute.reservation; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 21 | + |
| 22 | +import com.google.api.gax.rpc.NotFoundException; |
| 23 | +import com.google.cloud.compute.v1.Instance; |
| 24 | +import com.google.cloud.compute.v1.InstancesClient; |
| 25 | +import com.google.cloud.compute.v1.Reservation; |
| 26 | +import com.google.cloud.compute.v1.ReservationsClient; |
| 27 | +import compute.CreateInstance; |
| 28 | +import compute.DeleteInstance; |
| 29 | +import compute.Util; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.UUID; |
| 32 | +import java.util.concurrent.ExecutionException; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import java.util.concurrent.TimeoutException; |
| 35 | +import org.junit.jupiter.api.AfterAll; |
| 36 | +import org.junit.jupiter.api.Assertions; |
| 37 | +import org.junit.jupiter.api.BeforeAll; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.junit.jupiter.api.Timeout; |
| 40 | +import org.junit.runner.RunWith; |
| 41 | +import org.junit.runners.JUnit4; |
| 42 | + |
| 43 | +@RunWith(JUnit4.class) |
| 44 | +@Timeout(value = 3, unit = TimeUnit.MINUTES) |
| 45 | +public class CreateReservationFromVmIT { |
| 46 | + |
| 47 | + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 48 | + private static final String ZONE = "us-east4-c"; |
| 49 | + private static ReservationsClient reservationsClient; |
| 50 | + private static InstancesClient instancesClient; |
| 51 | + private static String reservationName; |
| 52 | + private static String instanceForReservation; |
| 53 | + static String javaVersion = System.getProperty("java.version").substring(0, 2); |
| 54 | + |
| 55 | + // Check if the required environment variables are set. |
| 56 | + public static void requireEnvVar(String envVarName) { |
| 57 | + assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) |
| 58 | + .that(System.getenv(envVarName)).isNotEmpty(); |
| 59 | + } |
| 60 | + |
| 61 | + @BeforeAll |
| 62 | + public static void setUp() |
| 63 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 64 | + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); |
| 65 | + requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 66 | + reservationsClient = ReservationsClient.create(); |
| 67 | + instancesClient = InstancesClient.create(); |
| 68 | + |
| 69 | + reservationName = "test-reservation-from-vm-" + javaVersion + "-" |
| 70 | + + UUID.randomUUID().toString().substring(0, 8); |
| 71 | + instanceForReservation = "test-instance-for-reserv-" + javaVersion + "-" |
| 72 | + + UUID.randomUUID().toString().substring(0, 8); |
| 73 | + |
| 74 | + // Cleanup existing stale resources. |
| 75 | + Util.cleanUpExistingInstances("test-instance-for-reserv-" + javaVersion, PROJECT_ID, ZONE); |
| 76 | + Util.cleanUpExistingReservations("test-reservation-from-vm-" + javaVersion, PROJECT_ID, ZONE); |
| 77 | + |
| 78 | + CreateInstance.createInstance(PROJECT_ID, ZONE, instanceForReservation); |
| 79 | + } |
| 80 | + |
| 81 | + @AfterAll |
| 82 | + public static void cleanup() |
| 83 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 84 | + // Delete resources created for testing. |
| 85 | + DeleteInstance.deleteInstance(PROJECT_ID, ZONE, instanceForReservation); |
| 86 | + |
| 87 | + reservationsClient.close(); |
| 88 | + instancesClient.close(); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testCreateComputeReservationFromVm() |
| 93 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 94 | + CreateReservationFromVm.createComputeReservationFromVm( |
| 95 | + PROJECT_ID, ZONE, reservationName, instanceForReservation); |
| 96 | + |
| 97 | + Instance instance = instancesClient.get(PROJECT_ID, ZONE, instanceForReservation); |
| 98 | + Reservation reservation = |
| 99 | + reservationsClient.get(PROJECT_ID, ZONE, reservationName); |
| 100 | + |
| 101 | + Assertions.assertNotNull(reservation); |
| 102 | + assertThat(reservation.getName()).isEqualTo(reservationName); |
| 103 | + Assertions.assertEquals(instance.getMinCpuPlatform(), |
| 104 | + reservation.getSpecificReservation().getInstanceProperties().getMinCpuPlatform()); |
| 105 | + Assertions.assertEquals(instance.getGuestAcceleratorsList(), |
| 106 | + reservation.getSpecificReservation().getInstanceProperties().getGuestAcceleratorsList()); |
| 107 | + |
| 108 | + DeleteReservation.deleteReservation(PROJECT_ID, ZONE, reservationName); |
| 109 | + |
| 110 | + // Test that reservation is deleted |
| 111 | + Assertions.assertThrows( |
| 112 | + NotFoundException.class, |
| 113 | + () -> GetReservation.getReservation(PROJECT_ID, reservationName, ZONE)); |
| 114 | + } |
| 115 | +} |
0 commit comments