use of bio.terra.cli.serialization.userfacing.UFWorkspace in project terra-cli by DataBiosphere.
the class BqDatasetControlled method resolve.
@Test
@DisplayName("resolve a controlled dataset")
void resolve() throws IOException {
workspaceCreator.login();
// `terra workspace set --id=$id --format=json`
UFWorkspace workspace = TestCommand.runAndParseCommandExpectSuccess(UFWorkspace.class, "workspace", "set", "--id=" + getWorkspaceId());
// `terra resource create bq-dataset --name=$name --dataset-id=$datasetId --format=json`
String name = "resolve";
String datasetId = randomDatasetId();
TestCommand.runCommandExpectSuccess("resource", "create", "bq-dataset", "--name=" + name, "--dataset-id=" + datasetId);
// `terra resource resolve --name=$name --format=json`
String resolved = TestCommand.runAndParseCommandExpectSuccess(String.class, "resource", "resolve", "--name=" + name);
assertEquals(workspace.googleProjectId + "." + datasetId, resolved, "default resolve includes [project id].[dataset id]");
// `terra resource resolve --name=$name --bq-path=PROJECT_ID_ONLY --format=json`
String resolvedProjectIdOnly = TestCommand.runAndParseCommandExpectSuccess(String.class, "resource", "resolve", "--name=" + name, "--bq-path=PROJECT_ID_ONLY");
assertEquals(workspace.googleProjectId, resolvedProjectIdOnly, "resolve with option PROJECT_ID_ONLY only includes the project id");
// `terra resource resolve --name=$name --bq-path=DATASET_ID_ONLY --format=json`
String resolvedDatasetIdOnly = TestCommand.runAndParseCommandExpectSuccess(String.class, "resource", "resolve", "--name=" + name, "--bq-path=DATASET_ID_ONLY");
assertEquals(datasetId, resolvedDatasetIdOnly, "resolve with option DATASET_ID_ONLY only includes the project id");
// `terra resources delete --name=$name`
TestCommand.runCommandExpectSuccess("resource", "delete", "--name=" + name, "--quiet");
}
use of bio.terra.cli.serialization.userfacing.UFWorkspace in project terra-cli by DataBiosphere.
the class BqDatasetControlled method createWithAllOptions.
@Test
@DisplayName("create a controlled dataset, specifying all options")
void createWithAllOptions() throws IOException {
workspaceCreator.login();
// `terra workspace set --id=$id --format=json`
UFWorkspace workspace = TestCommand.runAndParseCommandExpectSuccess(UFWorkspace.class, "workspace", "set", "--id=" + getWorkspaceId());
// `terra resources create bq-dataset --name=$name --dataset-id=$datasetId --access=$access
// --cloning=$cloning --description=$description --location=$location --format=json`
String name = "createWithAllOptions";
String datasetId = randomDatasetId();
AccessScope access = AccessScope.PRIVATE_ACCESS;
CloningInstructionsEnum cloning = CloningInstructionsEnum.DEFINITION;
String description = "\"create with all options\"";
String location = "us-east1";
UFBqDataset createdDataset = TestCommand.runAndParseCommandExpectSuccess(UFBqDataset.class, "resource", "create", "bq-dataset", "--name=" + name, "--dataset-id=" + datasetId, "--access=" + access, "--cloning=" + cloning, "--description=" + description, "--location=" + location);
// check that the properties match
assertEquals(name, createdDataset.name, "create output matches name");
assertEquals(workspace.googleProjectId, createdDataset.projectId, "create output matches project id");
assertEquals(datasetId, createdDataset.datasetId, "create output matches dataset id");
assertEquals(access, createdDataset.accessScope, "create output matches access");
assertEquals(cloning, createdDataset.cloningInstructions, "create output matches cloning");
assertEquals(description, createdDataset.description, "create output matches description");
assertEquals(workspaceCreator.email.toLowerCase(), createdDataset.privateUserName.toLowerCase(), "create output matches private user name");
Dataset createdDatasetOnCloud = ExternalBQDatasets.getBQClient(workspaceCreator.getCredentialsWithCloudPlatformScope()).getDataset(DatasetId.of(workspace.googleProjectId, datasetId));
assertNotNull(createdDatasetOnCloud, "looking up dataset via BQ API succeeded");
assertEquals(location, createdDatasetOnCloud.getLocation(), "dataset location matches create input");
// `terra resources describe --name=$name --format=json`
UFBqDataset describeResource = TestCommand.runAndParseCommandExpectSuccess(UFBqDataset.class, "resource", "describe", "--name=" + name);
// check that the properties match
assertEquals(name, describeResource.name, "describe resource output matches name");
assertEquals(datasetId, describeResource.datasetId, "describe resource output matches dataset id");
assertEquals(access, describeResource.accessScope, "describe output matches access");
assertEquals(cloning, describeResource.cloningInstructions, "describe output matches cloning");
assertEquals(description, describeResource.description, "describe output matches description");
assertEquals(workspaceCreator.email.toLowerCase(), describeResource.privateUserName.toLowerCase(), "describe output matches private user name");
// `terra resources delete --name=$name`
TestCommand.runCommandExpectSuccess("resource", "delete", "--name=" + name, "--quiet");
}
use of bio.terra.cli.serialization.userfacing.UFWorkspace in project terra-cli by DataBiosphere.
the class CleanupTestUserWorkspaces method deleteWorkspaces.
/**
* List all workspaces the test user has access to and try to delete each one that the test user
* owns. Deletes up to 100 workspaces at a time.
*/
private static void deleteWorkspaces(TestUser testUser, boolean isDryRun) throws IOException {
System.out.println("Deleting workspaces for testuser " + testUser.email);
TestContext.clearGlobalContextDir();
testUser.login();
// `terra workspace list`
List<UFWorkspace> listWorkspaces = TestCommand.runAndParseCommandExpectSuccess(new TypeReference<>() {
}, "workspace", "list", "--limit=100");
List<UFWorkspaceUser> listWorkspaceUsers;
for (UFWorkspace workspace : listWorkspaces) {
try {
// `terra workspace list-users`
listWorkspaceUsers = TestCommand.runAndParseCommandExpectSuccess(new TypeReference<>() {
}, "workspace", "list-users", "--workspace=" + workspace.id);
// find the user in the list
Optional<UFWorkspaceUser> workspaceUser = listWorkspaceUsers.stream().filter(user -> user.email.equalsIgnoreCase(testUser.email)).findAny();
// skip deleting if the test user is not an owner
if (workspaceUser.isEmpty() || !workspaceUser.get().roles.contains(WorkspaceUser.Role.OWNER)) {
System.out.println("Skip deleting workspace because test user is not an owner: id=" + workspace.id + ", testuser=" + testUser.email);
continue;
}
System.out.println("Deleting workspace: id=" + workspace.id + ", testuser=" + testUser.email);
if (!isDryRun) {
// `terra workspace delete --workspace=$id`
TestCommand.runCommandExpectSuccess("workspace", "delete", "--workspace=" + workspace.id, "--quiet");
System.out.println("Cleaned up workspace: id=" + workspace.id + ", testuser=" + testUser.email);
}
deletedWorkspaces.add(workspace.id);
} catch (Throwable ex) {
System.out.println("Error deleting workspace: id=" + workspace.id + ", testuser=" + testUser.email);
ex.printStackTrace();
failedWorkspaces.add(workspace.id);
continue;
}
}
// `terra auth revoke`
TestCommand.runCommandExpectSuccess("auth", "revoke");
}
use of bio.terra.cli.serialization.userfacing.UFWorkspace in project terra-cli by DataBiosphere.
the class PassthroughApps method gcloudConfigured.
@Test
@DisplayName("gcloud is configured with the workspace project and user")
void gcloudConfigured() throws IOException {
workspaceCreator.login();
// `terra workspace set --id=$id`
UFWorkspace workspace = TestCommand.runAndParseCommandExpectSuccess(UFWorkspace.class, "workspace", "set", "--id=" + getWorkspaceId());
// `terra gcloud config get-value project`
TestCommand.Result cmd = TestCommand.runCommand("gcloud", "config", "get-value", "project");
assertThat("gcloud project = workspace project", cmd.stdOut, CoreMatchers.containsString(workspace.googleProjectId));
// `terra gcloud config get-value account`
// Normally, when a human is involved, `gcloud auth login` or `gcloud auth
// activate-service-account` writes `account` to properties file at
// ~/.config/gcloud/configurations/config_default.
//
// However, there is no programmatic way to simulate this. `gcloud auth login` only supports
// interactive mode. `gcloud auth activate-service-account` requires --key-file param. Even if
// CLOUDSDK_AUTH_ACCESS_TOKEN is set, it wants --key-file param.
//
// When a human is involved, `account` in ~/.config/gcloud/configurations/config_default is
// used. During unit tests, that is not used. Authentication is done through other means, such
// as via CLOUDSDK_AUTH_ACCESS_TOKEN. So having test manually construct
// ~/.config/gcloud/configurations/config_default and then assert its contents, is not useful.
//
// If `gcloud auth login` or `gcloud auth activate-service-account` can ever be done
// programmatically (without key file), uncomment this test.
// cmd = TestCommand.runCommand("gcloud", "config", "get-value", "account");
// assertThat(
// "gcloud account = test user email",
// cmd.stdOut,
// CoreMatchers.containsString(Context.requireUser().getEmail()));
}
use of bio.terra.cli.serialization.userfacing.UFWorkspace in project terra-cli by DataBiosphere.
the class Workspace method createFailsWithoutSpendAccess.
@Test
@DisplayName("workspace create fails without spend profile access")
void createFailsWithoutSpendAccess() throws IOException {
// select a test user and login
TestUser testUser = TestUser.chooseTestUserWithoutSpendAccess();
testUser.login();
final String workspaceName = "bad-profile-6789";
// `terra workspace create`
String stdErr = TestCommand.runCommandExpectExitCode(1, "workspace", "create", "--name=" + workspaceName);
assertThat("error message includes spend profile unauthorized", stdErr, CoreMatchers.containsString("Accessing the spend profile failed. Ask an administrator to grant you access."));
// workspace was deleted
List<UFWorkspace> listWorkspaces = TestCommand.runAndParseCommandExpectSuccess(new TypeReference<>() {
}, "workspace", "list", "--limit=100");
assertFalse(listWorkspaces.stream().anyMatch(w -> workspaceName.equals(w.name)));
}
Aggregations