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));
}
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));
}
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));
}
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)));
}
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"));
}
Aggregations