Search in sources :

Example 1 with DefaultStore

use of io.cdap.cdap.internal.app.store.DefaultStore in project cdap by caskdata.

the class SqlProfileServiceTest method setup.

@BeforeClass
public static void setup() throws IOException, TableAlreadyExistsException {
    cConf = CConfiguration.create();
    // any plugin which requires transaction will be excluded
    cConf.set(Constants.REQUIREMENTS_DATASET_TYPE_EXCLUDE, Joiner.on(",").join(Table.TYPE, KeyValueTable.TYPE));
    pg = PostgresInstantiator.createAndStart(cConf, TEMP_FOLDER.newFolder());
    injector = AppFabricTestHelper.getInjector(cConf);
    structuredTableAdmin = injector.getInstance(StructuredTableAdmin.class);
    TransactionRunner transactionRunner = injector.getInstance(TransactionRunner.class);
    StoreDefinition.createAllTables(structuredTableAdmin);
    profileService = new ProfileService(cConf, injector.getInstance(MetricsSystemClient.class), transactionRunner);
    defaultStore = new DefaultStore(transactionRunner);
}
Also used : DefaultStore(io.cdap.cdap.internal.app.store.DefaultStore) StructuredTableAdmin(io.cdap.cdap.spi.data.StructuredTableAdmin) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) BeforeClass(org.junit.BeforeClass)

Example 2 with DefaultStore

use of io.cdap.cdap.internal.app.store.DefaultStore in project cdap by caskdata.

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)

Aggregations

DefaultStore (io.cdap.cdap.internal.app.store.DefaultStore)2 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)1 MetricStore (io.cdap.cdap.api.metrics.MetricStore)1 Store (io.cdap.cdap.app.store.Store)1 ProfileConflictException (io.cdap.cdap.common.ProfileConflictException)1 ProfileId (io.cdap.cdap.proto.id.ProfileId)1 ProgramId (io.cdap.cdap.proto.id.ProgramId)1 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)1 Profile (io.cdap.cdap.proto.profile.Profile)1 StructuredTableAdmin (io.cdap.cdap.spi.data.StructuredTableAdmin)1 TransactionRunner (io.cdap.cdap.spi.data.transaction.TransactionRunner)1 RunId (org.apache.twill.api.RunId)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1