Skip to content

Commit e018e46

Browse files
Created new test class for CreateReservationFromVm
1 parent ef2b607 commit e018e46

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.TimeoutException;
34+
import org.junit.jupiter.api.AfterAll;
35+
import org.junit.jupiter.api.Assertions;
36+
import org.junit.jupiter.api.BeforeAll;
37+
import org.junit.jupiter.api.Test;
38+
39+
public class CreateReservationFromVmIT {
40+
41+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
42+
private static final String ZONE = "us-east4-c";
43+
private static ReservationsClient reservationsClient;
44+
private static InstancesClient instancesClient;
45+
private static String RESERVATION_NAME;
46+
private static String INSTANCE_FOR_RESERVATION;
47+
static String javaVersion = System.getProperty("java.version").substring(0, 2);
48+
49+
// Check if the required environment variables are set.
50+
public static void requireEnvVar(String envVarName) {
51+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
52+
.that(System.getenv(envVarName)).isNotEmpty();
53+
}
54+
55+
@BeforeAll
56+
public static void setUp()
57+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
58+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
59+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
60+
reservationsClient = ReservationsClient.create();
61+
instancesClient = InstancesClient.create();
62+
63+
RESERVATION_NAME = "test-reservation-from-vm-" + javaVersion + "-"
64+
+ UUID.randomUUID().toString().substring(0, 8);
65+
INSTANCE_FOR_RESERVATION = "test-instance-for-reserv-" + javaVersion + "-"
66+
+ UUID.randomUUID().toString().substring(0, 8);
67+
68+
// Cleanup existing stale resources.
69+
Util.cleanUpExistingInstances("test-instance-for-reserv-" + javaVersion, PROJECT_ID, ZONE);
70+
Util.cleanUpExistingReservations("test-reservation-from-vm-" + javaVersion, PROJECT_ID, ZONE);
71+
72+
CreateInstance.createInstance(PROJECT_ID, ZONE, INSTANCE_FOR_RESERVATION);
73+
}
74+
75+
@AfterAll
76+
public static void cleanup()
77+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
78+
// Delete resources created for testing.
79+
DeleteInstance.deleteInstance(PROJECT_ID, ZONE, INSTANCE_FOR_RESERVATION);
80+
DeleteReservation.deleteReservation(PROJECT_ID, ZONE, RESERVATION_NAME);
81+
82+
// Test that reservation is deleted
83+
Assertions.assertThrows(
84+
NotFoundException.class,
85+
() -> GetReservation.getReservation(PROJECT_ID, RESERVATION_NAME, ZONE));
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, RESERVATION_NAME, INSTANCE_FOR_RESERVATION);
96+
97+
Instance instance = instancesClient.get(PROJECT_ID, ZONE, INSTANCE_FOR_RESERVATION);
98+
Reservation reservation =
99+
reservationsClient.get(PROJECT_ID, ZONE, RESERVATION_NAME);
100+
101+
Assertions.assertNotNull(reservation);
102+
assertThat(reservation.getName()).isEqualTo(RESERVATION_NAME);
103+
Assertions.assertEquals(instance.getMinCpuPlatform(),
104+
reservation.getSpecificReservation().getInstanceProperties().getMinCpuPlatform());
105+
Assertions.assertEquals(instance.getGuestAcceleratorsList(),
106+
reservation.getSpecificReservation().getInstanceProperties().getGuestAcceleratorsList());
107+
}
108+
}

0 commit comments

Comments
 (0)