use of co.cask.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.
the class ArtifactRepositoryTest method getFile.
private static File getFile() throws Exception {
File pluginDir = DirUtils.createTempDir(tmpDir);
// Create the plugin jar. 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);
// add the artifact
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")));
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "myPlugin", "1.0");
artifactRepository.addArtifact(artifactId, jarFile, parents);
return pluginDir;
}
use of co.cask.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.
the class ArtifactStoreTest method testDelete.
@Test
public void testDelete() throws Exception {
// write an artifact with an app
Id.Artifact parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0");
ApplicationClass appClass = new ApplicationClass(InspectionApp.class.getName(), "", new ReflectionSchemaGenerator().generate(InspectionApp.AConfig.class));
ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addApp(appClass).build());
writeArtifact(parentId, artifactMeta, "parent contents");
// write a child artifact that extends the parent with some plugins
Id.Artifact childId = Id.Artifact.from(Id.Namespace.DEFAULT, "myplugins", "1.0.0");
List<PluginClass> plugins = ImmutableList.of(new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of()), new PluginClass("atype", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
Set<ArtifactRange> parents = ImmutableSet.of(new ArtifactRange(parentId.getNamespace().getId(), parentId.getName(), new ArtifactVersion("0.1.0"), new ArtifactVersion("2.0.0")));
artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parents);
writeArtifact(childId, artifactMeta, "child contents");
// check parent has plugins from the child
Assert.assertFalse(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
// delete the child artifact
artifactStore.delete(childId);
// shouldn't be able to get artifact detail
try {
artifactStore.getArtifact(childId);
Assert.fail();
} catch (ArtifactNotFoundException e) {
// expected
}
// shouldn't see it in the list
List<ArtifactDetail> artifactList = artifactStore.getArtifacts(parentId.getNamespace().toEntityId());
Assert.assertEquals(1, artifactList.size());
Assert.assertEquals(parentId.getName(), artifactList.get(0).getDescriptor().getArtifactId().getName());
// shouldn't see any more plugins for parent
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
// delete parent
artifactStore.delete(parentId);
// nothing should be in the list
Assert.assertTrue(artifactStore.getArtifacts(parentId.getNamespace().toEntityId()).isEmpty());
// shouldn't be able to see app class either
Assert.assertTrue(artifactStore.getApplicationClasses(NamespaceId.DEFAULT, appClass.getClassName()).isEmpty());
}
use of co.cask.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.
the class ArtifactStoreTest method testPluginParentVersions.
// this test tests that when an artifact specifies a range of artifact versions it extends,
// those versions are honored
@Test
public void testPluginParentVersions() throws Exception {
// write an artifact that extends parent-[1.0.0, 2.0.0)
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "0.1.0");
Set<ArtifactRange> parentArtifacts = ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
Set<PluginClass> plugins = ImmutableSet.of(new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
ArtifactMeta meta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parentArtifacts);
writeArtifact(artifactId, meta, "some contents");
ArtifactDescriptor artifactInfo = artifactStore.getArtifact(artifactId).getDescriptor();
// check ids that are out of range. They should not return anything
List<Id.Artifact> badIds = Lists.newArrayList(// ids that are too low
Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "0.9.9"), Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0-SNAPSHOT"), // ids that are too high
Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "2.0.0"));
ArtifactMeta emptyMeta = new ArtifactMeta(ArtifactClasses.builder().build());
for (Id.Artifact badId : badIds) {
// write the parent artifact to make sure we don't get ArtifactNotFound exceptions with later calls
// we're testing range filtering, not the absence of the parent artifact
writeArtifact(badId, emptyMeta, "content");
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, badId).isEmpty());
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, badId, "atype").isEmpty());
try {
artifactStore.getPluginClasses(NamespaceId.DEFAULT, badId, "atype", "plugin1", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
Assert.fail();
} catch (PluginNotExistsException e) {
// expected
}
}
// check ids that are in range return what we expect
List<Id.Artifact> goodIds = Lists.newArrayList(// ids that are too low
Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0"), // ids that are too high
Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.9.9"), Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.99.999"), Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "2.0.0-SNAPSHOT"));
Map<ArtifactDescriptor, Set<PluginClass>> expectedPluginsMapList = ImmutableMap.of(artifactInfo, plugins);
Map<ArtifactDescriptor, PluginClass> expectedPluginsMap = ImmutableMap.of(artifactInfo, plugins.iterator().next());
for (Id.Artifact goodId : goodIds) {
// make sure parent actually exists
writeArtifact(goodId, emptyMeta, "content");
Assert.assertEquals(expectedPluginsMapList, artifactStore.getPluginClasses(NamespaceId.DEFAULT, goodId));
Assert.assertEquals(expectedPluginsMapList, artifactStore.getPluginClasses(NamespaceId.DEFAULT, goodId, "atype"));
Assert.assertEquals(expectedPluginsMap, artifactStore.getPluginClasses(NamespaceId.DEFAULT, goodId, "atype", "plugin1", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED));
}
}
use of co.cask.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.
the class ArtifactStoreTest method testGetArtifacts.
@Test
public void testGetArtifacts() throws Exception {
// add 1 version of another artifact1
Id.Artifact artifact1V1 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifact1", "1.0.0");
String contents1V1 = "first contents v1";
PluginClass plugin1V1 = new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of());
ArtifactMeta meta1V1 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin1V1).build());
writeArtifact(artifact1V1, meta1V1, contents1V1);
// add 2 versions of an artifact2
Id.Artifact artifact2V1 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifact2", "0.1.0");
Id.Artifact artifact2V2 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifact2", "0.1.1");
Id.Artifact artifact2V3 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifact2", "0.1.1-SNAPSHOT");
String contents2V1 = "second contents v1";
String contents2V2 = "second contents v2";
String contents2V3 = "second contents v3";
PluginClass plugin2V1 = new PluginClass("atype", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of());
PluginClass plugin2V2 = new PluginClass("atype", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of());
PluginClass plugin2V3 = new PluginClass("atype", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of());
ArtifactMeta meta2V1 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin2V1).build());
ArtifactMeta meta2V2 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin2V2).build());
ArtifactMeta meta2V3 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin2V3).build());
writeArtifact(artifact2V1, meta2V1, contents2V1);
writeArtifact(artifact2V2, meta2V2, contents2V2);
writeArtifact(artifact2V3, meta2V3, contents2V3);
// test we get 1 version of artifact1 and 2 versions of artifact2
List<ArtifactDetail> artifact1Versions = artifactStore.getArtifacts(artifact1V1.getNamespace().toEntityId(), artifact1V1.getName(), Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
Assert.assertEquals(1, artifact1Versions.size());
assertEqual(artifact1V1, meta1V1, contents1V1, artifact1Versions.get(0));
List<ArtifactDetail> artifact2Versions = artifactStore.getArtifacts(artifact2V1.getNamespace().toEntityId(), artifact2V1.getName(), Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
Assert.assertEquals(3, artifact2Versions.size());
assertEqual(artifact2V1, meta2V1, contents2V1, artifact2Versions.get(0));
assertEqual(artifact2V2, meta2V2, contents2V2, artifact2Versions.get(1));
assertEqual(artifact2V3, meta2V3, contents2V3, artifact2Versions.get(2));
// test get 2 versions of artifact 2
artifact2Versions = artifactStore.getArtifacts(artifact2V1.getNamespace().toEntityId(), artifact2V1.getName(), 2, ArtifactSortOrder.UNORDERED);
Assert.assertEquals(2, artifact2Versions.size());
assertEqual(artifact2V1, meta2V1, contents2V1, artifact2Versions.get(0));
assertEqual(artifact2V2, meta2V2, contents2V2, artifact2Versions.get(1));
// test get sorted version of artifact 2
artifact2Versions = artifactStore.getArtifacts(artifact2V1.getNamespace().toEntityId(), artifact2V1.getName(), 3, ArtifactSortOrder.DESC);
Assert.assertEquals(3, artifact2Versions.size());
assertEqual(artifact2V2, meta2V2, contents2V2, artifact2Versions.get(0));
assertEqual(artifact2V3, meta2V3, contents2V3, artifact2Versions.get(1));
assertEqual(artifact2V1, meta2V1, contents2V1, artifact2Versions.get(2));
// test get sorted and limited version of artifact 2
artifact2Versions = artifactStore.getArtifacts(artifact2V1.getNamespace().toEntityId(), artifact2V1.getName(), 2, ArtifactSortOrder.DESC);
Assert.assertEquals(2, artifact2Versions.size());
assertEqual(artifact2V2, meta2V2, contents2V2, artifact2Versions.get(0));
assertEqual(artifact2V3, meta2V3, contents2V3, artifact2Versions.get(1));
artifact2Versions = artifactStore.getArtifacts(artifact2V1.getNamespace().toEntityId(), artifact2V1.getName(), 3, ArtifactSortOrder.ASC);
Assert.assertEquals(3, artifact2Versions.size());
assertEqual(artifact2V1, meta2V1, contents2V1, artifact2Versions.get(0));
assertEqual(artifact2V3, meta2V3, contents2V3, artifact2Versions.get(1));
assertEqual(artifact2V2, meta2V2, contents2V2, artifact2Versions.get(2));
// test we get all 3 in the getArtifactSummaries() call for the namespace
List<ArtifactDetail> artifactVersions = artifactStore.getArtifacts(NamespaceId.DEFAULT);
Assert.assertEquals(4, artifactVersions.size());
assertEqual(artifact1V1, meta1V1, contents1V1, artifactVersions.get(0));
assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(1));
assertEqual(artifact2V2, meta2V2, contents2V2, artifactVersions.get(2));
assertEqual(artifact2V3, meta2V3, contents2V3, artifactVersions.get(3));
// test get using a range
// this range should get everything
ArtifactRange range = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "artifact2", new ArtifactVersion("0.1.0"), new ArtifactVersion("0.1.2"));
artifactVersions = artifactStore.getArtifacts(range, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
Assert.assertEquals(3, artifactVersions.size());
assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(0));
assertEqual(artifact2V2, meta2V2, contents2V2, artifactVersions.get(1));
assertEqual(artifact2V3, meta2V3, contents2V3, artifactVersions.get(2));
// test get one version
artifactVersions = artifactStore.getArtifacts(range, 1, ArtifactSortOrder.UNORDERED);
Assert.assertEquals(1, artifactVersions.size());
assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(0));
// test get sorted versions
artifactVersions = artifactStore.getArtifacts(range, 3, ArtifactSortOrder.DESC);
Assert.assertEquals(3, artifact2Versions.size());
assertEqual(artifact2V2, meta2V2, contents2V2, artifactVersions.get(0));
assertEqual(artifact2V3, meta2V3, contents2V3, artifactVersions.get(1));
assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(2));
artifactVersions = artifactStore.getArtifacts(range, 3, ArtifactSortOrder.ASC);
Assert.assertEquals(3, artifact2Versions.size());
assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(0));
assertEqual(artifact2V3, meta2V3, contents2V3, artifactVersions.get(1));
assertEqual(artifact2V2, meta2V2, contents2V2, artifactVersions.get(2));
// this range should get just v0.1.1
range = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "artifact2", new ArtifactVersion("0.1.1"), new ArtifactVersion("1.0.0"));
artifactVersions = artifactStore.getArtifacts(range, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
Assert.assertEquals(1, artifactVersions.size());
assertEqual(artifact2V2, meta2V2, contents2V2, artifactVersions.get(0));
// this range should get just v0.1.0 and v0.1.1-SNAPSHOT
range = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "artifact2", new ArtifactVersion("0.0.0"), new ArtifactVersion("0.1.1"));
artifactVersions = artifactStore.getArtifacts(range, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
Assert.assertEquals(2, artifactVersions.size());
assertEqual(artifact2V1, meta2V1, contents2V1, artifactVersions.get(0));
assertEqual(artifact2V3, meta2V3, contents2V3, artifactVersions.get(1));
}
use of co.cask.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.
the class ArtifactStoreTest method testConcurrentSnapshotWrite.
@Category(SlowTests.class)
@Test
public void testConcurrentSnapshotWrite() throws Exception {
// write parent
Id.Artifact parentArtifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0");
ArtifactMeta parentMeta = new ArtifactMeta(ArtifactClasses.builder().build());
writeArtifact(parentArtifactId, parentMeta, "content");
final ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
// start up a bunch of threads that will try and write the same artifact at the same time
// only one of them should be able to write it
int numThreads = 20;
final Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "abc", "1.0.0-SNAPSHOT");
// use a barrier so they all try and write at the same time
final CyclicBarrier barrier = new CyclicBarrier(numThreads);
final CountDownLatch latch = new CountDownLatch(numThreads);
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
for (int i = 0; i < numThreads; i++) {
final String writer = String.valueOf(i);
executorService.execute(new Runnable() {
@Override
public void run() {
try {
barrier.await();
ArtifactMeta meta = new ArtifactMeta(ArtifactClasses.builder().addPlugin(new PluginClass("plugin-type", "plugin" + writer, "", "classname", "cfg", ImmutableMap.<String, PluginPropertyField>of())).build(), ImmutableSet.of(parentArtifacts));
writeArtifact(artifactId, meta, writer);
} catch (InterruptedException | BrokenBarrierException | ArtifactAlreadyExistsException | IOException e) {
// something went wrong, fail the test
throw new RuntimeException(e);
} catch (WriteConflictException e) {
// these are ok though unexpected (means couldn't write after a bunch of retries too)
} finally {
latch.countDown();
}
}
});
}
// wait for all writers to finish
latch.await();
// figure out which was the last writer by reading our data. all the writers should have been able to write,
// and they should have all overwritten each other in a consistent manner
ArtifactDetail detail = artifactStore.getArtifact(artifactId);
// figure out the winning writer from the plugin name, which is 'plugin<writer>'
String pluginName = detail.getMeta().getClasses().getPlugins().iterator().next().getName();
String winnerWriter = pluginName.substring("plugin".length());
ArtifactMeta expectedMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugin(new PluginClass("plugin-type", "plugin" + winnerWriter, "", "classname", "cfg", ImmutableMap.<String, PluginPropertyField>of())).build(), ImmutableSet.of(parentArtifacts));
assertEqual(artifactId, expectedMeta, winnerWriter, detail);
// check only 1 plugin remains and that its the correct one
Map<ArtifactDescriptor, Set<PluginClass>> pluginMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "plugin-type");
Map<ArtifactDescriptor, Set<PluginClass>> expected = Maps.newHashMap();
expected.put(detail.getDescriptor(), ImmutableSet.<PluginClass>of(new PluginClass("plugin-type", "plugin" + winnerWriter, "", "classname", "cfg", ImmutableMap.<String, PluginPropertyField>of())));
Assert.assertEquals(expected, pluginMap);
}
Aggregations