Search in sources :

Example 1 with NucleusPaths

use of com.aws.greengrass.util.NucleusPaths in project aws-greengrass-nucleus by aws-greengrass.

the class PluginComponentTest method setupPackageStore.

static void setupPackageStore(Kernel kernel, ComponentIdentifier componentId, ComponentIdentifier... pluginIds) throws IOException, PackagingException, URISyntaxException {
    Path localStoreContentPath = Paths.get(PluginComponentTest.class.getResource("local_store_content").toURI());
    NucleusPaths nucleusPaths = kernel.getNucleusPaths();
    PreloadComponentStoreHelper.preloadRecipesFromTestResourceDir(localStoreContentPath.resolve("recipes"), nucleusPaths.recipePath());
    FileUtils.copyDirectory(localStoreContentPath.resolve("artifacts").toFile(), nucleusPaths.artifactPath().toFile());
    ComponentStore e2ETestComponentStore = kernel.getContext().get(ComponentStore.class);
    Path jarFilePath = e2ETestComponentStore.resolveArtifactDirectoryPath(componentId).resolve("plugin-tests.jar");
    // Copy over the same jar file as the plugin-1.1.0 artifact
    Path artifact1_1_0 = e2ETestComponentStore.resolveArtifactDirectoryPath(new ComponentIdentifier(componentName, new Semver("1.1.0"))).resolve(componentName + JAR_FILE_EXTENSION);
    Path artifactPath1_0_0 = e2ETestComponentStore.resolveArtifactDirectoryPath(componentId).resolve(componentName + JAR_FILE_EXTENSION);
    // set the artifact dir as writable so we can copy
    Platform.getInstance().setPermissions(FileSystemPermission.builder().ownerRead(true).ownerWrite(true).ownerExecute(true).otherRead(true).otherExecute(true).build(), artifactPath1_0_0.getParent().getParent(), FileSystemPermission.Option.Recurse);
    FileUtils.copyFile(jarFilePath.toFile(), artifact1_1_0.toFile());
    FileUtils.copyFile(jarFilePath.toFile(), artifactPath1_0_0.toFile());
    for (ComponentIdentifier pluginId : pluginIds) {
        Path artifactPath = e2ETestComponentStore.resolveArtifactDirectoryPath(pluginId).resolve(pluginId.getName() + JAR_FILE_EXTENSION);
        FileUtils.copyFile(jarFilePath.toFile(), artifactPath.toFile());
    }
}
Also used : Path(java.nio.file.Path) NucleusPaths(com.aws.greengrass.util.NucleusPaths) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) Semver(com.vdurmont.semver4j.Semver) ComponentStore(com.aws.greengrass.componentmanager.ComponentStore)

Example 2 with NucleusPaths

use of com.aws.greengrass.util.NucleusPaths in project aws-greengrass-nucleus by aws-greengrass.

the class ComponentManagerIntegTest method GIVEN_component_with_artifact_WHEN_prepareArtifacts_THEN_set_permissions_on_artifacts.

@Test
void GIVEN_component_with_artifact_WHEN_prepareArtifacts_THEN_set_permissions_on_artifacts() throws Exception {
    ComponentIdentifier ident = new ComponentIdentifier("aws.iot.gg.test.integ.perm", new Semver("1.0.0"));
    NucleusPaths nucleusPaths = kernel.getNucleusPaths();
    nucleusPaths.setComponentStorePath(tempRootDir);
    ComponentStore store = new ComponentStore(nucleusPaths, platformResolver, recipeLoader);
    kernel.getContext().put(ComponentStore.class, store);
    File scriptFile = store.resolveArtifactDirectoryPath(ident).resolve("script.sh").toFile();
    File emptyFile = store.resolveArtifactDirectoryPath(ident).resolve("empty.txt").toFile();
    ArtifactDownloader mockDownloader = mock(ArtifactDownloader.class);
    when(mockDownloader.downloadRequired()).thenReturn(true);
    when(mockDownloader.checkDownloadable()).thenReturn(Optional.empty());
    when(mockDownloader.getArtifactFile()).thenReturn(scriptFile).thenReturn(emptyFile);
    when(mockDownloader.canUnarchiveArtifact()).thenReturn(true);
    when(mockDownloader.canSetFilePermissions()).thenReturn(true);
    when(mockDownloader.checkComponentStoreSize()).thenReturn(true);
    when(mockDownloader.download()).thenAnswer(downloadToPath("script.sh", scriptFile)).thenAnswer(downloadToPath("empty.txt", emptyFile));
    ArtifactDownloaderFactory mockDownloaderFactory = mock(ArtifactDownloaderFactory.class);
    when(mockDownloaderFactory.getArtifactDownloader(any(), any(), any())).thenReturn(mockDownloader);
    kernel.getContext().put(ArtifactDownloaderFactory.class, mockDownloaderFactory);
    Files.copy(Paths.get(this.getClass().getResource("aws.iot.gg.test.integ.perm-1.0.0.yaml").toURI()), nucleusPaths.recipePath().resolve(PreloadComponentStoreHelper.getRecipeStorageFilenameFromTestSource("aws.iot.gg.test.integ.perm-1.0.0.yaml")));
    // THEN
    kernel.getContext().get(ComponentManager.class).preparePackages(Collections.singletonList(ident)).get(10, TimeUnit.SECONDS);
    assertThat(nucleusPaths.artifactPath(ident).resolve("script.sh"), hasPermission(FileSystemPermission.builder().ownerRead(true).groupRead(true).otherRead(true).ownerWrite(!PlatformResolver.isWindows && !SystemUtils.USER_NAME.equals(ROOT)).ownerExecute(true).groupExecute(true).build()));
    assertThat(nucleusPaths.artifactPath(ident).resolve("empty.txt"), hasPermission(FileSystemPermission.builder().ownerRead(true).groupRead(true).ownerWrite(!PlatformResolver.isWindows && !SystemUtils.USER_NAME.equals(ROOT)).build()));
}
Also used : ArtifactDownloaderFactory(com.aws.greengrass.componentmanager.builtins.ArtifactDownloaderFactory) NucleusPaths(com.aws.greengrass.util.NucleusPaths) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) Semver(com.vdurmont.semver4j.Semver) FileMatchers.anExistingFile(org.hamcrest.io.FileMatchers.anExistingFile) File(java.io.File) ArtifactDownloader(com.aws.greengrass.componentmanager.builtins.ArtifactDownloader) ComponentStore(com.aws.greengrass.componentmanager.ComponentStore) Test(org.junit.jupiter.api.Test)

Example 3 with NucleusPaths

use of com.aws.greengrass.util.NucleusPaths in project aws-greengrass-nucleus by aws-greengrass.

the class ComponentManagerIntegTest method GIVEN_component_with_archived_artifact_WHEN_prepareArtifacts_THEN_unarchives_artifacts.

@Test
void GIVEN_component_with_archived_artifact_WHEN_prepareArtifacts_THEN_unarchives_artifacts() throws Exception {
    // GIVEN
    ComponentIdentifier ident = new ComponentIdentifier("aws.iot.gg.test.integ.zip", new Semver("1.0.0"));
    NucleusPaths nucleusPaths = kernel.getNucleusPaths();
    nucleusPaths.setComponentStorePath(tempRootDir);
    ComponentStore store = new ComponentStore(nucleusPaths, platformResolver, recipeLoader);
    kernel.getContext().put(ComponentStore.class, store);
    ArtifactDownloader mockDownloader = mock(ArtifactDownloader.class);
    File artifactFile = store.resolveArtifactDirectoryPath(ident).resolve("zip.zip").toFile();
    when(mockDownloader.downloadRequired()).thenReturn(true);
    when(mockDownloader.checkDownloadable()).thenReturn(Optional.empty());
    when(mockDownloader.getArtifactFile()).thenReturn(artifactFile);
    when(mockDownloader.canUnarchiveArtifact()).thenReturn(true);
    when(mockDownloader.canSetFilePermissions()).thenReturn(true);
    when(mockDownloader.checkComponentStoreSize()).thenReturn(true);
    when(mockDownloader.download()).thenAnswer(downloadToPath("zip.zip", artifactFile));
    ArtifactDownloaderFactory mockDownloaderFactory = mock(ArtifactDownloaderFactory.class);
    when(mockDownloaderFactory.getArtifactDownloader(any(), any(), any())).thenReturn(mockDownloader);
    kernel.getContext().put(ArtifactDownloaderFactory.class, mockDownloaderFactory);
    Files.copy(Paths.get(this.getClass().getResource("aws.iot.gg.test.integ.zip-1.0.0.yaml").toURI()), nucleusPaths.recipePath().resolve(PreloadComponentStoreHelper.getRecipeStorageFilenameFromTestSource("aws.iot.gg.test.integ.zip-1.0.0.yaml")));
    // THEN
    kernel.getContext().get(ComponentManager.class).preparePackages(Collections.singletonList(ident)).get(10, TimeUnit.SECONDS);
    Path zipPath = nucleusPaths.unarchiveArtifactPath(ident, "zip");
    assertThat(zipPath.toFile(), anExistingDirectory());
    assertThat(zipPath.resolve("zip").toFile(), anExistingDirectory());
    assertThat(zipPath.resolve("zip").resolve("1").toFile(), anExistingFile());
    assertThat(zipPath.resolve("zip").resolve("2").toFile(), anExistingFile());
    // check everyone can enter dir
    assertThat(zipPath.resolve("zip"), hasPermission(FileSystemPermission.builder().ownerRead(true).ownerWrite(true).ownerExecute(true).groupRead(true).groupExecute(true).otherRead(true).otherExecute(true).build()));
    // check perms match what we gave
    FileSystemPermission allRead = FileSystemPermission.builder().ownerRead(true).groupRead(true).otherRead(true).ownerWrite(!PlatformResolver.isWindows && !SystemUtils.USER_NAME.equals(ROOT)).build();
    assertThat(zipPath.resolve("zip").resolve("1"), hasPermission(allRead));
    assertThat(zipPath.resolve("zip").resolve("2"), hasPermission(allRead));
}
Also used : Path(java.nio.file.Path) ArtifactDownloaderFactory(com.aws.greengrass.componentmanager.builtins.ArtifactDownloaderFactory) NucleusPaths(com.aws.greengrass.util.NucleusPaths) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) FileSystemPermission(com.aws.greengrass.util.FileSystemPermission) Semver(com.vdurmont.semver4j.Semver) ArtifactDownloader(com.aws.greengrass.componentmanager.builtins.ArtifactDownloader) FileMatchers.anExistingFile(org.hamcrest.io.FileMatchers.anExistingFile) File(java.io.File) ComponentStore(com.aws.greengrass.componentmanager.ComponentStore) Test(org.junit.jupiter.api.Test)

Example 4 with NucleusPaths

use of com.aws.greengrass.util.NucleusPaths in project aws-greengrass-nucleus by aws-greengrass.

the class DockerImageArtifactDownloadTest method before.

@BeforeEach
void before() throws Exception {
    Instant credentialsExpiry = Instant.now().plusSeconds(10);
    AuthorizationData authorizationData = AuthorizationData.builder().authorizationToken(Base64.getEncoder().encodeToString("username:password".getBytes(StandardCharsets.UTF_8))).expiresAt(credentialsExpiry).build();
    GetAuthorizationTokenResponse response = GetAuthorizationTokenResponse.builder().authorizationData(authorizationData).build();
    lenient().when(ecrClient.getAuthorizationToken(any(GetAuthorizationTokenRequest.class))).thenReturn(response);
    lenient().when(dockerClient.dockerInstalled()).thenReturn(true);
    AtomicBoolean mqttOnline = new AtomicBoolean(true);
    lenient().when(mqttClient.getMqttOnline()).thenReturn(mqttOnline);
    kernel = new Kernel();
    NucleusPaths nucleusPaths = kernel.getNucleusPaths();
    nucleusPaths.setComponentStorePath(tempRootDir);
    ComponentStore store = new ComponentStore(nucleusPaths, platformResolver, recipeLoader);
    EcrAccessor ecrAccessor = new EcrAccessor(ecrClient);
    kernel.getContext().put(ComponentStore.class, store);
    kernel.getContext().put(EcrAccessor.class, ecrAccessor);
    kernel.getContext().put(DefaultDockerClient.class, dockerClient);
    kernel.getContext().put(MqttClient.class, mqttClient);
    preloadLocalStoreContent();
    componentManager = kernel.getContext().get(ComponentManager.class);
}
Also used : GetAuthorizationTokenResponse(software.amazon.awssdk.services.ecr.model.GetAuthorizationTokenResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GetAuthorizationTokenRequest(software.amazon.awssdk.services.ecr.model.GetAuthorizationTokenRequest) AuthorizationData(software.amazon.awssdk.services.ecr.model.AuthorizationData) EcrAccessor(com.aws.greengrass.componentmanager.plugins.docker.EcrAccessor) NucleusPaths(com.aws.greengrass.util.NucleusPaths) Instant(java.time.Instant) ComponentManager(com.aws.greengrass.componentmanager.ComponentManager) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) ComponentStore(com.aws.greengrass.componentmanager.ComponentStore) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with NucleusPaths

use of com.aws.greengrass.util.NucleusPaths in project aws-greengrass-nucleus by aws-greengrass.

the class DeviceConfigurationTest method beforeEach.

@BeforeEach
void beforeEach() {
    NucleusPaths nucleusPaths = mock(NucleusPaths.class);
    Topics rootConfigTopics = mock(Topics.class);
    when(rootConfigTopics.findOrDefault(any(), anyString(), anyString(), anyString())).thenReturn(new ArrayList<>());
    lenient().when(configuration.lookup(anyString(), anyString(), anyString())).thenReturn(mock(Topic.class));
    when(configuration.lookup(anyString(), anyString(), anyString(), anyString())).thenReturn(mockTopic);
    lenient().when(configuration.getRoot()).thenReturn(rootConfigTopics);
    when(mockKernel.getConfig()).thenReturn(configuration);
    lenient().when(mockKernel.getNucleusPaths()).thenReturn(nucleusPaths);
    Topics topics = Topics.of(mock(Context.class), SERVICES_NAMESPACE_TOPIC, mock(Topics.class));
    when(mockTopics.subscribe(any())).thenReturn(mockTopics);
    when(configuration.lookupTopics(anyString(), anyString(), anyString())).thenReturn(mockTopics);
    when(configuration.lookupTopics(anyString(), anyString(), anyString(), anyString())).thenReturn(mockTopics);
    when(configuration.lookupTopics(anyString())).thenReturn(topics);
    lenient().when(configuration.lookupTopics(anyString())).thenReturn(topics);
}
Also used : Context(com.aws.greengrass.dependency.Context) Topics(com.aws.greengrass.config.Topics) NucleusPaths(com.aws.greengrass.util.NucleusPaths) Topic(com.aws.greengrass.config.Topic) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

NucleusPaths (com.aws.greengrass.util.NucleusPaths)10 Test (org.junit.jupiter.api.Test)5 ComponentStore (com.aws.greengrass.componentmanager.ComponentStore)4 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)4 Semver (com.vdurmont.semver4j.Semver)4 Path (java.nio.file.Path)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 ArtifactDownloader (com.aws.greengrass.componentmanager.builtins.ArtifactDownloader)2 ArtifactDownloaderFactory (com.aws.greengrass.componentmanager.builtins.ArtifactDownloaderFactory)2 File (java.io.File)2 FileMatchers.anExistingFile (org.hamcrest.io.FileMatchers.anExistingFile)2 ComponentManager (com.aws.greengrass.componentmanager.ComponentManager)1 RecipeLoader (com.aws.greengrass.componentmanager.converter.RecipeLoader)1 EcrAccessor (com.aws.greengrass.componentmanager.plugins.docker.EcrAccessor)1 PlatformResolver (com.aws.greengrass.config.PlatformResolver)1 Topic (com.aws.greengrass.config.Topic)1 Topics (com.aws.greengrass.config.Topics)1 Context (com.aws.greengrass.dependency.Context)1 EZPlugins (com.aws.greengrass.dependency.EZPlugins)1 Kernel (com.aws.greengrass.lifecyclemanager.Kernel)1