Search in sources :

Example 46 with ArtifactRange

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

the class ArtifactStoreTest method testSnapshotMutability.

@Test
public void testSnapshotMutability() 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");
    ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
    // write the snapshot once
    PluginClass plugin1 = PluginClass.builder().setName("plugin1").setType("atype").setDescription("").setClassName("c.c.c.plugin1").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build();
    PluginClass plugin2 = PluginClass.builder().setName("plugin2").setType("atype").setDescription("").setClassName("c.c.c.plugin2").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build();
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "myplugins", "1.0.0-SNAPSHOT");
    ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugin1, plugin2).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactId, artifactMeta, "abc123");
    // update meta and jar contents
    artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin2).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactId, artifactMeta, "xyz321");
    // check the metadata and contents got updated
    ArtifactDetail detail = artifactStore.getArtifact(artifactId);
    assertEqual(artifactId, artifactMeta, "xyz321", detail);
    // check that plugin1 was deleted and plugin2 remains
    Assert.assertEquals(ImmutableMap.of(detail.getDescriptor(), plugin2), artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, plugin2.getType(), plugin2.getName(), null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED));
    try {
        artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, plugin1.getType(), plugin1.getName(), null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
        Assert.fail();
    } catch (PluginNotExistsException e) {
    // expected
    }
}
Also used : PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 47 with ArtifactRange

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

the class ArtifactStoreTest method testUniversalPlugin.

@Test
public void testUniversalPlugin() throws Exception {
    // First, deploy an artifact in the SYSTEM scope that doesn't have any plugin inside.
    ArtifactId artifactId = NamespaceId.SYSTEM.artifact("artifact", "1.0.0");
    writeArtifact(Id.Artifact.fromEntityId(artifactId), new ArtifactMeta(ArtifactClasses.builder().build()), "test");
    // Deploy an artifact that has a plugin in the DEFAULT scope, but without any parent artifact
    PluginClass pluginClass1 = PluginClass.builder().setName("plugin1").setType("type1").setDescription("plugin1").setClassName("plugin1").setProperties(ImmutableMap.of()).build();
    ArtifactId pluginArtifactId1 = NamespaceId.DEFAULT.artifact("plugin-artifact1", "0.0.1");
    writeArtifact(Id.Artifact.fromEntityId(pluginArtifactId1), new ArtifactMeta(ArtifactClasses.builder().addPlugin(pluginClass1).build()), "test");
    // Get the available plugins for the artifact, should get the plugin1
    SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = artifactStore.getPluginClasses(NamespaceId.DEFAULT, Id.Artifact.fromEntityId(artifactId));
    Assert.assertEquals(1, plugins.size());
    List<PluginClass> pluginsClasses = plugins.values().stream().flatMap(Set::stream).collect(Collectors.toList());
    Assert.assertEquals(1, pluginsClasses.size());
    Assert.assertEquals(pluginClass1, pluginsClasses.get(0));
    // Get the available plugins for the plugin artifact itself, should also get the plugin1
    plugins = artifactStore.getPluginClasses(NamespaceId.DEFAULT, Id.Artifact.fromEntityId(pluginArtifactId1));
    Assert.assertEquals(1, plugins.size());
    pluginsClasses = plugins.values().stream().flatMap(Set::stream).collect(Collectors.toList());
    Assert.assertEquals(1, pluginsClasses.size());
    Assert.assertEquals(pluginClass1, pluginsClasses.get(0));
    // Deploy an artifact that has a plugin in the DEFAULT scope with a parent artifact
    PluginClass pluginClass2 = PluginClass.builder().setName("plugin2").setType("type2").setDescription("plugin2").setClassName("plugin2").setProperties(ImmutableMap.of()).build();
    ArtifactId pluginArtifactId2 = NamespaceId.DEFAULT.artifact("plugin-artifact2", "0.0.1");
    ArtifactRange parentArtifactRange = new ArtifactRange(artifactId.getNamespace(), artifactId.getArtifact(), ArtifactVersionRange.parse("[1.0.0,2.0.0)"));
    writeArtifact(Id.Artifact.fromEntityId(pluginArtifactId2), new ArtifactMeta(ArtifactClasses.builder().addPlugin(pluginClass2).build(), Collections.singleton(parentArtifactRange)), "test");
    // Get the available plugins for the artifact again, should get plugin1 and plugin2
    plugins = artifactStore.getPluginClasses(NamespaceId.DEFAULT, Id.Artifact.fromEntityId(artifactId));
    Assert.assertEquals(2, plugins.size());
    // Get and verify the plugins.
    pluginsClasses = plugins.values().stream().flatMap(Set::stream).collect(Collectors.toList());
    Assert.assertEquals(2, pluginsClasses.size());
    // The plugins are sorted by the ArtifactDescriptor, hence order is guaranteed
    Assert.assertEquals(Arrays.asList(pluginClass1, pluginClass2), pluginsClasses);
    // Get available plugin by type.
    for (PluginClass pluginClass : Arrays.asList(pluginClass1, pluginClass2)) {
        plugins = artifactStore.getPluginClasses(NamespaceId.DEFAULT, Id.Artifact.fromEntityId(artifactId), pluginClass.getType());
        Assert.assertEquals(1, plugins.size());
        pluginsClasses = plugins.values().stream().flatMap(Set::stream).collect(Collectors.toList());
        Assert.assertEquals(1, pluginsClasses.size());
        Assert.assertEquals(pluginClass, pluginsClasses.get(0));
    }
    // Get plugins by parent ArtifactRange
    for (PluginClass pluginClass : Arrays.asList(pluginClass1, pluginClass2)) {
        SortedMap<ArtifactDescriptor, PluginClass> result = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactRange, pluginClass.getType(), pluginClass.getName(), null, 10, ArtifactSortOrder.UNORDERED);
        Assert.assertEquals(1, result.size());
        Assert.assertEquals(pluginClass, result.values().stream().findFirst().get());
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 48 with ArtifactRange

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

the class ArtifactStoreTest method testGetPlugins.

@Test
public void testGetPlugins() throws Exception {
    ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
    // we have 2 plugins of type A and 2 plugins of type B
    PluginClass pluginA1 = PluginClass.builder().setName("p1").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).build();
    PluginClass pluginA2 = PluginClass.builder().setName("p2").setType("A").setDescription("desc").setClassName("c.p2").setConfigFieldName("conf").setProperties(ImmutableMap.of("stream", new PluginPropertyField("stream", "description", "string", true, false))).build();
    PluginClass pluginB1 = PluginClass.builder().setName("p1").setType("B").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("createIfNotExist", new PluginPropertyField("createIfNotExist", "desc", "boolean", false, false))).build();
    PluginClass pluginB2 = PluginClass.builder().setName("p2").setType("B").setDescription("desc").setClassName("c.p2").setConfigFieldName("stuff").setProperties(ImmutableMap.of("numer", new PluginPropertyField("numerator", "description", "double", true, false), "denom", new PluginPropertyField("denominator", "description", "double", true, false))).build();
    // add artifacts
    // not interested in artifact contents for this test, using some dummy value
    String contents = "0";
    // 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, contents);
    // artifact artifactX-1.0.0 contains plugin A1
    Id.Artifact artifactXv100 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactX", "1.0.0");
    ArtifactMeta metaXv100 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(pluginA1).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactXv100, metaXv100, contents);
    ArtifactDescriptor artifactXv100Info = artifactStore.getArtifact(artifactXv100).getDescriptor();
    // artifact artifactX-1.1.0 contains plugin A1
    Id.Artifact artifactXv110 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactX", "1.1.0");
    ArtifactMeta metaXv110 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(pluginA1).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactXv110, metaXv110, contents);
    ArtifactDescriptor artifactXv110Info = artifactStore.getArtifact(artifactXv110).getDescriptor();
    // artifact artifactX-2.0.0 contains plugins A1 and A2
    Id.Artifact artifactXv200 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactX", "2.0.0");
    ArtifactMeta metaXv200 = new ArtifactMeta(ArtifactClasses.builder().addPlugins(pluginA1, pluginA2).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactXv200, metaXv200, contents);
    ArtifactDescriptor artifactXv200Info = artifactStore.getArtifact(artifactXv200).getDescriptor();
    // artifact artifactY-1.0.0 contains plugin B1
    Id.Artifact artifactYv100 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactY", "1.0.0");
    ArtifactMeta metaYv100 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(pluginB1).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactYv100, metaYv100, contents);
    ArtifactDescriptor artifactYv100Info = artifactStore.getArtifact(artifactYv100).getDescriptor();
    // artifact artifactY-2.0.0 contains plugin B2
    Id.Artifact artifactYv200 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactY", "2.0.0");
    ArtifactMeta metaYv200 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(pluginB2).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactYv200, metaYv200, contents);
    ArtifactDescriptor artifactYv200Info = artifactStore.getArtifact(artifactYv200).getDescriptor();
    // artifact artifactZ-1.0.0 contains plugins A1 and B1
    Id.Artifact artifactZv100 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactZ", "1.0.0");
    ArtifactMeta metaZv100 = new ArtifactMeta(ArtifactClasses.builder().addPlugins(pluginA1, pluginB1).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactZv100, metaZv100, contents);
    ArtifactDescriptor artifactZv100Info = artifactStore.getArtifact(artifactZv100).getDescriptor();
    // artifact artifactZ-2.0.0 contains plugins A1, A2, B1, and B2
    Id.Artifact artifactZv200 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactZ", "2.0.0");
    ArtifactMeta metaZv200 = new ArtifactMeta(ArtifactClasses.builder().addPlugins(pluginA1, pluginA2, pluginB1, pluginB2).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactZv200, metaZv200, contents);
    ArtifactDescriptor artifactZv200Info = artifactStore.getArtifact(artifactZv200).getDescriptor();
    // test getting all plugins in the namespace
    Map<ArtifactDescriptor, Set<PluginClass>> expected = Maps.newHashMap();
    expected.put(artifactXv100Info, ImmutableSet.of(pluginA1));
    expected.put(artifactXv110Info, ImmutableSet.of(pluginA1));
    expected.put(artifactXv200Info, ImmutableSet.of(pluginA1, pluginA2));
    expected.put(artifactYv100Info, ImmutableSet.of(pluginB1));
    expected.put(artifactYv200Info, ImmutableSet.of(pluginB2));
    expected.put(artifactZv100Info, ImmutableSet.of(pluginA1, pluginB1));
    expected.put(artifactZv200Info, ImmutableSet.of(pluginA1, pluginA2, pluginB1, pluginB2));
    Map<ArtifactDescriptor, Set<PluginClass>> actual = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId);
    Assert.assertEquals(expected, actual);
    // test getting all plugins by namespace and type
    // get all of type A
    expected = Maps.newHashMap();
    expected.put(artifactXv100Info, ImmutableSet.of(pluginA1));
    expected.put(artifactXv110Info, ImmutableSet.of(pluginA1));
    expected.put(artifactXv200Info, ImmutableSet.of(pluginA1, pluginA2));
    expected.put(artifactZv100Info, ImmutableSet.of(pluginA1));
    expected.put(artifactZv200Info, ImmutableSet.of(pluginA1, pluginA2));
    actual = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "A");
    Assert.assertEquals(expected, actual);
    // get all of type B
    expected = Maps.newHashMap();
    expected.put(artifactYv100Info, ImmutableSet.of(pluginB1));
    expected.put(artifactYv200Info, ImmutableSet.of(pluginB2));
    expected.put(artifactZv100Info, ImmutableSet.of(pluginB1));
    expected.put(artifactZv200Info, ImmutableSet.of(pluginB1, pluginB2));
    actual = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "B");
    Assert.assertEquals(expected, actual);
    // test getting plugins by namespace, type, and name
    // get all of type A and name p1
    Map<ArtifactDescriptor, PluginClass> expectedMap = Maps.newHashMap();
    expectedMap.put(artifactXv100Info, pluginA1);
    expectedMap.put(artifactXv110Info, pluginA1);
    expectedMap.put(artifactXv200Info, pluginA1);
    expectedMap.put(artifactZv100Info, pluginA1);
    expectedMap.put(artifactZv200Info, pluginA1);
    Map<ArtifactDescriptor, PluginClass> actualMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "A", "p1", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(expectedMap, actualMap);
    // test get limited number
    actualMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "A", "p1", null, 1, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(ImmutableMap.of(artifactXv100Info, pluginA1), actualMap);
    // test get DESC order
    actualMap = new TreeMap<>(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "A", "p1", null, Integer.MAX_VALUE, ArtifactSortOrder.DESC));
    Assert.assertEquals(expectedMap, new TreeMap<>(actualMap).descendingMap());
    // test Predicate
    Predicate<ArtifactId> predicate = input -> {
        try {
            return input.getParent().equals(NamespaceId.DEFAULT) && input.getArtifact().equals("artifactX") && ArtifactVersionRange.parse("[1.0.0, 1.1.0)").versionIsInRange(new ArtifactVersion(input.getVersion()));
        } catch (InvalidArtifactRangeException e) {
            return false;
        }
    };
    expectedMap = Maps.newHashMap();
    expectedMap.put(artifactXv100Info, pluginA1);
    actualMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "A", "p1", predicate, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(expectedMap, actualMap);
    // test limit and order combined
    actualMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "A", "p1", null, 1, ArtifactSortOrder.DESC);
    Assert.assertEquals(ImmutableMap.of(artifactZv200Info, pluginA1), actualMap);
    // test limit, order, predicate combined
    actualMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "A", "p1", predicate, 1, ArtifactSortOrder.DESC);
    Assert.assertEquals(ImmutableMap.of(artifactXv100Info, pluginA1), actualMap);
    // get all of type A and name p2
    expectedMap = Maps.newHashMap();
    expectedMap.put(artifactXv200Info, pluginA2);
    expectedMap.put(artifactZv200Info, pluginA2);
    actualMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "A", "p2", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(expectedMap, actualMap);
    // get all of type B and name p1
    expectedMap = Maps.newHashMap();
    expectedMap.put(artifactYv100Info, pluginB1);
    expectedMap.put(artifactZv100Info, pluginB1);
    expectedMap.put(artifactZv200Info, pluginB1);
    actualMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "B", "p1", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(expectedMap, actualMap);
    // get all of type B and name p2
    expectedMap = Maps.newHashMap();
    expectedMap.put(artifactYv200Info, pluginB2);
    expectedMap.put(artifactZv200Info, pluginB2);
    actualMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "B", "p2", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
    Assert.assertEquals(expectedMap, actualMap);
}
Also used : Arrays(java.util.Arrays) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ArtifactClasses(io.cdap.cdap.api.artifact.ArtifactClasses) Bytes(io.cdap.cdap.api.common.Bytes) Ids(io.cdap.cdap.proto.id.Ids) Future(java.util.concurrent.Future) CharStreams(com.google.common.io.CharStreams) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) After(org.junit.After) Map(java.util.Map) ClassRule(org.junit.ClassRule) CyclicBarrier(java.util.concurrent.CyclicBarrier) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Table(io.cdap.cdap.api.dataset.table.Table) Set(java.util.Set) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Executors(java.util.concurrent.Executors) Id(io.cdap.cdap.common.id.Id) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) SortedMap(java.util.SortedMap) Location(org.apache.twill.filesystem.Location) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) InvalidArtifactRangeException(io.cdap.cdap.api.artifact.InvalidArtifactRangeException) InspectionApp(io.cdap.cdap.internal.app.runtime.artifact.app.inspection.InspectionApp) DefaultImpersonator(io.cdap.cdap.security.impersonation.DefaultImpersonator) ArrayList(java.util.ArrayList) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) SlowTests(io.cdap.cdap.test.SlowTests) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) ArtifactVersionRange(io.cdap.cdap.api.artifact.ArtifactVersionRange) ImmutableList(com.google.common.collect.ImmutableList) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ExecutorService(java.util.concurrent.ExecutorService) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) Charsets(com.google.common.base.Charsets) Cube(io.cdap.cdap.api.dataset.lib.cube.Cube) Files(java.nio.file.Files) Test(org.junit.Test) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Maps(com.google.common.collect.Maps) InputStreamReader(java.io.InputStreamReader) File(java.io.File) EntityImpersonator(io.cdap.cdap.security.impersonation.EntityImpersonator) Requirements(io.cdap.cdap.api.plugin.Requirements) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) TreeMap(java.util.TreeMap) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) AllProgramsApp(io.cdap.cdap.AllProgramsApp) Assert(org.junit.Assert) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) InvalidArtifactRangeException(io.cdap.cdap.api.artifact.InvalidArtifactRangeException) TreeMap(java.util.TreeMap) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 49 with ArtifactRange

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

the class ArtifactStoreTest method testSamePluginDifferentArtifacts.

@Test
public void testSamePluginDifferentArtifacts() throws Exception {
    ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
    // add one artifact with a couple plugins
    Id.Artifact artifact1 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins1", "1.0.0");
    Set<PluginClass> plugins = ImmutableSet.of(PluginClass.builder().setName("plugin1").setType("atype").setDescription("").setClassName("c.c.c.plugin1").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build(), PluginClass.builder().setName("plugin2").setType("atype").setDescription("").setClassName("c.c.c.plugin2").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build());
    ArtifactMeta meta1 = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifact1, meta1, "something");
    ArtifactDescriptor artifact1Info = artifactStore.getArtifact(artifact1).getDescriptor();
    // add a different artifact with the same plugins
    Id.Artifact artifact2 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins2", "1.0.0");
    ArtifactMeta meta2 = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifact2, meta2, "something");
    ArtifactDescriptor artifact2Info = artifactStore.getArtifact(artifact2).getDescriptor();
    Id.Artifact parentArtifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0");
    writeArtifact(parentArtifactId, new ArtifactMeta(ArtifactClasses.builder().build()), "content");
    Map<ArtifactDescriptor, Set<PluginClass>> expected = Maps.newHashMap();
    expected.put(artifact1Info, plugins);
    expected.put(artifact2Info, plugins);
    Map<ArtifactDescriptor, Set<PluginClass>> actual = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId);
    Assert.assertEquals(expected, actual);
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 50 with ArtifactRange

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

the class ArtifactStoreTest method testExcludedPlugins.

@Test
public void testExcludedPlugins() throws Exception {
    ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
    // does not have any requirement
    PluginClass includedPlugin1 = PluginClass.builder().setName("includedPlugin1").setType("A").setDescription("desc").setClassName("c.p2").setConfigFieldName("conf").setProperties(ImmutableMap.of("stream", new PluginPropertyField("stream", "description", "string", true, false))).build();
    PluginClass excludedPlugin1 = PluginClass.builder().setName("excludedPlugin1").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE))).build();
    PluginClass excludedPlugin2 = PluginClass.builder().setName("excludedPlugin2").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(KeyValueTable.TYPE))).build();
    PluginClass excludedPlugin3 = PluginClass.builder().setName("excludedPlugin3").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, KeyValueTable.TYPE))).build();
    PluginClass excludedPlugin4 = PluginClass.builder().setName("excludedPlugin4").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, KeyValueTable.TYPE, Cube.TYPE))).build();
    PluginClass excludedPlugin5 = PluginClass.builder().setName("excludedPlugin5").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, Cube.TYPE))).build();
    PluginClass includedPlugin2 = PluginClass.builder().setName("includedPlugin2").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of("noTransactionNeeded"))).build();
    PluginClass includedPlugin3 = PluginClass.builder().setName("includedPlugin3").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).setRequirements(new Requirements(ImmutableSet.of("noTransactionNeeded", "tpfs"))).build();
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "ArtifactWithTransactionalPlugins", "1.0.0");
    ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(includedPlugin1, includedPlugin2, includedPlugin3, excludedPlugin1, excludedPlugin2, excludedPlugin3, excludedPlugin4, excludedPlugin5).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactId, artifactMeta, "no-content");
    ArtifactDescriptor artifactInfo = artifactStore.getArtifact(artifactId).getDescriptor();
    // plugins which have transaction or spark as requirement should be excluded from plugins listed for the artifact
    Map<ArtifactDescriptor, Set<PluginClass>> actual = artifactStore.getPluginClasses(NamespaceId.DEFAULT, artifactId);
    Set<PluginClass> expectedPlugins = ImmutableSet.of(includedPlugin1, includedPlugin2, includedPlugin3);
    Assert.assertEquals(ImmutableMap.of(artifactInfo, expectedPlugins), actual);
    // excluded plugins should also not be in the plugins listed for the namespace
    List<ArtifactDetail> actualArtifacts = artifactStore.getArtifacts(NamespaceId.DEFAULT);
    Assert.assertEquals(1, actualArtifacts.size());
    Assert.assertEquals(expectedPlugins, actualArtifacts.get(0).getMeta().getClasses().getPlugins());
    // excluded plugins should also not be in the plugins listed for the artifact id
    ArtifactDetail actualArtifact = artifactStore.getArtifact(artifactId);
    Assert.assertEquals(expectedPlugins, actualArtifact.getMeta().getClasses().getPlugins());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) Requirements(io.cdap.cdap.api.plugin.Requirements) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) 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