use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository 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.internal.app.runtime.artifact.ArtifactRepository in project cdap by caskdata.
the class SystemAppTask method run.
@Override
public void run(RunnableTaskContext context) throws Exception {
ArtifactId systemAppArtifactId = context.getArtifactId();
if (systemAppArtifactId == null) {
throw new IllegalArgumentException("Missing artifactId from the system app task request");
}
LOG.debug("Received system app task for artifact {}", systemAppArtifactId);
Injector injector = createInjector(cConf);
ArtifactRepository artifactRepository = injector.getInstance(ArtifactRepository.class);
Impersonator impersonator = injector.getInstance(Impersonator.class);
String systemAppNamespace = context.getNamespace();
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.from(systemAppNamespace), systemAppArtifactId.getName(), systemAppArtifactId.getVersion());
ArtifactLocalizerClient localizerClient = injector.getInstance(ArtifactLocalizerClient.class);
File artifactLocation = localizerClient.getUnpackedArtifactLocation(Artifacts.toProtoArtifactId(new NamespaceId(systemAppNamespace), systemAppArtifactId));
EntityImpersonator classLoaderImpersonator = new EntityImpersonator(artifactId.toEntityId(), impersonator);
try (CloseableClassLoader artifactClassLoader = artifactRepository.createArtifactClassLoader(new ArtifactDescriptor(artifactId.getNamespace().getId(), artifactId.toArtifactId(), Locations.toLocation(artifactLocation)), classLoaderImpersonator);
SystemAppTaskContext systemAppTaskContext = buildTaskSystemAppContext(injector, systemAppNamespace, systemAppArtifactId, artifactClassLoader)) {
RunnableTaskRequest taskRequest = context.getEmbeddedRequest();
String taskClassName = taskRequest.getClassName();
if (taskClassName == null) {
LOG.debug("No system app task to execute");
return;
}
LOG.debug("Requested to run system app task {}", taskClassName);
Class<?> clazz = artifactClassLoader.loadClass(taskClassName);
if (!(RunnableTask.class.isAssignableFrom(clazz))) {
throw new ClassCastException(String.format("%s is not a RunnableTask", taskClassName));
}
LOG.debug("Launching system app task {}", taskClassName);
RunnableTask runnableTask = (RunnableTask) injector.getInstance(clazz);
RunnableTaskContext runnableTaskContext = new RunnableTaskContext(taskRequest.getParam().getSimpleParam(), null, null, null, systemAppTaskContext) {
@Override
public void writeResult(byte[] data) throws IOException {
context.writeResult(data);
}
@Override
public void setTerminateOnComplete(boolean terminate) {
context.setTerminateOnComplete(terminate);
}
@Override
public boolean isTerminateOnComplete() {
return context.isTerminateOnComplete();
}
};
runnableTask.run(runnableTaskContext);
LOG.debug("System app task completed {}", taskClassName);
}
}
use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository in project cdap by caskdata.
the class EntityExistenceTest method setup.
@BeforeClass
public static void setup() throws Exception {
CConfiguration cConf = CConfiguration.create();
cConf.set(Constants.INSTANCE_NAME, EXISTS);
Injector injector = AppFabricTestHelper.getInjector(cConf);
NamespaceStore nsStore = injector.getInstance(NamespaceStore.class);
ArtifactRepository artifactRepository = injector.getInstance(ArtifactRepository.class);
cConf = injector.getInstance(CConfiguration.class);
nsStore.create(new NamespaceMeta.Builder().setName(EXISTS).build());
existenceVerifier = injector.getInstance(Key.get(new TypeLiteral<EntityExistenceVerifier<EntityId>>() {
}));
LocalLocationFactory lf = new LocalLocationFactory(TEMPORARY_FOLDER.newFolder());
File artifactFile = new File(AppJarHelper.createDeploymentJar(lf, AllProgramsApp.class).toURI());
artifactRepository.addArtifact(Id.Artifact.fromEntityId(ARTIFACT), artifactFile);
AppFabricTestHelper.deployApplication(Id.Namespace.fromEntityId(NAMESPACE), AllProgramsApp.class, null, cConf);
}
use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository in project cdap by caskdata.
the class ArtifactCacheServiceTest method setUp.
@Before
public void setUp() throws Exception {
cConf = createCConf();
tetheringStore = getInjector().getInstance(TetheringStore.class);
ArtifactCache artifactCache = new ArtifactCache(cConf);
artifactCacheService = new ArtifactCacheService(cConf, artifactCache, tetheringStore, null);
artifactCacheService.startAndWait();
getInjector().getInstance(ArtifactRepository.class).clear(NamespaceId.DEFAULT);
Id.Artifact.from(Id.Namespace.DEFAULT, "some-task", "1.0.0-SNAPSHOT");
LocationFactory locationFactory = getInjector().getInstance(LocationFactory.class);
appJar = AppJarHelper.createDeploymentJar(locationFactory, TaskWorkerServiceTest.TestRunnableClass.class);
artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "some-task", "1.0.0-SNAPSHOT");
File appJarFile = new File(tmpFolder.newFolder(), String.format("%s-%s.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
Locations.linkOrCopy(appJar, appJarFile);
artifactRepository = getInjector().getInstance(ArtifactRepository.class);
artifactRepository.addArtifact(artifactId, appJarFile);
addPeer();
}
use of io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository 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);
}
Aggregations