@@ -167,7 +167,9 @@ public static void cleanup() throws IOException, InterruptedException, Execution
167
167
requireEnvVar ("GOOGLE_APPLICATION_CREDENTIALS" );
168
168
requireEnvVar ("GOOGLE_CLOUD_PROJECT" );
169
169
170
- deleteFirewallRuleIfNotDeletedByGceEnforcer (PROJECT_ID , FIREWALL_RULE_CREATE );
170
+ if (!isFirewallRuleDeletedByGceEnforcer (PROJECT_ID , FIREWALL_RULE_CREATE )) {
171
+ DeleteFirewallRule .deleteFirewallRule (PROJECT_ID , FIREWALL_RULE_CREATE );
172
+ }
171
173
compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , MACHINE_NAME_ENCRYPTED );
172
174
compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , MACHINE_NAME );
173
175
compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , MACHINE_NAME_LIST_INSTANCE );
@@ -287,43 +289,56 @@ public static String getBase64EncodedKey() {
287
289
.encodeToString (stringBuilder .toString ().getBytes (StandardCharsets .US_ASCII ));
288
290
}
289
291
290
- public static void testListFirewallRules () throws IOException {
292
+ public static void testListFirewallRules ()
293
+ throws IOException , ExecutionException , InterruptedException {
294
+ final PrintStream out = System .out ;
291
295
ByteArrayOutputStream stdOut = new ByteArrayOutputStream ();
292
296
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.
295
303
stdOut .close ();
296
- System .setOut (null );
304
+ System .setOut (out );
297
305
}
298
306
299
307
public static void testPatchFirewallRule ()
300
308
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
+ }
310
319
}
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 );
311
324
}
312
325
313
- public static void deleteFirewallRuleIfNotDeletedByGceEnforcer (String projectId ,
326
+ public static boolean isFirewallRuleDeletedByGceEnforcer (String projectId ,
314
327
String firewallRule ) throws IOException , ExecutionException , InterruptedException {
315
328
/* (**INTERNAL method**)
316
329
This method will prevent test failure if the firewall rule was auto-deleted by GCE Enforcer.
317
330
(Feel free to remove this method if not running on a Google-owned project.)
318
331
*/
319
332
try {
320
333
GetFirewallRule .getFirewallRule (projectId , firewallRule );
321
- DeleteFirewallRule .deleteFirewallRule (projectId , firewallRule );
322
334
} catch (NotFoundException e ) {
323
335
System .out .println ("Rule already deleted ! " );
324
- } catch (InvalidArgumentException e ) {
336
+ return true ;
337
+ } catch (InvalidArgumentException | NullPointerException e ) {
325
338
System .out .println ("Rule is not ready (probably being deleted)." );
339
+ return true ;
326
340
}
341
+ return false ;
327
342
}
328
343
329
344
public static String getInstanceStatus (String instanceName ) throws IOException {
0 commit comments