Search in sources :

Example 16 with UFWorkspace

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");
}
Also used : UFWorkspace(bio.terra.cli.serialization.userfacing.UFWorkspace) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 17 with UFWorkspace

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");
}
Also used : CloningInstructionsEnum(bio.terra.workspace.model.CloningInstructionsEnum) UFBqDataset(bio.terra.cli.serialization.userfacing.resource.UFBqDataset) UFBqDataset(bio.terra.cli.serialization.userfacing.resource.UFBqDataset) Dataset(com.google.cloud.bigquery.Dataset) UFWorkspace(bio.terra.cli.serialization.userfacing.UFWorkspace) AccessScope(bio.terra.workspace.model.AccessScope) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 18 with UFWorkspace

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");
}
Also used : UserActionableException(bio.terra.cli.exception.UserActionableException) WorkspaceUser(bio.terra.cli.businessobject.WorkspaceUser) TestCommand(harness.TestCommand) TestUser(harness.TestUser) IOException(java.io.IOException) UUID(java.util.UUID) ArrayList(java.util.ArrayList) List(java.util.List) UFWorkspaceUser(bio.terra.cli.serialization.userfacing.UFWorkspaceUser) UFWorkspace(bio.terra.cli.serialization.userfacing.UFWorkspace) Optional(java.util.Optional) TestContext(harness.TestContext) TypeReference(com.fasterxml.jackson.core.type.TypeReference) UFWorkspaceUser(bio.terra.cli.serialization.userfacing.UFWorkspaceUser) UFWorkspace(bio.terra.cli.serialization.userfacing.UFWorkspace) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Example 19 with UFWorkspace

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()));
}
Also used : TestCommand(harness.TestCommand) UFWorkspace(bio.terra.cli.serialization.userfacing.UFWorkspace) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 20 with UFWorkspace

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)));
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) CoreMatchers(org.hamcrest.CoreMatchers) Matchers.emptyString(org.hamcrest.Matchers.emptyString) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) TestCommand(harness.TestCommand) Result(harness.TestCommand.Result) TestUser(harness.TestUser) IsEqualIgnoringCase.equalToIgnoringCase(org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) UFWorkspace(bio.terra.cli.serialization.userfacing.UFWorkspace) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Tag(org.junit.jupiter.api.Tag) TypeReference(com.fasterxml.jackson.core.type.TypeReference) UFStatus(bio.terra.cli.serialization.userfacing.UFStatus) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) List(java.util.List) ClearContextUnit(harness.baseclasses.ClearContextUnit) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) UFWorkspaceLight(bio.terra.cli.serialization.userfacing.UFWorkspaceLight) UFWorkspace(bio.terra.cli.serialization.userfacing.UFWorkspace) Matchers.emptyString(org.hamcrest.Matchers.emptyString) Matchers.containsString(org.hamcrest.Matchers.containsString) TestUser(harness.TestUser) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

UFWorkspace (bio.terra.cli.serialization.userfacing.UFWorkspace)31 DisplayName (org.junit.jupiter.api.DisplayName)21 Test (org.junit.jupiter.api.Test)21 UFStatus (bio.terra.cli.serialization.userfacing.UFStatus)8 TestUser (harness.TestUser)7 Workspace (bio.terra.cli.businessobject.Workspace)5 UFWorkspaceLight (bio.terra.cli.serialization.userfacing.UFWorkspaceLight)5 WorkspaceOverride (bio.terra.cli.command.shared.options.WorkspaceOverride)4 TestCommand (harness.TestCommand)4 UFBqDataset (bio.terra.cli.serialization.userfacing.resource.UFBqDataset)3 UFGcsObject (bio.terra.cli.serialization.userfacing.resource.UFGcsObject)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Matchers.emptyString (org.hamcrest.Matchers.emptyString)3 UFAuthStatus (bio.terra.cli.serialization.userfacing.UFAuthStatus)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 Result (harness.TestCommand.Result)2 IOException (java.io.IOException)2 List (java.util.List)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2