Search in sources :

Example 6 with CreateOptions

use of org.apache.beam.sdk.io.fs.CreateOptions in project beam by apache.

the class PackageUtilTest method testPackageUploadFailsWithPermissionsErrorGivesDetailedMessage.

@Test
public void testPackageUploadFailsWithPermissionsErrorGivesDetailedMessage() throws Exception {
    File tmpFile = makeFileWithContents("file.txt", "This is a test!");
    when(mockGcsUtil.getObjects(anyListOf(GcsPath.class))).thenReturn(ImmutableList.of(StorageObjectOrIOException.create(new FileNotFoundException("some/path"))));
    when(mockGcsUtil.create(any(GcsPath.class), any(GcsUtil.CreateOptions.class))).thenThrow(new IOException("Failed to write to GCS path " + STAGING_PATH, googleJsonResponseException(HttpStatusCodes.STATUS_CODE_FORBIDDEN, "Permission denied", "Test message")));
    try (PackageUtil directPackageUtil = PackageUtil.withExecutorService(MoreExecutors.newDirectExecutorService())) {
        directPackageUtil.stageClasspathElements(ImmutableList.of(makeStagedFile(tmpFile.getAbsolutePath())), STAGING_PATH, fastNanoClockAndSleeper, createOptions);
        fail("Expected RuntimeException");
    } catch (RuntimeException e) {
        assertThat("Expected RuntimeException wrapping IOException.", e.getCause(), instanceOf(RuntimeException.class));
        assertThat("Expected IOException containing detailed message.", e.getCause().getCause(), instanceOf(IOException.class));
        assertThat(e.getCause().getCause().getMessage(), Matchers.allOf(Matchers.containsString("Uploaded failed due to permissions error"), Matchers.containsString("Stale credentials can be resolved by executing 'gcloud auth application-default " + "login'")));
    } finally {
        verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
        verify(mockGcsUtil).create(any(GcsPath.class), any(GcsUtil.CreateOptions.class));
        verifyNoMoreInteractions(mockGcsUtil);
    }
}
Also used : GcsPath(org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath) FileNotFoundException(java.io.FileNotFoundException) StorageObjectOrIOException(org.apache.beam.sdk.extensions.gcp.util.GcsUtil.StorageObjectOrIOException) IOException(java.io.IOException) StagedFile(org.apache.beam.runners.dataflow.util.PackageUtil.StagedFile) File(java.io.File) StandardCreateOptions(org.apache.beam.sdk.io.fs.CreateOptions.StandardCreateOptions) CreateOptions(org.apache.beam.sdk.io.fs.CreateOptions) Test(org.junit.Test)

Example 7 with CreateOptions

use of org.apache.beam.sdk.io.fs.CreateOptions in project beam by apache.

the class PackageUtilTest method testPackageUploadWithFileSucceeds.

@Test
public void testPackageUploadWithFileSucceeds() throws Exception {
    Pipe pipe = Pipe.open();
    String contents = "This is a test!";
    File tmpFile = makeFileWithContents("file.txt", contents);
    when(mockGcsUtil.getObjects(anyListOf(GcsPath.class))).thenReturn(ImmutableList.of(StorageObjectOrIOException.create(new FileNotFoundException("some/path"))));
    when(mockGcsUtil.create(any(GcsPath.class), any(GcsUtil.CreateOptions.class))).thenReturn(pipe.sink());
    List<DataflowPackage> targets = defaultPackageUtil.stageClasspathElements(ImmutableList.of(makeStagedFile(tmpFile.getAbsolutePath())), STAGING_PATH, createOptions);
    DataflowPackage target = Iterables.getOnlyElement(targets);
    verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
    verify(mockGcsUtil).create(any(GcsPath.class), any(GcsUtil.CreateOptions.class));
    verifyNoMoreInteractions(mockGcsUtil);
    assertThat(target.getName(), endsWith(".txt"));
    assertThat(target.getLocation(), equalTo(STAGING_PATH + target.getName()));
    assertThat(new LineReader(Channels.newReader(pipe.source(), StandardCharsets.UTF_8.name())).readLine(), equalTo(contents));
}
Also used : LineReader(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.LineReader) GcsPath(org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath) FileNotFoundException(java.io.FileNotFoundException) Pipe(java.nio.channels.Pipe) StagedFile(org.apache.beam.runners.dataflow.util.PackageUtil.StagedFile) File(java.io.File) StandardCreateOptions(org.apache.beam.sdk.io.fs.CreateOptions.StandardCreateOptions) CreateOptions(org.apache.beam.sdk.io.fs.CreateOptions) DataflowPackage(com.google.api.services.dataflow.model.DataflowPackage) Test(org.junit.Test)

Example 8 with CreateOptions

use of org.apache.beam.sdk.io.fs.CreateOptions in project beam by apache.

the class PackageUtilTest method testPackageUploadWithExplicitPackageName.

@Test
public void testPackageUploadWithExplicitPackageName() throws Exception {
    Pipe pipe = Pipe.open();
    File tmpFile = makeFileWithContents("file.txt", "This is a test!");
    final String overriddenName = "alias.txt";
    when(mockGcsUtil.getObjects(anyListOf(GcsPath.class))).thenReturn(ImmutableList.of(StorageObjectOrIOException.create(new FileNotFoundException("some/path"))));
    when(mockGcsUtil.create(any(GcsPath.class), any(GcsUtil.CreateOptions.class))).thenReturn(pipe.sink());
    List<DataflowPackage> targets = defaultPackageUtil.stageClasspathElements(ImmutableList.of(makeStagedFile(tmpFile.getAbsolutePath(), overriddenName)), STAGING_PATH, createOptions);
    DataflowPackage target = Iterables.getOnlyElement(targets);
    verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
    verify(mockGcsUtil).create(any(GcsPath.class), any(GcsUtil.CreateOptions.class));
    verifyNoMoreInteractions(mockGcsUtil);
    assertThat(target.getName(), equalTo(overriddenName));
    assertThat(target.getLocation(), RegexMatcher.matches(STAGING_PATH + "alias.txt"));
}
Also used : GcsPath(org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath) FileNotFoundException(java.io.FileNotFoundException) Pipe(java.nio.channels.Pipe) StagedFile(org.apache.beam.runners.dataflow.util.PackageUtil.StagedFile) File(java.io.File) StandardCreateOptions(org.apache.beam.sdk.io.fs.CreateOptions.StandardCreateOptions) CreateOptions(org.apache.beam.sdk.io.fs.CreateOptions) DataflowPackage(com.google.api.services.dataflow.model.DataflowPackage) Test(org.junit.Test)

Example 9 with CreateOptions

use of org.apache.beam.sdk.io.fs.CreateOptions in project beam by apache.

the class PackageUtilTest method testPackageUploadFailsWhenIOExceptionThrown.

@Test(expected = RuntimeException.class)
public void testPackageUploadFailsWhenIOExceptionThrown() throws Exception {
    File tmpFile = makeFileWithContents("file.txt", "This is a test!");
    when(mockGcsUtil.getObjects(anyListOf(GcsPath.class))).thenReturn(ImmutableList.of(StorageObjectOrIOException.create(new FileNotFoundException("some/path"))));
    when(mockGcsUtil.create(any(GcsPath.class), any(GcsUtil.CreateOptions.class))).thenThrow(new IOException("Fake Exception: Upload error"));
    try (PackageUtil directPackageUtil = PackageUtil.withExecutorService(MoreExecutors.newDirectExecutorService())) {
        directPackageUtil.stageClasspathElements(ImmutableList.of(makeStagedFile(tmpFile.getAbsolutePath())), STAGING_PATH, fastNanoClockAndSleeper, createOptions);
    } finally {
        verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
        verify(mockGcsUtil, times(5)).create(any(GcsPath.class), any(GcsUtil.CreateOptions.class));
        verifyNoMoreInteractions(mockGcsUtil);
    }
}
Also used : GcsPath(org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath) FileNotFoundException(java.io.FileNotFoundException) StorageObjectOrIOException(org.apache.beam.sdk.extensions.gcp.util.GcsUtil.StorageObjectOrIOException) IOException(java.io.IOException) StagedFile(org.apache.beam.runners.dataflow.util.PackageUtil.StagedFile) File(java.io.File) StandardCreateOptions(org.apache.beam.sdk.io.fs.CreateOptions.StandardCreateOptions) CreateOptions(org.apache.beam.sdk.io.fs.CreateOptions) Test(org.junit.Test)

Aggregations

File (java.io.File)9 StagedFile (org.apache.beam.runners.dataflow.util.PackageUtil.StagedFile)9 GcsPath (org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath)9 CreateOptions (org.apache.beam.sdk.io.fs.CreateOptions)9 StandardCreateOptions (org.apache.beam.sdk.io.fs.CreateOptions.StandardCreateOptions)9 Test (org.junit.Test)9 FileNotFoundException (java.io.FileNotFoundException)8 Pipe (java.nio.channels.Pipe)6 DataflowPackage (com.google.api.services.dataflow.model.DataflowPackage)4 IOException (java.io.IOException)3 StorageObjectOrIOException (org.apache.beam.sdk.extensions.gcp.util.GcsUtil.StorageObjectOrIOException)3 ZipInputStream (java.util.zip.ZipInputStream)2 ArrayList (java.util.ArrayList)1 ZipEntry (java.util.zip.ZipEntry)1 GcsUtil (org.apache.beam.sdk.extensions.gcp.util.GcsUtil)1 LineReader (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.LineReader)1