Skip to content

Commit bbbba28

Browse files
Fixed tests
1 parent 53a7993 commit bbbba28

File tree

4 files changed

+106
-54
lines changed

4 files changed

+106
-54
lines changed

compute/cloud-client/src/main/java/compute/reservation/ConsumeSpecificSharedReservation.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.SPECIFIC_RESERVATION;
2222

23-
import com.google.api.gax.longrunning.OperationFuture;
2423
import com.google.cloud.compute.v1.AllocationSpecificSKUAllocationReservedInstanceProperties;
2524
import com.google.cloud.compute.v1.AllocationSpecificSKUReservation;
2625
import com.google.cloud.compute.v1.AttachedDisk;
@@ -169,11 +168,9 @@ public static void createInstance(String projectId, String zone, String instance
169168
.setInstanceResource(instanceResource)
170169
.build();
171170

172-
OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(
173-
insertInstanceRequest);
174-
175171
// Wait for the operation to complete.
176-
Operation response = operation.get(3, TimeUnit.MINUTES);
172+
Operation response = instancesClient.insertAsync(
173+
insertInstanceRequest).get(3, TimeUnit.MINUTES);
177174

178175
if (response.hasError()) {
179176
System.out.println("Instance creation failed ! ! " + response);

compute/cloud-client/src/test/java/compute/Util.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,11 @@ public static void cleanUpExistingRegionalInstanceTemplates(
9090

9191
for (InstanceTemplate instanceTemplate :
9292
instanceTemplatesClient.list(request).iterateAll()) {
93-
if (!instanceTemplate.hasCreationTimestamp() || !instanceTemplate.hasId()) {
94-
continue;
95-
}
9693
if (containPrefixToDeleteAndZone(instanceTemplate, prefixToDelete, zone)
9794
&& isCreatedBeforeThresholdTime(instanceTemplate.getCreationTimestamp())
9895
&& instanceTemplate.isInitialized()) {
9996
DeleteRegionalInstanceTemplate.deleteRegionalInstanceTemplate(
100-
projectId, zone, instanceTemplate.getName());
97+
projectId, region, instanceTemplate.getName());
10198
}
10299
}
103100
}
@@ -196,9 +193,6 @@ public static void cleanUpExistingReservations(
196193
throws IOException, ExecutionException, InterruptedException, TimeoutException {
197194
try (ReservationsClient reservationsClient = ReservationsClient.create()) {
198195
for (Reservation reservation : reservationsClient.list(projectId, zone).iterateAll()) {
199-
if (!reservationsClient.list(projectId, zone).iterateAll().iterator().hasNext()) {
200-
break;
201-
}
202196
if (containPrefixToDeleteAndZone(reservation, prefixToDelete, zone)
203197
&& isCreatedBeforeThresholdTime(reservation.getCreationTimestamp())) {
204198
DeleteReservation.deleteReservation(projectId, zone, reservation.getName());
@@ -233,7 +227,7 @@ public static boolean containPrefixToDeleteAndZone(
233227
&& ((StoragePool) resource).getZone().contains(zone);
234228
}
235229
} catch (NullPointerException e) {
236-
System.err.println("Resource not found, skipping deletion:");
230+
System.out.println("Resource not found, skipping deletion:");
237231
}
238232
return containPrefixAndZone;
239233
}
@@ -249,7 +243,7 @@ public static boolean containPrefixToDelete(
249243
containPrefixToDelete = ((Snapshot) resource).getName().contains(prefixToDelete);
250244
}
251245
} catch (NullPointerException e) {
252-
System.err.println("Resource not found, skipping deletion:");
246+
System.out.println("Resource not found, skipping deletion:");
253247
}
254248
return containPrefixToDelete;
255249
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
23+
import com.google.api.gax.rpc.NotFoundException;
24+
import com.google.cloud.compute.v1.Reservation;
25+
import compute.Util;
26+
import java.io.IOException;
27+
import java.util.List;
28+
import java.util.UUID;
29+
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.TimeoutException;
32+
import org.junit.Assert;
33+
import org.junit.jupiter.api.AfterAll;
34+
import org.junit.jupiter.api.Assertions;
35+
import org.junit.jupiter.api.BeforeAll;
36+
import org.junit.jupiter.api.Test;
37+
import org.junit.jupiter.api.Timeout;
38+
import org.junit.runner.RunWith;
39+
import org.junit.runners.JUnit4;
40+
41+
@RunWith(JUnit4.class)
42+
@Timeout(value = 25, unit = TimeUnit.MINUTES)
43+
public class CrudOperationReservationIT {
44+
45+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
46+
private static final String ZONE = "us-central1-a";
47+
private static String RESERVATION_NAME;
48+
private static final int NUMBER_OF_VMS = 3;
49+
50+
// Check if the required environment variables are set.
51+
public static void requireEnvVar(String envVarName) {
52+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
53+
.that(System.getenv(envVarName)).isNotEmpty();
54+
}
55+
56+
@BeforeAll
57+
public static void setUp()
58+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
59+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
60+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
61+
62+
// Cleanup existing stale resources.
63+
Util.cleanUpExistingReservations("test-reservation-", PROJECT_ID, ZONE);
64+
65+
RESERVATION_NAME = "test-reservation-" + UUID.randomUUID();
66+
CreateReservation.createReservation(
67+
PROJECT_ID, RESERVATION_NAME, NUMBER_OF_VMS, ZONE);
68+
}
69+
70+
@AfterAll
71+
public static void cleanup()
72+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
73+
// Delete the reservation created for testing.
74+
DeleteReservation.deleteReservation(PROJECT_ID, ZONE, RESERVATION_NAME);
75+
76+
// Test that reservations are deleted
77+
Assertions.assertThrows(
78+
NotFoundException.class,
79+
() -> GetReservation.getReservation(PROJECT_ID, RESERVATION_NAME, ZONE));
80+
}
81+
82+
@Test
83+
public void testGetReservation()
84+
throws IOException {
85+
Reservation reservation = GetReservation.getReservation(
86+
PROJECT_ID, RESERVATION_NAME, ZONE);
87+
assertNotNull(reservation);
88+
assertThat(reservation.getName()).isEqualTo(RESERVATION_NAME);
89+
}
90+
91+
@Test
92+
public void testListReservation() throws IOException {
93+
List<Reservation> reservations =
94+
ListReservations.listReservations(PROJECT_ID, ZONE);
95+
assertThat(reservations).isNotNull();
96+
Assert.assertTrue(reservations.get(0).getName().contains("test-"));
97+
}
98+
}

compute/cloud-client/src/test/java/compute/reservation/ReservationIT.java

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.SPECIFIC_RESERVATION;
2020
import static com.google.common.truth.Truth.assertThat;
2121
import static com.google.common.truth.Truth.assertWithMessage;
22-
import static org.junit.jupiter.api.Assertions.assertNotNull;
2322

2423
import com.google.api.gax.rpc.NotFoundException;
2524
import com.google.cloud.compute.v1.Instance;
@@ -35,7 +34,6 @@
3534
import java.io.ByteArrayOutputStream;
3635
import java.io.IOException;
3736
import java.io.PrintStream;
38-
import java.util.List;
3937
import java.util.UUID;
4038
import java.util.concurrent.ExecutionException;
4139
import java.util.concurrent.TimeUnit;
@@ -59,11 +57,10 @@
5957
public class ReservationIT {
6058

6159
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
62-
private static final String ZONE = "us-central1-a";
60+
private static final String ZONE = "asia-south1-a";
6361
private static final String REGION = ZONE.substring(0, ZONE.lastIndexOf('-'));
6462
private static ReservationsClient reservationsClient;
6563
private static InstancesClient instancesClient;
66-
private static String RESERVATION_NAME;
6764
private static String RESERVATION_NAME_GLOBAL;
6865
private static String RESERVATION_NAME_REGIONAL;
6966
private static String RESERVATION_SHARED_NAME;
@@ -76,7 +73,6 @@ public class ReservationIT {
7673
private static final String SPECIFIC_SHARED_INSTANCE_NAME =
7774
"test-shared-instance-" + UUID.randomUUID();
7875
private static final int NUMBER_OF_VMS = 3;
79-
8076
private static final String MACHINE_TYPE = "n2-standard-32";
8177
private static final String MIN_CPU_PLATFORM = "Intel Cascade Lake";
8278
private ByteArrayOutputStream stdOut;
@@ -106,7 +102,6 @@ public static void setUp()
106102
reservationsClient = ReservationsClient.create();
107103
instancesClient = InstancesClient.create();
108104

109-
RESERVATION_NAME = "test-reserv-" + UUID.randomUUID();
110105
RESERVATION_NAME_GLOBAL = "test-reserv-global-" + UUID.randomUUID();
111106
RESERVATION_NAME_REGIONAL = "test-reserv-regional-" + UUID.randomUUID();
112107
RESERVATION_SHARED_NAME = "test-shared-reserv-" + UUID.randomUUID();
@@ -154,15 +149,11 @@ public static void cleanup()
154149
DeleteInstance.deleteInstance(PROJECT_ID, ZONE, SPECIFIC_SHARED_INSTANCE_NAME);
155150

156151
// Delete all reservations created for testing.
157-
DeleteReservation.deleteReservation(PROJECT_ID, ZONE, RESERVATION_NAME);
158152
DeleteReservation.deleteReservation(PROJECT_ID, ZONE, RESERVATION_NAME_GLOBAL);
159153
DeleteReservation.deleteReservation(PROJECT_ID, ZONE, RESERVATION_NAME_REGIONAL);
160154
DeleteReservation.deleteReservation(PROJECT_ID, ZONE, RESERVATION_SHARED_NAME);
161155

162156
// Test that reservations are deleted
163-
Assertions.assertThrows(
164-
NotFoundException.class,
165-
() -> GetReservation.getReservation(PROJECT_ID, RESERVATION_NAME, ZONE));
166157
Assertions.assertThrows(
167158
NotFoundException.class,
168159
() -> GetReservation.getReservation(PROJECT_ID, RESERVATION_NAME_GLOBAL, ZONE));
@@ -192,35 +183,7 @@ public void afterEach() {
192183
}
193184

194185
@Test
195-
public void firstCreateReservationTest()
196-
throws IOException, ExecutionException, InterruptedException, TimeoutException {
197-
CreateReservation.createReservation(
198-
PROJECT_ID, RESERVATION_NAME, NUMBER_OF_VMS, ZONE);
199-
200-
assertThat(stdOut.toString()).contains("Reservation created. Operation Status: DONE");
201-
}
202-
203-
@Test
204-
public void secondGetReservationTest()
205-
throws IOException {
206-
Reservation reservation = GetReservation.getReservation(
207-
PROJECT_ID, RESERVATION_NAME, ZONE);
208-
209-
assertNotNull(reservation);
210-
assertThat(reservation.getName()).isEqualTo(RESERVATION_NAME);
211-
}
212-
213-
@Test
214-
public void thirdListReservationTest() throws IOException {
215-
List<Reservation> reservations =
216-
ListReservations.listReservations(PROJECT_ID, ZONE);
217-
218-
assertThat(reservations).isNotNull();
219-
Assert.assertTrue(reservations.get(0).getName().contains("test-"));
220-
}
221-
222-
@Test
223-
public void firstCreateReservationWithGlobalInstanceTemplateTest()
186+
public void testCreateReservationWithGlobalInstanceTemplate()
224187
throws IOException, ExecutionException, InterruptedException, TimeoutException {
225188
CreateReservationForInstanceTemplate.createReservationForInstanceTemplate(
226189
PROJECT_ID, RESERVATION_NAME_GLOBAL,
@@ -234,7 +197,7 @@ public void firstCreateReservationWithGlobalInstanceTemplateTest()
234197
}
235198

236199
@Test
237-
public void firstCreateReservationWithRegionInstanceTemplateTest()
200+
public void testCreateReservationWithRegionInstanceTemplate()
238201
throws IOException, ExecutionException, InterruptedException, TimeoutException {
239202
CreateReservationForInstanceTemplate.createReservationForInstanceTemplate(
240203
PROJECT_ID, RESERVATION_NAME_REGIONAL, REGIONAL_INSTANCE_TEMPLATE_URI,

0 commit comments

Comments
 (0)