Search in sources :

Example 31 with ArtifactId

use of co.cask.cdap.api.artifact.ArtifactId 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());
    Files.copy(Locations.newInputSupplier(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());
    Files.copy(Locations.newInputSupplier(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(co.cask.cdap.api.plugin.PluginSelector) TestPlugin2(co.cask.cdap.internal.app.plugins.test.TestPlugin2) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) PluginTestRunnable(co.cask.cdap.internal.app.runtime.artifact.app.plugin.PluginTestRunnable) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) Manifest(java.util.jar.Manifest) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) SortedMap(java.util.SortedMap) FilterClassLoader(co.cask.cdap.common.lang.FilterClassLoader) ProgramClassLoader(co.cask.cdap.internal.app.runtime.ProgramClassLoader) TestPlugin(co.cask.cdap.internal.app.plugins.test.TestPlugin) PluginInstantiator(co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) Id(co.cask.cdap.common.id.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.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 32 with ArtifactId

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

the class ArtifactRepositoryTest method testMacroPlugin.

@Test
public void testMacroPlugin() throws Exception {
    File pluginDir = TMP_FOLDER.newFolder();
    addPluginArtifact();
    SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = getPlugins();
    copyArtifacts(pluginDir, plugins);
    // set up test macro evaluator's substitutions
    Map<String, String> propertySubstitutions = ImmutableMap.<String, String>builder().put("expansiveHostname", "${hostname}/${path}:${port}").put("hostname", "${one}").put("path", "${two}").put("port", "${three}").put("one", "${host${hostScopeMacro}}").put("hostScopeMacro", "-local").put("host-local", "${l}${o}${c}${a}${l}${hostSuffix}").put("l", "l").put("o", "o").put("c", "c").put("a", "a").put("hostSuffix", "host").put("two", "${filename${fileTypeMacro}}").put("three", "${firstPortDigit}${secondPortDigit}").put("filename", "index").put("fileTypeMacro", "-html").put("filename-html", "index.html").put("filename-php", "index.php").put("firstPortDigit", "8").put("secondPortDigit", "0").put("aBoolean", "true").put("aByte", "101").put("aChar", "k").put("aDouble", "64.0").put("aFloat", "52.0").put("anInt", "42").put("aLong", "32").put("aShort", "81").build();
    // Instantiate the plugins and execute them
    try (PluginInstantiator instantiator = new PluginInstantiator(cConf, appClassLoader, pluginDir)) {
        for (Map.Entry<ArtifactDescriptor, Set<PluginClass>> entry : plugins.entrySet()) {
            for (PluginClass pluginClass : entry.getValue()) {
                Plugin pluginInfo = new Plugin(new ArrayList<ArtifactId>(), entry.getKey().getArtifactId(), pluginClass, PluginProperties.builder().add("class.name", TEST_EMPTY_CLASS).add("nullableLongFlag", "10").add("host", "${expansiveHostname}").add("aBoolean", "${aBoolean}").add("aByte", "${aByte}").add("aChar", "${aChar}").add("aDouble", "${aDouble}").add("anInt", "${anInt}").add("aFloat", "${aFloat}").add("aLong", "${aLong}").add("aShort", "${aShort}").build());
                TestMacroEvaluator testMacroEvaluator = new TestMacroEvaluator(propertySubstitutions, new HashMap<String, String>());
                Callable<String> plugin = instantiator.newInstance(pluginInfo, testMacroEvaluator);
                Assert.assertEquals("localhost/index.html:80,true,101,k,64.0,52.0,42,32,81", plugin.call());
                String pluginId = "5";
                PluginContext pluginContext = new DefaultPluginContext(instantiator, NamespaceId.DEFAULT.app("abc").worker("w"), ImmutableMap.of(pluginId, pluginInfo));
                PluginProperties resolvedProperties = pluginContext.getPluginProperties(pluginId, testMacroEvaluator);
                Map<String, String> expected = new HashMap<>();
                expected.put("class.name", TEST_EMPTY_CLASS);
                expected.put("nullableLongFlag", "10");
                expected.put("host", "localhost/index.html:80");
                expected.put("aBoolean", "true");
                expected.put("aByte", "101");
                expected.put("aChar", "k");
                expected.put("aDouble", "64.0");
                expected.put("anInt", "42");
                expected.put("aFloat", "52.0");
                expected.put("aLong", "32");
                expected.put("aShort", "81");
                Assert.assertEquals(expected, resolvedProperties.getProperties());
            }
        }
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) TestMacroEvaluator(co.cask.cdap.internal.app.runtime.plugin.TestMacroEvaluator) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) PluginContext(co.cask.cdap.api.plugin.PluginContext) DefaultPluginContext(co.cask.cdap.internal.app.runtime.DefaultPluginContext) HashMap(java.util.HashMap) PluginInstantiator(co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator) PluginClass(co.cask.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) PluginProperties(co.cask.cdap.api.plugin.PluginProperties) DefaultPluginContext(co.cask.cdap.internal.app.runtime.DefaultPluginContext) Plugin(co.cask.cdap.api.plugin.Plugin) TestPlugin(co.cask.cdap.internal.app.plugins.test.TestPlugin) Test(org.junit.Test)

Example 33 with ArtifactId

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

the class MapReduceContextConfigTest method testManyMacrosInAppSpec.

@Test
public void testManyMacrosInAppSpec() {
    Configuration hConf = new Configuration();
    MapReduceContextConfig cfg = new MapReduceContextConfig(hConf);
    StringBuilder appCfg = new StringBuilder();
    for (int i = 0; i < 100; i++) {
        appCfg.append("${").append(i).append("}");
        hConf.setInt(String.valueOf(i), i);
    }
    ApplicationSpecification appSpec = new DefaultApplicationSpecification("name", "desc", appCfg.toString(), new ArtifactId("artifact", new ArtifactVersion("1.0.0"), ArtifactScope.USER), Collections.<String, StreamSpecification>emptyMap(), Collections.<String, String>emptyMap(), Collections.<String, DatasetCreationSpec>emptyMap(), Collections.<String, FlowSpecification>emptyMap(), Collections.<String, MapReduceSpecification>emptyMap(), Collections.<String, SparkSpecification>emptyMap(), Collections.<String, WorkflowSpecification>emptyMap(), Collections.<String, ServiceSpecification>emptyMap(), Collections.<String, ScheduleCreationSpec>emptyMap(), Collections.<String, WorkerSpecification>emptyMap(), Collections.<String, Plugin>emptyMap());
    cfg.setApplicationSpecification(appSpec);
    Assert.assertEquals(appSpec.getConfiguration(), cfg.getApplicationSpecification().getConfiguration());
}
Also used : DefaultApplicationSpecification(co.cask.cdap.internal.app.DefaultApplicationSpecification) ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) Configuration(org.apache.hadoop.conf.Configuration) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) DefaultApplicationSpecification(co.cask.cdap.internal.app.DefaultApplicationSpecification) Test(org.junit.Test)

Example 34 with ArtifactId

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

the class ArtifactSelector method select.

@Override
public Map.Entry<ArtifactId, PluginClass> select(SortedMap<ArtifactId, PluginClass> plugins) {
    NavigableMap<ArtifactId, PluginClass> pluginMap;
    if (plugins instanceof NavigableMap) {
        pluginMap = (NavigableMap<ArtifactId, PluginClass>) plugins;
    } else {
        pluginMap = new TreeMap<>();
        pluginMap.putAll(plugins);
    }
    for (Map.Entry<ArtifactId, PluginClass> entry : pluginMap.descendingMap().entrySet()) {
        ArtifactId artifactId = entry.getKey();
        if ((scope == null || artifactId.getScope().equals(scope)) && (name == null || artifactId.getName().equals(name)) && (range == null || range.versionIsInRange(artifactId.getVersion()))) {
            return entry;
        }
    }
    throw new IllegalArgumentException(errMsg);
}
Also used : ArtifactId(co.cask.cdap.api.artifact.ArtifactId) NavigableMap(java.util.NavigableMap) PluginClass(co.cask.cdap.api.plugin.PluginClass) TreeMap(java.util.TreeMap) Map(java.util.Map) NavigableMap(java.util.NavigableMap) SortedMap(java.util.SortedMap)

Aggregations

ArtifactId (co.cask.cdap.api.artifact.ArtifactId)34 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)14 Test (org.junit.Test)13 File (java.io.File)11 NamespaceId (co.cask.cdap.proto.id.NamespaceId)9 PluginClass (co.cask.cdap.api.plugin.PluginClass)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Plugin (co.cask.cdap.api.plugin.Plugin)7 SortedMap (java.util.SortedMap)7 Id (co.cask.cdap.common.id.Id)5 AppDeploymentInfo (co.cask.cdap.internal.app.deploy.pipeline.AppDeploymentInfo)4 TestPlugin (co.cask.cdap.internal.app.plugins.test.TestPlugin)4 ArtifactDescriptor (co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor)4 PluginInstantiator (co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 HashSet (java.util.HashSet)4 Manifest (java.util.jar.Manifest)4 Location (org.apache.twill.filesystem.Location)4 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)3