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);
}
}
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"))));
}
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);
}
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);
}
}
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);
}
Aggregations