Skip to content

Commit add7429

Browse files
feat(iam): add utility class for ServiceAccount test (GoogleCloudPlatform#10058)
* feat(iam): add utility class for ServiceAccount test * fix typo * feat(iam): refactor ServiceAccountTests.java Stop reusing resources from other tests. Use Util functions to set up and tear down resources. * make await functions match * add await for key disabling * use UUID instead of time for naming * remove unused env var * use Util functions in QuickstartTests * use try/finally blocks to ensure clean up * use coreMatchers.not to invert Matcher logic * split tests by sample so they are in their own files * rename test functions after splitting them to avoid redundancy * unify tests format and practices Change assertions to test sample instead of api calls Use google assertThat Flush System.out * fix testRenameServiceAccount * fix testListServiceAccounts * add waiting time for assertions after enable/disable operations * Undo changes to quickstartTest * fix lint issue quickstartTest * Ignore test until issue is resolved GoogleCloudPlatform#10082
1 parent 2dfeea4 commit add7429

18 files changed

+1390
-229
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Copyright 2025 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import static com.google.common.truth.Truth.assertThat;
17+
import static org.junit.Assert.assertNotNull;
18+
19+
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
20+
import java.io.ByteArrayOutputStream;
21+
import java.io.IOException;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.BeforeClass;
26+
import org.junit.Rule;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
@RunWith(JUnit4.class)
32+
public class CreateServiceAccountIT {
33+
34+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
35+
private ByteArrayOutputStream bout;
36+
private String serviceAccountName;
37+
private final PrintStream originalOut = System.out;
38+
39+
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
40+
41+
private static void requireEnvVar(String varName) {
42+
assertNotNull(
43+
System.getenv(varName),
44+
String.format("Environment variable '%s' is required to perform these tests.", varName));
45+
}
46+
47+
@BeforeClass
48+
public static void checkRequirements() {
49+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
50+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
51+
}
52+
53+
@Before
54+
public void beforeTest() {
55+
bout = new ByteArrayOutputStream();
56+
System.setOut(new PrintStream(bout));
57+
58+
// Set up test
59+
serviceAccountName = Util.generateServiceAccountName();
60+
}
61+
62+
@After
63+
public void tearDown() throws IOException {
64+
// Cleanup test
65+
Util.tearDownTest_deleteServiceAccount(PROJECT_ID, serviceAccountName);
66+
67+
System.out.flush();
68+
System.setOut(originalOut);
69+
}
70+
71+
@Test
72+
public void testCreateServiceAccount() throws IOException {
73+
// Act
74+
CreateServiceAccount.createServiceAccount(PROJECT_ID, serviceAccountName);
75+
76+
// Assert
77+
assertThat(bout.toString()).contains("Created service account: " + serviceAccountName);
78+
}
79+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* Copyright 2025 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import static com.google.common.truth.Truth.assertThat;
17+
import static org.junit.Assert.assertNotNull;
18+
19+
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
20+
import java.io.ByteArrayOutputStream;
21+
import java.io.IOException;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.BeforeClass;
26+
import org.junit.Rule;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
@RunWith(JUnit4.class)
32+
public class CreateServiceAccountKeyIT {
33+
34+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
35+
private ByteArrayOutputStream bout;
36+
private String serviceAccountName;
37+
private final PrintStream originalOut = System.out;
38+
39+
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
40+
41+
private static void requireEnvVar(String varName) {
42+
assertNotNull(
43+
System.getenv(varName),
44+
String.format("Environment variable '%s' is required to perform these tests.", varName));
45+
}
46+
47+
@BeforeClass
48+
public static void checkRequirements() {
49+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
50+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
51+
}
52+
53+
@Before
54+
public void beforeTest() throws IOException, InterruptedException {
55+
bout = new ByteArrayOutputStream();
56+
System.setOut(new PrintStream(bout));
57+
58+
// Set up test
59+
serviceAccountName = Util.generateServiceAccountName();
60+
Util.setUpTest_createServiceAccount(PROJECT_ID, serviceAccountName);
61+
}
62+
63+
@After
64+
public void tearDown() throws IOException {
65+
// Cleanup test
66+
Util.tearDownTest_deleteServiceAccount(PROJECT_ID, serviceAccountName);
67+
68+
System.out.flush();
69+
System.setOut(originalOut);
70+
}
71+
72+
@Test
73+
public void testCreateServiceAccountKey() throws IOException, InterruptedException {
74+
// Act
75+
CreateServiceAccountKey.createKey(PROJECT_ID, serviceAccountName);
76+
77+
// Assert
78+
assertThat(bout.toString()).contains("Key created successfully");
79+
}
80+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* Copyright 2025 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import static com.google.common.truth.Truth.assertThat;
17+
import static org.junit.Assert.assertNotNull;
18+
19+
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
20+
import java.io.ByteArrayOutputStream;
21+
import java.io.IOException;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.BeforeClass;
26+
import org.junit.Rule;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
@RunWith(JUnit4.class)
32+
public class DeleteServiceAccountIT {
33+
34+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
35+
private ByteArrayOutputStream bout;
36+
private String serviceAccountName;
37+
private final PrintStream originalOut = System.out;
38+
39+
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
40+
41+
private static void requireEnvVar(String varName) {
42+
assertNotNull(
43+
System.getenv(varName),
44+
String.format("Environment variable '%s' is required to perform these tests.", varName));
45+
}
46+
47+
@BeforeClass
48+
public static void checkRequirements() {
49+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
50+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
51+
}
52+
53+
@Before
54+
public void beforeTest() throws IOException, InterruptedException {
55+
bout = new ByteArrayOutputStream();
56+
System.setOut(new PrintStream(bout));
57+
58+
// Set up test
59+
serviceAccountName = Util.generateServiceAccountName();
60+
Util.setUpTest_createServiceAccount(PROJECT_ID, serviceAccountName);
61+
}
62+
63+
@After
64+
public void tearDown() throws IOException {
65+
System.out.flush();
66+
System.setOut(originalOut);
67+
}
68+
69+
@Test
70+
public void testDeleteServiceAccount() throws IOException, InterruptedException {
71+
// Act
72+
DeleteServiceAccount.deleteServiceAccount(PROJECT_ID, serviceAccountName);
73+
74+
// Assert
75+
assertThat(bout.toString()).contains("Deleted service account: " + serviceAccountName);
76+
}
77+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Copyright 2025 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import static com.google.common.truth.Truth.assertThat;
17+
import static org.junit.Assert.assertNotNull;
18+
19+
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
20+
import com.google.iam.admin.v1.ServiceAccountKey;
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.BeforeClass;
27+
import org.junit.Rule;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
@RunWith(JUnit4.class)
33+
public class DeleteServiceAccountKeyIT {
34+
35+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
36+
private ByteArrayOutputStream bout;
37+
private String serviceAccountName;
38+
private String serviceAccountKeyId;
39+
private final PrintStream originalOut = System.out;
40+
41+
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
42+
43+
private static void requireEnvVar(String varName) {
44+
assertNotNull(
45+
System.getenv(varName),
46+
String.format("Environment variable '%s' is required to perform these tests.", varName));
47+
}
48+
49+
@BeforeClass
50+
public static void checkRequirements() {
51+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
52+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
53+
}
54+
55+
@Before
56+
public void beforeTest() throws IOException, InterruptedException {
57+
bout = new ByteArrayOutputStream();
58+
System.setOut(new PrintStream(bout));
59+
60+
// Set up test
61+
serviceAccountName = Util.generateServiceAccountName();
62+
Util.setUpTest_createServiceAccount(PROJECT_ID, serviceAccountName);
63+
ServiceAccountKey setupKey =
64+
Util.setUpTest_createServiceAccountKey(PROJECT_ID, serviceAccountName);
65+
serviceAccountKeyId = Util.getServiceAccountKeyIdFromKey(setupKey);
66+
}
67+
68+
@After
69+
public void tearDown() throws IOException {
70+
// Cleanup test
71+
Util.tearDownTest_deleteServiceAccount(PROJECT_ID, serviceAccountName);
72+
73+
System.out.flush();
74+
System.setOut(originalOut);
75+
}
76+
77+
@Test
78+
public void testDeleteServiceAccountKey() throws IOException, InterruptedException {
79+
// Act
80+
DeleteServiceAccountKey.deleteKey(PROJECT_ID, serviceAccountName, serviceAccountKeyId);
81+
82+
// Assert
83+
String got = bout.toString();
84+
assertThat(got).contains("Deleted key: " + serviceAccountKeyId);
85+
}
86+
}

iam/snippets/src/test/java/DenyIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
public class DenyIT {
3838

3939
private static final String PROJECT_ID = System.getenv("IAM_PROJECT_ID");
40-
private static final String GOOGLE_APPLICATION_CREDENTIALS = System.getenv("IAM_CREDENTIALS");
4140
private static String POLICY_ID;
4241

4342
private ByteArrayOutputStream stdOut;
@@ -55,7 +54,6 @@ public static void setUp()
5554
final PrintStream out = System.out;
5655
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
5756
System.setOut(new PrintStream(stdOut));
58-
requireEnvVar("IAM_CREDENTIALS");
5957
requireEnvVar("IAM_PROJECT_ID");
6058

6159
POLICY_ID = "limit-project-deletion" + UUID.randomUUID();

0 commit comments

Comments
 (0)