Search in sources :

Example 1 with NoOpInternalAuthenticator

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());
}
Also used : NoOpInternalAuthenticator(io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) TaskWorkerServiceTest(io.cdap.cdap.internal.app.worker.TaskWorkerServiceTest) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) File(java.io.File) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) LocationFactory(org.apache.twill.filesystem.LocationFactory) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test) TaskWorkerServiceTest(io.cdap.cdap.internal.app.worker.TaskWorkerServiceTest)

Example 2 with NoOpInternalAuthenticator

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));
        }
    }
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) NoOpInternalAuthenticator(io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator) HttpErrorStatusProvider(io.cdap.cdap.api.common.HttpErrorStatusProvider) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) LocationBodyProducer(io.cdap.cdap.common.http.LocationBodyProducer) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) File(java.io.File) Location(org.apache.twill.filesystem.Location) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with NoOpInternalAuthenticator

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);
}
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 4 with NoOpInternalAuthenticator

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());
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) NoOpInternalAuthenticator(io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) TaskWorkerServiceTest(io.cdap.cdap.internal.app.worker.TaskWorkerServiceTest) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) LocationFactory(org.apache.twill.filesystem.LocationFactory) ArtifactLocalizerCleaner(io.cdap.cdap.internal.app.worker.sidecar.ArtifactLocalizerCleaner) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) Id(io.cdap.cdap.common.id.Id) File(java.io.File) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test) TaskWorkerServiceTest(io.cdap.cdap.internal.app.worker.TaskWorkerServiceTest)

Example 5 with NoOpInternalAuthenticator

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());
}
Also used : NoOpInternalAuthenticator(io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) TaskWorkerServiceTest(io.cdap.cdap.internal.app.worker.TaskWorkerServiceTest) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) File(java.io.File) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) LocationFactory(org.apache.twill.filesystem.LocationFactory) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test) TaskWorkerServiceTest(io.cdap.cdap.internal.app.worker.TaskWorkerServiceTest)

Aggregations

NoOpInternalAuthenticator (io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator)5 File (java.io.File)5 Id (io.cdap.cdap.common.id.Id)4 ArtifactRepository (io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository)4 Location (org.apache.twill.filesystem.Location)4 Test (org.junit.Test)4 RemoteClientFactory (io.cdap.cdap.common.internal.remote.RemoteClientFactory)3 TaskWorkerServiceTest (io.cdap.cdap.internal.app.worker.TaskWorkerServiceTest)3 LocationFactory (org.apache.twill.filesystem.LocationFactory)3 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)2 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)2 RemoteClient (io.cdap.cdap.common.internal.remote.RemoteClient)2 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)2 HttpRequestConfig (io.cdap.common.http.HttpRequestConfig)2 LocalLocationFactory (org.apache.twill.filesystem.LocalLocationFactory)2 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)1 HttpErrorStatusProvider (io.cdap.cdap.api.common.HttpErrorStatusProvider)1 LocationBodyProducer (io.cdap.cdap.common.http.LocationBodyProducer)1 ArtifactLocalizerCleaner (io.cdap.cdap.internal.app.worker.sidecar.ArtifactLocalizerCleaner)1 HubPackage (io.cdap.cdap.internal.capability.autoinstall.HubPackage)1