Search in sources :

Example 66 with ArtifactRange

use of io.cdap.cdap.api.artifact.ArtifactRange in project cdap by caskdata.

the class ArtifactStoreTest method testPluginNamespaceIsolation.

@Test
public void testPluginNamespaceIsolation() throws Exception {
    // write some system artifact
    Id.Artifact systemAppArtifact = Id.Artifact.from(Id.Namespace.SYSTEM, "app", "1.0.0");
    ArtifactMeta systemAppMeta = new ArtifactMeta(ArtifactClasses.builder().addApp(new ApplicationClass("io.cdap.class", "desc", null)).build());
    writeArtifact(systemAppArtifact, systemAppMeta, "app contents");
    Set<ArtifactRange> usableBy = ImmutableSet.of(new ArtifactRange(systemAppArtifact.getNamespace().getId(), systemAppArtifact.getName(), systemAppArtifact.getVersion(), true, systemAppArtifact.getVersion(), true));
    PluginClass plugin = PluginClass.builder().setName("plugin1").setType("atype").setDescription("").setClassName("c.c.c.plugin1").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build();
    // write a plugins artifact in namespace1
    NamespaceId namespace1 = Ids.namespace("ns1");
    Id.Artifact artifact1 = Id.Artifact.from((Id.Namespace.fromEntityId(namespace1)), "plugins1", "1.0.0");
    ArtifactMeta meta1 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin).build(), usableBy);
    String contents1 = "plugin1 contents";
    writeArtifact(artifact1, meta1, contents1);
    // write a plugins artifact in namespace2
    NamespaceId namespace2 = Ids.namespace("ns2");
    Id.Artifact artifact2 = Id.Artifact.from(Id.Namespace.fromEntityId(namespace2), "plugins2", "1.0.0");
    ArtifactMeta meta2 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin).build(), usableBy);
    String contents2 = "plugin2 contents";
    writeArtifact(artifact2, meta2, contents2);
    try {
        // this should only get plugins from artifact1
        SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = artifactStore.getPluginClasses(namespace1, systemAppArtifact);
        Assert.assertEquals(1, plugins.size());
        ArtifactDescriptor artifactDescriptor = plugins.firstKey();
        Assert.assertEquals(artifact1.toArtifactId(), artifactDescriptor.getArtifactId());
        assertContentsEqual(contents1, artifactDescriptor.getLocation());
        // this should only get plugins from artifact2
        plugins = artifactStore.getPluginClasses(namespace2, systemAppArtifact);
        Assert.assertEquals(1, plugins.size());
        artifactDescriptor = plugins.firstKey();
        Assert.assertEquals(artifact2.toArtifactId(), artifactDescriptor.getArtifactId());
        assertContentsEqual(contents2, artifactDescriptor.getLocation());
    } finally {
        artifactStore.clear(namespace1);
        artifactStore.clear(namespace2);
        artifactStore.clear(NamespaceId.SYSTEM);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 67 with ArtifactRange

use of io.cdap.cdap.api.artifact.ArtifactRange in project cdap by caskdata.

the class PluginFinderTestBase method testFindPlugin.

@Test
public void testFindPlugin() throws Exception {
    // Deploy some artifacts
    File appArtifact = createAppJar(PluginTestApp.class);
    File pluginArtifact = createPluginJar(Plugin1.class);
    ArtifactId appArtifactId = NamespaceId.DEFAULT.artifact("app", "1.0");
    artifactRepo.addArtifact(Id.Artifact.fromEntityId(appArtifactId), appArtifact);
    ArtifactId pluginArtifactId = NamespaceId.DEFAULT.artifact("plugin", "1.0");
    artifactRepo.addArtifact(Id.Artifact.fromEntityId(pluginArtifactId), pluginArtifact, Collections.singleton(new ArtifactRange(appArtifactId.getNamespace(), appArtifactId.getArtifact(), new ArtifactVersion(appArtifactId.getVersion()), true, new ArtifactVersion(appArtifactId.getVersion()), true)), null);
    // Find the plugin
    PluginFinder finder = getPluginFinder();
    Map.Entry<ArtifactDescriptor, PluginClass> entry = finder.findPlugin(NamespaceId.DEFAULT, appArtifactId, "dummy", "Plugin1", new PluginSelector());
    Assert.assertNotNull(entry);
    Assert.assertEquals(pluginArtifactId.toApiArtifactId(), entry.getKey().getArtifactId());
    Assert.assertEquals("Plugin1", entry.getValue().getName());
    Assert.assertEquals("dummy", entry.getValue().getType());
    // Looking for a non-existing should throw
    try {
        finder.findPlugin(NamespaceId.DEFAULT, appArtifactId, "dummy2", "pluginx", new PluginSelector());
        Assert.fail("A PluginNotExistsException is expected");
    } catch (PluginNotExistsException e) {
    // expected
    }
    // Deploy the same plugin artifact to the system namespace, without any parent
    ArtifactId systemPluginArtifactId = NamespaceId.SYSTEM.artifact("pluginsystem", "1.0");
    artifactRepo.addArtifact(Id.Artifact.fromEntityId(systemPluginArtifactId), pluginArtifact);
    entry = finder.findPlugin(NamespaceId.DEFAULT, appArtifactId, "dummy", "Plugin1", new PluginSelector());
    // The selector always select the last one, hence the USER once will be selected
    Assert.assertNotNull(entry);
    Assert.assertEquals(pluginArtifactId.toApiArtifactId(), entry.getKey().getArtifactId());
    Assert.assertEquals("Plugin1", entry.getValue().getName());
    Assert.assertEquals("dummy", entry.getValue().getType());
    // Use a different selector that returns the first entry, hence expect the SYSTEM one being returned
    entry = finder.findPlugin(NamespaceId.DEFAULT, appArtifactId, "dummy", "Plugin1", new PluginSelector() {

        @Override
        public Map.Entry<io.cdap.cdap.api.artifact.ArtifactId, PluginClass> select(SortedMap<io.cdap.cdap.api.artifact.ArtifactId, PluginClass> plugins) {
            return plugins.entrySet().iterator().next();
        }
    });
    Assert.assertNotNull(entry);
    Assert.assertEquals(systemPluginArtifactId.toApiArtifactId(), entry.getKey().getArtifactId());
    Assert.assertEquals("Plugin1", entry.getValue().getName());
    Assert.assertEquals("dummy", entry.getValue().getType());
}
Also used : PluginSelector(io.cdap.cdap.api.plugin.PluginSelector) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) SortedMap(java.util.SortedMap) PluginClass(io.cdap.cdap.api.plugin.PluginClass) File(java.io.File) Map(java.util.Map) SortedMap(java.util.SortedMap) Test(org.junit.Test)

Example 68 with ArtifactRange

use of io.cdap.cdap.api.artifact.ArtifactRange in project cdap by caskdata.

the class UnitTestManager method addPluginArtifact.

@Override
public ArtifactManager addPluginArtifact(ArtifactId artifactId, ArtifactId parent, @Nullable Set<PluginClass> additionalPlugins, Class<?> pluginClass, Class<?>... pluginClasses) throws Exception {
    Set<ArtifactRange> parents = new HashSet<>();
    parents.add(new ArtifactRange(parent.getParent().getNamespace(), parent.getArtifact(), new ArtifactVersion(parent.getVersion()), true, new ArtifactVersion(parent.getVersion()), true));
    addPluginArtifact(artifactId, parents, additionalPlugins, pluginClass, pluginClasses);
    return artifactManagerFactory.create(artifactId);
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) HashSet(java.util.HashSet)

Example 69 with ArtifactRange

use of io.cdap.cdap.api.artifact.ArtifactRange in project cdap by caskdata.

the class DefaultArtifactRepository method shouldUpdateSytemArtifact.

private boolean shouldUpdateSytemArtifact(ArtifactDetail currentArtifactDetail, SystemArtifactInfo systemArtifactInfo) {
    if (!currentArtifactDetail.getDescriptor().getArtifactId().getVersion().isSnapshot()) {
        // if it's not a snapshot, don't bother trying to update it since artifacts are immutable
        return false;
    }
    // For snapshots check if it's different. Artifact update is disruptive, so we spend some cycles
    // to check if it's really needed
    Set<ArtifactRange> parents = systemArtifactInfo.getConfig().getParents();
    if (!Objects.equals(parents, currentArtifactDetail.getMeta().getUsableBy())) {
        return true;
    }
    if (!Objects.equals(systemArtifactInfo.getConfig().getProperties(), currentArtifactDetail.getMeta().getProperties())) {
        return true;
    }
    Set<PluginClass> additionalPlugins = systemArtifactInfo.getConfig().getPlugins();
    if (additionalPlugins != null && !currentArtifactDetail.getMeta().getClasses().getPlugins().containsAll(additionalPlugins)) {
        return true;
    }
    try (InputStream stream1 = currentArtifactDetail.getDescriptor().getLocation().getInputStream();
        InputStream stream2 = new FileInputStream(systemArtifactInfo.getArtifactFile())) {
        return !IOUtils.contentEquals(stream1, stream2);
    } catch (IOException e) {
        // In case of any IO problems, jsut update it
        return true;
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) IOException(java.io.IOException) PluginClass(io.cdap.cdap.api.plugin.PluginClass) FileInputStream(java.io.FileInputStream)

Example 70 with ArtifactRange

use of io.cdap.cdap.api.artifact.ArtifactRange in project cdap by caskdata.

the class ArtifactClientTestRun method testNotFound.

@Test
public void testNotFound() throws Exception {
    ArtifactId ghostId = NamespaceId.DEFAULT.artifact("ghost", "1.0.0");
    try {
        artifactClient.list(new NamespaceId("ghost"));
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    try {
        artifactClient.getArtifactInfo(ghostId);
        Assert.fail();
    } catch (ArtifactNotFoundException e) {
    // expected
    }
    try {
        artifactClient.listVersions(ghostId.getParent(), ghostId.getArtifact());
        Assert.fail();
    } catch (ArtifactNotFoundException e) {
    // expected
    }
    // test adding an artifact that extends a non-existent artifact
    Set<ArtifactRange> parents = Sets.newHashSet(new ArtifactRange(ghostId.getParent().getNamespace(), ghostId.getArtifact(), new ArtifactVersion("1"), new ArtifactVersion("2")));
    try {
        artifactClient.add(NamespaceId.DEFAULT, "abc", DUMMY_SUPPLIER, "1.0.0", parents);
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    try {
        artifactClient.getPluginTypes(ghostId);
        Assert.fail();
    } catch (ArtifactNotFoundException e) {
    // expected
    }
    try {
        artifactClient.getPluginSummaries(ghostId, "type");
        Assert.fail();
    } catch (ArtifactNotFoundException e) {
    // expected
    }
    try {
        artifactClient.getPluginInfo(ghostId, "type", "name");
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) Test(org.junit.Test)

Aggregations

ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)107 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)77 Test (org.junit.Test)68 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)54 PluginClass (io.cdap.cdap.api.plugin.PluginClass)42 Id (io.cdap.cdap.common.id.Id)40 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)36 File (java.io.File)28 HashSet (java.util.HashSet)21 ImmutableSet (com.google.common.collect.ImmutableSet)20 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)20 Set (java.util.Set)20 Manifest (java.util.jar.Manifest)20 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)16 PluginNotExistsException (io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException)14 PluginId (io.cdap.cdap.proto.id.PluginId)14 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)12 PluginPropertyField (io.cdap.cdap.api.plugin.PluginPropertyField)12 IOException (java.io.IOException)12 ArtifactDetail (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)10