Search in sources :

Example 56 with ArtifactRange

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

the class ArtifactRepositoryTest method testNamespaceIsolation.

@Test
public void testNamespaceIsolation() throws Exception {
    // create system app artifact
    Id.Artifact systemAppArtifactId = Id.Artifact.from(Id.Namespace.SYSTEM, "PluginTest", "1.0.0");
    File jar = createAppJar(PluginTestApp.class, new File(systemArtifactsDir1, "PluginTest-1.0.0.jar"), createManifest(ManifestFields.EXPORT_PACKAGE, PluginTestRunnable.class.getPackage().getName()));
    artifactRepository.addSystemArtifacts();
    Assert.assertTrue(jar.delete());
    Set<ArtifactRange> parents = ImmutableSet.of(new ArtifactRange(systemAppArtifactId.getNamespace().getId(), systemAppArtifactId.getName(), new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    NamespaceId namespace1 = Ids.namespace("ns1");
    NamespaceId namespace2 = Ids.namespace("ns2");
    Id.Artifact pluginArtifactId1 = Id.Artifact.from(Id.Namespace.fromEntityId(namespace1), "myPlugin", "1.0");
    Id.Artifact pluginArtifactId2 = Id.Artifact.from(Id.Namespace.fromEntityId(namespace2), "myPlugin", "1.0");
    try {
        // create plugin artifact in namespace1 that extends the system artifact
        // There should be two plugins there (TestPlugin and TestPlugin2).
        Manifest manifest = createManifest(ManifestFields.EXPORT_PACKAGE, TestPlugin.class.getPackage().getName());
        File jarFile = createPluginJar(TestPlugin.class, new File(tmpDir, "myPlugin-1.0.jar"), manifest);
        artifactRepository.addArtifact(pluginArtifactId1, jarFile, parents, null);
        // create plugin artifact in namespace2 that extends the system artifact
        artifactRepository.addArtifact(pluginArtifactId2, jarFile, parents, null);
        // check that only plugins from the artifact in the namespace are returned, and not plugins from both
        SortedMap<ArtifactDescriptor, Set<PluginClass>> extensions = artifactRepository.getPlugins(namespace1, systemAppArtifactId);
        Assert.assertEquals(1, extensions.keySet().size());
        Assert.assertEquals(2, extensions.values().iterator().next().size());
        extensions = artifactRepository.getPlugins(namespace2, systemAppArtifactId);
        Assert.assertEquals(1, extensions.keySet().size());
        Assert.assertEquals(2, extensions.values().iterator().next().size());
    } finally {
        artifactRepository.clear(NamespaceId.SYSTEM);
        artifactRepository.clear(namespace1);
        artifactRepository.clear(namespace2);
    }
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) PluginTestRunnable(io.cdap.cdap.internal.app.runtime.artifact.app.plugin.PluginTestRunnable) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginId(io.cdap.cdap.proto.id.PluginId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Manifest(java.util.jar.Manifest) File(java.io.File) Test(org.junit.Test)

Example 57 with ArtifactRange

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

the class ArtifactRepositoryTest method testMultipleParentVersions.

@Test(expected = InvalidArtifactException.class)
public void testMultipleParentVersions() throws InvalidArtifactException {
    Id.Artifact child = Id.Artifact.from(Id.Namespace.SYSTEM, "abc", "1.0.0");
    DefaultArtifactRepository.validateParentSet(child, ImmutableSet.of(new ArtifactRange(NamespaceId.SYSTEM.getNamespace(), "r1", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")), new ArtifactRange(NamespaceId.SYSTEM.getNamespace(), "r1", new ArtifactVersion("3.0.0"), new ArtifactVersion("4.0.0"))));
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginId(io.cdap.cdap.proto.id.PluginId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) Test(org.junit.Test)

Example 58 with ArtifactRange

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

the class ArtifactRepositoryTest method testInstantiateNestedConfigPlugins.

@Test
public void testInstantiateNestedConfigPlugins() throws Exception {
    File pluginDir = TMP_FOLDER.newFolder();
    // Create the plugin jar.
    Manifest manifest = createManifest(ManifestFields.EXPORT_PACKAGE, NestedConfigPlugin.class.getPackage().getName());
    File jarFile = createPluginJar(NestedConfigPlugin.class, new File(tmpDir, "nested-1.0.jar"), manifest);
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "nested", "1.0");
    artifactRepository.addArtifact(artifactId, jarFile, Collections.singleton(new ArtifactRange(APP_ARTIFACT_ID.getNamespace().getId(), APP_ARTIFACT_ID.getName(), new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"))), null);
    SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = artifactRepository.getPlugins(NamespaceId.DEFAULT, APP_ARTIFACT_ID);
    Assert.assertEquals(1, plugins.keySet().size());
    Assert.assertEquals(1, plugins.values().size());
    Assert.assertEquals(1, plugins.values().iterator().next().size());
    copyArtifacts(pluginDir, plugins);
    Gson gson = new Gson();
    ArtifactId artifact = plugins.firstKey().getArtifactId();
    // only 1 plugin
    PluginClass pluginClass = plugins.values().iterator().next().iterator().next();
    NestedConfigPlugin.Config expected = new NestedConfigPlugin.Config(1, new NestedConfigPlugin.NestedConfig("nested val1", "nested val2"));
    // test flat structure, all pre 6.5 plugins look like this
    instantiateAndValidate(pluginDir, new Plugin(Collections.emptyList(), artifact, pluginClass, PluginProperties.builder().add("X", "1").add("Nested1", "nested val1").add("Nested2", "nested val2").build()), expected, false);
    // test nested
    instantiateAndValidate(pluginDir, new Plugin(Collections.emptyList(), artifact, pluginClass, PluginProperties.builder().add("X", "1").add("Nested", gson.toJson(ImmutableMap.of("Nested1", "nested val1", "Nested2", "nested val2"))).build()), expected, false);
    // test macro gets correct default value, and macro fields get set correctly
    expected = new NestedConfigPlugin.Config(1, new NestedConfigPlugin.NestedConfig(null, null));
    instantiateAndValidate(pluginDir, new Plugin(Collections.emptyList(), artifact, pluginClass, PluginProperties.builder().add("X", "1").add("Nested1", "${macro1}").add("Nested2", "${macro2}").build()), expected, true);
    instantiateAndValidate(pluginDir, new Plugin(Collections.emptyList(), artifact, pluginClass, PluginProperties.builder().add("X", "1").add("Nested", "${I am macro}").build()), expected, true);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ArtifactConfig(io.cdap.cdap.common.conf.ArtifactConfig) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Gson(com.google.gson.Gson) Manifest(java.util.jar.Manifest) NestedConfigPlugin(io.cdap.cdap.internal.app.runtime.artifact.plugin.nested.NestedConfigPlugin) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginId(io.cdap.cdap.proto.id.PluginId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) File(java.io.File) TestPlugin(io.cdap.cdap.internal.app.plugins.test.TestPlugin) NestedConfigPlugin(io.cdap.cdap.internal.app.runtime.artifact.plugin.nested.NestedConfigPlugin) Plugin(io.cdap.cdap.api.plugin.Plugin) Test(org.junit.Test)

Example 59 with ArtifactRange

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

the class ArtifactRepositoryTest method testPluginSelector.

@Test
public void testPluginSelector() throws Exception {
    // No plugin yet
    ArtifactRange appArtifactRange = new ArtifactRange(APP_ARTIFACT_ID.getNamespace().getId(), APP_ARTIFACT_ID.getName(), APP_ARTIFACT_ID.getVersion(), true, APP_ARTIFACT_ID.getVersion(), true);
    try {
        artifactRepository.findPlugin(NamespaceId.DEFAULT, appArtifactRange, "plugin", "TestPlugin2", new PluginSelector());
        Assert.fail();
    } catch (PluginNotExistsException e) {
    // expected
    }
    File pluginDir = DirUtils.createTempDir(tmpDir);
    // Create a plugin jar. It contains two plugins, TestPlugin and TestPlugin2 inside.
    Id.Artifact artifact1Id = Id.Artifact.from(Id.Namespace.DEFAULT, "myPlugin", "1.0");
    Manifest manifest = createManifest(ManifestFields.EXPORT_PACKAGE, TestPlugin.class.getPackage().getName());
    File jarFile = createPluginJar(TestPlugin.class, new File(tmpDir, "myPlugin-1.0.jar"), manifest);
    // Build up the plugin repository.
    Set<ArtifactRange> parents = ImmutableSet.of(new ArtifactRange(APP_ARTIFACT_ID.getNamespace().getId(), APP_ARTIFACT_ID.getName(), new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    artifactRepository.addArtifact(artifact1Id, jarFile, parents, null);
    // Should get the only version.
    Map.Entry<ArtifactDescriptor, PluginClass> plugin = artifactRepository.findPlugin(NamespaceId.DEFAULT, appArtifactRange, "plugin", "TestPlugin2", new PluginSelector());
    Assert.assertNotNull(plugin);
    Assert.assertEquals(new ArtifactVersion("1.0"), plugin.getKey().getArtifactId().getVersion());
    Assert.assertEquals("TestPlugin2", plugin.getValue().getName());
    Locations.linkOrCopyOverwrite(plugin.getKey().getLocation(), new File(pluginDir, Artifacts.getFileName(plugin.getKey().getArtifactId())));
    // Create another plugin jar with later version and update the repository
    Id.Artifact artifact2Id = Id.Artifact.from(Id.Namespace.DEFAULT, "myPlugin", "2.0");
    jarFile = createPluginJar(TestPlugin.class, new File(tmpDir, "myPlugin-2.0.jar"), manifest);
    artifactRepository.addArtifact(artifact2Id, jarFile, parents, null);
    // Should select the latest version
    plugin = artifactRepository.findPlugin(NamespaceId.DEFAULT, appArtifactRange, "plugin", "TestPlugin2", new PluginSelector());
    Assert.assertNotNull(plugin);
    Assert.assertEquals(new ArtifactVersion("2.0"), plugin.getKey().getArtifactId().getVersion());
    Assert.assertEquals("TestPlugin2", plugin.getValue().getName());
    Locations.linkOrCopyOverwrite(plugin.getKey().getLocation(), new File(pluginDir, Artifacts.getFileName(plugin.getKey().getArtifactId())));
    // Load the Plugin class from the classLoader.
    try (PluginInstantiator instantiator = new PluginInstantiator(cConf, appClassLoader, pluginDir)) {
        ClassLoader pluginClassLoader = instantiator.getArtifactClassLoader(plugin.getKey().getArtifactId());
        Class<?> pluginClass = pluginClassLoader.loadClass(TestPlugin2.class.getName());
        // Use a custom plugin selector to select with smallest version
        plugin = artifactRepository.findPlugin(NamespaceId.DEFAULT, appArtifactRange, "plugin", "TestPlugin2", new PluginSelector() {

            @Override
            public Map.Entry<ArtifactId, PluginClass> select(SortedMap<ArtifactId, PluginClass> plugins) {
                return plugins.entrySet().iterator().next();
            }
        });
        Assert.assertNotNull(plugin);
        Assert.assertEquals(new ArtifactVersion("1.0"), plugin.getKey().getArtifactId().getVersion());
        Assert.assertEquals("TestPlugin2", plugin.getValue().getName());
        // Load the Plugin class again from the current plugin selected
        // The plugin class should be different (from different ClassLoader)
        // The empty class should be the same (from the plugin lib ClassLoader)
        pluginClassLoader = instantiator.getArtifactClassLoader(plugin.getKey().getArtifactId());
        Assert.assertNotSame(pluginClass, pluginClassLoader.loadClass(TestPlugin2.class.getName()));
        // From the pluginClassLoader, loading export classes from the template jar should be allowed
        Class<?> cls = pluginClassLoader.loadClass(PluginTestRunnable.class.getName());
        // The class should be loaded from the parent artifact's classloader
        Assert.assertSame(appClassLoader.loadClass(PluginTestRunnable.class.getName()), cls);
        // From the plugin classloader, all cdap api classes is loadable as well.
        cls = pluginClassLoader.loadClass(Application.class.getName());
        // The Application class should be the same as the one in the system classloader
        Assert.assertSame(Application.class, cls);
    }
}
Also used : PluginSelector(io.cdap.cdap.api.plugin.PluginSelector) TestPlugin2(io.cdap.cdap.internal.app.plugins.test.TestPlugin2) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) PluginTestRunnable(io.cdap.cdap.internal.app.runtime.artifact.app.plugin.PluginTestRunnable) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Manifest(java.util.jar.Manifest) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) SortedMap(java.util.SortedMap) ProgramClassLoader(io.cdap.cdap.internal.app.runtime.ProgramClassLoader) FilterClassLoader(io.cdap.cdap.common.lang.FilterClassLoader) TestPlugin(io.cdap.cdap.internal.app.plugins.test.TestPlugin) PluginInstantiator(io.cdap.cdap.internal.app.runtime.plugin.PluginInstantiator) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginId(io.cdap.cdap.proto.id.PluginId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) File(java.io.File) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 60 with ArtifactRange

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

the class ArtifactRangeTest method testWhitespace.

@Test
public void testWhitespace() throws InvalidArtifactRangeException {
    ArtifactRange range = ArtifactRanges.parseArtifactRange(NamespaceId.DEFAULT.getNamespace(), "name[ 1.0.0 , 2.0.0 )");
    Assert.assertEquals(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "name", new ArtifactVersion("1.0.0"), true, new ArtifactVersion("2.0.0"), false), range);
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) 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