-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(iam): add utility class for ServiceAccount test #10058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
iennae
merged 19 commits into
GoogleCloudPlatform:main
from
alarconesparza:alarconesparza-feat-service-account-util-10041
May 15, 2025
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3f45bf5
feat(iam): add utility class for ServiceAccount test
alarconesparza 0001e63
fix typo
alarconesparza 484951a
feat(iam): refactor ServiceAccountTests.java
alarconesparza 243a71c
make await functions match
alarconesparza fe3a1f6
add await for key disabling
alarconesparza dd239a3
use UUID instead of time for naming
alarconesparza 1ece256
remove unused env var
alarconesparza bfc6a6d
use Util functions in QuickstartTests
alarconesparza daa7898
use try/finally blocks to ensure clean up
alarconesparza b41f1a0
use coreMatchers.not to invert Matcher logic
alarconesparza f9e4143
split tests by sample so they are in their own files
alarconesparza edeb2ed
rename test functions after splitting them to avoid redundancy
alarconesparza 792f372
unify tests format and practices
alarconesparza b3870c2
fix testRenameServiceAccount
alarconesparza 48e10d9
fix testListServiceAccounts
alarconesparza 1a3d583
add waiting time for assertions after enable/disable operations
alarconesparza 90137d8
Undo changes to quickstartTest
alarconesparza fd86f71
fix lint issue quickstartTest
alarconesparza 243b94b
Ignore test until issue is resolved
alarconesparza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import com.google.cloud.testing.junit4.MultipleAttemptsRule; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class CreateServiceAccountIT { | ||
|
||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private ByteArrayOutputStream bout; | ||
private String serviceAccountName; | ||
private final PrintStream originalOut = System.out; | ||
|
||
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3); | ||
|
||
private static void requireEnvVar(String varName) { | ||
assertNotNull( | ||
System.getenv(varName), | ||
String.format("Environment variable '%s' is required to perform these tests.", varName)); | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); | ||
requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
} | ||
|
||
@Before | ||
public void beforeTest() { | ||
bout = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(bout)); | ||
|
||
// Set up test | ||
serviceAccountName = Util.generateServiceAccountName(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
// Cleanup test | ||
Util.tearDownTest_deleteServiceAccount(PROJECT_ID, serviceAccountName); | ||
|
||
System.out.flush(); | ||
System.setOut(originalOut); | ||
} | ||
|
||
@Test | ||
public void testCreateServiceAccount() throws IOException { | ||
// Act | ||
CreateServiceAccount.createServiceAccount(PROJECT_ID, serviceAccountName); | ||
|
||
// Assert | ||
assertThat(bout.toString()).contains("Created service account: " + serviceAccountName); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import com.google.cloud.testing.junit4.MultipleAttemptsRule; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class CreateServiceAccountKeyIT { | ||
|
||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private ByteArrayOutputStream bout; | ||
private String serviceAccountName; | ||
private final PrintStream originalOut = System.out; | ||
|
||
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3); | ||
|
||
private static void requireEnvVar(String varName) { | ||
assertNotNull( | ||
System.getenv(varName), | ||
String.format("Environment variable '%s' is required to perform these tests.", varName)); | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); | ||
requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
} | ||
|
||
@Before | ||
public void beforeTest() throws IOException, InterruptedException { | ||
bout = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(bout)); | ||
Comment on lines
+55
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
|
||
// Set up test | ||
serviceAccountName = Util.generateServiceAccountName(); | ||
Util.setUpTest_createServiceAccount(PROJECT_ID, serviceAccountName); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
// Cleanup test | ||
Util.tearDownTest_deleteServiceAccount(PROJECT_ID, serviceAccountName); | ||
|
||
System.out.flush(); | ||
System.setOut(originalOut); | ||
} | ||
|
||
@Test | ||
public void testCreateServiceAccountKey() throws IOException, InterruptedException { | ||
// Act | ||
CreateServiceAccountKey.createKey(PROJECT_ID, serviceAccountName); | ||
|
||
// Assert | ||
assertThat(bout.toString()).contains("Key created successfully"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import com.google.cloud.testing.junit4.MultipleAttemptsRule; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class DeleteServiceAccountIT { | ||
|
||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private ByteArrayOutputStream bout; | ||
private String serviceAccountName; | ||
private final PrintStream originalOut = System.out; | ||
|
||
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3); | ||
|
||
private static void requireEnvVar(String varName) { | ||
assertNotNull( | ||
System.getenv(varName), | ||
String.format("Environment variable '%s' is required to perform these tests.", varName)); | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); | ||
requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
} | ||
|
||
@Before | ||
public void beforeTest() throws IOException, InterruptedException { | ||
bout = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(bout)); | ||
Comment on lines
+55
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
|
||
// Set up test | ||
serviceAccountName = Util.generateServiceAccountName(); | ||
Util.setUpTest_createServiceAccount(PROJECT_ID, serviceAccountName); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
System.out.flush(); | ||
System.setOut(originalOut); | ||
} | ||
|
||
@Test | ||
public void testDeleteServiceAccount() throws IOException, InterruptedException { | ||
// Act | ||
DeleteServiceAccount.deleteServiceAccount(PROJECT_ID, serviceAccountName); | ||
|
||
// Assert | ||
assertThat(bout.toString()).contains("Deleted service account: " + serviceAccountName); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import com.google.cloud.testing.junit4.MultipleAttemptsRule; | ||
import com.google.iam.admin.v1.ServiceAccountKey; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class DeleteServiceAccountKeyIT { | ||
|
||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private ByteArrayOutputStream bout; | ||
private String serviceAccountName; | ||
private String serviceAccountKeyId; | ||
private final PrintStream originalOut = System.out; | ||
|
||
@Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3); | ||
|
||
private static void requireEnvVar(String varName) { | ||
assertNotNull( | ||
System.getenv(varName), | ||
String.format("Environment variable '%s' is required to perform these tests.", varName)); | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); | ||
requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
} | ||
|
||
@Before | ||
public void beforeTest() throws IOException, InterruptedException { | ||
bout = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(bout)); | ||
Comment on lines
+57
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
|
||
// Set up test | ||
serviceAccountName = Util.generateServiceAccountName(); | ||
Util.setUpTest_createServiceAccount(PROJECT_ID, serviceAccountName); | ||
ServiceAccountKey setupKey = | ||
Util.setUpTest_createServiceAccountKey(PROJECT_ID, serviceAccountName); | ||
serviceAccountKeyId = Util.getServiceAccountKeyIdFromKey(setupKey); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
// Cleanup test | ||
Util.tearDownTest_deleteServiceAccount(PROJECT_ID, serviceAccountName); | ||
|
||
System.out.flush(); | ||
System.setOut(originalOut); | ||
} | ||
|
||
@Test | ||
public void testDeleteServiceAccountKey() throws IOException, InterruptedException { | ||
// Act | ||
DeleteServiceAccountKey.deleteKey(PROJECT_ID, serviceAccountName, serviceAccountKeyId); | ||
|
||
// Assert | ||
String got = bout.toString(); | ||
assertThat(got).contains("Deleted key: " + serviceAccountKeyId); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is recommended to avoid capturing stdout for test validation due to high risk of ambiguity that invalidates test assertions