use of io.cdap.cdap.internal.capability.autoinstall.HubPackage 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);
}
use of io.cdap.cdap.internal.capability.autoinstall.HubPackage in project cdap by cdapio.
the class CapabilityApplier method autoInstallResources.
@VisibleForTesting
void autoInstallResources(String capability, @Nullable List<URL> hubs) {
ExecutorService executor = Executors.newFixedThreadPool(autoInstallThreads, Threads.createDaemonThreadFactory("installer-" + capability + "-%d"));
try {
for (URL hub : Optional.ofNullable(hubs).orElse(Collections.emptyList())) {
HubPackage[] hubPackages;
try {
URL url = new URL(hub.getProtocol(), hub.getHost(), hub.getPort(), hub.getPath() + "/packages.json");
String packagesJson = HttpClients.doGetAsString(url);
// Deserialize packages.json from hub
// See https://cdap.atlassian.net/wiki/spaces/DOCS/pages/554401840/Hub+API?src=search#Get-Hub-Catalog
hubPackages = GSON.fromJson(packagesJson, HubPackage[].class);
} catch (Exception e) {
LOG.warn("Failed to get packages.json from {} for capability {}. Ignoring error.", hub, capability, e);
continue;
}
String currentVersion = getCurrentVersion();
// Get the latest compatible version of each plugin from hub and install it
List<Future<?>> futures = Arrays.stream(hubPackages).filter(p -> p.versionIsInRange(currentVersion)).collect(Collectors.groupingBy(HubPackage::getName, Collectors.maxBy(Comparator.comparing(HubPackage::getArtifactVersion)))).values().stream().filter(Optional::isPresent).map(Optional::get).map(p -> executor.submit(() -> {
try {
LOG.debug("Installing plugins {} for capability {} from hub {}", p.getName(), capability, hub);
p.installPlugin(hub, artifactRepository, tmpDir);
} catch (Exception e) {
LOG.warn("Failed to install plugin {} for capability {} from hub {}. Ignoring error", p.getName(), capability, hub, e);
}
})).collect(Collectors.toList());
for (Future<?> future : futures) {
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
LOG.warn("Ignoring error during plugin install", e);
}
}
}
} finally {
executor.shutdownNow();
}
}
use of io.cdap.cdap.internal.capability.autoinstall.HubPackage in project cdap by cdapio.
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);
}
use of io.cdap.cdap.internal.capability.autoinstall.HubPackage in project cdap by cdapio.
the class AutoInstallTest method testDeserializePackageJson.
@Test
public void testDeserializePackageJson() throws Exception {
HubPackage[] deserializedHubPackages = readFile("packages.json", HubPackage[].class);
HubPackage[] expectedHubPackages = { new HubPackage("plugin-1", "1.0.0", "Description1", "Label1", "Author1", "Org1", "[6.3.0,7.0.0-SNAPSHOT)", 1473901763, false, Arrays.asList("category1"), true, "https://www.foo.com"), new HubPackage("plugin-2", "2.0.0", "Description2", "Label2", "Author2", "Org2", "[6.1.1,6.1.1]", 1510006834, true, Arrays.asList("category2", "category3"), false, null) };
Assert.assertArrayEquals(expectedHubPackages, deserializedHubPackages);
Assert.assertTrue(deserializedHubPackages[0].versionIsInRange("6.4.0"));
Assert.assertFalse(deserializedHubPackages[0].versionIsInRange("7.0.0"));
Assert.assertTrue(deserializedHubPackages[1].versionIsInRange("6.1.1"));
Assert.assertFalse(deserializedHubPackages[1].versionIsInRange("6.1.2"));
}
use of io.cdap.cdap.internal.capability.autoinstall.HubPackage in project cdap by caskdata.
the class AutoInstallTest method testDeserializePackageJson.
@Test
public void testDeserializePackageJson() throws Exception {
HubPackage[] deserializedHubPackages = readFile("packages.json", HubPackage[].class);
HubPackage[] expectedHubPackages = { new HubPackage("plugin-1", "1.0.0", "Description1", "Label1", "Author1", "Org1", "[6.3.0,7.0.0-SNAPSHOT)", 1473901763, false, Arrays.asList("category1"), true, "https://www.foo.com"), new HubPackage("plugin-2", "2.0.0", "Description2", "Label2", "Author2", "Org2", "[6.1.1,6.1.1]", 1510006834, true, Arrays.asList("category2", "category3"), false, null) };
Assert.assertArrayEquals(expectedHubPackages, deserializedHubPackages);
Assert.assertTrue(deserializedHubPackages[0].versionIsInRange("6.4.0"));
Assert.assertFalse(deserializedHubPackages[0].versionIsInRange("7.0.0"));
Assert.assertTrue(deserializedHubPackages[1].versionIsInRange("6.1.1"));
Assert.assertFalse(deserializedHubPackages[1].versionIsInRange("6.1.2"));
}
Aggregations