Skip to content

Commit 3629e5f

Browse files
Fixed tests
1 parent 684a880 commit 3629e5f

File tree

4 files changed

+80
-28
lines changed

4 files changed

+80
-28
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 tpu;
18+
19+
//[START tpu_queued_resources_get]
20+
21+
import com.google.cloud.tpu.v2alpha1.GetQueuedResourceRequest;
22+
import com.google.cloud.tpu.v2alpha1.QueuedResource;
23+
import com.google.cloud.tpu.v2alpha1.TpuClient;
24+
import java.io.IOException;
25+
26+
public class GetQueuedResource {
27+
public static void main(String[] args) throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
// Project ID or project number of the Google Cloud project.
30+
String projectId = "YOUR_PROJECT_ID";
31+
// The zone in which the TPU was created.
32+
String zone = "europe-west4-a";
33+
// The name for your Queued Resource.
34+
String queuedResourceId = "QUEUED_RESOURCE_ID";
35+
36+
getQueuedResource(projectId, zone, queuedResourceId);
37+
}
38+
39+
// Get a Queued Resource.
40+
public static QueuedResource getQueuedResource(
41+
String projectId, String zone, String queuedResourceId) throws IOException {
42+
String name = String.format("projects/%s/locations/%s/queuedResources/%s",
43+
projectId, zone, queuedResourceId);
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests.
46+
try (TpuClient tpuClient = TpuClient.create()) {
47+
GetQueuedResourceRequest request =
48+
GetQueuedResourceRequest.newBuilder().setName(name).build();
49+
50+
return tpuClient.getQueuedResource(request);
51+
}
52+
}
53+
}
54+
//[END tpu_queued_resources_get]

tpu/src/test/java/tpu/CreateQueuedResourceWithNetworkIT.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import com.google.api.gax.rpc.NotFoundException;
2122
import com.google.cloud.tpu.v2alpha1.QueuedResource;
2223
import java.io.ByteArrayOutputStream;
2324
import java.io.IOException;
@@ -26,6 +27,7 @@
2627
import java.util.concurrent.TimeUnit;
2728
import org.junit.Test;
2829
import org.junit.jupiter.api.AfterAll;
30+
import org.junit.jupiter.api.Assertions;
2931
import org.junit.jupiter.api.BeforeAll;
3032
import org.junit.jupiter.api.Timeout;
3133
import org.junit.runner.RunWith;
@@ -54,17 +56,13 @@ public static void setUp() throws IOException {
5456
}
5557

5658
@AfterAll
57-
public static void cleanup() throws IOException {
58-
final PrintStream out = System.out;
59-
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
60-
System.setOut(new PrintStream(stdOut));
59+
public static void cleanup() {
6160
DeleteForceQueuedResource.deleteForceQueuedResource(PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME);
6261

63-
// Test that resources are deleted
64-
assertThat(stdOut.toString()).contains("Deleted Queued Resource:");
65-
66-
stdOut.close();
67-
System.setOut(out);
62+
// Test that resource is deleted
63+
Assertions.assertThrows(
64+
NotFoundException.class,
65+
() -> GetQueuedResource.getQueuedResource(PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME));
6866
}
6967

7068
@Test
@@ -88,6 +86,7 @@ public void shouldCreateQueuedResourceWithSpecifiedNetwork() throws Exception {
8886
.contains(NETWORK_NAME));
8987
assertThat(queuedResource.getTpu().getNodeSpec(0).getNode().getNetworkConfig().getSubnetwork()
9088
.contains(NETWORK_NAME));
89+
9190
stdOut.close();
9291
System.setOut(out);
9392
}

tpu/src/test/java/tpu/CreateQueuedResourceWithStartupScriptIT.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static com.google.common.truth.Truth.assertWithMessage;
2121

22+
import com.google.api.gax.rpc.NotFoundException;
2223
import com.google.cloud.tpu.v2alpha1.QueuedResource;
2324
import java.io.ByteArrayOutputStream;
2425
import java.io.IOException;
2526
import java.io.PrintStream;
2627
import java.util.UUID;
2728
import java.util.concurrent.TimeUnit;
2829
import org.junit.jupiter.api.AfterAll;
30+
import org.junit.jupiter.api.Assertions;
2931
import org.junit.jupiter.api.BeforeAll;
3032
import org.junit.jupiter.api.Test;
3133
import org.junit.jupiter.api.Timeout;
@@ -61,24 +63,21 @@ public static void setUp() throws IOException {
6163
}
6264

6365
@AfterAll
64-
public static void cleanup() throws IOException {
65-
final PrintStream out = System.out;
66-
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
67-
System.setOut(new PrintStream(stdOut));
66+
public static void cleanup() {
6867
DeleteForceQueuedResource.deleteForceQueuedResource(PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME);
6968

70-
// Test that resources are deleted
71-
assertThat(stdOut.toString()).contains("Deleted Queued Resource:");
72-
73-
stdOut.close();
74-
System.setOut(out);
69+
// Test that resource is deleted
70+
Assertions.assertThrows(
71+
NotFoundException.class,
72+
() -> GetQueuedResource.getQueuedResource(PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME));
7573
}
7674

7775
@Test
7876
public void testCreateQueuedResourceWithStartupScript() throws Exception {
7977
final PrintStream out = System.out;
8078
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
8179
System.setOut(new PrintStream(stdOut));
80+
8281
QueuedResource queuedResource = CreateQueuedResourceWithStartupScript.createQueuedResource(
8382
PROJECT_ID,
8483
ZONE,

tpu/src/test/java/tpu/CreateSpotQueuedResourceIT.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,23 @@
2020
import static com.google.common.truth.Truth.assertWithMessage;
2121
import static org.junit.Assert.assertTrue;
2222

23+
import com.google.api.gax.rpc.NotFoundException;
2324
import com.google.cloud.tpu.v2alpha1.QueuedResource;
2425
import java.io.ByteArrayOutputStream;
2526
import java.io.IOException;
2627
import java.io.PrintStream;
2728
import java.util.UUID;
2829
import java.util.concurrent.TimeUnit;
2930
import org.junit.jupiter.api.AfterAll;
31+
import org.junit.jupiter.api.Assertions;
3032
import org.junit.jupiter.api.BeforeAll;
3133
import org.junit.jupiter.api.Test;
3234
import org.junit.jupiter.api.Timeout;
3335
import org.junit.runner.RunWith;
3436
import org.junit.runners.JUnit4;
3537

3638
@RunWith(JUnit4.class)
37-
@Timeout(value = 25, unit = TimeUnit.MINUTES)
39+
@Timeout(value = 6, unit = TimeUnit.MINUTES)
3840
public class CreateSpotQueuedResourceIT {
3941

4042
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
@@ -62,24 +64,21 @@ public static void setUp() throws IOException {
6264
}
6365

6466
@AfterAll
65-
public static void cleanup() throws IOException {
66-
final PrintStream out = System.out;
67-
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
68-
System.setOut(new PrintStream(stdOut));
67+
public static void cleanup() {
6968
DeleteForceQueuedResource.deleteForceQueuedResource(PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME);
7069

71-
// Test that resources are deleted
72-
assertThat(stdOut.toString()).contains("Deleted Queued Resource:");
73-
74-
stdOut.close();
75-
System.setOut(out);
70+
// Test that resource is deleted
71+
Assertions.assertThrows(
72+
NotFoundException.class,
73+
() -> GetQueuedResource.getQueuedResource(PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME));
7674
}
7775

7876
@Test
7977
public void testGetSpotQueuedResource() throws Exception {
8078
final PrintStream out = System.out;
8179
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
8280
System.setOut(new PrintStream(stdOut));
81+
8382
QueuedResource queuedResource = CreateSpotQueuedResource.createQueuedResource(
8483
PROJECT_ID,
8584
ZONE,
@@ -91,6 +90,7 @@ public void testGetSpotQueuedResource() throws Exception {
9190
assertThat(stdOut.toString()).contains("Queued Resource created: " + QUEUED_RESOURCE_NAME);
9291
assertTrue(queuedResource.getTpu().getNodeSpec(0).getNode()
9392
.getSchedulingConfig().getPreemptible());
93+
9494
stdOut.close();
9595
System.setOut(out);
9696
}

0 commit comments

Comments
 (0)