2525import  com .google .api .gax .rpc .NotFoundException ;
2626import  com .google .cloud .tpu .v2 .Node ;
2727import  com .google .cloud .tpu .v2 .TpuClient ;
28- import  com .google .protobuf .Timestamp ;
2928import  java .io .ByteArrayOutputStream ;
3029import  java .io .IOException ;
3130import  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 ;
3731import  java .util .UUID ;
3832import  java .util .concurrent .ExecutionException ;
3933import  java .util .concurrent .TimeUnit ;
@@ -75,13 +69,12 @@ public static void setUp()
7569    requireEnvVar ("GOOGLE_CLOUD_PROJECT" );
7670
7771    // Cleanup existing stale resources. 
78-     cleanUpExistingTpu ("test-tpu-"  + javaVersion , PROJECT_ID , ZONE );
72+     Util . cleanUpExistingTpu ("test-tpu-"  + javaVersion , PROJECT_ID , ZONE );
7973  }
8074
8175  @ AfterAll 
8276  public  static  void  cleanup () throws  Exception  {
8377    DeleteTpuVm .deleteTpuVm (PROJECT_ID , ZONE , TPU_VM_NAME );
84-     TimeUnit .MINUTES .sleep (5 );
8578
8679    // Test that TPUs is deleted 
8780    Assertions .assertThrows (
@@ -96,7 +89,6 @@ public void testCreateTpuVm() throws IOException, ExecutionException, Interrupte
9689    ByteArrayOutputStream  stdOut  = new  ByteArrayOutputStream ();
9790    System .setOut (new  PrintStream (stdOut ));
9891    CreateTpuVm .createTpuVm (PROJECT_ID , ZONE , TPU_VM_NAME , ACCELERATOR_TYPE , VERSION );
99-     TimeUnit .MINUTES .sleep (3 );
10092
10193    assertThat (stdOut .toString ()).contains ("TPU VM created: "  + TPU_VM_PATH_NAME );
10294    stdOut .close ();
@@ -140,45 +132,4 @@ public void testStartTpuVm() throws IOException, ExecutionException, Interrupted
140132
141133    assertThat (node .getState ()).isEqualTo (READY );
142134  }
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-   }
184135}
0 commit comments