25
25
import com .google .api .gax .rpc .NotFoundException ;
26
26
import com .google .cloud .tpu .v2 .Node ;
27
27
import com .google .cloud .tpu .v2 .TpuClient ;
28
- import com .google .protobuf .Timestamp ;
29
28
import java .io .ByteArrayOutputStream ;
30
29
import java .io .IOException ;
31
30
import java .io .PrintStream ;
32
- import java .time .Instant ;
33
- import java .time .OffsetDateTime ;
34
- import java .time .ZoneOffset ;
35
- import java .time .format .DateTimeFormatter ;
36
- import java .time .temporal .ChronoUnit ;
37
31
import java .util .UUID ;
38
32
import java .util .concurrent .ExecutionException ;
39
33
import java .util .concurrent .TimeUnit ;
@@ -75,13 +69,12 @@ public static void setUp()
75
69
requireEnvVar ("GOOGLE_CLOUD_PROJECT" );
76
70
77
71
// Cleanup existing stale resources.
78
- cleanUpExistingTpu ("test-tpu-" + javaVersion , PROJECT_ID , ZONE );
72
+ Util . cleanUpExistingTpu ("test-tpu-" + javaVersion , PROJECT_ID , ZONE );
79
73
}
80
74
81
75
@ AfterAll
82
76
public static void cleanup () throws Exception {
83
77
DeleteTpuVm .deleteTpuVm (PROJECT_ID , ZONE , TPU_VM_NAME );
84
- TimeUnit .MINUTES .sleep (5 );
85
78
86
79
// Test that TPUs is deleted
87
80
Assertions .assertThrows (
@@ -96,7 +89,6 @@ public void testCreateTpuVm() throws IOException, ExecutionException, Interrupte
96
89
ByteArrayOutputStream stdOut = new ByteArrayOutputStream ();
97
90
System .setOut (new PrintStream (stdOut ));
98
91
CreateTpuVm .createTpuVm (PROJECT_ID , ZONE , TPU_VM_NAME , ACCELERATOR_TYPE , VERSION );
99
- TimeUnit .MINUTES .sleep (3 );
100
92
101
93
assertThat (stdOut .toString ()).contains ("TPU VM created: " + TPU_VM_PATH_NAME );
102
94
stdOut .close ();
@@ -140,45 +132,4 @@ public void testStartTpuVm() throws IOException, ExecutionException, Interrupted
140
132
141
133
assertThat (node .getState ()).isEqualTo (READY );
142
134
}
143
-
144
- public static void cleanUpExistingTpu (String prefixToDelete , String projectId , String zone )
145
- throws IOException , ExecutionException , InterruptedException {
146
- try (TpuClient tpuClient = TpuClient .create ()) {
147
- String parent = String .format ("projects/%s/locations/%s" , projectId , zone );
148
- for (Node node : tpuClient .listNodes (parent ).iterateAll ()) {
149
- String creationTime = formatTimestamp (node .getCreateTime ());
150
- String name = node .getName ().substring (node .getName ().lastIndexOf ("/" ) + 1 );
151
- if (containPrefixToDeleteAndZone (node , prefixToDelete , zone )
152
- && isCreatedBeforeThresholdTime (creationTime )) {
153
- DeleteTpuVm .deleteTpuVm (projectId , zone , name );
154
- }
155
- }
156
- }
157
- }
158
-
159
- public static boolean containPrefixToDeleteAndZone (
160
- Node node , String prefixToDelete , String zone ) {
161
- boolean containPrefixAndZone = false ;
162
- try {
163
- containPrefixAndZone = node .getName ().contains (prefixToDelete )
164
- && node .getName ().split ("/" )[3 ].contains (zone );
165
-
166
- } catch (NullPointerException e ) {
167
- System .out .println ("Resource not found, skipping deletion:" );
168
- }
169
- return containPrefixAndZone ;
170
- }
171
-
172
- public static boolean isCreatedBeforeThresholdTime (String timestamp ) {
173
- return OffsetDateTime .parse (timestamp ).toInstant ()
174
- .isBefore (Instant .now ().minus (30 , ChronoUnit .MINUTES ));
175
- }
176
-
177
- private static String formatTimestamp (Timestamp timestamp ) {
178
- DateTimeFormatter formatter = DateTimeFormatter .ofPattern ("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" );
179
- OffsetDateTime offsetDateTime = OffsetDateTime .ofInstant (
180
- Instant .ofEpochSecond (timestamp .getSeconds (), timestamp .getNanos ()),
181
- ZoneOffset .UTC );
182
- return formatter .format (offsetDateTime );
183
- }
184
135
}
0 commit comments