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);
}
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));
}
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();
}
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));
}
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;
}
Aggregations