Search in sources :

Example 1 with GetComponentVersionArtifactResponse

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

Example 2 with GetComponentVersionArtifactResponse

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"));
}
Also used : 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) Semver(com.vdurmont.semver4j.Semver) GetComponentVersionArtifactRequest(software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 3 with GetComponentVersionArtifactResponse

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);
}
Also used : Path(java.nio.file.Path) RecipeMetadata(com.aws.greengrass.componentmanager.models.RecipeMetadata) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) GetComponentVersionArtifactResponse(software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse) ComponentArtifact(com.aws.greengrass.componentmanager.models.ComponentArtifact) Semver(com.vdurmont.semver4j.Semver) URI(java.net.URI) GetComponentVersionArtifactRequest(software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest) Test(org.junit.jupiter.api.Test)

Example 4 with GetComponentVersionArtifactResponse

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);
    }
}
Also used : GetComponentVersionArtifactResponse(software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse) PackageDownloadException(com.aws.greengrass.componentmanager.exceptions.PackageDownloadException) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) GetComponentVersionArtifactRequest(software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) PackageDownloadException(com.aws.greengrass.componentmanager.exceptions.PackageDownloadException) IOException(java.io.IOException) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException)

Aggregations

GetComponentVersionArtifactRequest (software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest)4 GetComponentVersionArtifactResponse (software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactResponse)4 PackageDownloadException (com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)3 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)3 RecipeMetadata (com.aws.greengrass.componentmanager.models.RecipeMetadata)3 Semver (com.vdurmont.semver4j.Semver)3 URI (java.net.URI)3 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)2 SdkClientException (software.amazon.awssdk.core.exception.SdkClientException)2 PackageLoadingException (com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)1 ComponentArtifact (com.aws.greengrass.componentmanager.models.ComponentArtifact)1 Path (java.nio.file.Path)1