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