Search in sources :

Example 1 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by caskdata.

the class ProfileHttpHandlerTest method testPutAndGetProfileWithTotalCPUSImplementedProvisioner.

@Test
public void testPutAndGetProfileWithTotalCPUSImplementedProvisioner() throws Exception {
    // put a profile with the mock provisioner with total cpus method implemented
    Profile expected = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo(MockProvisionerWithCpus.NAME, PROPERTY_SUMMARIES));
    ProfileId expectedProfileId = NamespaceId.DEFAULT.profile(expected.getName());
    putProfile(expectedProfileId, expected, HttpURLConnection.HTTP_OK);
    // get the profile
    Profile actual = getProfile(expectedProfileId, HttpURLConnection.HTTP_OK).get();
    expected.getProvisioner().setTotalProcessingCpusLabel(MockProvisionerWithCpus.TEST_LABEL);
    Assert.assertEquals(expected, actual);
    disableProfile(expectedProfileId, HttpURLConnection.HTTP_OK);
    deleteProfile(expectedProfileId, HttpURLConnection.HTTP_OK);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 2 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by caskdata.

the class ProfileHttpHandlerTest method testPutAndDeleteProfiles.

@Test
public void testPutAndDeleteProfiles() throws Exception {
    Profile invalidProfile = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo("nonExisting", PROPERTY_SUMMARIES));
    // adding a profile with non-existing provisioner should get a 400
    putProfile(NamespaceId.DEFAULT.profile(invalidProfile.getName()), invalidProfile, HttpURLConnection.HTTP_BAD_REQUEST);
    // put a profile with the mock provisioner
    Profile expected = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
    ProfileId expectedProfileId = NamespaceId.DEFAULT.profile(expected.getName());
    putProfile(expectedProfileId, expected, HttpURLConnection.HTTP_OK);
    // get the profile
    Profile actual = getProfile(expectedProfileId, HttpURLConnection.HTTP_OK).get();
    Assert.assertEquals(expected, actual);
    // list all profiles, should get 2 profiles
    List<Profile> profiles = listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
    Set<Profile> expectedList = ImmutableSet.of(Profile.NATIVE, expected);
    Assert.assertEquals(expectedList.size(), profiles.size());
    Assert.assertEquals(expectedList, new HashSet<>(profiles));
    // adding the same profile should still succeed
    putProfile(expectedProfileId, expected, HttpURLConnection.HTTP_OK);
    // get non-existing profile should get a 404
    deleteProfile(NamespaceId.DEFAULT.profile("nonExisting"), HttpURLConnection.HTTP_NOT_FOUND);
    // delete the profile should fail first time since it is by default enabled
    deleteProfile(expectedProfileId, HttpURLConnection.HTTP_CONFLICT);
    // disable the profile then delete should work
    disableProfile(expectedProfileId, HttpURLConnection.HTTP_OK);
    deleteProfile(expectedProfileId, HttpURLConnection.HTTP_OK);
    Assert.assertEquals(Collections.emptyList(), listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK));
    // if given some unrelated json, it should return a 400 instead of 500
    ProvisionerSpecification spec = new MockProvisioner().getSpec();
    ProvisionerDetail test = new ProvisionerDetail(spec.getName(), spec.getLabel(), spec.getDescription(), new ArrayList<>(), null, null, false);
    putProfile(NamespaceId.DEFAULT.profile(test.getName()), test, HttpURLConnection.HTTP_BAD_REQUEST);
    doAs(READ_ONLY_USER_NAME, () -> {
        putProfile(expectedProfileId, expected, HttpURLConnection.HTTP_FORBIDDEN);
        disableProfile(expectedProfileId, HttpURLConnection.HTTP_FORBIDDEN);
        enableProfile(expectedProfileId, HttpURLConnection.HTTP_FORBIDDEN);
        deleteProfile(expectedProfileId, HttpURLConnection.HTTP_FORBIDDEN);
    });
    doAs(READ_WRITE_USER_NAME, () -> {
        putProfile(expectedProfileId, expected, HttpURLConnection.HTTP_OK);
        disableProfile(expectedProfileId, HttpURLConnection.HTTP_OK);
        enableProfile(expectedProfileId, HttpURLConnection.HTTP_OK);
        disableProfile(expectedProfileId, HttpURLConnection.HTTP_OK);
        deleteProfile(expectedProfileId, HttpURLConnection.HTTP_OK);
    });
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerSpecification(io.cdap.cdap.runtime.spi.provisioner.ProvisionerSpecification) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) MockProvisioner(io.cdap.cdap.internal.provision.MockProvisioner) Profile(io.cdap.cdap.proto.profile.Profile) ProvisionerDetail(io.cdap.cdap.proto.provisioner.ProvisionerDetail) Test(org.junit.Test)

Example 3 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by caskdata.

the class ProfileHttpHandlerTest method testSystemProfiles.

@Test
public void testSystemProfiles() throws Exception {
    Assert.assertEquals(Collections.singletonList(Profile.NATIVE), listSystemProfiles(HttpURLConnection.HTTP_OK));
    Profile p1 = new Profile("p1", "label", "desc", EntityScope.SYSTEM, new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
    putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_OK);
    Optional<Profile> p1Optional = getSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    Assert.assertTrue(p1Optional.isPresent());
    Assert.assertEquals(p1, p1Optional.get());
    // check list contains both native and p1
    Set<Profile> expected = new HashSet<>();
    expected.add(Profile.NATIVE);
    expected.add(p1);
    Set<Profile> actual = new HashSet<>(listSystemProfiles(HttpURLConnection.HTTP_OK));
    Assert.assertEquals(expected, actual);
    // check that they're both visible to namespaces
    Assert.assertEquals(expected, new HashSet<>(listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK)));
    // check we can add a profile with the same name in a namespace
    Profile p2 = new Profile(p1.getName(), p1.getLabel(), p1.getDescription(), EntityScope.USER, p1.getProvisioner());
    ProfileId p2Id = NamespaceId.DEFAULT.profile(p2.getName());
    putProfile(p2Id, p2, HttpURLConnection.HTTP_OK);
    // check that all are visible to the namespace
    expected.add(p2);
    Assert.assertEquals(expected, new HashSet<>(listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK)));
    // check that namespaced profile is not visible in system list
    expected.remove(p2);
    Assert.assertEquals(expected, new HashSet<>(listSystemProfiles(HttpURLConnection.HTTP_OK)));
    disableProfile(p2Id, HttpURLConnection.HTTP_OK);
    deleteProfile(p2Id, HttpURLConnection.HTTP_OK);
    disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    doAs(READ_WRITE_USER_NAME, () -> {
        listSystemProfiles(HttpURLConnection.HTTP_OK);
        listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
        putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_OK);
        getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_OK);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
        enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
        deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    });
    doAs(READ_ONLY_SYSTEM_USER_NAME, () -> {
        listSystemProfiles(HttpURLConnection.HTTP_OK);
        listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
        putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_FORBIDDEN);
        getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_OK);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
    });
    doAs(READ_ONLY_USER_NAME, () -> {
        listSystemProfiles(HttpURLConnection.HTTP_FORBIDDEN);
        listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_FORBIDDEN);
        listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
        getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_FORBIDDEN);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
    });
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by caskdata.

the class ProfileHttpHandlerTest method testListAndGetProfiles.

@Test
public void testListAndGetProfiles() throws Exception {
    // no profile should be there in default namespace
    List<Profile> profiles = listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
    Assert.assertEquals(Collections.emptyList(), profiles);
    // try to list all profiles including system namespace before putting a new one, there should only exist a default
    // profile
    profiles = listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
    Assert.assertEquals(Collections.singletonList(Profile.NATIVE), profiles);
    // test get single profile endpoint
    ProfileId profileId = NamespaceId.DEFAULT.profile("p1");
    Profile expected = new Profile("p1", "label", "my profile for testing", new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
    putProfile(profileId, expected, HttpURLConnection.HTTP_OK);
    Profile actual = getProfile(profileId, HttpURLConnection.HTTP_OK).get();
    Assert.assertEquals(expected, actual);
    // get a nonexisting profile should get a not found code
    getProfile(NamespaceId.DEFAULT.profile("nonExisting"), HttpURLConnection.HTTP_NOT_FOUND);
    doAs(READ_ONLY_USER_NAME, () -> {
        listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
        getProfile(profileId, HttpURLConnection.HTTP_OK);
    });
    doAs(NO_ACCESS_USER_NAME, () -> {
        listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_FORBIDDEN);
        getProfile(profileId, HttpURLConnection.HTTP_FORBIDDEN);
    });
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 5 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by caskdata.

the class ProfileHttpHandlerTest method testPutPermissionChecks.

@Test
public void testPutPermissionChecks() throws Exception {
    Profile profile = new Profile(PERMISSIONS_TEST_PROFILE, "label", "default permissions testing profile", new ProvisionerInfo(MockProvisioner.NAME, Collections.emptyList()));
    ProfileId profileId = NamespaceId.DEFAULT.profile(PERMISSIONS_TEST_PROFILE);
    // Verify the UPDATE user does not have permissions to create a profile
    doAs(UPDATE_PROFILE_USER.getName(), () -> {
        putProfile(profileId, profile, HttpURLConnection.HTTP_FORBIDDEN);
        putSystemProfile(profile.getName(), profile, HttpURLConnection.HTTP_FORBIDDEN);
    });
    // Verify that the CREATE user can create both profiles
    doAs(CREATE_PROFILE_USER.getName(), () -> {
        putProfile(profileId, profile, HttpURLConnection.HTTP_OK);
        putSystemProfile(profile.getName(), profile, HttpURLConnection.HTTP_OK);
    });
    // Verify that the UPDATE user can modify the profiles after they have been created
    doAs(UPDATE_PROFILE_USER.getName(), () -> {
        putProfile(profileId, profile, HttpURLConnection.HTTP_OK);
        putSystemProfile(profile.getName(), profile, HttpURLConnection.HTTP_OK);
        // Disable the profiles in preparation for deletion.
        disableProfile(profileId, HttpURLConnection.HTTP_OK);
        disableSystemProfile(profile.getName(), HttpURLConnection.HTTP_OK);
        // Verify deletion failure
        deleteProfile(profileId, HttpURLConnection.HTTP_FORBIDDEN);
        deleteSystemProfile(profile.getName(), HttpURLConnection.HTTP_FORBIDDEN);
    });
    // Verify that the CREATE user cannot update profiles after they have been created
    doAs(CREATE_PROFILE_USER.getName(), () -> {
        putProfile(profileId, profile, HttpURLConnection.HTTP_FORBIDDEN);
        putSystemProfile(profile.getName(), profile, HttpURLConnection.HTTP_FORBIDDEN);
    });
    // Delete profiles
    doAs(DELETE_PROFILE_USER.getName(), () -> {
        deleteProfile(profileId, HttpURLConnection.HTTP_OK);
        deleteSystemProfile(profile.getName(), HttpURLConnection.HTTP_OK);
    });
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Aggregations

ProfileId (io.cdap.cdap.proto.id.ProfileId)56 Test (org.junit.Test)34 Profile (io.cdap.cdap.proto.profile.Profile)25 ProgramId (io.cdap.cdap.proto.id.ProgramId)13 ProvisionerInfo (io.cdap.cdap.proto.provisioner.ProvisionerInfo)13 Path (javax.ws.rs.Path)11 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)8 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)7 ProfileConflictException (io.cdap.cdap.common.ProfileConflictException)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6 HashMap (java.util.HashMap)6 MetricStore (io.cdap.cdap.api.metrics.MetricStore)5 NotFoundException (io.cdap.cdap.common.NotFoundException)5 ProgramSchedule (io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)5 ArrayList (java.util.ArrayList)5 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)4 ProfileService (io.cdap.cdap.internal.profile.ProfileService)4 Store (io.cdap.cdap.app.store.Store)3 TimeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)3 DefaultStore (io.cdap.cdap.internal.app.store.DefaultStore)3