Search in sources :

Example 11 with ArtifactInfo

use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.

the class DefaultArtifactRepository method addArtifact.

@Override
public ArtifactDetail addArtifact(final Id.Artifact artifactId, final File artifactFile, @Nullable Set<ArtifactRange> parentArtifacts, @Nullable Set<PluginClass> additionalPlugins, Map<String, String> properties) throws Exception {
    if (additionalPlugins != null) {
        validatePluginSet(additionalPlugins);
    }
    parentArtifacts = parentArtifacts == null ? Collections.emptySet() : parentArtifacts;
    CloseableClassLoader parentClassLoader = null;
    EntityImpersonator entityImpersonator = new EntityImpersonator(artifactId.toEntityId(), impersonator);
    if (!parentArtifacts.isEmpty()) {
        validateParentSet(artifactId, parentArtifacts);
        parentClassLoader = createParentClassLoader(artifactId, parentArtifacts, entityImpersonator);
    }
    try {
        ArtifactClasses artifactClasses = inspectArtifact(artifactId, artifactFile, additionalPlugins, parentClassLoader);
        ArtifactMeta meta = new ArtifactMeta(artifactClasses, parentArtifacts, properties);
        ArtifactDetail artifactDetail = artifactStore.write(artifactId, meta, Files.newInputStreamSupplier(artifactFile), entityImpersonator);
        ArtifactDescriptor descriptor = artifactDetail.getDescriptor();
        // info hides some fields that are available in detail, such as the location of the artifact
        ArtifactInfo artifactInfo = new ArtifactInfo(descriptor.getArtifactId(), artifactDetail.getMeta().getClasses(), artifactDetail.getMeta().getProperties());
        // add system metadata for artifacts
        writeSystemMetadata(artifactId.toEntityId(), artifactInfo);
        return artifactDetail;
    } finally {
        Closeables.closeQuietly(parentClassLoader);
    }
}
Also used : ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) ArtifactClasses(co.cask.cdap.api.artifact.ArtifactClasses) EntityImpersonator(co.cask.cdap.security.impersonation.EntityImpersonator) CloseableClassLoader(co.cask.cdap.api.artifact.CloseableClassLoader)

Example 12 with ArtifactInfo

use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.

the class ArtifactHttpHandlerTest method testAddAndGet.

@Test
public void testAddAndGet() throws Exception {
    File wordCountArtifact = buildAppArtifact(WordCountApp.class, "wordcount.jar");
    File configTestArtifact = buildAppArtifact(ConfigTestApp.class, "cfgtest.jar");
    // add 2 versions of the same app that doesn't use config
    ArtifactId wordcountId1 = NamespaceId.DEFAULT.artifact("wordcount", "1.0.0");
    ArtifactId wordcountId2 = NamespaceId.DEFAULT.artifact("wordcount", "2.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.code(), addArtifact(Id.Artifact.fromEntityId(wordcountId1), Files.newInputStreamSupplier(wordCountArtifact), null).getStatusLine().getStatusCode());
    Assert.assertEquals(HttpResponseStatus.OK.code(), addArtifact(Id.Artifact.fromEntityId(wordcountId2), Files.newInputStreamSupplier(wordCountArtifact), null).getStatusLine().getStatusCode());
    // and 1 version of another app that uses a config
    ArtifactId configTestAppId = NamespaceId.DEFAULT.artifact("cfgtest", "1.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.code(), addArtifact(Id.Artifact.fromEntityId(configTestAppId), Files.newInputStreamSupplier(configTestArtifact), null).getStatusLine().getStatusCode());
    // test get /artifacts endpoint
    Set<ArtifactSummary> expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0"), new ArtifactSummary("wordcount", "2.0.0"), new ArtifactSummary("cfgtest", "1.0.0"));
    Set<ArtifactSummary> actualArtifacts = getArtifacts(NamespaceId.DEFAULT);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts/wordcount endpoint
    expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0"), new ArtifactSummary("wordcount", "2.0.0"));
    actualArtifacts = getArtifacts(NamespaceId.DEFAULT, "wordcount");
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts/cfgtest/versions/1.0.0 endpoint
    Schema appConfigSchema = schemaGenerator.generate(ConfigTestApp.ConfigClass.class);
    ArtifactInfo actualInfo = getArtifact(configTestAppId);
    Assert.assertEquals("cfgtest", actualInfo.getName());
    Assert.assertEquals("1.0.0", actualInfo.getVersion());
    ApplicationClass appClass = new ApplicationClass(ConfigTestApp.class.getName(), "", appConfigSchema);
    Assert.assertTrue(actualInfo.getClasses().getApps().contains(appClass));
}
Also used : ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) Schema(co.cask.cdap.api.data.schema.Schema) ApplicationClass(co.cask.cdap.api.artifact.ApplicationClass) File(java.io.File) ConfigTestApp(co.cask.cdap.ConfigTestApp) Test(org.junit.Test)

Example 13 with ArtifactInfo

use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.

the class ArtifactHttpHandlerTest method testSystemArtifacts.

@Test
public void testSystemArtifacts() throws Exception {
    // add the app in the default namespace
    File systemArtifact = buildAppArtifact(WordCountApp.class, "wordcount-1.0.0.jar");
    ArtifactId defaultId = NamespaceId.DEFAULT.artifact("wordcount", "1.0.0");
    Assert.assertEquals(HttpResponseStatus.OK.code(), addArtifact(Id.Artifact.fromEntityId(defaultId), Files.newInputStreamSupplier(systemArtifact), null).getStatusLine().getStatusCode());
    // add system artifact
    addSystemArtifacts();
    // test get /artifacts
    Set<ArtifactSummary> expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.USER), new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
    Set<ArtifactSummary> actualArtifacts = getArtifacts(NamespaceId.DEFAULT);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts?scope=system
    expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
    actualArtifacts = getArtifacts(NamespaceId.DEFAULT, ArtifactScope.SYSTEM);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts?scope=user
    expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.USER));
    actualArtifacts = getArtifacts(NamespaceId.DEFAULT, ArtifactScope.USER);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts/wordcount?scope=user
    expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.USER));
    actualArtifacts = getArtifacts(NamespaceId.DEFAULT, "wordcount", ArtifactScope.USER);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts/wordcount?scope=system
    expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
    actualArtifacts = getArtifacts(NamespaceId.DEFAULT, "wordcount", ArtifactScope.SYSTEM);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts/wordcount/versions/1.0.0?scope=user
    ArtifactInfo actualInfo = getArtifact(defaultId, ArtifactScope.USER);
    Assert.assertEquals("wordcount", actualInfo.getName());
    Assert.assertEquals("1.0.0", actualInfo.getVersion());
    // test get /artifacts/wordcount/versions/1.0.0?scope=system
    actualInfo = getArtifact(defaultId, ArtifactScope.SYSTEM);
    Assert.assertEquals("wordcount", actualInfo.getName());
    Assert.assertEquals("1.0.0", actualInfo.getVersion());
    try {
        ArtifactId systemId = NamespaceId.SYSTEM.artifact("wordcount", "1.0.0");
        artifactRepository.addArtifact(Id.Artifact.fromEntityId(systemId), systemArtifact, new HashSet<ArtifactRange>(), null);
        Assert.fail();
    } catch (Exception e) {
    // no-op - expected exception while adding artifact in system namespace
    }
    cleanupSystemArtifactsDirectory();
}
Also used : ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) File(java.io.File) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) NotFoundException(co.cask.cdap.common.NotFoundException) Test(org.junit.Test)

Example 14 with ArtifactInfo

use of co.cask.cdap.api.artifact.ArtifactInfo in project cdap by caskdata.

the class ArtifactHttpHandlerTest method testSystemArtifactIsolation.

/**
 * Tests that system artifacts can be accessed in a user namespace only if appropriately scoped.
 */
@Test
public void testSystemArtifactIsolation() throws Exception {
    // add the app in the default namespace
    ArtifactId defaultId = NamespaceId.DEFAULT.artifact("wordcount", "1.0.0");
    // add system artifact wordcount
    addSystemArtifacts();
    // test get /artifacts
    Set<ArtifactSummary> expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
    Set<ArtifactSummary> actualArtifacts = getArtifacts(NamespaceId.DEFAULT);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts?scope=system
    expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
    actualArtifacts = getArtifacts(NamespaceId.DEFAULT, ArtifactScope.SYSTEM);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts?scope=user
    expectedArtifacts = Sets.newHashSet();
    actualArtifacts = getArtifacts(NamespaceId.DEFAULT, ArtifactScope.USER);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts/wordcount?scope=user
    actualArtifacts = getArtifacts(NamespaceId.DEFAULT, "wordcount", ArtifactScope.USER);
    Assert.assertNull(actualArtifacts);
    // test get /artifacts/wordcount?scope=system
    expectedArtifacts = Sets.newHashSet(new ArtifactSummary("wordcount", "1.0.0", ArtifactScope.SYSTEM));
    actualArtifacts = getArtifacts(NamespaceId.DEFAULT, "wordcount", ArtifactScope.SYSTEM);
    Assert.assertEquals(expectedArtifacts, actualArtifacts);
    // test get /artifacts/wordcount/versions/1.0.0?scope=user
    ArtifactInfo actualInfo = getArtifact(defaultId, ArtifactScope.USER);
    Assert.assertEquals(null, actualInfo);
    // test get /artifacts/wordcount/versions/1.0.0?scope=system
    actualInfo = getArtifact(defaultId, ArtifactScope.SYSTEM);
    Assert.assertEquals("wordcount", actualInfo.getName());
    Assert.assertEquals("1.0.0", actualInfo.getVersion());
    // test delete /default/artifacts/wordcount/versions/1.0.0
    deleteArtifact(Id.Artifact.fromEntityId(defaultId), 404);
    cleanupSystemArtifactsDirectory();
}
Also used : ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) Test(org.junit.Test)

Aggregations

ArtifactInfo (co.cask.cdap.api.artifact.ArtifactInfo)14 ArtifactId (co.cask.cdap.proto.id.ArtifactId)8 IOException (java.io.IOException)5 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)4 Test (org.junit.Test)4 ArtifactClasses (co.cask.cdap.api.artifact.ArtifactClasses)3 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)3 File (java.io.File)3 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)2 ArtifactScope (co.cask.cdap.api.artifact.ArtifactScope)2 CloseableClassLoader (co.cask.cdap.api.artifact.CloseableClassLoader)2 Schema (co.cask.cdap.api.data.schema.Schema)2 Table (co.cask.cdap.cli.util.table.Table)2 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)2 ArtifactDetail (co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 EntityImpersonator (co.cask.cdap.security.impersonation.EntityImpersonator)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 ConfigTestApp (co.cask.cdap.ConfigTestApp)1