Search in sources :

Example 16 with AllProgramsApp

use of io.cdap.cdap.AllProgramsApp in project cdap by caskdata.

the class DefaultStoreTest method testCheckDeletedWorkflow.

@Test
public void testCheckDeletedWorkflow() {
    // Deploy program with all types of programs.
    ApplicationSpecification spec = Specifications.from(new AllProgramsApp());
    ApplicationId appId = NamespaceId.DEFAULT.app(spec.getName());
    store.addApplication(appId, spec);
    Set<String> specsToBeDeleted = Sets.newHashSet();
    specsToBeDeleted.addAll(spec.getWorkflows().keySet());
    Assert.assertEquals(1, specsToBeDeleted.size());
    // Get the spec for app that contains only flow and mapreduce - removing workflows.
    spec = Specifications.from(new DefaultStoreTestApp());
    // Get the deleted program specs by sending a spec with same name as AllProgramsApp but with no programs
    List<ProgramSpecification> deletedSpecs = store.getDeletedProgramSpecifications(appId, spec);
    Assert.assertEquals(2, deletedSpecs.size());
    for (ProgramSpecification specification : deletedSpecs) {
        // Remove the spec that is verified, to check the count later.
        specsToBeDeleted.remove(specification.getName());
    }
    // 2 specs should have been deleted and 0 should be remaining.
    Assert.assertEquals(0, specsToBeDeleted.size());
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) DefaultStoreTestApp(io.cdap.cdap.DefaultStoreTestApp) ProgramSpecification(io.cdap.cdap.api.ProgramSpecification) AllProgramsApp(io.cdap.cdap.AllProgramsApp) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 17 with AllProgramsApp

use of io.cdap.cdap.AllProgramsApp in project cdap by caskdata.

the class DefaultStoreTest method testProgramRunCount.

@Test
public void testProgramRunCount() {
    ApplicationSpecification spec = Specifications.from(new AllProgramsApp());
    ApplicationId appId = NamespaceId.DEFAULT.app(spec.getName());
    ArtifactId testArtifact = NamespaceId.DEFAULT.artifact("testArtifact", "1.0").toApiArtifactId();
    ProgramId workflowId = appId.workflow(AllProgramsApp.NoOpWorkflow.NAME);
    ProgramId serviceId = appId.service(AllProgramsApp.NoOpService.NAME);
    ProgramId nonExistingAppProgramId = NamespaceId.DEFAULT.app("nonExisting").workflow("test");
    ProgramId nonExistingProgramId = appId.workflow("nonExisting");
    // add the application
    store.addApplication(appId, spec);
    // add some run records to workflow and service
    for (int i = 0; i < 5; i++) {
        setStart(workflowId.run(RunIds.generate()), Collections.emptyMap(), Collections.emptyMap(), testArtifact);
        setStart(serviceId.run(RunIds.generate()), Collections.emptyMap(), Collections.emptyMap(), testArtifact);
    }
    List<RunCountResult> result = store.getProgramRunCounts(ImmutableList.of(workflowId, serviceId, nonExistingAppProgramId, nonExistingProgramId));
    // compare the result
    Assert.assertEquals(4, result.size());
    for (RunCountResult runCountResult : result) {
        ProgramId programId = runCountResult.getProgramId();
        Long count = runCountResult.getCount();
        if (programId.equals(nonExistingAppProgramId) || programId.equals(nonExistingProgramId)) {
            Assert.assertNull(count);
            Assert.assertTrue(runCountResult.getException() instanceof NotFoundException);
        } else {
            Assert.assertNotNull(count);
            Assert.assertEquals(5L, count.longValue());
        }
    }
    // remove the app should remove all run count
    store.removeApplication(appId);
    for (RunCountResult runCountResult : store.getProgramRunCounts(ImmutableList.of(workflowId, serviceId))) {
        Assert.assertNull(runCountResult.getCount());
        Assert.assertTrue(runCountResult.getException() instanceof NotFoundException);
    }
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) RunCountResult(io.cdap.cdap.proto.RunCountResult) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) NotFoundException(io.cdap.cdap.common.NotFoundException) AllProgramsApp(io.cdap.cdap.AllProgramsApp) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) Test(org.junit.Test)

Example 18 with AllProgramsApp

use of io.cdap.cdap.AllProgramsApp in project cdap by caskdata.

the class CapabilityManagementServiceTest method testCapabilityManagement.

@Test
public void testCapabilityManagement() throws Exception {
    String appName = AllProgramsApp.NAME;
    String programName = AllProgramsApp.NoOpService.NAME;
    Class<AllProgramsApp> appClass = AllProgramsApp.class;
    String version = "1.0.0";
    String namespace = NamespaceId.SYSTEM.getNamespace();
    // deploy the artifact
    deployTestArtifact(namespace, appName, version, appClass);
    ApplicationId applicationId = new ApplicationId(namespace, appName, version);
    ProgramId programId = new ProgramId(applicationId, ProgramType.SERVICE, programName);
    String externalConfigPath = tmpFolder.newFolder("capability-config-test").getAbsolutePath();
    cConfiguration.set(Constants.Capability.CONFIG_DIR, externalConfigPath);
    String fileName = "cap1.json";
    File testJson = new File(CapabilityManagementServiceTest.class.getResource(String.format("/%s", fileName)).getPath());
    Files.copy(testJson, new File(externalConfigPath, fileName));
    capabilityManagementService.runTask();
    List<JsonObject> appList = getAppList(namespace);
    Assert.assertFalse(appList.isEmpty());
    // Capability management service might not yet have deployed application.
    // So wait till program exists and is in running state.
    waitState(programId, "RUNNING");
    assertProgramRuns(programId, ProgramRunStatus.RUNNING, 1);
    // remove the file and make sure it gets removed
    new File(externalConfigPath, fileName).delete();
    capabilityManagementService.runTask();
    Assert.assertTrue(getAppList(namespace).isEmpty());
    // insert a pending entry, this should get re-applied and enable the capability
    FileReader fileReader = new FileReader(testJson);
    CapabilityConfig capabilityConfig = GSON.fromJson(fileReader, CapabilityConfig.class);
    capabilityStatusStore.addOrUpdateCapabilityOperation("cap1", CapabilityAction.ENABLE, capabilityConfig);
    capabilityManagementService.runTask();
    capabilityStatusStore.checkAllEnabled(Collections.singleton("cap1"));
    Assert.assertEquals(0, capabilityStatusStore.getCapabilityRecords().values().stream().filter(capabilityRecord -> capabilityRecord.getCapabilityOperationRecord() != null).count());
    // pending task out of the way , on next run should be deleted
    capabilityManagementService.runTask();
    try {
        capabilityStatusStore.checkAllEnabled(Collections.singleton("cap1"));
        Assert.fail("expecting exception");
    } catch (CapabilityNotAvailableException ex) {
    // expected
    }
    // disable from delete and see if it is applied correctly
    CapabilityConfig disableConfig = new CapabilityConfig(capabilityConfig.getLabel(), CapabilityStatus.DISABLED, capabilityConfig.getCapability(), capabilityConfig.getApplications(), capabilityConfig.getPrograms(), capabilityConfig.getHubs());
    writeConfigAsFile(externalConfigPath, fileName, disableConfig);
    capabilityManagementService.runTask();
    try {
        capabilityStatusStore.checkAllEnabled(Collections.singleton("cap1"));
        Assert.fail("expecting exception");
    } catch (CapabilityNotAvailableException ex) {
    // expected
    }
    Assert.assertEquals(disableConfig, capabilityStatusStore.getConfigs(Collections.singleton("cap1")).get("cap1"));
    // enable again
    writeConfigAsFile(externalConfigPath, fileName, capabilityConfig);
    capabilityManagementService.runTask();
    capabilityStatusStore.checkAllEnabled(Collections.singleton("cap1"));
    // Capability management service might not yet have deployed application.
    // So wait till program exists and is in running state.
    waitState(programId, "RUNNING");
    assertProgramRuns(programId, ProgramRunStatus.RUNNING, 1);
    // cleanup
    new File(externalConfigPath, fileName).delete();
    capabilityManagementService.runTask();
}
Also used : JsonObject(com.google.gson.JsonObject) AllProgramsApp(io.cdap.cdap.AllProgramsApp) FileReader(java.io.FileReader) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) File(java.io.File) Test(org.junit.Test)

Example 19 with AllProgramsApp

use of io.cdap.cdap.AllProgramsApp in project cdap by caskdata.

the class CapabilityManagementServiceTest method testCapabilityRefreshWithSameArtifact.

@Test
public void testCapabilityRefreshWithSameArtifact() throws Exception {
    String externalConfigPath = tmpFolder.newFolder("capability-config-refresh-redeploy-same").getAbsolutePath();
    cConfiguration.set(Constants.Capability.CONFIG_DIR, externalConfigPath);
    String appName = AllProgramsApp.NAME;
    String programName = AllProgramsApp.NoOpService.NAME;
    Class<AllProgramsApp> appClass = AllProgramsApp.class;
    String appVersion = "1.0.0";
    String namespace = NamespaceId.SYSTEM.getNamespace();
    // deploy the artifact with older version.
    String artifactVersion = "1.0.0";
    deployTestArtifact(namespace, appName, artifactVersion, appClass);
    ApplicationId applicationId = new ApplicationId(namespace, appName, appVersion);
    ProgramId programId = new ProgramId(applicationId, ProgramType.SERVICE, programName);
    // check that app is not available
    List<JsonObject> appList = getAppList(namespace);
    Assert.assertTrue(appList.isEmpty());
    // enable the capability
    CapabilityConfig config = getTestConfig(artifactVersion);
    writeConfigAsFile(externalConfigPath, config.getCapability(), config);
    capabilityManagementService.runTask();
    // app should show up and program should have run
    appList = getAppList(namespace);
    Assert.assertFalse(appList.isEmpty());
    // Capability management service might not yet have deployed application.
    // So wait till program exists and is in running state.
    waitState(programId, "RUNNING");
    assertProgramRuns(programId, ProgramRunStatus.RUNNING, 1);
    String capability = config.getCapability();
    capabilityStatusStore.checkAllEnabled(Collections.singleton(capability));
    // disable capability. Program should stop, status should be disabled and app should still be present.
    CapabilityConfig disabledConfig = changeConfigStatus(config, CapabilityStatus.DISABLED);
    writeConfigAsFile(externalConfigPath, disabledConfig.getCapability(), disabledConfig);
    capabilityManagementService.runTask();
    assertProgramRuns(programId, ProgramRunStatus.KILLED, 1);
    assertProgramRuns(programId, ProgramRunStatus.RUNNING, 0);
    try {
        capabilityStatusStore.checkAllEnabled(Collections.singleton(capability));
        Assert.fail("expecting exception");
    } catch (CapabilityNotAvailableException ex) {
    // expected
    }
    appList = getAppList(namespace);
    Assert.assertFalse(appList.isEmpty());
    // enable the capability again but with same artifact.
    config = getTestConfig(artifactVersion);
    writeConfigAsFile(externalConfigPath, config.getCapability(), config);
    capabilityManagementService.runTask();
    // app should show up with newer artifact and program should have run.
    appList = getAppList(namespace);
    Assert.assertFalse(appList.isEmpty());
    // Verify that application is still same as before.
    Assert.assertEquals(artifactVersion, appList.get(0).get("artifact").getAsJsonObject().get("version").getAsString());
    // Capability management service might not yet have deployed application.
    // So wait till program exists and is in running state.
    waitState(programId, "RUNNING");
    assertProgramRuns(programId, ProgramRunStatus.RUNNING, 1);
    capability = config.getCapability();
    capabilityStatusStore.checkAllEnabled(Collections.singleton(capability));
    // disable capability again. Program should stop, status should be disabled and app should still be present.
    disabledConfig = changeConfigStatus(config, CapabilityStatus.DISABLED);
    writeConfigAsFile(externalConfigPath, disabledConfig.getCapability(), disabledConfig);
    capabilityManagementService.runTask();
    assertProgramRuns(programId, ProgramRunStatus.KILLED, 1);
    assertProgramRuns(programId, ProgramRunStatus.RUNNING, 0);
    try {
        capabilityStatusStore.checkAllEnabled(Collections.singleton(capability));
        Assert.fail("expecting exception");
    } catch (CapabilityNotAvailableException ex) {
    // expected
    }
    appList = getAppList(namespace);
    Assert.assertFalse(appList.isEmpty());
    // delete capability. Program should stop, status should be disabled and app should still be present.
    new File(externalConfigPath, disabledConfig.getCapability()).delete();
    capabilityManagementService.runTask();
    Assert.assertTrue(capabilityStatusStore.getConfigs(Collections.singleton(capability)).isEmpty());
    appList = getAppList(namespace);
    Assert.assertTrue(appList.isEmpty());
}
Also used : JsonObject(com.google.gson.JsonObject) AllProgramsApp(io.cdap.cdap.AllProgramsApp) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) File(java.io.File) Test(org.junit.Test)

Example 20 with AllProgramsApp

use of io.cdap.cdap.AllProgramsApp in project cdap by caskdata.

the class CapabilityManagementServiceTest method testCapabilityRefreshWithNewArtifact.

@Test
public void testCapabilityRefreshWithNewArtifact() throws Exception {
    String externalConfigPath = tmpFolder.newFolder("capability-config-refresh-redeploy").getAbsolutePath();
    cConfiguration.set(Constants.Capability.CONFIG_DIR, externalConfigPath);
    String appName = AllProgramsApp.NAME;
    String programName = AllProgramsApp.NoOpService.NAME;
    Class<AllProgramsApp> appClass = AllProgramsApp.class;
    String appVersion = "1.0.0";
    String namespace = NamespaceId.SYSTEM.getNamespace();
    // deploy the artifact with older version.
    String oldArtifactVersion = "1.0.0";
    deployTestArtifact(namespace, appName, oldArtifactVersion, appClass);
    ApplicationId applicationId = new ApplicationId(namespace, appName, appVersion);
    ProgramId programId = new ProgramId(applicationId, ProgramType.SERVICE, programName);
    // check that app is not available
    List<JsonObject> appList = getAppList(namespace);
    Assert.assertTrue(appList.isEmpty());
    // enable the capability
    CapabilityConfig config = getTestConfig(oldArtifactVersion);
    writeConfigAsFile(externalConfigPath, config.getCapability(), config);
    capabilityManagementService.runTask();
    // app should show up and program should have run
    appList = getAppList(namespace);
    Assert.assertFalse(appList.isEmpty());
    // Capability management service might not yet have deployed application.
    // So wait till program exists and is in running state.
    waitState(programId, "RUNNING");
    assertProgramRuns(programId, ProgramRunStatus.RUNNING, 1);
    String capability = config.getCapability();
    capabilityStatusStore.checkAllEnabled(Collections.singleton(capability));
    // disable capability. Program should stop, status should be disabled and app should still be present.
    CapabilityConfig disabledConfig = changeConfigStatus(config, CapabilityStatus.DISABLED);
    writeConfigAsFile(externalConfigPath, disabledConfig.getCapability(), disabledConfig);
    capabilityManagementService.runTask();
    assertProgramRuns(programId, ProgramRunStatus.KILLED, 1);
    assertProgramRuns(programId, ProgramRunStatus.RUNNING, 0);
    try {
        capabilityStatusStore.checkAllEnabled(Collections.singleton(capability));
        Assert.fail("expecting exception");
    } catch (CapabilityNotAvailableException ex) {
    // expected
    }
    appList = getAppList(namespace);
    Assert.assertFalse(appList.isEmpty());
    String newArtifactVersion = "2.0.0";
    // deploy the new artifact.
    deployTestArtifact(namespace, appName, newArtifactVersion, appClass);
    // enable the capability again.
    config = getTestConfig(newArtifactVersion);
    writeConfigAsFile(externalConfigPath, config.getCapability(), config);
    capabilityManagementService.runTask();
    // app should show up with newer artifact and program should have run.
    appList = getAppList(namespace);
    Assert.assertFalse(appList.isEmpty());
    // Verify that application is redeployed with newer artifact.
    Assert.assertEquals(newArtifactVersion, appList.get(0).get("artifact").getAsJsonObject().get("version").getAsString());
    // Capability management service might not yet have deployed application.
    // So wait till program exists and is in running state.
    waitState(programId, "RUNNING");
    assertProgramRuns(programId, ProgramRunStatus.RUNNING, 1);
    capability = config.getCapability();
    capabilityStatusStore.checkAllEnabled(Collections.singleton(capability));
    // disable capability again. Program should stop, status should be disabled and app should still be present.
    disabledConfig = changeConfigStatus(config, CapabilityStatus.DISABLED);
    writeConfigAsFile(externalConfigPath, disabledConfig.getCapability(), disabledConfig);
    capabilityManagementService.runTask();
    assertProgramRuns(programId, ProgramRunStatus.KILLED, 1);
    assertProgramRuns(programId, ProgramRunStatus.RUNNING, 0);
    try {
        capabilityStatusStore.checkAllEnabled(Collections.singleton(capability));
        Assert.fail("expecting exception");
    } catch (CapabilityNotAvailableException ex) {
    // expected
    }
    appList = getAppList(namespace);
    Assert.assertFalse(appList.isEmpty());
    // delete capability. Program should stop, status should be disabled and app should still be present.
    new File(externalConfigPath, disabledConfig.getCapability()).delete();
    capabilityManagementService.runTask();
    Assert.assertTrue(capabilityStatusStore.getConfigs(Collections.singleton(capability)).isEmpty());
    appList = getAppList(namespace);
    Assert.assertTrue(appList.isEmpty());
}
Also used : JsonObject(com.google.gson.JsonObject) AllProgramsApp(io.cdap.cdap.AllProgramsApp) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) File(java.io.File) Test(org.junit.Test)

Aggregations

AllProgramsApp (io.cdap.cdap.AllProgramsApp)70 Test (org.junit.Test)66 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)54 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)48 ProgramId (io.cdap.cdap.proto.id.ProgramId)28 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)18 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)14 ArrayList (java.util.ArrayList)12 HashSet (java.util.HashSet)12 File (java.io.File)10 Set (java.util.Set)10 Assert (org.junit.Assert)10 JsonObject (com.google.gson.JsonObject)9 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)9 ProgramType (io.cdap.cdap.api.app.ProgramType)8 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)8 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)8 Specifications (io.cdap.cdap.internal.app.deploy.Specifications)8 AppDeploymentInfo (io.cdap.cdap.internal.app.deploy.pipeline.AppDeploymentInfo)8 ProgramType (io.cdap.cdap.proto.ProgramType)8