Search in sources :

Example 21 with AllProgramsApp

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

the class CapabilityManagementServiceTest method testCapabilityRefresh.

@Test
public void testCapabilityRefresh() throws Exception {
    String externalConfigPath = tmpFolder.newFolder("capability-config-refresh").getAbsolutePath();
    cConfiguration.set(Constants.Capability.CONFIG_DIR, externalConfigPath);
    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);
    // check that app is not available
    List<JsonObject> appList = getAppList(namespace);
    Assert.assertTrue(appList.isEmpty());
    // enable the capability
    CapabilityConfig config = getTestConfig("1.0.0");
    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());
    // 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 22 with AllProgramsApp

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

the class ApplicationVerificationTest method testGoodApplication.

/**
 * Good test
 */
@Test
public void testGoodApplication() {
    ApplicationSpecification appSpec = Specifications.from(new AllProgramsApp());
    ApplicationSpecificationAdapter adapter = ApplicationSpecificationAdapter.create();
    ApplicationSpecification newSpec = adapter.fromJson(adapter.toJson(appSpec));
    ApplicationVerification app = new ApplicationVerification();
    VerifyResult result = app.verify(new ApplicationId("test", newSpec.getName()), newSpec);
    Assert.assertSame(result.getMessage(), result.getStatus(), VerifyResult.Status.SUCCESS);
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) ApplicationSpecificationAdapter(io.cdap.cdap.internal.app.ApplicationSpecificationAdapter) AllProgramsApp(io.cdap.cdap.AllProgramsApp) VerifyResult(io.cdap.cdap.app.verification.VerifyResult) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 23 with AllProgramsApp

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

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 24 with AllProgramsApp

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

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 25 with AllProgramsApp

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

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