Skip to content

Commit 495742e

Browse files
authored
fix(compute-samples): fixed flaky firewall rule deletion (GoogleCloudPlatform#6609)
* fix(compute-samples): fixed flaky firewall rule deletion * fix(compute-samples): fixed flaky firewall rule deletion * fix(compute-samples): fixed NullPointer caused by Sysout set to null * docs(compute-samples): included review comments * docs(compute-samples): included review comments * docs(compute-samples): lint fix
1 parent fbab6ca commit 495742e

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

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

+32-17
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ public static void cleanup() throws IOException, InterruptedException, Execution
167167
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
168168
requireEnvVar("GOOGLE_CLOUD_PROJECT");
169169

170-
deleteFirewallRuleIfNotDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE);
170+
if (!isFirewallRuleDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE)) {
171+
DeleteFirewallRule.deleteFirewallRule(PROJECT_ID, FIREWALL_RULE_CREATE);
172+
}
171173
compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_ENCRYPTED);
172174
compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME);
173175
compute.DeleteInstance.deleteInstance(PROJECT_ID, ZONE, MACHINE_NAME_LIST_INSTANCE);
@@ -287,43 +289,56 @@ public static String getBase64EncodedKey() {
287289
.encodeToString(stringBuilder.toString().getBytes(StandardCharsets.US_ASCII));
288290
}
289291

290-
public static void testListFirewallRules() throws IOException {
292+
public static void testListFirewallRules()
293+
throws IOException, ExecutionException, InterruptedException {
294+
final PrintStream out = System.out;
291295
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
292296
System.setOut(new PrintStream(stdOut));
293-
compute.ListFirewallRules.listFirewallRules(PROJECT_ID);
294-
assertThat(stdOut.toString()).contains(FIREWALL_RULE_CREATE);
297+
if (!isFirewallRuleDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE)) {
298+
compute.ListFirewallRules.listFirewallRules(PROJECT_ID);
299+
assertThat(stdOut.toString()).contains(FIREWALL_RULE_CREATE);
300+
}
301+
// Clear system output to not affect other tests.
302+
// Refrain from setting out to null.
295303
stdOut.close();
296-
System.setOut(null);
304+
System.setOut(out);
297305
}
298306

299307
public static void testPatchFirewallRule()
300308
throws IOException, InterruptedException, ExecutionException {
301-
try (FirewallsClient client = FirewallsClient.create()) {
302-
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
303-
System.setOut(new PrintStream(stdOut));
304-
Assert.assertEquals(1000, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority());
305-
compute.PatchFirewallRule.patchFirewallPriority(PROJECT_ID, FIREWALL_RULE_CREATE, 500);
306-
TimeUnit.SECONDS.sleep(5);
307-
Assert.assertEquals(500, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority());
308-
stdOut.close();
309-
System.setOut(null);
309+
final PrintStream out = System.out;
310+
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
311+
System.setOut(new PrintStream(stdOut));
312+
if (!isFirewallRuleDeletedByGceEnforcer(PROJECT_ID, FIREWALL_RULE_CREATE)) {
313+
try (FirewallsClient client = FirewallsClient.create()) {
314+
Assert.assertEquals(1000, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority());
315+
compute.PatchFirewallRule.patchFirewallPriority(PROJECT_ID, FIREWALL_RULE_CREATE, 500);
316+
TimeUnit.SECONDS.sleep(5);
317+
Assert.assertEquals(500, client.get(PROJECT_ID, FIREWALL_RULE_CREATE).getPriority());
318+
}
310319
}
320+
// Clear system output to not affect other tests.
321+
// Refrain from setting out to null as it will throw NullPointer in the subsequent tests.
322+
stdOut.close();
323+
System.setOut(out);
311324
}
312325

313-
public static void deleteFirewallRuleIfNotDeletedByGceEnforcer(String projectId,
326+
public static boolean isFirewallRuleDeletedByGceEnforcer(String projectId,
314327
String firewallRule) throws IOException, ExecutionException, InterruptedException {
315328
/* (**INTERNAL method**)
316329
This method will prevent test failure if the firewall rule was auto-deleted by GCE Enforcer.
317330
(Feel free to remove this method if not running on a Google-owned project.)
318331
*/
319332
try {
320333
GetFirewallRule.getFirewallRule(projectId, firewallRule);
321-
DeleteFirewallRule.deleteFirewallRule(projectId, firewallRule);
322334
} catch (NotFoundException e) {
323335
System.out.println("Rule already deleted ! ");
324-
} catch (InvalidArgumentException e) {
336+
return true;
337+
} catch (InvalidArgumentException | NullPointerException e) {
325338
System.out.println("Rule is not ready (probably being deleted).");
339+
return true;
326340
}
341+
return false;
327342
}
328343

329344
public static String getInstanceStatus(String instanceName) throws IOException {

0 commit comments

Comments
 (0)