Skip to content

Commit e8a069c

Browse files
authored
docs(compute-samples): added method to check for GCE Enforcer based firewall rule deletion (GoogleCloudPlatform#6308)
* docs(compute-samples): included method to check if firewall rule is auto-deleted by gce enforcer * docs(compute-samples): moved firewall tests to setup section
1 parent 5f4f656 commit e8a069c

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

compute/cloud-client/src/test/java/compute/SnippetsIT.java

+43-22
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
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.compute.v1.FirewallsClient;
2324
import com.google.cloud.compute.v1.Instance;
2425
import com.google.cloud.compute.v1.Instance.Status;
@@ -62,7 +63,6 @@ public class SnippetsIT {
6263
private static String BUCKET_NAME;
6364
private static String IMAGE_NAME;
6465
private static String FIREWALL_RULE_CREATE;
65-
private static String FIREWALL_RULE_DELETE;
6666
private static String NETWORK_NAME;
6767
private static String RAW_KEY;
6868

@@ -88,7 +88,6 @@ public static void setUp() throws IOException, InterruptedException, ExecutionEx
8888
BUCKET_NAME = "my-new-test-bucket" + UUID.randomUUID();
8989
IMAGE_NAME = "windows-sql-cloud";
9090
FIREWALL_RULE_CREATE = "firewall-rule-" + UUID.randomUUID();
91-
FIREWALL_RULE_DELETE = "firewall-rule-" + UUID.randomUUID();
9291
NETWORK_NAME = "global/networks/default";
9392
RAW_KEY = getBase64EncodedKey();
9493

@@ -100,8 +99,11 @@ public static void setUp() throws IOException, InterruptedException, ExecutionEx
10099
.createEncryptedInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED, RAW_KEY);
101100
TimeUnit.SECONDS.sleep(10);
102101
compute.CreateFirewallRule.createFirewall(PROJECT_ID, FIREWALL_RULE_CREATE, NETWORK_NAME);
103-
compute.CreateFirewallRule.createFirewall(PROJECT_ID, FIREWALL_RULE_DELETE, NETWORK_NAME);
104102
TimeUnit.SECONDS.sleep(10);
103+
// Moving the following tests to setup section as the created firewall rule is auto-deleted
104+
// by GCE Enforcer within a few minutes.
105+
testListFirewallRules();
106+
testPatchFirewallRule();
105107

106108
// Create a Google Cloud Storage bucket for UsageReports
107109
Storage storage = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService();
@@ -118,7 +120,7 @@ public static void cleanup() throws IOException, InterruptedException, Execution
118120
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
119121
System.setOut(new PrintStream(stdOut));
120122

121-
compute.DeleteFirewallRule.deleteFirewallRule(PROJECT_ID, FIREWALL_RULE_CREATE);
123+
deleteFirewallRuleIfNotDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE);
122124
compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED);
123125
compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME);
124126
compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_LIST_INSTANCE);
@@ -144,6 +146,43 @@ public static String getBase64EncodedKey() {
144146
.encodeToString(stringBuilder.toString().getBytes(StandardCharsets.US_ASCII));
145147
}
146148

149+
public static void testListFirewallRules() throws IOException {
150+
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
151+
System.setOut(new PrintStream(stdOut));
152+
compute.ListFirewallRules.listFirewallRules(PROJECT_ID);
153+
assertThat(stdOut.toString()).contains(FIREWALL_RULE_CREATE);
154+
stdOut.close();
155+
System.setOut(null);
156+
}
157+
158+
public static void testPatchFirewallRule() throws IOException, InterruptedException {
159+
try (FirewallsClient client = FirewallsClient.create()) {
160+
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
161+
System.setOut(new PrintStream(stdOut));
162+
Assert.assertEquals(1000, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority());
163+
compute.PatchFirewallRule.patchFirewallPriority(PROJECT_ID, FIREWALL_RULE_CREATE, 500);
164+
TimeUnit.SECONDS.sleep(5);
165+
Assert.assertEquals(500, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority());
166+
stdOut.close();
167+
System.setOut(null);
168+
}
169+
}
170+
171+
public static void deleteFirewallRuleIfNotDeletedByGceEnforcer(String projectId,
172+
String firewallRule) throws IOException {
173+
/* (**INTERNAL method**)
174+
This method will prevent test failure if the firewall rule was auto-deleted by GCE Enforcer.
175+
(Feel free to remove this method if not running on a Google-owned project.)
176+
*/
177+
try {
178+
GetFirewallRule.getFirewallRule(projectId, firewallRule);
179+
} catch (NotFoundException e) {
180+
System.out.println("Rule already deleted ! ");
181+
return;
182+
}
183+
DeleteFirewallRule.deleteFirewallRule(projectId, firewallRule);
184+
}
185+
147186
public static Status getInstanceStatus(String instanceName) throws IOException {
148187
try (InstancesClient instancesClient = InstancesClient.create()) {
149188
Instance response = instancesClient.get(PROJECT_ID, ZONE, instanceName);
@@ -247,27 +286,9 @@ public void testListImagesByPage() throws IOException {
247286
public void testCreateFirewallRule() throws IOException {
248287
// Assert that firewall rule has been created as part of the setup.
249288
compute.GetFirewallRule.getFirewallRule(PROJECT_ID, FIREWALL_RULE_CREATE);
250-
compute.GetFirewallRule.getFirewallRule(PROJECT_ID, FIREWALL_RULE_DELETE);
251-
assertThat(stdOut.toString()).contains(FIREWALL_RULE_CREATE);
252-
assertThat(stdOut.toString()).contains(FIREWALL_RULE_DELETE);
253-
}
254-
255-
@Test
256-
public void testListFirewallRules() throws IOException {
257-
compute.ListFirewallRules.listFirewallRules(PROJECT_ID);
258289
assertThat(stdOut.toString()).contains(FIREWALL_RULE_CREATE);
259290
}
260291

261-
@Test
262-
public void testPatchFirewallRule() throws IOException, InterruptedException {
263-
try (FirewallsClient client = FirewallsClient.create()) {
264-
Assert.assertTrue(client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority() == 1000);
265-
compute.PatchFirewallRule.patchFirewallPriority(PROJECT_ID, FIREWALL_RULE_CREATE, 500);
266-
TimeUnit.SECONDS.sleep(5);
267-
Assert.assertTrue(client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority() == 500);
268-
}
269-
}
270-
271292
@Test
272293
public void testInstanceOperations()
273294
throws IOException, ExecutionException, InterruptedException {

0 commit comments

Comments
 (0)