1616
1717package tpu ;
1818
19+ import static com .google .common .truth .Truth .assertThat ;
1920import static org .mockito .Mockito .any ;
2021import static org .mockito .Mockito .mock ;
2122import static org .mockito .Mockito .mockStatic ;
2425import static org .mockito .Mockito .when ;
2526
2627import com .google .api .gax .longrunning .OperationFuture ;
27- import com .google .cloud .tpu .v2 .CreateNodeRequest ;
2828import com .google .cloud .tpu .v2 .DeleteNodeRequest ;
29+ import com .google .cloud .tpu .v2 .GetNodeRequest ;
2930import com .google .cloud .tpu .v2 .LocationName ;
31+ import com .google .cloud .tpu .v2 .Node ;
3032import com .google .cloud .tpu .v2 .TpuClient ;
3133import com .google .cloud .tpu .v2 .TpuSettings ;
34+ import java .io .ByteArrayOutputStream ;
3235import java .io .IOException ;
36+ import java .io .PrintStream ;
3337import java .util .concurrent .ExecutionException ;
34- import org .junit .Test ;
38+ import org .junit .jupiter .api .BeforeAll ;
39+ import org .junit .jupiter .api .Test ;
3540import org .junit .jupiter .api .Timeout ;
3641import org .junit .runner .RunWith ;
3742import org .junit .runners .JUnit4 ;
@@ -43,39 +48,47 @@ public class TpuVmIT {
4348 private static final String PROJECT_ID = "project-id" ;
4449 private static final String ZONE = "asia-east1-c" ;
4550 private static final String NODE_NAME = "test-tpu" ;
46- private static final String TPU_TYPE = "v2-8" ;
47- private static final String TPU_SOFTWARE_VERSION = "tpu-vm-tf-2.12.1" ;
51+ private static ByteArrayOutputStream bout ;
52+
53+ @ BeforeAll
54+ public static void setUp () {
55+ bout = new ByteArrayOutputStream ();
56+ System .setOut (new PrintStream (bout ));
57+ }
4858
4959 @ Test
50- public void testCreateTpuVm () throws Exception {
60+ public void testGetTpuVm () throws IOException {
5161 try (MockedStatic <TpuClient > mockedTpuClient = mockStatic (TpuClient .class )) {
52- TpuClient mockTpuClient = mock (TpuClient .class );
53- mockedTpuClient . when (() -> TpuClient .create ( any ( TpuSettings . class )))
54- . thenReturn ( mockTpuClient );
62+ Node mockNode = mock (Node .class );
63+ TpuClient mockClient = mock ( TpuClient .class );
64+ GetTpuVm mockGetTpuVm = mock ( GetTpuVm . class );
5565
56- OperationFuture mockFuture = mock (OperationFuture .class );
57- when (mockTpuClient .createNodeAsync (any (CreateNodeRequest .class )))
58- .thenReturn (mockFuture );
59- CreateTpuVm .createTpuVm (
60- PROJECT_ID , ZONE , NODE_NAME ,
61- TPU_TYPE , TPU_SOFTWARE_VERSION );
66+ mockedTpuClient .when (TpuClient ::create ).thenReturn (mockClient );
67+ when (mockClient .getNode (any (GetNodeRequest .class ))).thenReturn (mockNode );
6268
63- verify (mockTpuClient , times (1 )).createNodeAsync (any (CreateNodeRequest .class ));
69+ Node returnedNode = GetTpuVm .getTpuVm (PROJECT_ID , ZONE , NODE_NAME );
70+
71+ verify (mockGetTpuVm , times (1 ))
72+ .getTpuVm (PROJECT_ID , ZONE , NODE_NAME );
73+ assertThat (returnedNode ).isEqualTo (mockNode );
6474 }
6575 }
6676
6777 @ Test
6878 public void testDeleteTpuVm () throws IOException , ExecutionException , InterruptedException {
6979 try (MockedStatic <TpuClient > mockedTpuClient = mockStatic (TpuClient .class )) {
7080 TpuClient mockTpuClient = mock (TpuClient .class );
81+ OperationFuture mockFuture = mock (OperationFuture .class );
82+
7183 mockedTpuClient .when (() -> TpuClient .create (any (TpuSettings .class )))
7284 .thenReturn (mockTpuClient );
73-
74- OperationFuture mockFuture = mock (OperationFuture .class );
7585 when (mockTpuClient .deleteNodeAsync (any (DeleteNodeRequest .class )))
7686 .thenReturn (mockFuture );
87+
7788 DeleteTpuVm .deleteTpuVm (PROJECT_ID , ZONE , NODE_NAME );
89+ String output = bout .toString ();
7890
91+ assertThat (output ).contains ("TPU VM deleted" );
7992 verify (mockTpuClient , times (1 )).deleteNodeAsync (any (DeleteNodeRequest .class ));
8093 }
8194 }
@@ -93,4 +106,4 @@ public void testListTpuVm() throws IOException {
93106 verify (mockListTpuVms , times (1 )).listTpuVms (PROJECT_ID , ZONE );
94107 }
95108 }
96- }
109+ }
0 commit comments