Skip to content

Commit 977208d

Browse files
Created ConsumeReservationIT cals, fixed ReservationIT
1 parent bcf0f74 commit 977208d

File tree

2 files changed

+89
-40
lines changed

2 files changed

+89
-40
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.NO_RESERVATION;
20+
import static com.google.common.truth.Truth.assertWithMessage;
21+
22+
import com.google.cloud.compute.v1.Instance;
23+
import com.google.cloud.compute.v1.InstancesClient;
24+
import compute.DeleteInstance;
25+
import compute.Util;
26+
import java.io.IOException;
27+
import java.util.UUID;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeoutException;
30+
import org.junit.jupiter.api.AfterAll;
31+
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.BeforeAll;
33+
import org.junit.jupiter.api.Test;
34+
35+
public class ConsumeReservationIT {
36+
37+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
38+
private static final String ZONE = "us-west1-a";
39+
private static InstancesClient instancesClient;
40+
static String javaVersion = System.getProperty("java.version").substring(0, 2);
41+
private static final String INSTANCE_NOT_CONSUME_RESERVATION_NAME =
42+
"test-instance-not-consume-" + javaVersion + "-"
43+
+ UUID.randomUUID().toString().substring(0, 8);
44+
private static final String MACHINE_TYPE = "n2-standard-32";
45+
46+
// Check if the required environment variables are set.
47+
public static void requireEnvVar(String envVarName) {
48+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
49+
.that(System.getenv(envVarName)).isNotEmpty();
50+
}
51+
52+
@BeforeAll
53+
public static void setUp()
54+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
55+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
56+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
57+
58+
// Initialize the clients once for all tests
59+
instancesClient = InstancesClient.create();
60+
61+
// Cleanup existing stale resources.
62+
Util.cleanUpExistingInstances("test-instance-not-consume-" + javaVersion, PROJECT_ID, ZONE);
63+
}
64+
65+
@AfterAll
66+
public static void cleanup()
67+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
68+
// Delete the instance created for testing.
69+
DeleteInstance.deleteInstance(PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
70+
71+
// Close the client after all tests
72+
instancesClient.close();
73+
}
74+
75+
@Test
76+
public void testCreateInstanceNotConsumeReservation()
77+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
78+
79+
CreateInstanceNotConsumeReservation.createInstanceNotConsumeReservation(
80+
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME, MACHINE_TYPE);
81+
82+
// Verify that the instance was created with the correct consumeReservationType
83+
Instance instance = instancesClient.get(
84+
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
85+
86+
Assertions.assertEquals(NO_RESERVATION.toString(),
87+
instance.getReservationAffinity().getConsumeReservationType());
88+
}
89+
}

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

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@
1616

1717
package compute.reservation;
1818

19-
import static com.google.cloud.compute.v1.ReservationAffinity.ConsumeReservationType.NO_RESERVATION;
2019
import static com.google.common.truth.Truth.assertThat;
2120
import static com.google.common.truth.Truth.assertWithMessage;
2221

2322
import com.google.api.gax.rpc.NotFoundException;
24-
import com.google.cloud.compute.v1.Instance;
25-
import com.google.cloud.compute.v1.InstancesClient;
2623
import com.google.cloud.compute.v1.Reservation;
2724
import com.google.cloud.compute.v1.ReservationsClient;
2825
import compute.CreateInstanceTemplate;
2926
import compute.CreateRegionalInstanceTemplate;
30-
import compute.DeleteInstance;
3127
import compute.DeleteInstanceTemplate;
3228
import compute.DeleteRegionalInstanceTemplate;
3329
import compute.Util;
@@ -55,7 +51,6 @@ public class ReservationIT {
5551
private static final String ZONE = "us-west1-a";
5652
private static final String REGION = ZONE.substring(0, ZONE.lastIndexOf('-'));
5753
private static ReservationsClient reservationsClient;
58-
private static InstancesClient instancesClient;
5954
private static String RESERVATION_NAME_GLOBAL;
6055
private static String RESERVATION_NAME_REGIONAL;
6156
private static String GLOBAL_INSTANCE_TEMPLATE_URI;
@@ -66,11 +61,7 @@ public class ReservationIT {
6661
private static final String REGIONAL_INSTANCE_TEMPLATE_NAME =
6762
"test-regional-inst-temp-" + javaVersion + "-"
6863
+ UUID.randomUUID().toString().substring(0, 8);
69-
private static final String INSTANCE_NOT_CONSUME_RESERVATION_NAME =
70-
"test-instance-not-consume-" + javaVersion + "-"
71-
+ UUID.randomUUID().toString().substring(0, 8);
7264
private static final int NUMBER_OF_VMS = 3;
73-
private static final String MACHINE_TYPE = "n2-standard-32";
7465

7566
// Check if the required environment variables are set.
7667
public static void requireEnvVar(String envVarName) {
@@ -89,7 +80,6 @@ public static void setUp()
8980

9081
// Cleanup existing stale resources.
9182
Util.cleanUpExistingInstanceTemplates("test-global-inst-temp-" + javaVersion, PROJECT_ID);
92-
Util.cleanUpExistingInstanceTemplates("test-instance-not-consume-" + javaVersion, PROJECT_ID);
9383
Util.cleanUpExistingRegionalInstanceTemplates(
9484
"test-regional-inst-temp-" + javaVersion, PROJECT_ID, ZONE);
9585
Util.cleanUpExistingReservations(
@@ -109,16 +99,6 @@ public static void setUp()
10999
String.format("projects/%s/regions/%s/instanceTemplates/%s",
110100
PROJECT_ID, REGION, REGIONAL_INSTANCE_TEMPLATE_NAME);
111101

112-
// Initialize the clients once for all tests
113-
reservationsClient = ReservationsClient.create();
114-
instancesClient = InstancesClient.create();
115-
116-
// Cleanup existing stale resources.
117-
Util.cleanUpExistingInstances("test-instance-", PROJECT_ID, ZONE);
118-
Util.cleanUpExistingInstanceTemplates("test-global-inst-temp-", PROJECT_ID);
119-
Util.cleanUpExistingRegionalInstanceTemplates("test-regional-inst-temp-", PROJECT_ID, ZONE);
120-
Util.cleanUpExistingReservations("test-reserv-", PROJECT_ID, ZONE);
121-
122102
// Create instance template with GLOBAL location.
123103
CreateInstanceTemplate.createInstanceTemplate(PROJECT_ID, GLOBAL_INSTANCE_TEMPLATE_NAME);
124104
assertThat(stdOut.toString())
@@ -152,9 +132,6 @@ public static void cleanup()
152132
.contains("Instance template deletion operation status for "
153133
+ REGIONAL_INSTANCE_TEMPLATE_NAME);
154134

155-
// Delete the instance created for testing.
156-
DeleteInstance.deleteInstance(PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
157-
158135
// Delete all reservations created for testing.
159136
DeleteReservation.deleteReservation(PROJECT_ID, ZONE, RESERVATION_NAME_GLOBAL);
160137
DeleteReservation.deleteReservation(PROJECT_ID, ZONE, RESERVATION_NAME_REGIONAL);
@@ -167,9 +144,7 @@ public static void cleanup()
167144
NotFoundException.class,
168145
() -> GetReservation.getReservation(PROJECT_ID, RESERVATION_NAME_REGIONAL, ZONE));
169146

170-
// Close the client after all tests
171147
reservationsClient.close();
172-
instancesClient.close();
173148

174149
stdOut.close();
175150
System.setOut(out);
@@ -201,19 +176,4 @@ public void testCreateReservationWithRegionInstanceTemplate()
201176
Assert.assertTrue(reservation.getZone().contains(ZONE));
202177
Assert.assertEquals(RESERVATION_NAME_REGIONAL, reservation.getName());
203178
}
204-
205-
@Test
206-
public void testCreateInstanceNotConsumeReservation()
207-
throws IOException, ExecutionException, InterruptedException, TimeoutException {
208-
209-
CreateInstanceNotConsumeReservation.createInstanceNotConsumeReservation(
210-
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME, MACHINE_TYPE);
211-
212-
// Verify that the instance was created with the correct consumeReservationType
213-
Instance instance = instancesClient.get(
214-
PROJECT_ID, ZONE, INSTANCE_NOT_CONSUME_RESERVATION_NAME);
215-
216-
Assert.assertEquals(NO_RESERVATION.toString(),
217-
instance.getReservationAffinity().getConsumeReservationType());
218-
}
219179
}

0 commit comments

Comments
 (0)