use of io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator in project cdap by caskdata.
the class ArtifactLocalizerServiceTest method testUnpackArtifact.
@Test
public void testUnpackArtifact() throws Exception {
LocationFactory locationFactory = new LocalLocationFactory(TEMP_FOLDER.newFolder());
ArtifactRepository artifactRepository = getInjector().getInstance(ArtifactRepository.class);
ArtifactLocalizerClient client = new ArtifactLocalizerClient(cConf, new NoOpInternalAuthenticator());
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "some-task", "1.0.0-SNAPSHOT");
Location appJar = AppJarHelper.createDeploymentJar(locationFactory, TaskWorkerServiceTest.TestRunnableClass.class);
File appJarFile = new File(TEMP_FOLDER.newFolder(), String.format("%s-%s.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
File newAppJarFile = new File(TEMP_FOLDER.newFolder(), String.format("%s-%s-copy.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
Locations.linkOrCopy(appJar, appJarFile);
appJar.delete();
artifactRepository.addArtifact(artifactId, appJarFile);
File unpackedDir = client.getUnpackedArtifactLocation(artifactId.toEntityId());
// Make sure the artifact was actually cached
validateUnpackDir(unpackedDir);
// Call the sidecar again and make sure the same path was returned
File sameUnpackedDir = client.getUnpackedArtifactLocation(artifactId.toEntityId());
Assert.assertEquals(unpackedDir, sameUnpackedDir);
// Delete and recreate the artifact to update the last modified date
artifactRepository.deleteArtifact(artifactId);
Thread.sleep(1000);
Files.copy(appJarFile, newAppJarFile);
artifactRepository.addArtifact(artifactId, newAppJarFile);
File newUnpackDir = client.getUnpackedArtifactLocation(artifactId.toEntityId());
// Make sure the two paths arent the same and the old directory still exists
Assert.assertNotEquals(unpackedDir, newUnpackDir);
validateUnpackDir(newUnpackDir);
validateUnpackDir(unpackedDir);
// Assert that the old cache directory is deleted after we run cleanup
this.localizerService.forceCleanup();
Assert.assertFalse(unpackedDir.exists());
}
use of io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator in project cdap by caskdata.
the class ArtifactCacheHttpHandlerInternal method fetchArtifact.
@GET
@Path("peers/{peer}/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}")
public void fetchArtifact(HttpRequest request, HttpResponder responder, @PathParam("peer") String peer, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion) throws Exception {
ArtifactId artifactId = new ArtifactId(namespaceId, artifactName, artifactVersion);
try {
String endpoint = tetheringStore.getPeer(peer).getEndpoint();
RemoteClientFactory factory = new RemoteClientFactory(new NoOpDiscoveryServiceClient(endpoint), new NoOpInternalAuthenticator(), remoteAuthenticator);
HttpRequestConfig config = new DefaultHttpRequestConfig(true);
RemoteClient remoteClient = factory.createRemoteClient("", config, Constants.Gateway.INTERNAL_API_VERSION_3);
File artifactPath = cache.getArtifact(artifactId, peer, remoteClient);
Location artifactLocation = Locations.toLocation(artifactPath);
responder.sendContent(HttpResponseStatus.OK, new LocationBodyProducer(artifactLocation), new DefaultHttpHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM));
} catch (Exception ex) {
if (ex instanceof HttpErrorStatusProvider) {
HttpResponseStatus status = HttpResponseStatus.valueOf(((HttpErrorStatusProvider) ex).getStatusCode());
responder.sendString(status, exceptionToJson(ex));
} else {
responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, exceptionToJson(ex));
}
}
}
use of io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator 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.common.internal.remote.NoOpInternalAuthenticator in project cdap by caskdata.
the class ArtifactCacheTest method testArtifactCache.
@Test
public void testArtifactCache() throws Exception {
LocationFactory locationFactory = getInjector().getInstance(LocationFactory.class);
ArtifactRepository artifactRepository = getInjector().getInstance(ArtifactRepository.class);
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "some-task", "1.0.0-SNAPSHOT");
Location appJar = AppJarHelper.createDeploymentJar(locationFactory, TaskWorkerServiceTest.TestRunnableClass.class);
File appJarFile = new File(tmpFolder.newFolder(), String.format("%s-%s.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
Locations.linkOrCopy(appJar, appJarFile);
artifactRepository.addArtifact(artifactId, appJarFile);
CConfiguration cConf = CConfiguration.create();
String cacheDir = tmpFolder.toString();
cConf.set(Constants.ArtifactCache.LOCAL_DATA_DIR, cacheDir);
TetheringStore store = getInjector().getInstance(TetheringStore.class);
ArtifactCache cache = new ArtifactCache(cConf);
// Add a couple of tethered peers
addPeer(store, "peer1");
addPeer(store, "peer2");
RemoteClientFactory factory = new RemoteClientFactory(new NoOpDiscoveryServiceClient(getEndPoint("").toString()), new NoOpInternalAuthenticator());
HttpRequestConfig config = new DefaultHttpRequestConfig(true);
RemoteClient remoteClient = factory.createRemoteClient("", config, Constants.Gateway.INTERNAL_API_VERSION_3);
// Get artifact from first peer
File peer1ArtifactPath = cache.getArtifact(artifactId.toEntityId(), "peer1", remoteClient);
// Get the artifact again. The same path was returned
Assert.assertEquals(peer1ArtifactPath, cache.getArtifact(artifactId.toEntityId(), "peer1", remoteClient));
// Get artifact from another peer. It should be cached in a different path
File peer2ArtifactPath = cache.getArtifact(artifactId.toEntityId(), "peer2", remoteClient);
Assert.assertNotEquals(peer1ArtifactPath, peer2ArtifactPath);
// Delete and recreate the artifact to update the last modified date
artifactRepository.deleteArtifact(artifactId);
// This sleep is needed to delay the file copy so that the lastModified time on the file is different
Thread.sleep(1000);
artifactRepository.addArtifact(artifactId, appJarFile);
// Artifact should be cached in a different path
File newPeer1ArtifactPath = cache.getArtifact(artifactId.toEntityId(), "peer1", remoteClient);
Assert.assertNotEquals(peer1ArtifactPath, newPeer1ArtifactPath);
// Run the artifact cleaner
ArtifactLocalizerCleaner cleaner = new ArtifactLocalizerCleaner(Paths.get(cacheDir).resolve("peers"), 1);
cleaner.run();
// Older artifact should been deleted
Assert.assertFalse(peer1ArtifactPath.exists());
// Latest artifact should still be cached
Assert.assertTrue(newPeer1ArtifactPath.exists());
}
use of io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator in project cdap by caskdata.
the class ArtifactLocalizerServiceTest method testArtifact.
@Test
public void testArtifact() throws Exception {
LocationFactory locationFactory = getInjector().getInstance(LocationFactory.class);
ArtifactRepository artifactRepository = getInjector().getInstance(ArtifactRepository.class);
ArtifactLocalizerClient client = new ArtifactLocalizerClient(cConf, new NoOpInternalAuthenticator());
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "some-task", "1.0.0-SNAPSHOT");
Location appJar = AppJarHelper.createDeploymentJar(locationFactory, TaskWorkerServiceTest.TestRunnableClass.class);
File appJarFile = new File(tmpFolder.newFolder(), String.format("%s-%s.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
File newAppJarFile = new File(tmpFolder.newFolder(), String.format("%s-%s-copy.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
Locations.linkOrCopy(appJar, appJarFile);
appJar.delete();
artifactRepository.addArtifact(artifactId, appJarFile);
File artifactPath = client.getArtifactLocation(artifactId.toEntityId());
// Make sure the artifact was actually cached
Assert.assertTrue(artifactPath.exists());
Assert.assertTrue(artifactPath.isFile());
// Call the sidecar again and make sure the same path was returned
File sameArtifactPath = client.getArtifactLocation(artifactId.toEntityId());
Assert.assertEquals(artifactPath, sameArtifactPath);
// Delete and recreate the artifact to update the last modified date
artifactRepository.deleteArtifact(artifactId);
// This sleep is needed to delay the file copy so that the lastModified time on the file is different
Thread.sleep(1000);
// Wait a bit before recreating the artifact to make sure the last modified time is different
Files.copy(appJarFile, newAppJarFile);
artifactRepository.addArtifact(artifactId, newAppJarFile);
File newArtifactPath = client.getArtifactLocation(artifactId.toEntityId());
// Make sure the two paths arent the same and that the old one is gone
Assert.assertNotEquals(artifactPath, newArtifactPath);
Assert.assertTrue(newArtifactPath.exists());
}
Aggregations