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 compute .Util .getZone ;
21
+
22
+ import com .google .cloud .compute .v1 .AllocationSpecificSKUAllocationReservedInstanceProperties ;
23
+ import com .google .cloud .compute .v1 .AllocationSpecificSKUReservation ;
24
+ import com .google .cloud .compute .v1 .InsertReservationRequest ;
25
+ import com .google .cloud .compute .v1 .Operation ;
26
+ import com .google .cloud .compute .v1 .Reservation ;
27
+ import com .google .cloud .compute .v1 .ReservationsClient ;
28
+ import java .io .ByteArrayOutputStream ;
29
+ import java .io .IOException ;
30
+ import java .io .PrintStream ;
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 .AfterEach ;
36
+ import org .junit .jupiter .api .BeforeAll ;
37
+ import org .junit .jupiter .api .BeforeEach ;
38
+ import org .junit .jupiter .api .Test ;
39
+
40
+ public class ReservationIT {
41
+
42
+ private static String PROJECT_ID ;
43
+ private static String ZONE ;
44
+ private static String RESERVATION_NAME ;
45
+
46
+ private ByteArrayOutputStream stdOut ;
47
+
48
+ @ BeforeAll
49
+ public static void setUp ()
50
+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
51
+ PROJECT_ID = System .getenv ("GOOGLE_CLOUD_PROJECT" );
52
+ ZONE = getZone ();
53
+ RESERVATION_NAME = "test-reservation-" + UUID .randomUUID ();
54
+
55
+ // Create the reservation.
56
+ try (ReservationsClient reservationsClient = ReservationsClient .create ()) {
57
+
58
+ Reservation reservation = Reservation .newBuilder ()
59
+ .setName (RESERVATION_NAME )
60
+ .setSpecificReservation (
61
+ AllocationSpecificSKUReservation .newBuilder ()
62
+ .setCount (1 )
63
+ .setInstanceProperties (
64
+ AllocationSpecificSKUAllocationReservedInstanceProperties .newBuilder ()
65
+ .setMachineType ("n1-standard-1" )
66
+ .build ())
67
+ .build ())
68
+ .build ();
69
+
70
+ InsertReservationRequest reservationRequest = InsertReservationRequest .newBuilder ()
71
+ .setProject (PROJECT_ID )
72
+ .setZone (ZONE )
73
+ .setReservationResource (reservation )
74
+ .build ();
75
+
76
+ Operation response = reservationsClient .insertAsync (reservationRequest )
77
+ .get (3 , TimeUnit .MINUTES );
78
+
79
+ if (response .getStatus () == Operation .Status .DONE ) {
80
+ System .out .println ("Reservation created." );
81
+ } else {
82
+ System .out .println ("Reservation creation failed!" );
83
+ }
84
+
85
+ assertThat (reservation .getName ()).isEqualTo (RESERVATION_NAME );
86
+ }
87
+ }
88
+
89
+ @ BeforeEach
90
+ public void beforeEach () {
91
+ stdOut = new ByteArrayOutputStream ();
92
+ System .setOut (new PrintStream (stdOut ));
93
+ }
94
+
95
+ @ AfterEach
96
+ public void afterEach () {
97
+ stdOut = null ;
98
+ System .setOut (null );
99
+ }
100
+
101
+ @ Test
102
+ public void testDeleteReservation ()
103
+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
104
+ DeleteReservation .deleteReservation (PROJECT_ID , ZONE , RESERVATION_NAME );
105
+
106
+ assertThat (stdOut .toString ()).contains ("Deleted reservation: " + RESERVATION_NAME );
107
+ }
108
+ }
0 commit comments