Search in sources :

Example 21 with ArtifactId

use of io.cdap.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class AppLifecycleHttpHandlerTest method testDelete.

/**
 * Tests deleting applications with versioned and non-versioned API.
 */
@Test
public void testDelete() throws Exception {
    // Delete an non-existing app
    HttpResponse response = doDelete(getVersionedAPIPath("apps/XYZ", Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
    Assert.assertEquals(404, response.getResponseCode());
    // Start a service from the App
    deploy(AllProgramsApp.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
    Id.Program program = Id.Program.from(TEST_NAMESPACE1, AllProgramsApp.NAME, ProgramType.SERVICE, AllProgramsApp.NoOpService.NAME);
    startProgram(program);
    waitState(program, "RUNNING");
    // Try to delete an App while its service is running
    response = doDelete(getVersionedAPIPath("apps/" + AllProgramsApp.NAME, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
    Assert.assertEquals(409, response.getResponseCode());
    Assert.assertEquals("'" + program.getApplication() + "' could not be deleted. Reason: The following programs are still running: " + program.getId(), response.getResponseBodyAsString());
    stopProgram(program);
    waitState(program, "STOPPED");
    startProgram(program);
    waitState(program, "RUNNING");
    // Try to delete all Apps while service is running
    response = doDelete(getVersionedAPIPath("apps", Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
    Assert.assertEquals(409, response.getResponseCode());
    Assert.assertEquals("'" + program.getNamespace() + "' could not be deleted. Reason: The following programs are still running: " + program.getApplicationId() + ": " + program.getId(), response.getResponseBodyAsString());
    stopProgram(program);
    waitState(program, "STOPPED");
    // Delete the app in the wrong namespace
    response = doDelete(getVersionedAPIPath("apps/" + AllProgramsApp.NAME, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE2));
    Assert.assertEquals(404, response.getResponseCode());
    // Delete an non-existing app with version
    response = doDelete(getVersionedAPIPath("apps/XYZ/versions/" + VERSION1, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
    Assert.assertEquals(404, response.getResponseCode());
    // Deploy an app with version
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, AllProgramsApp.class.getSimpleName(), VERSION1);
    addAppArtifact(artifactId, AllProgramsApp.class);
    AppRequest<? extends Config> appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()));
    ApplicationId appId = NamespaceId.DEFAULT.app(AllProgramsApp.NAME, VERSION1);
    Assert.assertEquals(200, deploy(appId, appRequest).getResponseCode());
    // Start a service for the App
    ProgramId program1 = appId.program(ProgramType.SERVICE, AllProgramsApp.NoOpService.NAME);
    startProgram(program1, 200);
    waitState(program1, "RUNNING");
    // Try to delete an App while its service is running
    response = doDelete(getVersionedAPIPath(String.format("apps/%s/versions/%s", appId.getApplication(), appId.getVersion()), Constants.Gateway.API_VERSION_3_TOKEN, appId.getNamespace()));
    Assert.assertEquals(409, response.getResponseCode());
    Assert.assertEquals("'" + program1.getParent() + "' could not be deleted. Reason: The following programs" + " are still running: " + program1.getProgram(), response.getResponseBodyAsString());
    stopProgram(program1, null, 200, null);
    waitState(program1, "STOPPED");
    // Delete the app with version in the wrong namespace
    response = doDelete(getVersionedAPIPath(String.format("apps/%s/versions/%s", appId.getApplication(), appId.getVersion()), Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE2));
    Assert.assertEquals(404, response.getResponseCode());
    // Delete the app with version after stopping the service
    response = doDelete(getVersionedAPIPath(String.format("apps/%s/versions/%s", appId.getApplication(), appId.getVersion()), Constants.Gateway.API_VERSION_3_TOKEN, appId.getNamespace()));
    Assert.assertEquals(200, response.getResponseCode());
    response = doDelete(getVersionedAPIPath(String.format("apps/%s/versions/%s", appId.getApplication(), appId.getVersion()), Constants.Gateway.API_VERSION_3_TOKEN, appId.getNamespace()));
    Assert.assertEquals(404, response.getResponseCode());
    // Delete the App after stopping the service
    response = doDelete(getVersionedAPIPath("apps/" + AllProgramsApp.NAME, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
    Assert.assertEquals(200, response.getResponseCode());
    response = doDelete(getVersionedAPIPath("apps/" + AllProgramsApp.NAME, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
    Assert.assertEquals(404, response.getResponseCode());
    // deleting the app should not delete the artifact
    response = doGet(getVersionedAPIPath("artifacts/" + artifactId.getName(), Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
    Assert.assertEquals(200, response.getResponseCode());
    List<ArtifactSummary> summaries = readResponse(response, new TypeToken<List<ArtifactSummary>>() {
    }.getType());
    Assert.assertFalse(summaries.isEmpty());
    // cleanup
    deleteNamespace(NamespaceId.DEFAULT.getNamespace());
}
Also used : ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) TypeToken(com.google.gson.reflect.TypeToken) HttpResponse(io.cdap.common.http.HttpResponse) AllProgramsApp(io.cdap.cdap.AllProgramsApp) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ProfileId(io.cdap.cdap.proto.id.ProfileId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ProgramId(io.cdap.cdap.proto.id.ProgramId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) Test(org.junit.Test)

Example 22 with ArtifactId

use of io.cdap.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class AppLifecycleHttpHandlerTest method testDeployAppWithDisabledProfileInSchedule.

@Test
public void testDeployAppWithDisabledProfileInSchedule() throws Exception {
    // put my profile and disable it
    ProfileId profileId = new NamespaceId(TEST_NAMESPACE1).profile("MyProfile");
    Profile profile = new Profile("MyProfile", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), Profile.NATIVE.getProvisioner());
    putProfile(profileId, profile, 200);
    disableProfile(profileId, 200);
    // deploy an app with schedule with some disabled profile in the schedule property
    AppWithSchedule.AppConfig config = new AppWithSchedule.AppConfig(true, true, true, ImmutableMap.of(SystemArguments.PROFILE_NAME, "USER:MyProfile"));
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.fromEntityId(TEST_NAMESPACE_META1.getNamespaceId()), AppWithSchedule.NAME, VERSION1);
    addAppArtifact(artifactId, AppWithSchedule.class);
    AppRequest<? extends Config> request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), config, null, null, true);
    // deploy should fail with a 409
    ApplicationId defaultAppId = TEST_NAMESPACE_META1.getNamespaceId().app(AppWithSchedule.NAME);
    Assert.assertEquals(409, deploy(defaultAppId, request).getResponseCode());
    // enable
    enableProfile(profileId, 200);
    Assert.assertEquals(200, deploy(defaultAppId, request).getResponseCode());
    // disable again so that we can delete it at namespace deletion
    disableProfile(profileId, 200);
    // clean up
    deleteApp(defaultAppId, 200);
    deleteArtifact(artifactId, 200);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ProfileId(io.cdap.cdap.proto.id.ProfileId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ProgramId(io.cdap.cdap.proto.id.ProgramId) AppWithSchedule(io.cdap.cdap.AppWithSchedule) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Profile(io.cdap.cdap.proto.profile.Profile) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) Test(org.junit.Test)

Example 23 with ArtifactId

use of io.cdap.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class AppLifecycleHttpHandlerTest method testOwnerUsingArtifact.

@Test
public void testOwnerUsingArtifact() throws Exception {
    ArtifactId artifactId = new ArtifactId(NamespaceId.DEFAULT.getNamespace(), "artifact", "1.0.0");
    addAppArtifact(Id.Artifact.fromEntityId(artifactId), AllProgramsApp.class);
    ApplicationId applicationId = new ApplicationId(NamespaceId.DEFAULT.getNamespace(), AllProgramsApp.NAME);
    // deploy an app with a owner
    String ownerPrincipal = "alice/somehost.net@somekdc.net";
    AppRequest<ConfigTestApp.ConfigClass> appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null, ownerPrincipal);
    Assert.assertEquals(HttpResponseCodes.SC_OK, deploy(applicationId, appRequest).getResponseCode());
    // should be able to retrieve the owner information of the app
    JsonObject appDetails = getAppDetails(NamespaceId.DEFAULT.getNamespace(), applicationId.getApplication());
    Assert.assertEquals(ownerPrincipal, appDetails.get(Constants.Security.PRINCIPAL).getAsString());
    // the dataset created by the app should have the app owner too
    Assert.assertEquals(ownerPrincipal, getDatasetMeta(applicationId.getNamespaceId().dataset(AllProgramsApp.DATASET_NAME)).getOwnerPrincipal());
    // trying to deploy the same app with another owner should fail
    String bobPrincipal = "bob/somehost.net@somekdc.net";
    appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null, bobPrincipal);
    Assert.assertEquals(HttpResponseCodes.SC_FORBIDDEN, deploy(applicationId, appRequest).getResponseCode());
    // trying to deploy the same app with different version and another owner should fail too
    appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null, bobPrincipal);
    Assert.assertEquals(HttpResponseCodes.SC_FORBIDDEN, deploy(new ApplicationId(applicationId.getNamespace(), applicationId.getApplication(), "1.0"), appRequest).getResponseCode());
    // trying to re-deploy the same app with same owner should pass
    appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null, ownerPrincipal);
    Assert.assertEquals(HttpResponseCodes.SC_OK, deploy(applicationId, appRequest).getResponseCode());
    // trying to re-deploy the same app with different version but same owner should pass
    appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null, ownerPrincipal);
    Assert.assertEquals(HttpResponseCodes.SC_OK, deploy(new ApplicationId(applicationId.getNamespace(), applicationId.getApplication(), "1.0"), appRequest).getResponseCode());
    // clean up the app
    Assert.assertEquals(200, doDelete(getVersionedAPIPath("apps/" + applicationId.getApplication(), applicationId.getNamespace())).getResponseCode());
    // deletion of app should delete the dataset owner information as they themselves are not deleted
    Assert.assertEquals(ownerPrincipal, getDatasetMeta(applicationId.getNamespaceId().dataset(AllProgramsApp.DATASET_NAME)).getOwnerPrincipal());
    // cleanup
    deleteNamespace(NamespaceId.DEFAULT.getNamespace());
}
Also used : ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) JsonObject(com.google.gson.JsonObject) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) Test(org.junit.Test)

Example 24 with ArtifactId

use of io.cdap.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class ApplicationLifecycleServiceTest method testDeployArtifactAndApplicationCleansUpArtifactOnFailure.

// test that the call to deploy an artifact and application in a single step will delete the artifact
// if the application could not be created
@Test(expected = ArtifactNotFoundException.class)
public void testDeployArtifactAndApplicationCleansUpArtifactOnFailure() throws Exception {
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "missing-mr", "1.0.0-SNAPSHOT");
    Location appJar = AppJarHelper.createDeploymentJar(locationFactory, MissingMapReduceWorkflowApp.class);
    File appJarFile = new File(tmpFolder.newFolder(), String.format("%s-%s.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
    Locations.linkOrCopyOverwrite(appJar, appJarFile);
    appJar.delete();
    try {
        applicationLifecycleService.deployAppAndArtifact(NamespaceId.DEFAULT, "appName", artifactId, appJarFile, null, null, programId -> {
        }, true);
        Assert.fail("expected application deployment to fail.");
    } catch (Exception e) {
    // expected
    }
    // the artifact should have been cleaned up, and this should throw a not found exception
    artifactRepository.getArtifact(artifactId);
}
Also used : 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) File(java.io.File) CapabilityNotAvailableException(io.cdap.cdap.internal.capability.CapabilityNotAvailableException) IOException(java.io.IOException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 25 with ArtifactId

use of io.cdap.cdap.proto.id.ArtifactId 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)

Aggregations

ArtifactId (io.cdap.cdap.proto.id.ArtifactId)214 Test (org.junit.Test)118 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)94 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)68 IOException (java.io.IOException)50 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)42 ProgramId (io.cdap.cdap.proto.id.ProgramId)42 Id (io.cdap.cdap.common.id.Id)40 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)40 File (java.io.File)34 PluginClass (io.cdap.cdap.api.plugin.PluginClass)32 Path (javax.ws.rs.Path)32 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)30 Set (java.util.Set)30 ImmutableSet (com.google.common.collect.ImmutableSet)28 Location (org.apache.twill.filesystem.Location)28 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)26 ArtifactDetail (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)26 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)22 EntityId (io.cdap.cdap.proto.id.EntityId)20