Search in sources :

Example 31 with Profile

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

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 32 with Profile

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

the class ProfileServiceTest method testProfileDeletion.

@Test
public void testProfileDeletion() throws Exception {
    ProfileId myProfile = NamespaceId.DEFAULT.profile("MyProfile");
    ProfileId myProfile2 = NamespaceId.DEFAULT.profile("MyProfile2");
    Profile profile1 = new Profile("MyProfile", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), Profile.NATIVE.getProvisioner());
    Profile profile2 = new Profile("MyProfile2", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), ProfileStatus.DISABLED, Profile.NATIVE.getProvisioner());
    profileService.saveProfile(myProfile, profile1);
    // add profile2 and disable it, profile2 can get deleted at any time
    profileService.saveProfile(myProfile2, profile2);
    profileService.disableProfile(myProfile2);
    // Should not be able to delete because the profile is by default enabled
    try {
        profileService.deleteProfile(myProfile);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    try {
        profileService.deleteAllProfiles(NamespaceId.DEFAULT);
        Assert.fail();
    } catch (ProfileConflictException e) {
        // expected and check profile 2 is not getting deleted
        Assert.assertEquals(profile2, profileService.getProfile(myProfile2));
    }
    // add assignment and disable it, deletion should also fail
    profileService.addProfileAssignment(myProfile, NamespaceId.DEFAULT);
    profileService.disableProfile(myProfile);
    try {
        profileService.deleteProfile(myProfile);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    try {
        profileService.deleteAllProfiles(NamespaceId.DEFAULT);
        Assert.fail();
    } catch (ProfileConflictException e) {
        // expected and check profile 2 is not getting deleted
        Assert.assertEquals(profile2, profileService.getProfile(myProfile2));
    }
    profileService.removeProfileAssignment(myProfile, NamespaceId.DEFAULT);
    // add an active record to DefaultStore, deletion should still fail
    Store store = getDefaultStore();
    ProgramId programId = NamespaceId.DEFAULT.app("myApp").workflow("myProgram");
    ArtifactId artifactId = NamespaceId.DEFAULT.artifact("testArtifact", "1.0").toApiArtifactId();
    RunId runId = RunIds.generate(System.currentTimeMillis());
    ProgramRunId programRunId = programId.run(runId.getId());
    Map<String, String> systemArgs = Collections.singletonMap(SystemArguments.PROFILE_NAME, myProfile.getScopedName());
    int sourceId = 0;
    store.setProvisioning(programRunId, Collections.emptyMap(), systemArgs, AppFabricTestHelper.createSourceId(++sourceId), artifactId);
    store.setProvisioned(programRunId, 0, AppFabricTestHelper.createSourceId(++sourceId));
    store.setStart(programRunId, null, systemArgs, AppFabricTestHelper.createSourceId(++sourceId));
    try {
        profileService.deleteProfile(myProfile);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    try {
        profileService.deleteAllProfiles(NamespaceId.DEFAULT);
        Assert.fail();
    } catch (ProfileConflictException e) {
        // expected and check profile 2 is not getting deleted
        Assert.assertEquals(profile2, profileService.getProfile(myProfile2));
    }
    // set the run to stopped then deletion should work
    store.setStop(programRunId, System.currentTimeMillis() + 1000, ProgramController.State.ERROR.getRunStatus(), AppFabricTestHelper.createSourceId(++sourceId));
    // now profile deletion should succeed
    profileService.deleteProfile(myProfile);
    Assert.assertEquals(Collections.singletonList(profile2), profileService.getProfiles(NamespaceId.DEFAULT, false));
    profileService.saveProfile(myProfile, profile1);
    profileService.disableProfile(myProfile);
    profileService.deleteAllProfiles(NamespaceId.DEFAULT);
    Assert.assertEquals(Collections.emptyList(), profileService.getProfiles(NamespaceId.DEFAULT, false));
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) MetricStore(io.cdap.cdap.api.metrics.MetricStore) Store(io.cdap.cdap.app.store.Store) DefaultStore(io.cdap.cdap.internal.app.store.DefaultStore) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ProgramId(io.cdap.cdap.proto.id.ProgramId) RunId(org.apache.twill.api.RunId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ProfileConflictException(io.cdap.cdap.common.ProfileConflictException) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 33 with Profile

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

the class AppFabricTestBase method getProfile.

protected Optional<Profile> getProfile(ProfileId profileId, int expectedCode) throws Exception {
    HttpResponse response = doGet(String.format("/v3/namespaces/%s/profiles/%s", profileId.getNamespace(), profileId.getProfile()));
    assertResponseCode(expectedCode, response);
    if (expectedCode == HttpResponseStatus.OK.code()) {
        return Optional.of(GSON.fromJson(response.getResponseBodyAsString(), Profile.class));
    }
    return Optional.empty();
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) Profile(io.cdap.cdap.proto.profile.Profile)

Example 34 with Profile

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

the class ProfileHttpHandler method getSystemProfiles.

@GET
@Path("/profiles")
public void getSystemProfiles(HttpRequest request, HttpResponder responder) throws Exception {
    NamespaceId namespaceId = NamespaceId.SYSTEM;
    accessEnforcer.enforceOnParent(EntityType.PROFILE, namespaceId, authenticationContext.getPrincipal(), StandardPermission.LIST);
    List<Profile> profiles = verifyCpuLabelsProfiles(profileService.getProfiles(namespaceId, true), NamespaceId.SYSTEM);
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(profiles));
}
Also used : NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Profile(io.cdap.cdap.proto.profile.Profile) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 35 with Profile

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

the class ProfileHttpHandler method verifyCpuLabelsProfiles.

private List<Profile> verifyCpuLabelsProfiles(List<Profile> profileList, NamespaceId namespaceId) throws BadRequestException, MethodNotAllowedException {
    List<Profile> verifiedProfiles = new ArrayList<>();
    for (Profile profile : profileList) {
        ProfileId profileId = getValidatedProfile(namespaceId, profile.getName());
        verifiedProfiles.add(verifyCpuLabelsProfile(profile, profileId));
    }
    return verifiedProfiles;
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ArrayList(java.util.ArrayList) Profile(io.cdap.cdap.proto.profile.Profile)

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