Search in sources :

Example 1 with Requirements

use of io.cdap.cdap.api.annotation.Requirements in project cdap by caskdata.

the class CapabilityManagementServiceTest method testApplicationDeployment.

@Test
public void testApplicationDeployment() throws Exception {
    String externalConfigPath = tmpFolder.newFolder("capability-config-app").getAbsolutePath();
    cConfiguration.set(Constants.Capability.CONFIG_DIR, externalConfigPath);
    capabilityManagementService.runTask();
    String testVersion = "1.0.0";
    // Deploy application with capability
    Class<CapabilityAppWithWorkflow> appWithWorkflowClass = CapabilityAppWithWorkflow.class;
    Requirements declaredAnnotation = appWithWorkflowClass.getDeclaredAnnotation(Requirements.class);
    // verify this app has capabilities
    Assert.assertTrue(declaredAnnotation.capabilities().length > 0);
    try {
        capabilityStatusStore.checkAllEnabled(Arrays.asList(declaredAnnotation.capabilities()));
        Assert.fail("expecting exception");
    } catch (CapabilityNotAvailableException ex) {
    }
    String appNameWithCapability = appWithWorkflowClass.getSimpleName() + UUID.randomUUID();
    deployTestArtifact(Id.Namespace.DEFAULT.getId(), appNameWithCapability, testVersion, appWithWorkflowClass);
    try {
        // deploy app
        Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, appNameWithCapability, testVersion);
        applicationLifecycleService.deployApp(NamespaceId.DEFAULT, appNameWithCapability, testVersion, artifactId, null, programId -> {
        });
        Assert.fail("Expecting exception");
    } catch (CapabilityNotAvailableException ex) {
    // expected
    }
    // Deploy application without capability
    Class<WorkflowAppWithFork> appNoCapabilityClass = WorkflowAppWithFork.class;
    Requirements declaredAnnotation1 = appNoCapabilityClass.getDeclaredAnnotation(Requirements.class);
    // verify this app has no capabilities
    Assert.assertNull(declaredAnnotation1);
    String appNameWithOutCapability = appWithWorkflowClass.getSimpleName() + UUID.randomUUID();
    deployArtifactAndApp(appNoCapabilityClass, appNameWithOutCapability, testVersion);
    // enable the capabilities
    List<CapabilityConfig> capabilityConfigs = Arrays.stream(declaredAnnotation.capabilities()).map(capability -> new CapabilityConfig("Test capability", CapabilityStatus.ENABLED, capability, Collections.emptyList(), Collections.emptyList(), Collections.emptyList())).collect(Collectors.toList());
    for (CapabilityConfig capabilityConfig : capabilityConfigs) {
        writeConfigAsFile(externalConfigPath, capabilityConfig.getCapability(), capabilityConfig);
    }
    capabilityManagementService.runTask();
    // deployment should go through now
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, appNameWithCapability, testVersion);
    applicationLifecycleService.deployApp(NamespaceId.DEFAULT, appNameWithCapability, testVersion, artifactId, null, programId -> {
    });
    applicationLifecycleService.removeApplication(NamespaceId.DEFAULT.app(appNameWithCapability, testVersion));
    applicationLifecycleService.removeApplication(NamespaceId.DEFAULT.app(appNameWithOutCapability, testVersion));
    artifactRepository.deleteArtifact(Id.Artifact.from(new Id.Namespace(NamespaceId.DEFAULT.getNamespace()), appNameWithCapability, testVersion));
    artifactRepository.deleteArtifact(Id.Artifact.from(new Id.Namespace(NamespaceId.DEFAULT.getNamespace()), appNameWithOutCapability, testVersion));
    for (CapabilityConfig capabilityConfig : capabilityConfigs) {
        new File(externalConfigPath, capabilityConfig.getCapability()).delete();
    }
    capabilityManagementService.runTask();
}
Also used : Manifest(java.util.jar.Manifest) JsonObject(com.google.gson.JsonObject) ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) Arrays(java.util.Arrays) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) CapabilityAppWithWorkflow(io.cdap.cdap.CapabilityAppWithWorkflow) AppJarHelper(io.cdap.cdap.common.test.AppJarHelper) AppFabricTestHelper(io.cdap.cdap.internal.AppFabricTestHelper) Gson(com.google.gson.Gson) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) After(org.junit.After) Map(java.util.Map) AfterClass(org.junit.AfterClass) ApplicationLifecycleService(io.cdap.cdap.internal.app.services.ApplicationLifecycleService) UUID(java.util.UUID) ProgramRunStatus(io.cdap.cdap.proto.ProgramRunStatus) Collectors(java.util.stream.Collectors) Id(io.cdap.cdap.common.id.Id) List(java.util.List) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) Constants(io.cdap.cdap.common.conf.Constants) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) PluginJarHelper(io.cdap.cdap.common.test.PluginJarHelper) BeforeClass(org.junit.BeforeClass) Location(org.apache.twill.filesystem.Location) ManifestFields(io.cdap.cdap.app.program.ManifestFields) AppFabricTestBase(io.cdap.cdap.internal.app.services.http.AppFabricTestBase) HashMap(java.util.HashMap) CapabilitySleepingWorkflowApp(io.cdap.cdap.CapabilitySleepingWorkflowApp) ApplicationWithPrograms(io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) Requirements(io.cdap.cdap.api.annotation.Requirements) ProgramType(io.cdap.cdap.proto.ProgramType) Files(com.google.common.io.Files) Locations(io.cdap.cdap.common.io.Locations) ProgramLifecycleService(io.cdap.cdap.internal.app.services.ProgramLifecycleService) CapabilitySleepingWorkflowPluginApp(io.cdap.cdap.CapabilitySleepingWorkflowPluginApp) ProgramId(io.cdap.cdap.proto.id.ProgramId) FileWriter(java.io.FileWriter) ProgramDescriptor(io.cdap.cdap.app.program.ProgramDescriptor) Test(org.junit.Test) IOException(java.io.IOException) LocationFactory(org.apache.twill.filesystem.LocationFactory) File(java.io.File) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) WorkflowAppWithFork(io.cdap.cdap.WorkflowAppWithFork) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) FileReader(java.io.FileReader) AllProgramsApp(io.cdap.cdap.AllProgramsApp) Assert(org.junit.Assert) Collections(java.util.Collections) WorkflowAppWithFork(io.cdap.cdap.WorkflowAppWithFork) Requirements(io.cdap.cdap.api.annotation.Requirements) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) CapabilityAppWithWorkflow(io.cdap.cdap.CapabilityAppWithWorkflow) File(java.io.File) Test(org.junit.Test)

Example 2 with Requirements

use of io.cdap.cdap.api.annotation.Requirements in project cdap by caskdata.

the class CapabilityApplierTest method testGetAppsWithCapability.

@Test
public void testGetAppsWithCapability() throws Exception {
    // Deploy application with capability
    Class<CapabilityAppWithWorkflow> appWithWorkflowClass = CapabilityAppWithWorkflow.class;
    Requirements declaredAnnotation = appWithWorkflowClass.getDeclaredAnnotation(Requirements.class);
    // verify this app has capabilities
    Assert.assertTrue(declaredAnnotation.capabilities().length > 0);
    String appNameWithCapabilities = appWithWorkflowClass.getSimpleName() + UUID.randomUUID();
    for (String capability : declaredAnnotation.capabilities()) {
        CapabilityConfig capabilityConfig = new CapabilityConfig("Enable", CapabilityStatus.ENABLED, capability, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
        capabilityWriter.addOrUpdateCapability(capability, CapabilityStatus.ENABLED, capabilityConfig);
    }
    deployArtifactAndApp(appWithWorkflowClass, appNameWithCapabilities);
    // Deploy application without capability
    Class<WorkflowAppWithFork> appNoCapabilityClass = WorkflowAppWithFork.class;
    Requirements declaredAnnotation1 = appNoCapabilityClass.getDeclaredAnnotation(Requirements.class);
    // verify this app has no capabilities
    Assert.assertNull(declaredAnnotation1);
    String appNameWithoutCapability = appNoCapabilityClass.getSimpleName() + UUID.randomUUID();
    deployArtifactAndApp(appNoCapabilityClass, appNameWithoutCapability);
    // verify that list applications return the application tagged with capability only
    for (String capability : declaredAnnotation.capabilities()) {
        EntityResult<ApplicationId> appsForCapability = capabilityApplier.getApplications(NamespaceId.DEFAULT, capability, null, 0, 10);
        Set<ApplicationId> applicationIds = new HashSet<>(appsForCapability.getEntities());
        List<ApplicationDetail> appsReturned = new ArrayList<>(applicationLifecycleService.getAppDetails(applicationIds).values());
        appsReturned.forEach(applicationDetail -> Assert.assertEquals(appNameWithCapabilities, applicationDetail.getArtifact().getName()));
    }
    // delete the app and verify nothing is returned.
    applicationLifecycleService.removeApplication(NamespaceId.DEFAULT.app(appNameWithCapabilities, TEST_VERSION));
    for (String capability : declaredAnnotation.capabilities()) {
        Set<ApplicationId> applicationIds = new HashSet<>(capabilityApplier.getApplications(NamespaceId.DEFAULT, capability, null, 0, 10).getEntities());
        List<ApplicationDetail> appsReturned = new ArrayList<>(applicationLifecycleService.getAppDetails(applicationIds).values());
        Assert.assertTrue(appsReturned.isEmpty());
    }
    applicationLifecycleService.removeApplication(NamespaceId.DEFAULT.app(appNameWithoutCapability, TEST_VERSION));
    artifactRepository.deleteArtifact(Id.Artifact.from(new Id.Namespace(NamespaceId.DEFAULT.getNamespace()), appNameWithoutCapability, TEST_VERSION));
    for (String capability : declaredAnnotation.capabilities()) {
        capabilityWriter.deleteCapability(capability);
    }
}
Also used : WorkflowAppWithFork(io.cdap.cdap.WorkflowAppWithFork) ArrayList(java.util.ArrayList) Requirements(io.cdap.cdap.api.annotation.Requirements) ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) CapabilityAppWithWorkflow(io.cdap.cdap.CapabilityAppWithWorkflow) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Requirements

use of io.cdap.cdap.api.annotation.Requirements in project cdap by caskdata.

the class ApplicationLifecycleServiceTest method testCapabilityMetaDataDeletion.

@Test
public void testCapabilityMetaDataDeletion() throws Exception {
    Class<CapabilityAppWithWorkflow> appWithWorkflowClass = CapabilityAppWithWorkflow.class;
    Requirements declaredAnnotation = appWithWorkflowClass.getDeclaredAnnotation(Requirements.class);
    Set<String> expected = Arrays.stream(declaredAnnotation.capabilities()).collect(Collectors.toSet());
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, appWithWorkflowClass.getSimpleName(), "1.0.0-SNAPSHOT");
    Location appJar = AppJarHelper.createDeploymentJar(locationFactory, appWithWorkflowClass);
    File appJarFile = new File(tmpFolder.newFolder(), String.format("%s-%s.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
    Locations.linkOrCopyOverwrite(appJar, appJarFile);
    appJar.delete();
    // deploy app
    try {
        applicationLifecycleService.deployAppAndArtifact(NamespaceId.DEFAULT, appWithWorkflowClass.getSimpleName(), artifactId, appJarFile, null, null, programId -> {
        }, true);
        Assert.fail("Expecting exception");
    } catch (CapabilityNotAvailableException ex) {
    // expected
    }
    for (String capability : declaredAnnotation.capabilities()) {
        CapabilityConfig capabilityConfig = new CapabilityConfig("Test", CapabilityStatus.ENABLED, capability, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
        capabilityWriter.addOrUpdateCapability(capability, CapabilityStatus.ENABLED, capabilityConfig);
    }
    applicationLifecycleService.deployAppAndArtifact(NamespaceId.DEFAULT, appWithWorkflowClass.getSimpleName(), artifactId, appJarFile, null, null, programId -> {
    }, true);
    // Check for the capability metadata
    ApplicationId appId = NamespaceId.DEFAULT.app(appWithWorkflowClass.getSimpleName());
    MetadataEntity appMetadataId = appId.toMetadataEntity();
    Assert.assertFalse(metadataStorage.read(new Read(appMetadataId, MetadataScope.SYSTEM, MetadataKind.PROPERTY)).isEmpty());
    Map<String, String> metadataProperties = metadataStorage.read(new Read(appMetadataId)).getProperties(MetadataScope.SYSTEM);
    String capabilityMetaData = metadataProperties.get(AppSystemMetadataWriter.CAPABILITY_TAG);
    Set<String> actual = Arrays.stream(capabilityMetaData.split(AppSystemMetadataWriter.CAPABILITY_DELIMITER)).collect(Collectors.toSet());
    Assert.assertEquals(expected, actual);
    // Remove the application and verify that all metadata is removed
    applicationLifecycleService.removeApplication(appId);
    Assert.assertTrue(metadataStorage.read(new Read(appMetadataId)).isEmpty());
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) CapabilityNotAvailableException(io.cdap.cdap.internal.capability.CapabilityNotAvailableException) CapabilityConfig(io.cdap.cdap.internal.capability.CapabilityConfig) Requirements(io.cdap.cdap.api.annotation.Requirements) Read(io.cdap.cdap.spi.metadata.Read) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ProgramId(io.cdap.cdap.proto.id.ProgramId) CapabilityAppWithWorkflow(io.cdap.cdap.CapabilityAppWithWorkflow) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) File(java.io.File) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 4 with Requirements

use of io.cdap.cdap.api.annotation.Requirements in project cdap by caskdata.

the class CapabilityApplierTest method testGetAppsForCapabilityPagination.

@Test
public void testGetAppsForCapabilityPagination() throws Exception {
    // Deploy two applications with capability
    Class<CapabilityAppWithWorkflow> appWithWorkflowClass = CapabilityAppWithWorkflow.class;
    Requirements declaredAnnotation = appWithWorkflowClass.getDeclaredAnnotation(Requirements.class);
    // verify this app has capabilities
    Assert.assertTrue(declaredAnnotation.capabilities().length > 0);
    String appNameWithCapability1 = appWithWorkflowClass.getSimpleName() + UUID.randomUUID();
    for (String capability : declaredAnnotation.capabilities()) {
        capabilityWriter.addOrUpdateCapability(capability, CapabilityStatus.ENABLED, new CapabilityConfig("Enable", CapabilityStatus.ENABLED, appNameWithCapability1, Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
    }
    deployArtifactAndApp(appWithWorkflowClass, appNameWithCapability1);
    String appNameWithCapability2 = appWithWorkflowClass.getSimpleName() + UUID.randomUUID();
    deployArtifactAndApp(appWithWorkflowClass, appNameWithCapability2);
    // search with offset and limit
    String capability = declaredAnnotation.capabilities()[0];
    EntityResult<ApplicationId> appsForCapability = capabilityApplier.getApplications(NamespaceId.DEFAULT, capability, null, 0, 1);
    Assert.assertEquals(1, appsForCapability.getEntities().size());
    // next search with pagination
    EntityResult<ApplicationId> appsForCapabilityNext = capabilityApplier.getApplications(NamespaceId.DEFAULT, capability, appsForCapability.getCursor(), 1, 1);
    Assert.assertEquals(1, appsForCapabilityNext.getEntities().size());
    appsForCapabilityNext = capabilityApplier.getApplications(NamespaceId.DEFAULT, capability, appsForCapability.getCursor(), 2, 1);
    Assert.assertEquals(0, appsForCapabilityNext.getEntities().size());
    applicationLifecycleService.removeApplication(NamespaceId.DEFAULT.app(appNameWithCapability1, TEST_VERSION));
    applicationLifecycleService.removeApplication(NamespaceId.DEFAULT.app(appNameWithCapability2, TEST_VERSION));
    artifactRepository.deleteArtifact(Id.Artifact.from(new Id.Namespace(NamespaceId.DEFAULT.getNamespace()), appNameWithCapability1, TEST_VERSION));
    artifactRepository.deleteArtifact(Id.Artifact.from(new Id.Namespace(NamespaceId.DEFAULT.getNamespace()), appNameWithCapability2, TEST_VERSION));
    for (String capability1 : declaredAnnotation.capabilities()) {
        capabilityWriter.deleteCapability(capability1);
    }
}
Also used : CapabilityAppWithWorkflow(io.cdap.cdap.CapabilityAppWithWorkflow) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Requirements(io.cdap.cdap.api.annotation.Requirements) Test(org.junit.Test)

Aggregations

CapabilityAppWithWorkflow (io.cdap.cdap.CapabilityAppWithWorkflow)4 Requirements (io.cdap.cdap.api.annotation.Requirements)4 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)4 Test (org.junit.Test)4 WorkflowAppWithFork (io.cdap.cdap.WorkflowAppWithFork)2 Id (io.cdap.cdap.common.id.Id)2 ApplicationDetail (io.cdap.cdap.proto.ApplicationDetail)2 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)2 ProgramId (io.cdap.cdap.proto.id.ProgramId)2 File (java.io.File)2 Location (org.apache.twill.filesystem.Location)2 Files (com.google.common.io.Files)1 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 AllProgramsApp (io.cdap.cdap.AllProgramsApp)1 CapabilitySleepingWorkflowApp (io.cdap.cdap.CapabilitySleepingWorkflowApp)1 CapabilitySleepingWorkflowPluginApp (io.cdap.cdap.CapabilitySleepingWorkflowPluginApp)1 ArtifactScope (io.cdap.cdap.api.artifact.ArtifactScope)1 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)1 MetadataEntity (io.cdap.cdap.api.metadata.MetadataEntity)1