Skip to content

Commit 5432076

Browse files
committed
docs(compute-samples): added tests for instance templates
1 parent 7c47898 commit 5432076

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package compute;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
import static com.google.common.truth.Truth.assertWithMessage;
5+
6+
import com.google.cloud.compute.v1.Instance;
7+
import com.google.cloud.compute.v1.InstancesClient;
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.IOException;
10+
import java.io.PrintStream;
11+
import java.util.UUID;
12+
import java.util.concurrent.ExecutionException;
13+
import java.util.concurrent.TimeUnit;
14+
import org.junit.After;
15+
import org.junit.AfterClass;
16+
import org.junit.Before;
17+
import org.junit.BeforeClass;
18+
import org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
import org.junit.runners.JUnit4;
21+
22+
@RunWith(JUnit4.class)
23+
public class InstanceTemplatesIT {
24+
25+
26+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
27+
private static String TEMPLATE_NAME;
28+
private static String TEMPLATE_NAME_FROM_INSTANCE;
29+
private static String TEMPLATE_NAME_WITH_SUBNET;
30+
private static String ZONE;
31+
private static String MACHINE_NAME;
32+
33+
private ByteArrayOutputStream stdOut;
34+
35+
// Check if the required environment variables are set.
36+
public static void requireEnvVar(String envVarName) {
37+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
38+
.that(System.getenv(envVarName)).isNotEmpty();
39+
}
40+
41+
@BeforeClass
42+
public static void setup() throws IOException, ExecutionException, InterruptedException {
43+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
44+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
45+
46+
TEMPLATE_NAME = "template-name-" + UUID.randomUUID();
47+
ZONE = "us-central1-a";
48+
MACHINE_NAME = "my-new-test-instance" + UUID.randomUUID();
49+
TEMPLATE_NAME_FROM_INSTANCE = "my-new-test-instance" + UUID.randomUUID();
50+
TEMPLATE_NAME_WITH_SUBNET = "my-new-test-instance" + UUID.randomUUID();
51+
52+
CreateInstanceTemplate.createInstanceTemplate(PROJECT_ID, TEMPLATE_NAME);
53+
CreateInstance.createInstance(PROJECT_ID, ZONE, MACHINE_NAME);
54+
TimeUnit.SECONDS.sleep(10);
55+
CreateTemplateFromInstance.createTemplateFromInstance(PROJECT_ID, TEMPLATE_NAME_FROM_INSTANCE,
56+
getInstance(ZONE, MACHINE_NAME).getSelfLink());
57+
CreateTemplateWithSubnet.createTemplateWithSubnet(PROJECT_ID, "global/networks/default",
58+
"regions/asia-east1/subnetworks/default", TEMPLATE_NAME_WITH_SUBNET);
59+
TimeUnit.SECONDS.sleep(10);
60+
}
61+
62+
@AfterClass
63+
public static void cleanup() throws IOException, ExecutionException, InterruptedException {
64+
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
65+
System.setOut(new PrintStream(stdOut));
66+
DeleteInstanceTemplate.deleteInstanceTemplate(PROJECT_ID, TEMPLATE_NAME);
67+
DeleteInstanceTemplate.deleteInstanceTemplate(PROJECT_ID, TEMPLATE_NAME_FROM_INSTANCE);
68+
DeleteInstanceTemplate.deleteInstanceTemplate(PROJECT_ID, TEMPLATE_NAME_WITH_SUBNET);
69+
System.setOut(null);
70+
}
71+
72+
public static Instance getInstance(String zone, String instanceName) throws IOException {
73+
try (InstancesClient instancesClient = InstancesClient.create()) {
74+
return instancesClient.get(PROJECT_ID, zone, instanceName);
75+
}
76+
}
77+
78+
@Before
79+
public void beforeEach() {
80+
stdOut = new ByteArrayOutputStream();
81+
System.setOut(new PrintStream(stdOut));
82+
}
83+
84+
@After
85+
public void afterEach() {
86+
stdOut = null;
87+
System.setOut(null);
88+
}
89+
90+
@Test
91+
public void testGetInstanceTemplate() throws IOException {
92+
GetInstanceTemplate.getInstanceTemplate(PROJECT_ID, TEMPLATE_NAME);
93+
assertThat(stdOut.toString()).contains(TEMPLATE_NAME);
94+
GetInstanceTemplate.getInstanceTemplate(PROJECT_ID, TEMPLATE_NAME_FROM_INSTANCE);
95+
assertThat(stdOut.toString()).contains(TEMPLATE_NAME_FROM_INSTANCE);
96+
GetInstanceTemplate.getInstanceTemplate(PROJECT_ID, TEMPLATE_NAME_WITH_SUBNET);
97+
assertThat(stdOut.toString()).contains(TEMPLATE_NAME_WITH_SUBNET);
98+
}
99+
100+
@Test
101+
public void testListInstanceTemplates() throws IOException {
102+
ListInstanceTemplates.listInstanceTemplates(PROJECT_ID);
103+
assertThat(stdOut.toString()).contains(TEMPLATE_NAME);
104+
assertThat(stdOut.toString()).contains(TEMPLATE_NAME_FROM_INSTANCE);
105+
assertThat(stdOut.toString()).contains(TEMPLATE_NAME_WITH_SUBNET);
106+
}
107+
108+
}

0 commit comments

Comments
 (0)