Skip to content

Commit d00dbb4

Browse files
Fixed tests and empty lines
1 parent b6eee05 commit d00dbb4

File tree

9 files changed

+30
-170
lines changed

9 files changed

+30
-170
lines changed

tpu/src/main/java/tpu/CreateTpuVm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void main(String[] args)
3939
// see https://cloud.google.com/tpu/docs/regions-zones
4040
String zone = "europe-west4-a";
4141
// The name for your TPU.
42-
String nodeName = "YOUR_TPY_NAME";
42+
String nodeName = "YOUR_TPU_NAME";
4343
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
4444
// For more information about supported accelerator types for each TPU version,
4545
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.

tpu/src/main/java/tpu/CreateTpuVmWithStartupScript.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void main(String[] args)
4040
// see https://cloud.google.com/tpu/docs/regions-zones
4141
String zone = "europe-west4-a";
4242
// The name for your TPU.
43-
String nodeName = "YOUR_TPY_NAME";
43+
String nodeName = "YOUR_TPU_NAME";
4444
// The accelerator type that specifies the version and size of the Cloud TPU you want to create.
4545
// For more information about supported accelerator types for each TPU version,
4646
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
@@ -79,16 +79,17 @@ public static Node createTpuVmWithStartupScript(String projectId, String zone,
7979
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
8080
String parent = String.format("projects/%s/locations/%s", projectId, zone);
8181

82-
// Create metadata map
82+
String startupScriptContent = "#!/bin/bash\necho \"Hello from the startup script!\"";
83+
// Add startup script to metadata
8384
Map<String, String> metadata = new HashMap<>();
84-
metadata.put("startup-script", "your-script-here"); // Script content here
85+
metadata.put("startup-script", startupScriptContent);
8586

8687
Node tpuVm =
8788
Node.newBuilder()
8889
.setName(nodeName)
8990
.setAcceleratorType(acceleratorType)
9091
.setRuntimeVersion(tpuSoftwareVersion)
91-
.putAllLabels(metadata)
92+
.putAllMetadata(metadata)
9293
.build();
9394

9495
CreateNodeRequest request =
@@ -98,10 +99,7 @@ public static Node createTpuVmWithStartupScript(String projectId, String zone,
9899
.setNode(tpuVm)
99100
.build();
100101

101-
Node response = tpuClient.createNodeAsync(request).get();
102-
103-
System.out.printf("TPU VM created: %s\n", response.getName());
104-
return response;
102+
return tpuClient.createNodeAsync(request).get();
105103
}
106104
}
107105
}

tpu/src/main/java/tpu/DeleteForceQueuedResource.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package tpu;
1818

1919
//[START tpu_queued_resources_delete_force]
20-
2120
import com.google.api.gax.retrying.RetrySettings;
2221
import com.google.api.gax.rpc.UnknownException;
2322
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;

tpu/src/main/java/tpu/DeleteTpuVm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static void main(String[] args)
3939
// see https://cloud.google.com/tpu/docs/regions-zones
4040
String zone = "europe-west4-a";
4141
// The name for your TPU.
42-
String nodeName = "YOUR_TPY_NAME";
42+
String nodeName = "YOUR_TPU_NAME";
4343

4444
deleteTpuVm(projectId, zone, nodeName);
4545
}

tpu/src/main/java/tpu/GetTpuVm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static void main(String[] args) throws IOException {
3434
// see https://cloud.google.com/tpu/docs/regions-zones
3535
String zone = "europe-west4-a";
3636
// The name for your TPU.
37-
String nodeName = "YOUR_TPY_NAME";
37+
String nodeName = "YOUR_TPU_NAME";
3838

3939
getTpuVm(projectId, zone, nodeName);
4040
}

tpu/src/test/java/tpu/CreateTpuVmWithStartupScriptIT.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,25 @@
2020
import static com.google.common.truth.Truth.assertWithMessage;
2121
import static org.junit.Assert.assertNotNull;
2222

23-
import com.google.api.gax.rpc.NotFoundException;
2423
import com.google.cloud.tpu.v2.Node;
2524
import java.io.IOException;
2625
import java.util.UUID;
2726
import java.util.concurrent.ExecutionException;
2827
import java.util.concurrent.TimeUnit;
2928
import org.junit.Assert;
3029
import org.junit.jupiter.api.AfterAll;
31-
import org.junit.jupiter.api.Assertions;
3230
import org.junit.jupiter.api.BeforeAll;
3331
import org.junit.jupiter.api.Test;
3432
import org.junit.jupiter.api.Timeout;
3533
import org.junit.runner.RunWith;
3634
import org.junit.runners.JUnit4;
3735

3836
@RunWith(JUnit4.class)
39-
@Timeout(value = 25, unit = TimeUnit.MINUTES)
37+
@Timeout(value = 6, unit = TimeUnit.MINUTES)
4038
public class CreateTpuVmWithStartupScriptIT {
4139
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4240
private static final String ZONE = "europe-west4-a";
43-
static String javaVersion = System.getProperty("java.version").substring(0, 2);
44-
private static final String NODE_NAME = "test-tpu-with-script-" + javaVersion + "-"
45-
+ UUID.randomUUID().toString().substring(0, 8);
41+
private static final String NODE_NAME = "test-tpu-with-script-" + UUID.randomUUID();
4642
private static final String TPU_TYPE = "v2-8";
4743
private static final String TPU_SOFTWARE_VERSION = "tpu-vm-tf-2.14.1";
4844

@@ -52,23 +48,14 @@ public static void requireEnvVar(String envVarName) {
5248
}
5349

5450
@BeforeAll
55-
public static void setUp()
56-
throws IOException, ExecutionException, InterruptedException {
51+
public static void setUp() {
5752
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
5853
requireEnvVar("GOOGLE_CLOUD_PROJECT");
59-
60-
// Cleanup existing stale resources.
61-
Util.cleanUpExistingTpu("test-tpu-with-script-" + javaVersion, PROJECT_ID, ZONE);
6254
}
6355

6456
@AfterAll
6557
public static void cleanup() throws Exception {
6658
DeleteTpuVm.deleteTpuVm(PROJECT_ID, ZONE, NODE_NAME);
67-
68-
// Test that TPUs is deleted
69-
Assertions.assertThrows(
70-
NotFoundException.class,
71-
() -> GetTpuVm.getTpuVm(PROJECT_ID, ZONE, NODE_NAME));
7259
}
7360

7461
@Test
@@ -79,8 +66,8 @@ public void testCreateTpuVmWithStartupScript()
7966

8067
assertNotNull(node);
8168
assertThat(node.getName().equals(NODE_NAME));
82-
Assert.assertTrue(node.containsLabels("startup-script"));
83-
Assert.assertTrue(node.getLabelsMap().containsValue("your-script-here"));
84-
69+
Assert.assertTrue(node.containsMetadata("startup-script"));
70+
Assert.assertTrue(node.getMetadataMap().containsValue("#!/bin/bash\n"
71+
+ "echo \"Hello from the startup script!\""));
8572
}
8673
}

tpu/src/test/java/tpu/CreateQueuedResourceWithNetworkIT.java renamed to tpu/src/test/java/tpu/QueuedResourceIT.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,48 @@
1717
package tpu;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.truth.Truth.assertWithMessage;
2021

21-
import com.google.api.gax.rpc.NotFoundException;
2222
import com.google.cloud.tpu.v2alpha1.QueuedResource;
23-
import java.io.IOException;
2423
import java.util.UUID;
2524
import java.util.concurrent.TimeUnit;
2625
import org.junit.Test;
2726
import org.junit.jupiter.api.AfterAll;
28-
import org.junit.jupiter.api.Assertions;
2927
import org.junit.jupiter.api.BeforeAll;
3028
import org.junit.jupiter.api.Timeout;
3129
import org.junit.runner.RunWith;
3230
import org.junit.runners.JUnit4;
3331

3432
@RunWith(JUnit4.class)
3533
@Timeout(value = 6, unit = TimeUnit.MINUTES)
36-
public class CreateQueuedResourceWithNetworkIT {
34+
public class QueuedResourceIT {
3735

3836
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
3937
private static final String ZONE = "europe-west4-a";
40-
static String javaVersion = System.getProperty("java.version").substring(0, 2);
41-
private static final String NODE_NAME = "test-tpu-queued-resource-network-" + javaVersion + "-"
42-
+ UUID.randomUUID().toString().substring(0, 8);
38+
private static final String NODE_NAME = "test-tpu-queued-resource-network-" + UUID.randomUUID();
4339
private static final String TPU_TYPE = "v2-8";
4440
private static final String TPU_SOFTWARE_VERSION = "tpu-vm-tf-2.14.1";
45-
private static final String QUEUED_RESOURCE_NAME = "queued-resource-network-" + javaVersion + "-"
46-
+ UUID.randomUUID().toString().substring(0, 8);
41+
private static final String QUEUED_RESOURCE_NAME = "queued-resource-network-" + UUID.randomUUID();
4742
private static final String NETWORK_NAME = "default";
4843

49-
@BeforeAll
50-
public static void setUp() throws IOException {
44+
public static void requireEnvVar(String envVarName) {
45+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
46+
.that(System.getenv(envVarName)).isNotEmpty();
47+
}
5148

52-
// Cleanup existing stale resources.
53-
Util.cleanUpExistingQueuedResources("queued-resource-network-", PROJECT_ID, ZONE);
49+
@BeforeAll
50+
public static void setUp() {
51+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
52+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
5453
}
5554

5655
@AfterAll
5756
public static void cleanup() {
5857
DeleteForceQueuedResource.deleteForceQueuedResource(PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME);
59-
60-
// Test that resource is deleted
61-
Assertions.assertThrows(
62-
NotFoundException.class,
63-
() -> GetQueuedResource.getQueuedResource(PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME));
6458
}
6559

6660
@Test
6761
public void testCreateQueuedResourceWithSpecifiedNetwork() throws Exception {
68-
6962
QueuedResource queuedResource = CreateQueuedResourceWithNetwork.createQueuedResourceWithNetwork(
7063
PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME, NODE_NAME,
7164
TPU_TYPE, TPU_SOFTWARE_VERSION, NETWORK_NAME);

tpu/src/test/java/tpu/TpuVmIT.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020
import static com.google.common.truth.Truth.assertWithMessage;
2121
import static org.junit.Assert.assertNotNull;
2222

23-
import com.google.api.gax.rpc.NotFoundException;
2423
import com.google.cloud.tpu.v2.Node;
2524
import java.io.IOException;
2625
import java.util.UUID;
2726
import java.util.concurrent.ExecutionException;
2827
import java.util.concurrent.TimeUnit;
2928
import org.junit.jupiter.api.AfterAll;
30-
import org.junit.jupiter.api.Assertions;
3129
import org.junit.jupiter.api.BeforeAll;
3230
import org.junit.jupiter.api.MethodOrderer;
3331
import org.junit.jupiter.api.Order;
@@ -39,13 +37,11 @@
3937

4038
@RunWith(JUnit4.class)
4139
@Timeout(value = 15, unit = TimeUnit.MINUTES)
42-
@TestMethodOrder(MethodOrderer. OrderAnnotation. class)
40+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
4341
public class TpuVmIT {
4442
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
4543
private static final String ZONE = "asia-east1-c";
46-
static String javaVersion = System.getProperty("java.version").substring(0, 2);
47-
private static final String NODE_NAME = "test-tpu-" + javaVersion + "-"
48-
+ UUID.randomUUID().toString().substring(0, 8);
44+
private static final String NODE_NAME = "test-tpu-" + UUID.randomUUID();
4945
private static final String TPU_TYPE = "v2-8";
5046
private static final String TPU_SOFTWARE_VERSION = "tpu-vm-tf-2.12.1";
5147
private static final String NODE_PATH_NAME =
@@ -57,29 +53,19 @@ public static void requireEnvVar(String envVarName) {
5753
}
5854

5955
@BeforeAll
60-
public static void setUp()
61-
throws IOException, ExecutionException, InterruptedException {
56+
public static void setUp() {
6257
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
6358
requireEnvVar("GOOGLE_CLOUD_PROJECT");
64-
65-
// Cleanup existing stale resources.
66-
Util.cleanUpExistingTpu("test-tpu-" + javaVersion, PROJECT_ID, ZONE);
6759
}
6860

6961
@AfterAll
7062
public static void cleanup() throws Exception {
7163
DeleteTpuVm.deleteTpuVm(PROJECT_ID, ZONE, NODE_NAME);
72-
73-
// Test that TPUs is deleted
74-
Assertions.assertThrows(
75-
NotFoundException.class,
76-
() -> GetTpuVm.getTpuVm(PROJECT_ID, ZONE, NODE_NAME));
7764
}
7865

7966
@Test
8067
@Order(1)
8168
public void testCreateTpuVm() throws IOException, ExecutionException, InterruptedException {
82-
8369
Node node = CreateTpuVm.createTpuVm(
8470
PROJECT_ID, ZONE, NODE_NAME, TPU_TYPE, TPU_SOFTWARE_VERSION);
8571

tpu/src/test/java/tpu/Util.java

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)