Skip to content

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions iam/snippets/src/test/java/DenyIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
public class DenyIT {

private static final String PROJECT_ID = System.getenv("IAM_PROJECT_ID");
private static final String GOOGLE_APPLICATION_CREDENTIALS = System.getenv("IAM_CREDENTIALS");
private static String POLICY_ID;

private ByteArrayOutputStream stdOut;
Expand All @@ -55,7 +54,6 @@ public static void setUp()
final PrintStream out = System.out;
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
requireEnvVar("IAM_CREDENTIALS");
requireEnvVar("IAM_PROJECT_ID");

POLICY_ID = "limit-project-deletion" + UUID.randomUUID();
Expand Down
37 changes: 8 additions & 29 deletions iam/snippets/src/test/java/QuickstartTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
import static org.junit.Assert.assertNotNull;

import com.google.cloud.iam.admin.v1.IAMClient;
import com.google.iam.admin.v1.CreateServiceAccountRequest;
import com.google.iam.admin.v1.DeleteServiceAccountRequest;
import com.google.iam.admin.v1.ProjectName;
import com.google.iam.admin.v1.ServiceAccount;
import com.google.iam.admin.v1.ServiceAccountName;
import com.google.iam.v1.Binding;
import com.google.iam.v1.Policy;
import java.io.IOException;
Expand All @@ -41,13 +37,13 @@
public class QuickstartTests {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String SERVICE_ACCOUNT =
"iam-test-account-" + UUID.randomUUID().toString().split("-")[0];
"iam-test-account-" + UUID.randomUUID().toString().split("-")[0];
private String serviceAccountEmail;

private static void requireEnvVar(String varName) {
assertNotNull(
System.getenv(varName),
String.format("Environment variable '%s' is required to perform these tests.", varName));
System.getenv(varName),
String.format("Environment variable '%s' is required to perform these tests.", varName));
}

@BeforeClass
Expand All @@ -58,33 +54,16 @@ public static void checkRequirements() {

// Creates a service account to use during the test
@Before
public void setUp() throws IOException {
try (IAMClient iamClient = IAMClient.create()) {
ServiceAccount serviceAccount = ServiceAccount
.newBuilder()
.setDisplayName("test-display-name")
.build();
CreateServiceAccountRequest request = CreateServiceAccountRequest.newBuilder()
.setName(ProjectName.of(PROJECT_ID).toString())
.setAccountId(SERVICE_ACCOUNT)
.setServiceAccount(serviceAccount)
.build();

serviceAccount = iamClient.createServiceAccount(request);
serviceAccountEmail = serviceAccount.getEmail();
}
public void setUp() throws IOException, InterruptedException {
ServiceAccount serviceAccount =
Util.setUpTest_createServiceAccount(PROJECT_ID, SERVICE_ACCOUNT);
serviceAccountEmail = serviceAccount.getEmail();
}

// Deletes the service account used in the test.
@After
public void tearDown() throws IOException {
try (IAMClient iamClient = IAMClient.create()) {
String serviceAccountName = SERVICE_ACCOUNT + "@" + PROJECT_ID + ".iam.gserviceaccount.com";
DeleteServiceAccountRequest request = DeleteServiceAccountRequest.newBuilder()
.setName(ServiceAccountName.of(PROJECT_ID, serviceAccountName).toString())
.build();
iamClient.deleteServiceAccount(request);
}
Util.tearDownTest_deleteServiceAccount(PROJECT_ID, SERVICE_ACCOUNT);
}

@Test
Expand Down
2 changes: 0 additions & 2 deletions iam/snippets/src/test/java/RoleIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class RoleIT {
private ByteArrayOutputStream bout;

private static final String projectId = System.getenv("IAM_PROJECT_ID");
private static final String GOOGLE_APPLICATION_CREDENTIALS = System.getenv("IAM_CREDENTIALS");
private static final String _suffix = UUID.randomUUID().toString().substring(0, 6);
private static final String roleId = "testRole" + _suffix;
private static final String roleName = "projects/" + projectId + "/roles/" + roleId;
Expand All @@ -57,7 +56,6 @@ public static void checkRequirements() throws IOException {
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();

requireEnvVar("IAM_PROJECT_ID");
requireEnvVar("IAM_CREDENTIALS");

stdOut.close();
System.setOut(out);
Expand Down
Loading