Search in sources :

Example 61 with Profile

use of io.cdap.cdap.proto.profile.Profile in project cdap by caskdata.

the class ProfileServiceTest method testProfileService.

@Test
public void testProfileService() throws Exception {
    // get non-existing profile
    try {
        profileService.getProfile(NamespaceId.DEFAULT.profile("nonExisting"));
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    // delete non-existing profile
    try {
        profileService.deleteProfile(NamespaceId.DEFAULT.profile("nonExisting"));
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    ProfileId profileId = NamespaceId.DEFAULT.profile("MyProfile");
    Profile expected = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
    // add a profile
    profileService.saveProfile(profileId, expected);
    // get the profile
    Assert.assertEquals(expected, profileService.getProfile(profileId));
    // add a profile which already exists, should succeed and the profile property should be updated
    expected = new Profile("MyProfile", "label", "my 2nd profile for updating", new ProvisionerInfo("anotherProvisioner", Collections.emptyList()));
    profileService.saveProfile(profileId, expected);
    Assert.assertEquals(expected, profileService.getProfile(profileId));
    // add another profile to default namespace
    ProfileId profileId2 = NamespaceId.DEFAULT.profile("MyProfile2");
    Profile profile2 = new Profile("MyProfile2", "label", "my 2nd profile for testing", new ProvisionerInfo("anotherProvisioner", PROPERTY_SUMMARIES));
    profileService.saveProfile(profileId2, profile2);
    // add default profile
    profileService.saveProfile(ProfileId.NATIVE, Profile.NATIVE);
    // get all profiles
    List<Profile> profiles = ImmutableList.of(expected, profile2, Profile.NATIVE);
    Assert.assertEquals(profiles, profileService.getProfiles(NamespaceId.DEFAULT, true));
    // by default the profile status should be enabled
    Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId).getStatus());
    Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId2).getStatus());
    // by default the profile will be enabled, so enable it will throw a ProfileConflictException
    try {
        profileService.enableProfile(profileId);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    // disable the profile should success
    profileService.disableProfile(profileId);
    // check the profile status to the disabled
    Assert.assertEquals(ProfileStatus.DISABLED, profileService.getProfile(profileId).getStatus());
    // disable again should throw ProfileConflictException
    try {
        profileService.disableProfile(profileId);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    // enable should work this time
    profileService.enableProfile(profileId);
    Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId).getStatus());
    // delete the second profile should fail since it is enabled
    try {
        profileService.deleteProfile(profileId2);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    profileService.disableProfile(profileId2);
    profileService.deleteProfile(profileId2);
    Assert.assertEquals(ImmutableList.of(expected), profileService.getProfiles(NamespaceId.DEFAULT, false));
    // add one and delete all profiles
    profileService.saveProfile(profileId2, profile2);
    profileService.disableProfile(profileId);
    profileService.disableProfile(profileId2);
    profileService.deleteAllProfiles(NamespaceId.DEFAULT);
    Assert.assertEquals(Collections.EMPTY_LIST, profileService.getProfiles(NamespaceId.DEFAULT, false));
    // try to enable and disable an non-existing profile should throw NotFoundException
    try {
        profileService.enableProfile(profileId);
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    try {
        profileService.disableProfile(profileId);
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) NotFoundException(io.cdap.cdap.common.NotFoundException) ProfileConflictException(io.cdap.cdap.common.ProfileConflictException) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 62 with Profile

use of io.cdap.cdap.proto.profile.Profile in project cdap by caskdata.

the class ProfileServiceTest method testProfileOverrides.

@Test
public void testProfileOverrides() throws Exception {
    List<ProvisionerPropertyValue> provisionerProperties = new ArrayList<>();
    provisionerProperties.add(new ProvisionerPropertyValue("editable1", "val", true));
    provisionerProperties.add(new ProvisionerPropertyValue("editable2", "val", true));
    provisionerProperties.add(new ProvisionerPropertyValue("final", "finalval", false));
    ProvisionerInfo provisionerInfo = new ProvisionerInfo("provisioner", provisionerProperties);
    Profile profile = new Profile("name", "label", "desc", provisionerInfo);
    ProfileId profileId = NamespaceId.DEFAULT.profile("p");
    profileService.saveProfile(profileId, profile);
    try {
        Map<String, String> args = new HashMap<>();
        args.put("editable1", "newval");
        args.put("final", "shouldnotwork");
        args.put("newarg", "val");
        // resolved properties should include all the stored properties,
        // with 'final' not overridden and 'editable1' overridden.
        List<ProvisionerPropertyValue> expectedProperties = new ArrayList<>();
        expectedProperties.add(new ProvisionerPropertyValue("editable1", "newval", true));
        expectedProperties.add(new ProvisionerPropertyValue("editable2", "val", true));
        expectedProperties.add(new ProvisionerPropertyValue("final", "finalval", false));
        expectedProperties.add(new ProvisionerPropertyValue("newarg", "val", true));
        provisionerInfo = new ProvisionerInfo(provisionerInfo.getName(), expectedProperties);
        Profile expected = new Profile(profile.getName(), "label", profile.getDescription(), provisionerInfo);
        Profile actual = profileService.getProfile(profileId, args);
        Assert.assertEquals(expected, actual);
    } finally {
        profileService.disableProfile(profileId);
        profileService.deleteProfile(profileId);
    }
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerPropertyValue(io.cdap.cdap.proto.provisioner.ProvisionerPropertyValue) HashMap(java.util.HashMap) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) ArrayList(java.util.ArrayList) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 63 with Profile

use of io.cdap.cdap.proto.profile.Profile in project cdap by caskdata.

the class ProfileServiceTest method testProfileMetricsDeletion.

@Test
public void testProfileMetricsDeletion() throws Exception {
    ProfileId myProfile = NamespaceId.DEFAULT.profile("MyProfile");
    Profile profile = new Profile("MyProfile", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), Profile.NATIVE.getProvisioner());
    ProgramRunId runId = NamespaceId.DEFAULT.app("myApp").workflow("myProgram").run(RunIds.generate());
    // create and disable the profile
    profileService.saveProfile(myProfile, profile);
    profileService.disableProfile(myProfile);
    // emit some metrics
    MetricsCollectionService metricService = getInjector().getInstance(MetricsCollectionService.class);
    MetricsContext metricsContext = metricService.getContext(getMetricsTags(runId, myProfile));
    metricsContext.increment(Constants.Metrics.Program.PROGRAM_NODE_MINUTES, 30L);
    MetricStore metricStore = getInjector().getInstance(MetricStore.class);
    Tasks.waitFor(30L, () -> getMetric(metricStore, runId, myProfile, "system." + Constants.Metrics.Program.PROGRAM_NODE_MINUTES), 10, TimeUnit.SECONDS);
    // delete and verify the metrics are gone
    profileService.deleteProfile(myProfile);
    Tasks.waitFor(0L, () -> getMetric(metricStore, runId, myProfile, "system." + Constants.Metrics.Program.PROGRAM_NODE_MINUTES), 10, TimeUnit.SECONDS);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) MetricStore(io.cdap.cdap.api.metrics.MetricStore) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) MetricsContext(io.cdap.cdap.api.metrics.MetricsContext) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 64 with Profile

use of io.cdap.cdap.proto.profile.Profile in project cdap by caskdata.

the class ProfileServiceTest method testProfileCreationTime.

@Test
public void testProfileCreationTime() throws Exception {
    ProfileId myProfile = NamespaceId.DEFAULT.profile("MyProfile");
    long creationTime = TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    Profile profile = new Profile("MyProfile", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), ProfileStatus.ENABLED, Profile.NATIVE.getProvisioner(), creationTime);
    profileService.saveProfile(myProfile, profile);
    Assert.assertEquals(creationTime, profileService.getProfile(myProfile).getCreatedTsSeconds());
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 65 with Profile

use of io.cdap.cdap.proto.profile.Profile in project cdap by caskdata.

the class ProfileServiceTest method testProfileCreationDisabled.

@Test(expected = MethodNotAllowedException.class)
public void testProfileCreationDisabled() throws Exception {
    CConfiguration cConfWithProfileCreationDisabled = CConfiguration.copy(cConf);
    cConfWithProfileCreationDisabled.setBoolean(Constants.Profile.UPDATE_ALLOWED, false);
    ProfileService service = new ProfileService(cConfWithProfileCreationDisabled, getInjector().getInstance(MetricsSystemClient.class), getInjector().getInstance(TransactionRunner.class));
    ProfileId profileId = NamespaceId.DEFAULT.profile("MyProfile");
    Profile profile = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
    // Add a profile
    service.saveProfile(profileId, profile);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) MetricsSystemClient(io.cdap.cdap.api.metrics.MetricsSystemClient) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Aggregations

Profile (io.cdap.cdap.proto.profile.Profile)86 ProfileId (io.cdap.cdap.proto.id.ProfileId)50 Test (org.junit.Test)48 ProvisionerInfo (io.cdap.cdap.proto.provisioner.ProvisionerInfo)32 NotFoundException (io.cdap.cdap.common.NotFoundException)16 ProfileConflictException (io.cdap.cdap.common.ProfileConflictException)14 ProgramId (io.cdap.cdap.proto.id.ProgramId)14 Field (io.cdap.cdap.spi.data.table.field.Field)14 ArrayList (java.util.ArrayList)14 ProvisionerPropertyValue (io.cdap.cdap.proto.provisioner.ProvisionerPropertyValue)10 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)10 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)8 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)8 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)6 Store (io.cdap.cdap.app.store.Store)6 DefaultStore (io.cdap.cdap.internal.app.store.DefaultStore)6 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)6 Injector (com.google.inject.Injector)4