Search in sources :

Example 1 with CreateOptions

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

the class PackageUtilTest method testPackageUploadWithDirectorySucceeds.

@Test
public void testPackageUploadWithDirectorySucceeds() throws Exception {
    Pipe pipe = Pipe.open();
    File tmpDirectory = tmpFolder.newFolder("folder");
    tmpFolder.newFolder("folder", "empty_directory");
    tmpFolder.newFolder("folder", "directory");
    makeFileWithContents("folder/file.txt", "This is a test!");
    makeFileWithContents("folder/directory/file.txt", "This is also 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))).thenReturn(pipe.sink());
    defaultPackageUtil.stageClasspathElements(ImmutableList.of(makeStagedFile(tmpDirectory.getAbsolutePath())), STAGING_PATH, createOptions);
    verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
    verify(mockGcsUtil).create(any(GcsPath.class), any(GcsUtil.CreateOptions.class));
    verifyNoMoreInteractions(mockGcsUtil);
    List<String> zipEntryNames = new ArrayList<>();
    try (ZipInputStream inputStream = new ZipInputStream(Channels.newInputStream(pipe.source()))) {
        for (ZipEntry entry = inputStream.getNextEntry(); entry != null; entry = inputStream.getNextEntry()) {
            zipEntryNames.add(entry.getName());
        }
    }
    assertThat(zipEntryNames, containsInAnyOrder("directory/file.txt", "empty_directory/", "file.txt"));
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) GcsPath(org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) 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) Test(org.junit.Test)

Example 2 with CreateOptions

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

the class PackageUtilTest method testPackageUploadIsNotSkippedWhenSizesAreDifferent.

@Test
public void testPackageUploadIsNotSkippedWhenSizesAreDifferent() throws Exception {
    Pipe pipe = Pipe.open();
    File tmpDirectory = tmpFolder.newFolder("folder");
    tmpFolder.newFolder("folder", "empty_directory");
    tmpFolder.newFolder("folder", "directory");
    makeFileWithContents("folder/file.txt", "This is a test!");
    makeFileWithContents("folder/directory/file.txt", "This is also a test!");
    when(mockGcsUtil.getObjects(anyListOf(GcsPath.class))).thenReturn(ImmutableList.of(StorageObjectOrIOException.create(createStorageObject(STAGING_PATH, Long.MAX_VALUE))));
    when(mockGcsUtil.create(any(GcsPath.class), any(GcsUtil.CreateOptions.class))).thenReturn(pipe.sink());
    defaultPackageUtil.stageClasspathElements(ImmutableList.of(makeStagedFile(tmpDirectory.getAbsolutePath())), STAGING_PATH, createOptions);
    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) 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) Test(org.junit.Test)

Example 3 with CreateOptions

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

the class PackageUtilTest method testStagingPreservesClasspath.

@Test
public void testStagingPreservesClasspath() throws Exception {
    File smallFile = makeFileWithContents("small.txt", "small");
    File largeFile = makeFileWithContents("large.log", "large 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))).thenAnswer(invocation -> Pipe.open().sink());
    List<DataflowPackage> targets = defaultPackageUtil.stageClasspathElements(ImmutableList.of(makeStagedFile(smallFile.getAbsolutePath()), makeStagedFile(largeFile.getAbsolutePath())), STAGING_PATH, createOptions);
    // Verify that the packages are returned small, then large, matching input order even though
    // the large file would be uploaded first.
    assertThat(targets.get(0).getName(), endsWith(".txt"));
    assertThat(targets.get(1).getName(), endsWith(".log"));
}
Also used : GcsPath(org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath) FileNotFoundException(java.io.FileNotFoundException) 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 4 with CreateOptions

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

the class PackageUtilTest method testPackageUploadEventuallySucceeds.

@Test
public void testPackageUploadEventuallySucceeds() throws Exception {
    Pipe pipe = Pipe.open();
    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(// First attempt fails
    new IOException("Fake Exception: 410 Gone")).thenReturn(// second attempt succeeds
    pipe.sink());
    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(2)).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) Pipe(java.nio.channels.Pipe) 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) GcsUtil(org.apache.beam.sdk.extensions.gcp.util.GcsUtil) StandardCreateOptions(org.apache.beam.sdk.io.fs.CreateOptions.StandardCreateOptions) CreateOptions(org.apache.beam.sdk.io.fs.CreateOptions) Test(org.junit.Test)

Example 5 with CreateOptions

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

the class PackageUtilTest method testPackageUploadWithEmptyDirectorySucceeds.

@Test
public void testPackageUploadWithEmptyDirectorySucceeds() throws Exception {
    Pipe pipe = Pipe.open();
    File tmpDirectory = tmpFolder.newFolder("folder");
    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(tmpDirectory.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(".jar"));
    assertThat(target.getLocation(), equalTo(STAGING_PATH + target.getName()));
    try (ZipInputStream zipInputStream = new ZipInputStream(Channels.newInputStream(pipe.source()))) {
        assertNull(zipInputStream.getNextEntry());
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) 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)

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