Skip to content

Commit

Permalink
refactor integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Mar 29, 2016
1 parent 2ce11cb commit a432aef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static CloudStorageFileSystemProvider getProvider() {
if (provider != null) {
return (CloudStorageFileSystemProvider) provider;
}
logger.warning("Could not find CloudStorageFileSystemProvider via the SPI");
logger.warning("Could not find CloudStorageFileSystemProvider via SPI");
return new CloudStorageFileSystemProvider();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.google.gcloud.storage.BucketInfo;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.StorageOptions;
import com.google.gcloud.storage.contrib.nio.CloudStorageConfiguration;
import com.google.gcloud.storage.contrib.nio.CloudStorageFileSystem;
import com.google.gcloud.storage.testing.RemoteGcsHelper;

Expand All @@ -24,12 +23,14 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
Expand All @@ -55,6 +56,7 @@
* to your browsers is your "Service Account JSON Key".
*/
@RunWith(JUnit4.class)
@SuppressWarnings("resource")
public class ITGcsNio {

private static final List<String> FILE_CONTENTS =
Expand All @@ -63,38 +65,38 @@ public class ITGcsNio {
"Ils sont doués de raison et de conscience et doivent agir ",
"les uns envers les autres dans un esprit de fraternité.");

private static final Logger log = Logger.getLogger(ITGcsNio.class.getName());
private static final String BUCKET = RemoteGcsHelper.generateBucketName();
private static final String SML_FILE = "tmp-test-small-file.txt";
private static final int SML_SIZE = 100;
// it's big, relatively speaking.
private static final String BIG_FILE = "tmp-test-big-file.txt";
// arbitrary size that's not too round.
private static final int BIG_SIZE = 2 * 1024 * 1024 - 50;
private static final String BIG_FILE = "tmp-test-big-file.txt"; // it's big, relatively speaking.
private static final int BIG_SIZE = 2 * 1024 * 1024 - 50; // arbitrary size that's not too round.
private static final String PREFIX = "tmp-test-file";

private static final Logger logger = Logger.getLogger(ITGcsNio.class.getName());
private static final Random random = new Random();

private static String bucket;
private static Storage storage;
private static StorageOptions storageOptions;

private final Random rnd = new Random();

@BeforeClass
public static void beforeClass() throws IOException {
public static void beforeClass() {
bucket = RemoteGcsHelper.generateBucketName();
// loads the credentials from local disk as par README
RemoteGcsHelper gcsHelper = RemoteGcsHelper.create();
storageOptions = gcsHelper.options();
storage = storageOptions.service();
// create and populate test bucket
storage.create(BucketInfo.of(BUCKET));
storage.create(BucketInfo.of(bucket));
fillFile(storage, SML_FILE, SML_SIZE);
fillFile(storage, BIG_FILE, BIG_SIZE);
}

@AfterClass
public static void afterClass() throws ExecutionException, InterruptedException {
if (storage != null
&& !RemoteGcsHelper.forceDelete(storage, BUCKET, 5, TimeUnit.SECONDS)
&& log.isLoggable(Level.WARNING)) {
log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET);
&& !RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS)
&& logger.isLoggable(Level.WARNING)) {
logger.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", bucket);
}
}

Expand All @@ -104,27 +106,33 @@ private static byte[] randomContents(int size) {
return bytes;
}

private static void fillFile(Storage storage, String fname, int size) throws IOException {
storage.create(BlobInfo.builder(BUCKET, fname).build(), randomContents(size));
private static void fillFile(Storage storage, String fname, int size) {
storage.create(BlobInfo.builder(bucket, fname).build(), randomContents(size));
}

@Test
public void testFileExists() throws IOException {
CloudStorageFileSystem testBucket = getTestBucket();
public void testFileExists() {
CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket);
Path path = testBucket.getPath(SML_FILE);
assertThat(Files.exists(path)).isTrue();
}

@Test
public void testFileExistsUsingSpi() {
Path path = Paths.get(URI.create(String.format("gs://%s/%s", bucket, SML_FILE)));
assertThat(Files.exists(path)).isTrue();
}

@Test
public void testFileSize() throws IOException {
CloudStorageFileSystem testBucket = getTestBucket();
CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket);
Path path = testBucket.getPath(SML_FILE);
assertThat(Files.size(path)).isEqualTo(SML_SIZE);
}

@Test(timeout = 60_000)
public void testReadByteChannel() throws IOException {
CloudStorageFileSystem testBucket = getTestBucket();
CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket);
Path path = testBucket.getPath(SML_FILE);
long size = Files.size(path);
SeekableByteChannel chan = Files.newByteChannel(path, StandardOpenOption.READ);
Expand All @@ -150,7 +158,7 @@ public void testReadByteChannel() throws IOException {

@Test
public void testSeek() throws IOException {
CloudStorageFileSystem testBucket = getTestBucket();
CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket);
Path path = testBucket.getPath(BIG_FILE);
int size = BIG_SIZE;
byte[] contents = randomContents(size);
Expand Down Expand Up @@ -179,7 +187,7 @@ public void testSeek() throws IOException {

@Test
public void testCreate() throws IOException {
CloudStorageFileSystem testBucket = getTestBucket();
CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket);
Path path = testBucket.getPath(PREFIX + randomSuffix());
// file shouldn't exist initially. If it does it's either because it's a leftover
// from a previous run (so we should delete the file)
Expand All @@ -201,7 +209,7 @@ public void testCreate() throws IOException {

@Test
public void testWrite() throws IOException {
CloudStorageFileSystem testBucket = getTestBucket();
CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket);
Path path = testBucket.getPath(PREFIX + randomSuffix());
// file shouldn't exist initially. If it does it's either because it's a leftover
// from a previous run (so we should delete the file)
Expand Down Expand Up @@ -233,7 +241,7 @@ public void testWrite() throws IOException {

@Test
public void testCreateAndWrite() throws IOException {
CloudStorageFileSystem testBucket = getTestBucket();
CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket);
Path path = testBucket.getPath(PREFIX + randomSuffix());
// file shouldn't exist initially (see above).
assertThat(Files.exists(path)).isFalse();
Expand Down Expand Up @@ -262,7 +270,7 @@ public void testCreateAndWrite() throws IOException {

@Test
public void testWriteOnClose() throws Exception {
CloudStorageFileSystem testBucket = getTestBucket();
CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket);
Path path = testBucket.getPath(PREFIX + randomSuffix());
// file shouldn't exist initially (see above)
assertThat(Files.exists(path)).isFalse();
Expand Down Expand Up @@ -296,7 +304,7 @@ public void testWriteOnClose() throws Exception {

@Test
public void testCopy() throws IOException {
CloudStorageFileSystem testBucket = getTestBucket();
CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket);
Path src = testBucket.getPath(SML_FILE);
Path dst = testBucket.getPath(PREFIX + randomSuffix());
// file shouldn't exist initially (see above).
Expand Down Expand Up @@ -330,19 +338,6 @@ private int readFully(ReadableByteChannel chan, byte[] outputBuf) throws IOExcep
}

private String randomSuffix() {
return "-" + rnd.nextInt(99999);
}

private CloudStorageFileSystem getTestBucket() throws IOException {
// in typical usage we use the single-argument version of forBucket
// and rely on the user being logged into their project with the
// gcloud tool, and then everything authenticates automagically
// (or we just use paths that start with "gs://" and rely on NIO's magic).
//
// However for the tests we want to be able to run in automated environments
// where we can set environment variables but not necessarily install gcloud
// or run it. That's why we're setting the credentials programmatically.
return CloudStorageFileSystem.forBucket(
BUCKET, CloudStorageConfiguration.DEFAULT, storageOptions);
return "-" + random.nextInt(99999);
}
}

0 comments on commit a432aef

Please sign in to comment.