Search in sources :

Example 1 with RecipeMetadata

use of com.aws.greengrass.componentmanager.models.RecipeMetadata in project aws-greengrass-nucleus by aws-greengrass.

the class ComponentStoreTest method GIVEN_a_valid_metadata_file_WHEN_getRecipeMetadata_THEN_return.

@Test
void GIVEN_a_valid_metadata_file_WHEN_getRecipeMetadata_THEN_return() throws Exception {
    preloadRecipeMetadataFileFromTestResource("HelloWorld@1.0.0.metadata.json");
    // defined in HelloWorld@1.0.0.metadata.json
    String expectedArn = "testArn";
    RecipeMetadata recipeMetadata = componentStore.getRecipeMetadata(new ComponentIdentifier("HelloWorld", new Semver("1.0.0")));
    assertThat(recipeMetadata.getComponentVersionArn(), equalTo(expectedArn));
}
Also used : RecipeMetadata(com.aws.greengrass.componentmanager.models.RecipeMetadata) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) Semver(com.vdurmont.semver4j.Semver) Test(org.junit.jupiter.api.Test)

Example 2 with RecipeMetadata

use of com.aws.greengrass.componentmanager.models.RecipeMetadata in project aws-greengrass-nucleus by aws-greengrass.

the class ComponentManagerTest method GIVEN_requirement_is_from_local_group_and_has_no_local_version_WHEN_resolve_version_THEN_use_cloud_version.

@Test
void GIVEN_requirement_is_from_local_group_and_has_no_local_version_WHEN_resolve_version_THEN_use_cloud_version() throws Exception {
    ComponentIdentifier componentA_1_0_0 = new ComponentIdentifier(componentA, v1_0_0);
    ComponentMetadata componentA_1_0_0_md = new ComponentMetadata(componentA_1_0_0, Collections.emptyMap());
    // no local version
    when(componentStore.findBestMatchAvailableComponent(eq(componentA), any())).thenReturn(Optional.empty());
    // has cloud version
    com.amazon.aws.iot.greengrass.component.common.ComponentRecipe recipe = com.amazon.aws.iot.greengrass.component.common.ComponentRecipe.builder().componentName(componentA).componentVersion(v1_0_0).componentType(ComponentType.GENERIC).recipeFormatVersion(RecipeFormatVersion.JAN_25_2020).build();
    ResolvedComponentVersion resolvedComponentVersion = ResolvedComponentVersion.builder().componentName(componentA).componentVersion(v1_0_0.getValue()).recipe(SdkBytes.fromByteArray(MAPPER.writeValueAsBytes(recipe))).arn(TEST_ARN).build();
    when(componentManagementServiceHelper.resolveComponentVersion(anyString(), any(), any())).thenReturn(resolvedComponentVersion);
    // mock return metadata from the id
    when(componentStore.getPackageMetadata(any())).thenReturn(componentA_1_0_0_md);
    // WHEN
    ComponentMetadata componentMetadata = componentManager.resolveComponentVersion(componentA, Collections.singletonMap(DeploymentDocumentConverter.LOCAL_DEPLOYMENT_GROUP_NAME, Requirement.buildNPM("^1.0")));
    // THEN
    assertThat(componentMetadata, is(componentA_1_0_0_md));
    verify(componentStore).findBestMatchAvailableComponent(componentA, Requirement.buildNPM("^1.0"));
    verify(componentManagementServiceHelper).resolveComponentVersion(componentA, null, Collections.singletonMap(DeploymentDocumentConverter.LOCAL_DEPLOYMENT_GROUP_NAME, Requirement.buildNPM("^1.0")));
    verify(componentStore).getPackageMetadata(componentA_1_0_0);
    verify(componentStore).saveComponentRecipe(recipe);
    verify(componentStore).saveRecipeMetadata(componentA_1_0_0, new RecipeMetadata(TEST_ARN));
}
Also used : ResolvedComponentVersion(software.amazon.awssdk.services.greengrassv2data.model.ResolvedComponentVersion) RecipeMetadata(com.aws.greengrass.componentmanager.models.RecipeMetadata) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) ComponentMetadata(com.aws.greengrass.componentmanager.models.ComponentMetadata) Test(org.junit.jupiter.api.Test)

Example 3 with RecipeMetadata

use of com.aws.greengrass.componentmanager.models.RecipeMetadata in project aws-greengrass-nucleus by aws-greengrass.

the class ComponentManagerTest method GIVEN_component_is_local_active_WHEN_cloud_returns_a_recipe_THEN_use_cloud_recipe.

@Test
void GIVEN_component_is_local_active_WHEN_cloud_returns_a_recipe_THEN_use_cloud_recipe() throws Exception {
    ComponentIdentifier componentA_1_0_0 = new ComponentIdentifier(componentA, v1_0_0);
    ComponentMetadata componentA_1_0_0_md = new ComponentMetadata(componentA_1_0_0, Collections.emptyMap());
    Topics serviceConfigTopics = mock(Topics.class);
    Topic versionTopic = mock(Topic.class);
    Topics runtimeTopics = mock(Topics.class);
    Topic digestTopic = mock(Topic.class);
    com.amazon.aws.iot.greengrass.component.common.ComponentRecipe newRecipe = com.amazon.aws.iot.greengrass.component.common.ComponentRecipe.builder().componentName("SampleComponent2").componentVersion(new Semver("2.0.0")).componentType(ComponentType.PLUGIN).recipeFormatVersion(RecipeFormatVersion.JAN_25_2020).build();
    GreengrassService mockKernelService = mock(GreengrassService.class);
    when(kernel.findServiceTopic(componentA)).thenReturn(mock(Topics.class));
    when(kernel.locate(componentA)).thenReturn(mockService);
    when(kernel.getMain()).thenReturn(mockKernelService);
    when(mockKernelService.getRuntimeConfig()).thenReturn(runtimeTopics);
    when(runtimeTopics.lookup(any())).thenReturn(digestTopic);
    when(mockService.getServiceConfig()).thenReturn(serviceConfigTopics);
    when(serviceConfigTopics.findLeafChild(VERSION_CONFIG_KEY)).thenReturn(versionTopic);
    when(versionTopic.getOnce()).thenReturn(v1_0_0.getValue());
    ResolvedComponentVersion resolvedComponentVersion = ResolvedComponentVersion.builder().componentName(componentA).componentVersion(v1_0_0.getValue()).recipe(SdkBytes.fromByteArray(MAPPER.writeValueAsBytes(newRecipe))).arn(TEST_ARN).build();
    when(componentManagementServiceHelper.resolveComponentVersion(anyString(), any(), any())).thenReturn(resolvedComponentVersion);
    when(componentStore.getPackageMetadata(any())).thenReturn(componentA_1_0_0_md);
    String recipeString = SerializerFactory.getRecipeSerializer().writeValueAsString(newRecipe);
    when(componentStore.saveComponentRecipe(any())).thenReturn(recipeString);
    ComponentMetadata componentMetadata = componentManager.resolveComponentVersion(componentA, Collections.singletonMap("X", Requirement.buildNPM("^1.0")));
    assertThat(componentMetadata, is(componentA_1_0_0_md));
    verify(componentManagementServiceHelper).resolveComponentVersion(componentA, v1_0_0, Collections.singletonMap("X", Requirement.buildNPM("^1.0")));
    verify(componentStore).saveComponentRecipe(newRecipe);
    verify(componentStore).getPackageMetadata(componentA_1_0_0);
    verify(componentStore).saveRecipeMetadata(componentA_1_0_0, new RecipeMetadata(TEST_ARN));
    verify(digestTopic).withValue(Digest.calculate(recipeString));
}
Also used : Topics(com.aws.greengrass.config.Topics) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentMetadata(com.aws.greengrass.componentmanager.models.ComponentMetadata) Semver(com.vdurmont.semver4j.Semver) ResolvedComponentVersion(software.amazon.awssdk.services.greengrassv2data.model.ResolvedComponentVersion) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) RecipeMetadata(com.aws.greengrass.componentmanager.models.RecipeMetadata) Topic(com.aws.greengrass.config.Topic) Test(org.junit.jupiter.api.Test)

Example 4 with RecipeMetadata

use of com.aws.greengrass.componentmanager.models.RecipeMetadata in project aws-greengrass-nucleus by aws-greengrass.

the class ComponentStoreTest method GIVEN_existing_metadata_file_WHEN_saveRecipeMetadata_THEN_file_is_written_with_right_content.

@Test
void GIVEN_existing_metadata_file_WHEN_saveRecipeMetadata_THEN_file_is_written_with_right_content() throws Exception {
    // GIVEN
    preloadRecipeMetadataFileFromTestResource("HelloWorld@1.0.0.metadata.json");
    String componentName = "HelloWorld";
    String version = "1.0.0";
    File expectedRecipeMetadataFile = getExpectedRecipeMetadataFile(componentName, version);
    assertThat(expectedRecipeMetadataFile, is(anExistingFile()));
    String updatedArn = "updatedArn";
    // WHEN
    componentStore.saveRecipeMetadata(new ComponentIdentifier(componentName, new Semver(version)), new RecipeMetadata(updatedArn));
    // THEN
    assertThat(expectedRecipeMetadataFile, is(anExistingFile()));
    String expectedContent = SerializerFactory.getFailSafeJsonObjectMapper().writeValueAsString(new RecipeMetadata(updatedArn));
    String actualContent = new String(Files.readAllBytes(expectedRecipeMetadataFile.toPath()));
    assertThat(actualContent, is(equalTo(expectedContent)));
}
Also used : RecipeMetadata(com.aws.greengrass.componentmanager.models.RecipeMetadata) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) Semver(com.vdurmont.semver4j.Semver) FileMatchers.anExistingFile(org.hamcrest.io.FileMatchers.anExistingFile) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 5 with RecipeMetadata

use of com.aws.greengrass.componentmanager.models.RecipeMetadata in project aws-greengrass-nucleus by aws-greengrass.

the class GreengrassRepositoryDownloaderTest method GIVEN_http_connection_error_WHEN_attempt_download_THEN_retry_called.

@Test
void GIVEN_http_connection_error_WHEN_attempt_download_THEN_retry_called(ExtensionContext context) throws Exception {
    ignoreExceptionOfType(context, IOException.class);
    GetComponentVersionArtifactResponse result = GetComponentVersionArtifactResponse.builder().preSignedUrl("https://www.amazon.com/artifact.txt").build();
    when(client.getComponentVersionArtifact(any(GetComponentVersionArtifactRequest.class))).thenReturn(result);
    ComponentIdentifier pkgId = new ComponentIdentifier("CoolService", new Semver("1.0.0"));
    lenient().when(componentStore.getRecipeMetadata(pkgId)).thenReturn(new RecipeMetadata(TEST_ARN));
    GreengrassRepositoryDownloader downloader = spy(new GreengrassRepositoryDownloader(clientFactory, pkgId, ComponentArtifact.builder().artifactUri(new URI("greengrass:binary")).build(), null, componentStore));
    doReturn(httpClient).when(downloader).getSdkHttpClient();
    doReturn(request).when(httpClient).prepareRequest(any());
    when(request.call()).thenThrow(IOException.class);
    downloader.setClientExceptionRetryConfig(RetryUtils.RetryConfig.builder().maxAttempt(2).retryableExceptions(Arrays.asList(SdkClientException.class, IOException.class)).build());
    PackageDownloadException e = assertThrows(PackageDownloadException.class, () -> downloader.download(0, 100, MessageDigest.getInstance("SHA-256")));
    // assert retry called
    verify(request, times(2)).call();
    assertThat(e.getLocalizedMessage(), containsStringIgnoringCase("Failed to download artifact"));
}
Also used : SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) RecipeMetadata(com.aws.greengrass.componentmanager.models.RecipeMetadata) GetComponentVersionArtifactResponse(software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) PackageDownloadException(com.aws.greengrass.componentmanager.exceptions.PackageDownloadException) IOException(java.io.IOException) Semver(com.vdurmont.semver4j.Semver) GetComponentVersionArtifactRequest(software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)10 RecipeMetadata (com.aws.greengrass.componentmanager.models.RecipeMetadata)10 Semver (com.vdurmont.semver4j.Semver)9 Test (org.junit.jupiter.api.Test)9 PackageDownloadException (com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)3 URI (java.net.URI)3 GetComponentVersionArtifactRequest (software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest)3 GetComponentVersionArtifactResponse (software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse)3 ResolvedComponentVersion (software.amazon.awssdk.services.greengrassv2data.model.ResolvedComponentVersion)3 ComponentMetadata (com.aws.greengrass.componentmanager.models.ComponentMetadata)2 File (java.io.File)2 IOException (java.io.IOException)2 FileMatchers.anExistingFile (org.hamcrest.io.FileMatchers.anExistingFile)2 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)2 InvalidArtifactUriException (com.aws.greengrass.componentmanager.exceptions.InvalidArtifactUriException)1 MissingRequiredComponentsException (com.aws.greengrass.componentmanager.exceptions.MissingRequiredComponentsException)1 NoAvailableComponentVersionException (com.aws.greengrass.componentmanager.exceptions.NoAvailableComponentVersionException)1 PackageLoadingException (com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)1 PackagingException (com.aws.greengrass.componentmanager.exceptions.PackagingException)1 SizeLimitException (com.aws.greengrass.componentmanager.exceptions.SizeLimitException)1