use of software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse 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"));
}
use of software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse in project aws-greengrass-nucleus by aws-greengrass.
the class GreengrassRepositoryDownloaderTest method GIVEN_http_connection_bad_request_WHEN_attempt_download_THEN_download_error_thrown.
@Test
void GIVEN_http_connection_bad_request_WHEN_attempt_download_THEN_download_error_thrown() throws Exception {
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());
doReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(HTTP_BAD_REQUEST).build()).build()).when(request).call();
PackageDownloadException e = assertThrows(PackageDownloadException.class, () -> downloader.download(0, 100, MessageDigest.getInstance("SHA-256")));
// assert retry called
verify(request, times(1)).call();
assertThat(e.getLocalizedMessage(), containsStringIgnoringCase("Failed to download the artifact"));
}
use of software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse in project aws-greengrass-nucleus by aws-greengrass.
the class GreengrassRepositoryDownloaderTest method GIVEN_artifact_url_WHEN_attempt_download_THEN_task_succeed.
@Test
void GIVEN_artifact_url_WHEN_attempt_download_THEN_task_succeed() throws Exception {
// build downloader
Path mockArtifactPath = ComponentTestResourceHelper.getPathForTestPackage(ComponentTestResourceHelper.MONITORING_SERVICE_PACKAGE_NAME, "1.0.0").resolve("monitor_artifact_100.txt");
String checksum = Base64.getEncoder().encodeToString(MessageDigest.getInstance(SHA256).digest(Files.readAllBytes(mockArtifactPath)));
ComponentArtifact artifact = ComponentArtifact.builder().algorithm(SHA256).checksum(checksum).artifactUri(new URI("greengrass:774pP05xtua0RCcwj9uALSdAqGr_vC631EdOBkJxnec=/artifact.txt")).build();
ComponentIdentifier pkgId = new ComponentIdentifier("CoolService", new Semver("1.0.0"));
lenient().when(componentStore.getRecipeMetadata(pkgId)).thenReturn(new RecipeMetadata(TEST_ARN));
Path testCache = ComponentTestResourceHelper.getPathForLocalTestCache();
Path saveToPath = testCache.resolve("CoolService").resolve("1.0.0");
Files.createDirectories(saveToPath);
GreengrassRepositoryDownloader downloader = spy(new GreengrassRepositoryDownloader(clientFactory, pkgId, artifact, saveToPath, componentStore));
assertThat(downloader.getArtifactFilename(), is("artifact.txt"));
// mock requests to get downloadSize and local file name
GetComponentVersionArtifactResponse result = GetComponentVersionArtifactResponse.builder().preSignedUrl("https://www.amazon.com/artifact.txt").build();
when(client.getComponentVersionArtifact(getComponentVersionArtifactRequestArgumentCaptor.capture())).thenReturn(result);
// mock requests to return partial stream
doReturn(httpClient).when(downloader).getSdkHttpClient();
doReturn(request).when(httpClient).prepareRequest(any());
when(request.call()).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(HTTP_OK).putHeader(CONTENT_LENGTH_HEADER, String.valueOf(Files.size(mockArtifactPath))).build()).responseBody(AbortableInputStream.create(Files.newInputStream(mockArtifactPath))).build()).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(HTTP_PARTIAL).putHeader(CONTENT_LENGTH_HEADER, String.valueOf(Files.size(mockArtifactPath))).build()).responseBody(AbortableInputStream.create(Files.newInputStream(mockArtifactPath))).build());
downloader.download();
GetComponentVersionArtifactRequest generatedRequest = getComponentVersionArtifactRequestArgumentCaptor.getValue();
assertEquals(TEST_ARN, generatedRequest.arn());
assertEquals("774pP05xtua0RCcwj9uALSdAqGr_vC631EdOBkJxnec=/artifact.txt", generatedRequest.artifactName());
byte[] originalFile = Files.readAllBytes(mockArtifactPath);
Path artifactFilePath = saveToPath.resolve("artifact.txt");
byte[] downloadFile = Files.readAllBytes(artifactFilePath);
assertThat(Arrays.equals(originalFile, downloadFile), is(true));
ComponentTestResourceHelper.cleanDirectory(testCache);
}
use of software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse in project aws-greengrass-nucleus by aws-greengrass.
the class GreengrassRepositoryDownloader method getArtifactDownloadURL.
@SuppressWarnings({ "PMD.AvoidCatchingGenericException", "PMD.AvoidRethrowingException" })
private String getArtifactDownloadURL(ComponentIdentifier componentIdentifier, String artifactName) throws InterruptedException, PackageDownloadException {
String arn;
try {
arn = componentStore.getRecipeMetadata(componentIdentifier).getComponentVersionArn();
} catch (PackageLoadingException e) {
throw new PackageDownloadException(getErrorString("Failed to get component version arn from component store"), e);
}
try {
return RetryUtils.runWithRetry(clientExceptionRetryConfig, () -> {
GetComponentVersionArtifactRequest getComponentArtifactRequest = GetComponentVersionArtifactRequest.builder().artifactName(artifactName).arn(arn).build();
GetComponentVersionArtifactResponse getComponentArtifactResult = clientFactory.getGreengrassV2DataClient().getComponentVersionArtifact(getComponentArtifactRequest);
return getComponentArtifactResult.preSignedUrl();
}, "get-artifact-size", logger);
} catch (InterruptedException e) {
throw e;
} catch (Exception e) {
throw new PackageDownloadException(getErrorString("Failed to get download size"), e);
}
}
Aggregations