Search in sources :

Example 1 with TestUserSpecification

use of bio.terra.testrunner.runner.config.TestUserSpecification in project terra-workspace-manager by DataBiosphere.

the class EnumerateResources method doSetup.

@Override
public void doSetup(List<TestUserSpecification> testUsers, WorkspaceApi workspaceApi) throws Exception {
    // initialize workspace
    super.doSetup(testUsers, workspaceApi);
    assertThat("There must be two test users defined for this test.", testUsers != null && testUsers.size() == 2);
    TestUserSpecification workspaceOwner = testUsers.get(0);
    workspaceReader = testUsers.get(1);
    // static assumptions
    assertThat(PAGE_SIZE * 2, lessThan(RESOURCE_COUNT));
    assertThat(PAGE_SIZE * 3, greaterThan(RESOURCE_COUNT));
    ApiClient ownerApiClient = ClientTestUtils.getClientForTestUser(workspaceOwner, server);
    ownerControlledGcpResourceApi = new ControlledGcpResourceApi(ownerApiClient);
    ownerReferencedGcpResourceApi = new ReferencedGcpResourceApi(ownerApiClient);
    ownerResourceApi = new ResourceApi(ownerApiClient);
    ApiClient readerApiClient = ClientTestUtils.getClientForTestUser(workspaceReader, server);
    readerResourceApi = new ResourceApi(readerApiClient);
    // Create a cloud context for the workspace
    CloudContextMaker.createGcpCloudContext(getWorkspaceId(), workspaceApi);
    // create the resources for the test
    logger.info("Creating {} resources", RESOURCE_COUNT);
    resourceList = MultiResourcesUtils.makeResources(ownerReferencedGcpResourceApi, ownerControlledGcpResourceApi, getWorkspaceId());
    logger.info("Created {} resources", resourceList.size());
}
Also used : ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) ResourceApi(bio.terra.workspace.api.ResourceApi) ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) TestUserSpecification(bio.terra.testrunner.runner.config.TestUserSpecification) ApiClient(bio.terra.workspace.client.ApiClient)

Example 2 with TestUserSpecification

use of bio.terra.testrunner.runner.config.TestUserSpecification in project terra-workspace-manager by DataBiosphere.

the class EnumerateJobs method doSetup.

@Override
public void doSetup(List<TestUserSpecification> testUsers, WorkspaceApi workspaceApi) throws Exception {
    // initialize workspace
    super.doSetup(testUsers, workspaceApi);
    TestUserSpecification workspaceOwner = testUsers.get(0);
    // If we like the alpha1 API for job enumeration, then we can maybe piggyback on
    // the EnumerateResources test instead of creating our own set.
    ApiClient ownerApiClient = ClientTestUtils.getClientForTestUser(workspaceOwner, server);
    ownerControlledGcpResourceApi = new ControlledGcpResourceApi(ownerApiClient);
    ownerReferencedGcpResourceApi = new ReferencedGcpResourceApi(ownerApiClient);
    alpha1Api = new Alpha1Api(ownerApiClient);
    // Create a cloud context for the workspace
    CloudContextMaker.createGcpCloudContext(getWorkspaceId(), workspaceApi);
    // create the resources for the test
    logger.info("Creating {} resources", RESOURCE_COUNT);
    resourceList = MultiResourcesUtils.makeResources(ownerReferencedGcpResourceApi, ownerControlledGcpResourceApi, getWorkspaceId());
    logger.info("Created {} resources", resourceList.size());
    logger.info("Cleaning up {} resources", resourceList.size());
    MultiResourcesUtils.cleanupResources(resourceList, ownerControlledGcpResourceApi, getWorkspaceId());
    logger.info("Cleaned up {} resources", resourceList.size());
}
Also used : ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) Alpha1Api(bio.terra.workspace.api.Alpha1Api) ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) TestUserSpecification(bio.terra.testrunner.runner.config.TestUserSpecification) ApiClient(bio.terra.workspace.client.ApiClient)

Example 3 with TestUserSpecification

use of bio.terra.testrunner.runner.config.TestUserSpecification in project terra-workspace-manager by DataBiosphere.

the class GetRoles method doSetup.

@Override
public void doSetup(List<TestUserSpecification> testUsers, WorkspaceApi workspaceApi) throws Exception {
    super.doSetup(testUsers, workspaceApi);
    for (TestUserSpecification testUser : testUsers) {
        logger.info("Granting role {} for user {} on workspace id {}", IAM_ROLE.toString(), testUser.userEmail, getWorkspaceId().toString());
        final var body = new GrantRoleRequestBody().memberEmail(testUser.userEmail);
        // grant the role
        workspaceApi.grantRole(body, getWorkspaceId(), IAM_ROLE);
    }
}
Also used : GrantRoleRequestBody(bio.terra.workspace.model.GrantRoleRequestBody) TestUserSpecification(bio.terra.testrunner.runner.config.TestUserSpecification)

Example 4 with TestUserSpecification

use of bio.terra.testrunner.runner.config.TestUserSpecification in project terra-workspace-manager by DataBiosphere.

the class RemoveUser method doSetup.

@Override
protected void doSetup(List<TestUserSpecification> testUsers, WorkspaceApi ownerWorkspaceApi) throws Exception {
    super.doSetup(testUsers, ownerWorkspaceApi);
    assertThat("There must be at least three test users defined for this test.", testUsers != null && testUsers.size() > 2);
    TestUserSpecification workspaceOwner = testUsers.get(0);
    this.privateResourceUser = testUsers.get(1);
    this.sharedResourceUser = testUsers.get(2);
    assertNotEquals(privateResourceUser.userEmail, sharedResourceUser.userEmail, "The two test users are distinct");
    // Add one user as a reader, and one as both a reader and writer.
    ownerWorkspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(privateResourceUser.userEmail), getWorkspaceId(), IamRole.READER);
    ownerWorkspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(privateResourceUser.userEmail), getWorkspaceId(), IamRole.WRITER);
    ownerWorkspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(sharedResourceUser.userEmail), getWorkspaceId(), IamRole.WRITER);
    // Create a GCP cloud context.
    projectId = CloudContextMaker.createGcpCloudContext(getWorkspaceId(), ownerWorkspaceApi);
    // Create a shared GCS bucket with one object inside.
    ControlledGcpResourceApi ownerResourceApi = ClientTestUtils.getControlledGcpResourceClient(workspaceOwner, server);
    String sharedBucketName = BUCKET_PREFIX + UUID.randomUUID();
    sharedBucket = makeControlledGcsBucketUserShared(ownerResourceApi, getWorkspaceId(), sharedBucketName, CloningInstructionsEnum.NOTHING);
    GcsBucketUtils.addFileToBucket(sharedBucket, workspaceOwner, projectId);
    // Create a private GCS bucket for privateResourceUser with one object inside.
    String privateBucketName = BUCKET_PREFIX + UUID.randomUUID();
    ControlledGcpResourceApi privateUserResourceApi = ClientTestUtils.getControlledGcpResourceClient(privateResourceUser, server);
    privateBucket = makeControlledGcsBucketUserPrivate(privateUserResourceApi, getWorkspaceId(), privateBucketName, CloningInstructionsEnum.NOTHING);
    GcsBucketUtils.addFileToBucket(privateBucket, privateResourceUser, projectId);
    // Create a private BQ dataset for privateResourceUser and populate it.
    String datasetResourceName = RandomStringUtils.randomAlphabetic(8).toLowerCase();
    privateDataset = BqDatasetUtils.makeControlledBigQueryDatasetUserPrivate(privateUserResourceApi, getWorkspaceId(), datasetResourceName, null, CloningInstructionsEnum.NOTHING);
    BqDatasetUtils.populateBigQueryDataset(privateDataset, privateResourceUser, projectId);
    // Create a private notebook for privateResourceUser.
    String notebookInstanceId = RandomStringUtils.randomAlphabetic(8).toLowerCase();
    privateNotebook = NotebookUtils.makeControlledNotebookUserPrivate(getWorkspaceId(), notebookInstanceId, /*location=*/
    null, privateUserResourceApi);
}
Also used : GrantRoleRequestBody(bio.terra.workspace.model.GrantRoleRequestBody) ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) TestUserSpecification(bio.terra.testrunner.runner.config.TestUserSpecification)

Example 5 with TestUserSpecification

use of bio.terra.testrunner.runner.config.TestUserSpecification in project terra-workspace-manager by DataBiosphere.

the class EnumerateResources method doUserJourney.

@Override
public void doUserJourney(TestUserSpecification testUser, WorkspaceApi workspaceApi) throws Exception {
    // Add second user to the workspace as a reader
    workspaceApi.grantRole(new GrantRoleRequestBody().memberEmail(workspaceReader.userEmail), getWorkspaceId(), IamRole.READER);
    // Case 1: fetch all
    ResourceList enumList = ownerResourceApi.enumerateResources(getWorkspaceId(), 0, RESOURCE_COUNT, null, null);
    logResult("fetchall", enumList);
    // Make sure we got all of the expected ids
    matchFullResourceList(enumList.getResources());
    // Repeat case 1 as the workspace reader.
    // As this is the first operation after modifying workspace IAM groups, retry here to compensate
    // for the delay in GCP IAM propagation.
    ResourceList readerEnumList = ClientTestUtils.getWithRetryOnException(() -> readerResourceApi.enumerateResources(getWorkspaceId(), 0, RESOURCE_COUNT, null, null));
    logResult("fetchall reader", readerEnumList);
    matchFullResourceList(readerEnumList.getResources());
    // Case 2: fetch by pages
    ResourceList page1List = ownerResourceApi.enumerateResources(getWorkspaceId(), 0, PAGE_SIZE, null, null);
    logResult("page1", page1List);
    assertThat(page1List.getResources().size(), equalTo(PAGE_SIZE));
    ResourceList page2List = ownerResourceApi.enumerateResources(getWorkspaceId(), PAGE_SIZE, PAGE_SIZE, null, null);
    logResult("page2", page2List);
    assertThat(page2List.getResources().size(), equalTo(PAGE_SIZE));
    ResourceList page3List = ownerResourceApi.enumerateResources(getWorkspaceId(), 2 * PAGE_SIZE, PAGE_SIZE, null, null);
    logResult("page3", page3List);
    assertThat(page3List.getResources().size(), lessThan(PAGE_SIZE));
    List<ResourceDescription> descriptionList = new ArrayList<>();
    descriptionList.addAll(page1List.getResources());
    descriptionList.addAll(page2List.getResources());
    descriptionList.addAll(page3List.getResources());
    matchFullResourceList(descriptionList);
    // Case 3: no results if offset is too high
    ResourceList enumEmptyList = ownerResourceApi.enumerateResources(getWorkspaceId(), 10 * PAGE_SIZE, PAGE_SIZE, null, null);
    assertThat(enumEmptyList.getResources().size(), equalTo(0));
    // Case 4: filter by resource type
    ResourceList buckets = ownerResourceApi.enumerateResources(getWorkspaceId(), 0, RESOURCE_COUNT, ResourceType.GCS_BUCKET, null);
    logResult("buckets", buckets);
    long expectedBuckets = resourceList.stream().filter(m -> m.getResourceType() == ResourceType.GCS_BUCKET).count();
    logger.info("Counted {} buckets created", expectedBuckets);
    // Note - assertThat exits out on an int -> long compare, so just don't do that.
    long actualBuckets = buckets.getResources().size();
    assertThat(actualBuckets, equalTo(expectedBuckets));
    // Case 5: filter by stewardship type
    ResourceList referencedList = ownerResourceApi.enumerateResources(getWorkspaceId(), 0, RESOURCE_COUNT, null, StewardshipType.REFERENCED);
    logResult("referenced", referencedList);
    long expectedReferenced = resourceList.stream().filter(m -> m.getStewardshipType() == StewardshipType.REFERENCED).count();
    logger.info("Counted {} referenced created", expectedReferenced);
    long actualReferenced = referencedList.getResources().size();
    assertThat(actualReferenced, equalTo(expectedReferenced));
    // Case 6: filter by resource and stewardship
    ResourceList controlledBucketList = ownerResourceApi.enumerateResources(getWorkspaceId(), 0, RESOURCE_COUNT, ResourceType.GCS_BUCKET, StewardshipType.CONTROLLED);
    logResult("controlledBucket", controlledBucketList);
    long expectedControlledBuckets = resourceList.stream().filter(m -> (m.getStewardshipType() == StewardshipType.CONTROLLED && m.getResourceType() == ResourceType.GCS_BUCKET)).count();
    logger.info("Counted {} controlled buckets created", expectedControlledBuckets);
    long actualControlledBuckets = controlledBucketList.getResources().size();
    assertThat(actualControlledBuckets, equalTo(expectedControlledBuckets));
    // Case 7: validate error on invalid pagination params
    ApiException invalidPaginationException = assertThrows(ApiException.class, () -> ownerResourceApi.enumerateResources(getWorkspaceId(), -11, 2, ResourceType.GCS_BUCKET, StewardshipType.CONTROLLED));
    assertThat(invalidPaginationException.getMessage(), containsString("Invalid pagination"));
    invalidPaginationException = assertThrows(ApiException.class, () -> ownerResourceApi.enumerateResources(getWorkspaceId(), 0, 0, ResourceType.GCS_BUCKET, StewardshipType.CONTROLLED));
    assertThat(invalidPaginationException.getMessage(), containsString("Invalid pagination"));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ControlledGcpResourceApi(bio.terra.workspace.api.ControlledGcpResourceApi) ClientTestUtils(scripts.utils.ClientTestUtils) CloudContextMaker(scripts.utils.CloudContextMaker) LoggerFactory(org.slf4j.LoggerFactory) ResourceList(bio.terra.workspace.model.ResourceList) ArrayList(java.util.ArrayList) WorkspaceAllocateTestScriptBase(scripts.utils.WorkspaceAllocateTestScriptBase) Matchers.lessThan(org.hamcrest.Matchers.lessThan) MultiResourcesUtils(scripts.utils.MultiResourcesUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ReferencedGcpResourceApi(bio.terra.workspace.api.ReferencedGcpResourceApi) StewardshipType(bio.terra.workspace.model.StewardshipType) ControlledResourceMetadata(bio.terra.workspace.model.ControlledResourceMetadata) WorkspaceApi(bio.terra.workspace.api.WorkspaceApi) ResourceMetadata(bio.terra.workspace.model.ResourceMetadata) ResourceType(bio.terra.workspace.model.ResourceType) Logger(org.slf4j.Logger) ApiException(bio.terra.workspace.client.ApiException) ApiClient(bio.terra.workspace.client.ApiClient) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) IamRole(bio.terra.workspace.model.IamRole) List(java.util.List) TestUserSpecification(bio.terra.testrunner.runner.config.TestUserSpecification) Matchers.equalTo(org.hamcrest.Matchers.equalTo) GrantRoleRequestBody(bio.terra.workspace.model.GrantRoleRequestBody) ResourceDescription(bio.terra.workspace.model.ResourceDescription) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.containsString(org.hamcrest.Matchers.containsString) ResourceApi(bio.terra.workspace.api.ResourceApi) ResourceList(bio.terra.workspace.model.ResourceList) GrantRoleRequestBody(bio.terra.workspace.model.GrantRoleRequestBody) ResourceDescription(bio.terra.workspace.model.ResourceDescription) ArrayList(java.util.ArrayList) ApiException(bio.terra.workspace.client.ApiException)

Aggregations

TestUserSpecification (bio.terra.testrunner.runner.config.TestUserSpecification)9 ControlledGcpResourceApi (bio.terra.workspace.api.ControlledGcpResourceApi)6 ReferencedGcpResourceApi (bio.terra.workspace.api.ReferencedGcpResourceApi)4 GrantRoleRequestBody (bio.terra.workspace.model.GrantRoleRequestBody)4 ApiClient (bio.terra.workspace.client.ApiClient)3 List (java.util.List)3 ResourceApi (bio.terra.workspace.api.ResourceApi)2 ApiException (bio.terra.workspace.client.ApiException)2 UUID (java.util.UUID)2 Collectors (java.util.stream.Collectors)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 KubernetesClientUtils (bio.terra.testrunner.common.utils.KubernetesClientUtils)1 DisruptiveScript (bio.terra.testrunner.runner.DisruptiveScript)1 Alpha1Api (bio.terra.workspace.api.Alpha1Api)1 WorkspaceApi (bio.terra.workspace.api.WorkspaceApi)1 AccessScope (bio.terra.workspace.model.AccessScope)1 CloningInstructionsEnum (bio.terra.workspace.model.CloningInstructionsEnum)1 ControlledResourceCommonFields (bio.terra.workspace.model.ControlledResourceCommonFields)1