Search in sources :

Example 1 with DependencyProperties

use of com.amazon.aws.iot.greengrass.component.common.DependencyProperties in project aws-greengrass-nucleus by aws-greengrass.

the class RecipeLoader method loadFromFile.

/**
 * Converts from the recipe file with platform resolving.
 *
 * @param recipeFileContent recipe file content
 * @return Optional package recipe
 * @throws PackageLoadingException when failed to convert recipe file.
 */
public Optional<ComponentRecipe> loadFromFile(String recipeFileContent) throws PackageLoadingException {
    com.amazon.aws.iot.greengrass.component.common.ComponentRecipe componentRecipe = parseRecipe(recipeFileContent, RecipeFormat.YAML);
    if (componentRecipe.getManifests() == null || componentRecipe.getManifests().isEmpty()) {
        throw new PackageLoadingException(String.format("Recipe file %s-%s.yaml is missing manifests", componentRecipe.getComponentName(), componentRecipe.getComponentVersion().toString()));
    }
    Optional<PlatformSpecificManifest> optionalPlatformSpecificManifest = platformResolver.findBestMatch(componentRecipe.getManifests());
    if (!optionalPlatformSpecificManifest.isPresent()) {
        return Optional.empty();
    }
    PlatformSpecificManifest platformSpecificManifest = optionalPlatformSpecificManifest.get();
    Set<String> selectors = collectAllSelectors(componentRecipe.getManifests());
    Map<String, DependencyProperties> dependencyPropertiesMap = new HashMap<>();
    if (componentRecipe.getComponentDependencies() != null) {
        dependencyPropertiesMap.putAll(componentRecipe.getComponentDependencies());
    }
    ComponentRecipe packageRecipe = ComponentRecipe.builder().componentName(componentRecipe.getComponentName()).version(componentRecipe.getComponentVersion()).publisher(componentRecipe.getComponentPublisher()).recipeTemplateVersion(componentRecipe.getRecipeFormatVersion()).componentType(componentRecipe.getComponentType()).dependencies(dependencyPropertiesMap).lifecycle(convertLifecycleFromFile(componentRecipe.getLifecycle(), platformSpecificManifest, selectors)).artifacts(convertArtifactsFromFile(platformSpecificManifest.getArtifacts())).componentConfiguration(componentRecipe.getComponentConfiguration()).build();
    return Optional.of(packageRecipe);
}
Also used : PlatformSpecificManifest(com.amazon.aws.iot.greengrass.component.common.PlatformSpecificManifest) HashMap(java.util.HashMap) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) DependencyProperties(com.amazon.aws.iot.greengrass.component.common.DependencyProperties) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe)

Example 2 with DependencyProperties

use of com.amazon.aws.iot.greengrass.component.common.DependencyProperties in project aws-greengrass-nucleus by aws-greengrass.

the class RecipeLoaderTest method GIVEN_a_recipe_with_all_fields_and_mocked_resolved_platform_WHEN_converts_THEN_fields_are_populated_correctly.

@Test
void GIVEN_a_recipe_with_all_fields_and_mocked_resolved_platform_WHEN_converts_THEN_fields_are_populated_correctly() throws Exception {
    // GIVEN
    // read file
    String filename = "sample_recipe_with_all_fields.yaml";
    String recipeFileContent = new String(Files.readAllBytes(Paths.get(getClass().getResource(filename).toURI())));
    // WHEN
    Optional<ComponentRecipe> optionalRecipe = recipeLoader.loadFromFile(recipeFileContent);
    // THEN
    assertThat(optionalRecipe.isPresent(), is(true));
    ComponentRecipe recipe = optionalRecipe.get();
    assertThat(recipe.getComponentName(), is("FooService"));
    assertThat(recipe.getVersion().getValue(), is("1.0.0"));
    assertThat(recipe.getComponentType().name().toLowerCase(), is("plugin"));
    // GG_NEEDS_REVIEW: TODO enrich testing fields after making lifecycle section strongly typed
    assertThat(recipe.getLifecycle(), aMapWithSize(2));
    assertThat(recipe.getLifecycle(), hasEntry("install", "echo install"));
    assertThat(recipe.getLifecycle(), hasEntry("run", "echo run"));
    assertThat(recipe.getArtifacts().size(), is(2));
    ComponentArtifact artifact = recipe.getArtifacts().get(0);
    assertThat(artifact.getArtifactUri().toString(), is("s3://some-bucket/hello_world.py"));
    assertThat(artifact.getChecksum(), is("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"));
    assertThat(artifact.getAlgorithm(), is("SHA-256"));
    assertThat(recipe.getDependencies().size(), is(2));
    assertThat(recipe.getDependencies(), hasEntry("BarService", new DependencyProperties("^1.1", DependencyType.SOFT)));
    assertThat(recipe.getDependencies(), hasEntry("BazService", new DependencyProperties("^2.0", DependencyType.HARD)));
}
Also used : DependencyProperties(com.amazon.aws.iot.greengrass.component.common.DependencyProperties) ComponentArtifact(com.aws.greengrass.componentmanager.models.ComponentArtifact) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe) Test(org.junit.jupiter.api.Test)

Example 3 with DependencyProperties

use of com.amazon.aws.iot.greengrass.component.common.DependencyProperties in project aws-greengrass-nucleus by aws-greengrass.

the class KernelConfigResolverTest method GIVEN_deployment_with_dependencies_WHEN_config_resolution_requested_THEN_in_nested_namespace_cross_component_configuration_interpolated_for_inner_level.

@Test
void GIVEN_deployment_with_dependencies_WHEN_config_resolution_requested_THEN_in_nested_namespace_cross_component_configuration_interpolated_for_inner_level() throws Exception {
    // A depends on both B and C
    // GIVEN
    ComponentIdentifier componentIdentifierA = new ComponentIdentifier(TEST_INPUT_PACKAGE_A, new Semver("1.2.0"));
    ComponentIdentifier componentIdentifierB = new ComponentIdentifier(TEST_INPUT_PACKAGE_B, new Semver("2.3.0"));
    ComponentIdentifier componentIdentifierC = new ComponentIdentifier(TEST_INPUT_PACKAGE_C, new Semver("3.4.0"));
    Map<String, DependencyProperties> componentADependencies = new HashMap<>();
    componentADependencies.put(TEST_INPUT_PACKAGE_B, DependencyProperties.builder().versionRequirement("2.3").build());
    componentADependencies.put(TEST_INPUT_PACKAGE_C, DependencyProperties.builder().versionRequirement("3.4").build());
    ComponentRecipe componentRecipeA = new ComponentRecipe(RecipeFormatVersion.JAN_25_2020, TEST_INPUT_PACKAGE_A, componentIdentifierA.getVersion(), "", "", null, new HashMap<String, Object>() {

        {
            put(LIFECYCLE_RUN_KEY, "java -jar {configuration:/jarPath}/test.jar");
        }
    }, Collections.emptyList(), componentADependencies, null);
    ComponentRecipe componentRecipeB = new ComponentRecipe(RecipeFormatVersion.JAN_25_2020, TEST_INPUT_PACKAGE_B, componentIdentifierB.getVersion(), "", "", null, null, Collections.emptyList(), Collections.emptyMap(), null);
    ComponentRecipe componentRecipeC = new ComponentRecipe(RecipeFormatVersion.JAN_25_2020, TEST_INPUT_PACKAGE_C, componentIdentifierC.getVersion(), "", "", null, null, Collections.emptyList(), Collections.emptyMap(), null);
    DeploymentPackageConfiguration componentDeploymentConfigA = DeploymentPackageConfiguration.builder().packageName(TEST_INPUT_PACKAGE_A).rootComponent(true).resolvedVersion("=1.2").configurationUpdateOperation(new ConfigurationUpdateOperation(new HashMap<String, Object>() {

        {
            // using work path because only root component supports update config
            put("jarPath", String.format("{randomName:{{{%s:work:path}:{%s:work:path}}:randomVal}}", TEST_INPUT_PACKAGE_B, TEST_INPUT_PACKAGE_C));
        }
    }, new ArrayList<>())).build();
    DeploymentPackageConfiguration componentDeploymentConfigB = DeploymentPackageConfiguration.builder().packageName(TEST_INPUT_PACKAGE_B).rootComponent(false).resolvedVersion("=2.3").build();
    DeploymentPackageConfiguration componentDeploymentConfigC = DeploymentPackageConfiguration.builder().packageName(TEST_INPUT_PACKAGE_C).rootComponent(false).resolvedVersion("=3.4").build();
    DeploymentDocument document = DeploymentDocument.builder().deploymentPackageConfigurationList(Arrays.asList(componentDeploymentConfigA, componentDeploymentConfigB, componentDeploymentConfigC)).timestamp(10_000L).build();
    // WHEN
    Map<ComponentIdentifier, ComponentRecipe> componentsToResolve = new HashMap<>();
    componentsToResolve.put(componentIdentifierA, componentRecipeA);
    componentsToResolve.put(componentIdentifierB, componentRecipeB);
    componentsToResolve.put(componentIdentifierC, componentRecipeC);
    Path expectedCompBWorkPath = Paths.get("/work/" + TEST_INPUT_PACKAGE_B).toAbsolutePath();
    Path expectedCompCWorkPath = Paths.get("/work/" + TEST_INPUT_PACKAGE_C).toAbsolutePath();
    when(nucleusPaths.workPath(TEST_INPUT_PACKAGE_B)).thenReturn(expectedCompBWorkPath);
    when(nucleusPaths.workPath(TEST_INPUT_PACKAGE_C)).thenReturn(expectedCompCWorkPath);
    Map<String, Object> servicesConfig = serviceConfigurationProperlyResolved(document, componentsToResolve);
    // THEN
    String expectedRunCommand = String.format("java -jar {randomName:{{%s:%s}:randomVal}}/test.jar", expectedCompBWorkPath, expectedCompCWorkPath);
    assertThat("jarPath should be replace by the {randomName:{{/work/PackageB:/work/PackageC}:randomVal}}", getServiceRunCommand(TEST_INPUT_PACKAGE_A, servicesConfig), equalTo(expectedRunCommand));
}
Also used : ConfigurationUpdateOperation(com.aws.greengrass.deployment.model.ConfigurationUpdateOperation) Path(java.nio.file.Path) HashMap(java.util.HashMap) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) Semver(com.vdurmont.semver4j.Semver) DeploymentPackageConfiguration(com.aws.greengrass.deployment.model.DeploymentPackageConfiguration) DependencyProperties(com.amazon.aws.iot.greengrass.component.common.DependencyProperties) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with DependencyProperties

use of com.amazon.aws.iot.greengrass.component.common.DependencyProperties in project aws-greengrass-nucleus by aws-greengrass.

the class KernelConfigResolverTest method GIVEN_deployment_with_dependencies_WHEN_config_resolution_requested_THEN_cross_component_work_path_interpolated.

@Test
void GIVEN_deployment_with_dependencies_WHEN_config_resolution_requested_THEN_cross_component_work_path_interpolated() throws Exception {
    // A depends on both B and C
    // GIVEN
    ComponentIdentifier componentIdentifierA = new ComponentIdentifier(TEST_INPUT_PACKAGE_A, new Semver("1.2.0"));
    ComponentIdentifier componentIdentifierB = new ComponentIdentifier(TEST_INPUT_PACKAGE_B, new Semver("2.3.0"));
    ComponentIdentifier componentIdentifierC = new ComponentIdentifier(TEST_INPUT_PACKAGE_C, new Semver("3.4.0"));
    Map<String, DependencyProperties> componentADependencies = new HashMap<>();
    componentADependencies.put(TEST_INPUT_PACKAGE_B, DependencyProperties.builder().versionRequirement("2.3").build());
    componentADependencies.put(TEST_INPUT_PACKAGE_C, DependencyProperties.builder().versionRequirement("3.4").build());
    ComponentRecipe componentRecipeA = new ComponentRecipe(RecipeFormatVersion.JAN_25_2020, TEST_INPUT_PACKAGE_A, componentIdentifierA.getVersion(), "", "", null, new HashMap<String, Object>() {

        {
            put(LIFECYCLE_RUN_KEY, String.format("java -jar {%s:work:path}/test.jar -Dtestval={%s:work:path}", TEST_INPUT_PACKAGE_B, TEST_INPUT_PACKAGE_C));
        }
    }, Collections.emptyList(), componentADependencies, null);
    ComponentRecipe componentRecipeB = new ComponentRecipe(RecipeFormatVersion.JAN_25_2020, TEST_INPUT_PACKAGE_B, componentIdentifierB.getVersion(), "", "", null, null, Collections.emptyList(), Collections.emptyMap(), null);
    ComponentRecipe componentRecipeC = new ComponentRecipe(RecipeFormatVersion.JAN_25_2020, TEST_INPUT_PACKAGE_C, componentIdentifierC.getVersion(), "", "", null, null, Collections.emptyList(), Collections.emptyMap(), null);
    DeploymentPackageConfiguration componentDeploymentConfigA = DeploymentPackageConfiguration.builder().packageName(TEST_INPUT_PACKAGE_A).rootComponent(true).resolvedVersion("=1.2").build();
    DeploymentPackageConfiguration componentDeploymentConfigB = DeploymentPackageConfiguration.builder().packageName(TEST_INPUT_PACKAGE_B).rootComponent(false).resolvedVersion("=2.3").build();
    DeploymentPackageConfiguration componentDeploymentConfigC = DeploymentPackageConfiguration.builder().packageName(TEST_INPUT_PACKAGE_C).rootComponent(false).resolvedVersion("=3.4").build();
    DeploymentDocument document = DeploymentDocument.builder().deploymentPackageConfigurationList(Arrays.asList(componentDeploymentConfigA, componentDeploymentConfigB, componentDeploymentConfigC)).timestamp(10_000L).build();
    // WHEN
    Map<ComponentIdentifier, ComponentRecipe> componentsToResolve = new HashMap<>();
    componentsToResolve.put(componentIdentifierA, componentRecipeA);
    componentsToResolve.put(componentIdentifierB, componentRecipeB);
    componentsToResolve.put(componentIdentifierC, componentRecipeC);
    Path expectedCompBWorkPath = Paths.get("/work/" + TEST_INPUT_PACKAGE_B).toAbsolutePath();
    Path expectedCompCWorkPath = Paths.get("/work/" + TEST_INPUT_PACKAGE_C).toAbsolutePath();
    when(nucleusPaths.workPath(TEST_INPUT_PACKAGE_B)).thenReturn(expectedCompBWorkPath);
    when(nucleusPaths.workPath(TEST_INPUT_PACKAGE_C)).thenReturn(expectedCompCWorkPath);
    Map<String, Object> servicesConfig = serviceConfigurationProperlyResolved(document, componentsToResolve);
    // THEN
    String expectedRunCommand = String.format("java -jar %s/test.jar -Dtestval=%s", expectedCompBWorkPath, expectedCompCWorkPath);
    assertThat("{artifacts:path} should be replace by the package's artifact path", getServiceRunCommand(TEST_INPUT_PACKAGE_A, servicesConfig), equalTo(expectedRunCommand));
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) Semver(com.vdurmont.semver4j.Semver) DeploymentPackageConfiguration(com.aws.greengrass.deployment.model.DeploymentPackageConfiguration) DependencyProperties(com.amazon.aws.iot.greengrass.component.common.DependencyProperties) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with DependencyProperties

use of com.amazon.aws.iot.greengrass.component.common.DependencyProperties in project aws-greengrass-nucleus by aws-greengrass.

the class RecipeLoaderTest method GIVEN_a_recipe_file_with_global_dependencies_WHEN_converts_THEN_fields_are_populated_correctly.

@Test
void GIVEN_a_recipe_file_with_global_dependencies_WHEN_converts_THEN_fields_are_populated_correctly() throws Exception {
    // GIVEN
    // read file
    String filename = "sample_recipe_with_all_fields.yaml";
    String recipeFileContent = new String(Files.readAllBytes(Paths.get(getClass().getResource(filename).toURI())));
    // WHEN
    Optional<ComponentRecipe> optionalRecipe = recipeLoader.loadFromFile(recipeFileContent);
    // THEN
    assertThat(optionalRecipe.isPresent(), is(true));
    ComponentRecipe recipe = optionalRecipe.get();
    assertThat(recipe.getComponentName(), is("FooService"));
    assertThat(recipe.getVersion().getValue(), is("1.0.0"));
    assertThat(recipe.getComponentType().name().toLowerCase(), is("plugin"));
    // GG_NEEDS_REVIEW: TODO enrich testing fields after making lifecycle section strongly typed
    assertThat(recipe.getLifecycle(), aMapWithSize(2));
    assertThat(recipe.getLifecycle(), hasEntry("install", "echo install"));
    assertThat(recipe.getLifecycle(), hasEntry("run", "echo run"));
    assertThat(recipe.getArtifacts().size(), is(2));
    ComponentArtifact artifact = recipe.getArtifacts().get(0);
    assertThat(artifact.getArtifactUri().toString(), is("s3://some-bucket/hello_world.py"));
    assertThat(artifact.getChecksum(), is("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"));
    assertThat(artifact.getAlgorithm(), is("SHA-256"));
    assertThat(recipe.getDependencies().size(), is(2));
    assertThat(recipe.getDependencies(), hasEntry("BarService", new DependencyProperties("^1.1", DependencyType.SOFT)));
    assertThat(recipe.getDependencies(), hasEntry("BazService", new DependencyProperties("^2.0", DependencyType.HARD)));
}
Also used : DependencyProperties(com.amazon.aws.iot.greengrass.component.common.DependencyProperties) ComponentArtifact(com.aws.greengrass.componentmanager.models.ComponentArtifact) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe) Test(org.junit.jupiter.api.Test)

Aggregations

DependencyProperties (com.amazon.aws.iot.greengrass.component.common.DependencyProperties)6 ComponentRecipe (com.aws.greengrass.componentmanager.models.ComponentRecipe)5 Test (org.junit.jupiter.api.Test)4 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)3 HashMap (java.util.HashMap)3 ComponentArtifact (com.aws.greengrass.componentmanager.models.ComponentArtifact)2 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)2 DeploymentPackageConfiguration (com.aws.greengrass.deployment.model.DeploymentPackageConfiguration)2 Semver (com.vdurmont.semver4j.Semver)2 Path (java.nio.file.Path)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 PlatformSpecificManifest (com.amazon.aws.iot.greengrass.component.common.PlatformSpecificManifest)1 KernelConfigResolver (com.aws.greengrass.componentmanager.KernelConfigResolver)1 PackageLoadingException (com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)1 CaseInsensitiveString (com.aws.greengrass.config.CaseInsensitiveString)1 Topics (com.aws.greengrass.config.Topics)1 ConfigurationUpdateOperation (com.aws.greengrass.deployment.model.ConfigurationUpdateOperation)1 IOException (java.io.IOException)1