use of io.cdap.cdap.proto.id.ProfileId in project cdap by caskdata.
the class AuthorizableTest method testProfile.
@Test
public void testProfile() {
ProfileId profileId = new ProfileId("ns", "test_secure");
Authorizable authorizable = Authorizable.fromEntityId(profileId);
Assert.assertEquals(profileId.toString(), authorizable.toString());
String widcardId = profileId.toString().replace("est", "*es?t");
Assert.assertEquals(widcardId, Authorizable.fromString(widcardId).toString());
}
use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.
the class ProfileService method deleteAllProfiles.
/**
* Delete all profiles in a given namespace. Deleting all profiles in SYSTEM namespace is not allowed.
*
* @param namespaceId the id of the namespace
*/
public void deleteAllProfiles(NamespaceId namespaceId) throws MethodNotAllowedException, NotFoundException, ProfileConflictException {
if (namespaceId.equals(NamespaceId.SYSTEM)) {
throw new MethodNotAllowedException("Deleting all system profiles is not allowed.");
}
List<ProfileId> deleted = new ArrayList<>();
TransactionRunners.run(transactionRunner, context -> {
ProfileStore profileStore = ProfileStore.get(context);
AppMetadataStore appMetadataStore = AppMetadataStore.create(context);
List<Profile> profiles = profileStore.getProfiles(namespaceId, false);
for (Profile profile : profiles) {
ProfileId profileId = namespaceId.profile(profile.getName());
deleteProfile(profileStore, appMetadataStore, profileId, profile);
deleted.add(profileId);
}
}, ProfileConflictException.class, NotFoundException.class);
// delete the metrics
for (ProfileId profileId : deleted) {
deleteMetrics(profileId);
}
}
use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.
the class ProfileMetricServiceTest method testRoundingLogic.
@Test
public void testRoundingLogic() throws Exception {
ProgramRunId runId = NamespaceId.DEFAULT.app("round").workflow("round").run(RunIds.generate());
ProfileId profileId = NamespaceId.DEFAULT.profile("roundProfile");
MetricsCollectionService collectionService = injector.getInstance(MetricsCollectionService.class);
MetricStore metricStore = injector.getInstance(MetricStore.class);
ProfileMetricService scheduledService = new ProfileMetricService(collectionService, runId, profileId, 1, 1);
// start and stop the service, the metric should still go up by 1
scheduledService.startUp();
scheduledService.shutDown();
Tasks.waitFor(1L, () -> getMetric(metricStore, runId, profileId, "system." + Constants.Metrics.Program.PROGRAM_NODE_MINUTES), 10, TimeUnit.SECONDS);
scheduledService.startUp();
// set the start up time to 90 seconds before the current time
scheduledService.setStartUpTime(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) - 90);
// 90 seconds should round up to 2 mins, so emit 1 min and test the rounding logic
scheduledService.emitMetric();
scheduledService.shutDown();
// the metric should go up by 2
Tasks.waitFor(3L, () -> getMetric(metricStore, runId, profileId, "system." + Constants.Metrics.Program.PROGRAM_NODE_MINUTES), 10, TimeUnit.SECONDS);
scheduledService.startUp();
// set the start up time to 65 seconds before the current time
scheduledService.setStartUpTime(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) - 65);
// 65 seconds should round down to 1 min, so emit 1 min and test the rest seconds are ignored
scheduledService.emitMetric();
scheduledService.shutDown();
// the metric should go up by 1
Tasks.waitFor(4L, () -> getMetric(metricStore, runId, profileId, "system." + Constants.Metrics.Program.PROGRAM_NODE_MINUTES), 10, TimeUnit.SECONDS);
}
use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.
the class ProfileServiceTest method testProfileUpdateDisallowed.
@Test(expected = MethodNotAllowedException.class)
public void testProfileUpdateDisallowed() throws Exception {
ProfileId profileId = NamespaceId.DEFAULT.profile("MyProfile");
Profile profile = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
profileService.saveProfile(profileId, profile);
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));
Profile newProfile = new Profile("MyProfile", "label", "my new profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
// Update the profile
service.saveProfile(profileId, newProfile);
}
use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.
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
}
}
Aggregations