Search in sources :

Example 1 with Spec

use of io.cdap.cdap.internal.capability.autoinstall.Spec in project cdap by caskdata.

the class AutoInstallTest method testAutoInstallPlugins.

@Test
public void testAutoInstallPlugins() throws Exception {
    // Setup mocks
    CConfiguration cConf = CConfiguration.create();
    cConf.set(Constants.CFG_LOCAL_DATA_DIR, TEMP_FOLDER.newFolder().getAbsolutePath());
    cConf.setInt(Constants.Capability.AUTO_INSTALL_THREADS, 5);
    ArtifactRepository artifactRepository = PowerMockito.mock(ArtifactRepository.class);
    RemoteClientFactory remoteClientFactory = new RemoteClientFactory(null, new NoOpInternalAuthenticator());
    CapabilityApplier capabilityApplier = new CapabilityApplier(null, null, null, null, null, artifactRepository, cConf, remoteClientFactory);
    CapabilityApplier ca = Mockito.spy(capabilityApplier);
    PowerMockito.mockStatic(HttpClients.class);
    PowerMockito.mockStatic(Files.class);
    PowerMockito.mockStatic(File.class);
    PowerMockito.mockStatic(Paths.class);
    PowerMockito.mockStatic(java.nio.file.Files.class);
    File mockFile = TEMP_FOLDER.newFile();
    Mockito.when(File.createTempFile(anyString(), anyString(), any())).thenReturn(mockFile);
    Path mockPath = PowerMockito.mock(Path.class);
    Mockito.when(Paths.get(mockFile.getPath())).thenReturn(mockPath);
    URL packagesUrl = new URL("https://my.hub.io/packages.json");
    HubPackage pkg1 = new HubPackage("my-plugin", "1.0.0", "My Plugin", "My Plugin", "Cask", "Cask", "[6.1.1,6.3.0]", 1554766945, true, Collections.singletonList("hydrator-plugin"), false, null);
    HubPackage pkg2 = new HubPackage("my-plugin", "2.0.0", "My Plugin", "My Plugin", "Cask", "Cask", "[6.3.1,6.4.0]", 1554766945, true, Collections.singletonList("hydrator-plugin"), false, null);
    HubPackage pkg3 = new HubPackage("my-plugin", "3.0.0", "My Plugin", "My Plugin", "Cask", "Cask", "[6.4.1,7.0.0-SNAPSHOT)", 1554766945, true, Collections.singletonList("hydrator-plugin"), false, null);
    String packagesJson = GSON.toJson(ImmutableList.of(pkg1, pkg2, pkg3));
    URL specUrl = new URL("https://my.hub.io/packages/my-plugin/2.0.0/spec.json");
    List<Spec.Action.Argument> arguments = Arrays.asList(new Spec.Action.Argument("config", "my-plugin-2.0.0.json", false), new Spec.Action.Argument("jar", "my-plugin-2.0.0.jar", false), new Spec.Action.Argument("name", "my-plugin", false), new Spec.Action.Argument("version", "2.0.0", false));
    Spec.Action action = new Spec.Action("one_step_deploy_plugin", "Deploy my plugin", arguments);
    Spec spec = new Spec("1.0", "My Plugin", "My Plugin", "Cask", "Cask", 1554766945, "[6.3.1,6.4.0]", Collections.singletonList("hydrator-plugin"), true, Collections.singletonList(action));
    String specJson = GSON.toJson(spec);
    URL configUrl = new URL("https://my.hub.io/packages/my-plugin/2.0.0/my-plugin-2.0.0.json");
    Map<String, String> properties = ImmutableMap.of("key1", "value1", "key2", "value2");
    Map<String, Object> config = ImmutableMap.of("parents", ImmutableList.of("system:cdap-data-pipeline[6.3.1,6.4.0]", "system:cdap-data-streams[6.3.1,6.4.0]"), "properties", properties);
    String configJson = GSON.toJson(config);
    // Mock http requests to hub
    Mockito.when(HttpClients.doGetAsString(packagesUrl)).thenReturn(packagesJson);
    Mockito.when(HttpClients.doGetAsString(specUrl)).thenReturn(specJson);
    Mockito.when(HttpClients.doGetAsString(configUrl)).thenReturn(configJson);
    // Set current CDAP version
    Mockito.doReturn("6.4.0").when(ca).getCurrentVersion();
    // Test plugin auto install
    ca.autoInstallResources("mycapability", Collections.singletonList(new URL("https://my.hub.io")));
    // Verify that the correct version of the plugin was installed
    Set<ArtifactRange> ranges = ImmutableSet.of(ArtifactRanges.parseArtifactRange("system:cdap-data-pipeline[6.3.1,6.4.0]"), ArtifactRanges.parseArtifactRange("system:cdap-data-streams[6.3.1,6.4.0]"));
    Id.Artifact artifact = Id.Artifact.from(Id.Namespace.DEFAULT, "my-plugin", "2.0.0");
    Mockito.verify(artifactRepository, Mockito.times(1)).addArtifact(artifact, mockFile, ranges, ImmutableSet.of());
    Mockito.verify(artifactRepository, Mockito.times(1)).writeArtifactProperties(artifact, properties);
    // Verify that temp file was deleted
    PowerMockito.verifyStatic();
    java.nio.file.Files.deleteIfExists(mockPath);
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) NoOpInternalAuthenticator(io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Matchers.anyString(org.mockito.Matchers.anyString) URL(java.net.URL) HubPackage(io.cdap.cdap.internal.capability.autoinstall.HubPackage) Path(java.nio.file.Path) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) Id(io.cdap.cdap.common.id.Id) Spec(io.cdap.cdap.internal.capability.autoinstall.Spec) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with Spec

use of io.cdap.cdap.internal.capability.autoinstall.Spec in project cdap by caskdata.

the class AutoInstallTest method testDeserializeSpecJson.

@Test
public void testDeserializeSpecJson() throws Exception {
    Spec deserializedSpec = readFile("spec.json", Spec.class);
    List<Spec.Action.Argument> arguments = Arrays.asList(new Spec.Action.Argument("name", "my-plugin", false), new Spec.Action.Argument("version", "1.3.0", false), new Spec.Action.Argument("scope", "user", false), new Spec.Action.Argument("config", "my-plugins-1.3.0.json", false), new Spec.Action.Argument("jar", "my-plugins-1.3.0.jar", false));
    List<Spec.Action> actions = Collections.singletonList(new Spec.Action("one_step_deploy_plugin", "Label", arguments));
    Spec expectedSpec = new Spec("1.0", "Label", "Description", "Author", "Org", 1603825065, "[6.1.1,7.0.0-SNAPSHOT)", Collections.singletonList("hydrator-plugin"), true, actions);
    Assert.assertEquals(expectedSpec, deserializedSpec);
}
Also used : Spec(io.cdap.cdap.internal.capability.autoinstall.Spec) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Spec (io.cdap.cdap.internal.capability.autoinstall.Spec)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)1 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)1 Id (io.cdap.cdap.common.id.Id)1 NoOpInternalAuthenticator (io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator)1 RemoteClientFactory (io.cdap.cdap.common.internal.remote.RemoteClientFactory)1 ArtifactRepository (io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository)1 HubPackage (io.cdap.cdap.internal.capability.autoinstall.HubPackage)1 File (java.io.File)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 Matchers.anyString (org.mockito.Matchers.anyString)1