19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import static com .google .common .truth .Truth .assertWithMessage ;
21
21
22
+ import com .google .api .gax .rpc .NotFoundException ;
22
23
import com .google .cloud .compute .v1 .FirewallsClient ;
23
24
import com .google .cloud .compute .v1 .Instance ;
24
25
import com .google .cloud .compute .v1 .Instance .Status ;
@@ -62,7 +63,6 @@ public class SnippetsIT {
62
63
private static String BUCKET_NAME ;
63
64
private static String IMAGE_NAME ;
64
65
private static String FIREWALL_RULE_CREATE ;
65
- private static String FIREWALL_RULE_DELETE ;
66
66
private static String NETWORK_NAME ;
67
67
private static String RAW_KEY ;
68
68
@@ -88,7 +88,6 @@ public static void setUp() throws IOException, InterruptedException, ExecutionEx
88
88
BUCKET_NAME = "my-new-test-bucket" + UUID .randomUUID ();
89
89
IMAGE_NAME = "windows-sql-cloud" ;
90
90
FIREWALL_RULE_CREATE = "firewall-rule-" + UUID .randomUUID ();
91
- FIREWALL_RULE_DELETE = "firewall-rule-" + UUID .randomUUID ();
92
91
NETWORK_NAME = "global/networks/default" ;
93
92
RAW_KEY = getBase64EncodedKey ();
94
93
@@ -100,8 +99,11 @@ public static void setUp() throws IOException, InterruptedException, ExecutionEx
100
99
.createEncryptedInstance (PROJECT_ID , ZONE , MACHINE_NAME_ENCRYPTED , RAW_KEY );
101
100
TimeUnit .SECONDS .sleep (10 );
102
101
compute .CreateFirewallRule .createFirewall (PROJECT_ID , FIREWALL_RULE_CREATE , NETWORK_NAME );
103
- compute .CreateFirewallRule .createFirewall (PROJECT_ID , FIREWALL_RULE_DELETE , NETWORK_NAME );
104
102
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 ();
105
107
106
108
// Create a Google Cloud Storage bucket for UsageReports
107
109
Storage storage = StorageOptions .newBuilder ().setProjectId (PROJECT_ID ).build ().getService ();
@@ -118,7 +120,7 @@ public static void cleanup() throws IOException, InterruptedException, Execution
118
120
ByteArrayOutputStream stdOut = new ByteArrayOutputStream ();
119
121
System .setOut (new PrintStream (stdOut ));
120
122
121
- compute . DeleteFirewallRule . deleteFirewallRule (PROJECT_ID , FIREWALL_RULE_CREATE );
123
+ deleteFirewallRuleIfNotDeletedByGceEnforcer (PROJECT_ID , FIREWALL_RULE_CREATE );
122
124
compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , MACHINE_NAME_ENCRYPTED );
123
125
compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , MACHINE_NAME );
124
126
compute .DeleteInstance .deleteInstance (PROJECT_ID , ZONE , MACHINE_NAME_LIST_INSTANCE );
@@ -144,6 +146,43 @@ public static String getBase64EncodedKey() {
144
146
.encodeToString (stringBuilder .toString ().getBytes (StandardCharsets .US_ASCII ));
145
147
}
146
148
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
+
147
186
public static Status getInstanceStatus (String instanceName ) throws IOException {
148
187
try (InstancesClient instancesClient = InstancesClient .create ()) {
149
188
Instance response = instancesClient .get (PROJECT_ID , ZONE , instanceName );
@@ -247,27 +286,9 @@ public void testListImagesByPage() throws IOException {
247
286
public void testCreateFirewallRule () throws IOException {
248
287
// Assert that firewall rule has been created as part of the setup.
249
288
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 );
258
289
assertThat (stdOut .toString ()).contains (FIREWALL_RULE_CREATE );
259
290
}
260
291
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
-
271
292
@ Test
272
293
public void testInstanceOperations ()
273
294
throws IOException , ExecutionException , InterruptedException {
0 commit comments