-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support enumeration of filesystems when credentials are not available #1189
Changes from 1 commit
4711cd5
189cd05
3ebee20
44cd268
356da96
ed02398
32e779b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.storage.contrib.nio; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.testing.NullPointerTester; | ||
import org.junit.Before; | ||
import org.junit.Ignore; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.URI; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.ReadableByteChannel; | ||
import java.nio.channels.SeekableByteChannel; | ||
import java.nio.file.AtomicMoveNotSupportedException; | ||
import java.nio.file.CopyOption; | ||
import java.nio.file.FileAlreadyExistsException; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Files; | ||
import java.nio.file.NoSuchFileException; | ||
import java.nio.file.OpenOption; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.spi.FileSystemProvider; | ||
import java.util.List; | ||
|
||
import static com.google.cloud.storage.contrib.nio.CloudStorageFileSystem.forBucket; | ||
import static com.google.common.truth.Truth.assertThat; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.nio.file.StandardCopyOption.ATOMIC_MOVE; | ||
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; | ||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; | ||
import static java.nio.file.StandardOpenOption.CREATE; | ||
import static java.nio.file.StandardOpenOption.CREATE_NEW; | ||
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; | ||
import static java.nio.file.StandardOpenOption.WRITE; | ||
|
||
/** | ||
* Unit tests for {@link CloudStorageFileSystemProvider} when gcloud is not configured. | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
*/ | ||
//@RunWith(JUnit4.class) | ||
@Ignore // these tests must be run manually because they require gcloud to NOT be configured. | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
public class CloudStorageLateInitializationTest { | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
@Rule public final ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Before | ||
public void before() { | ||
if (System.getenv().containsKey("GOOGLE_APPLICATION_CREDENTIALS")) { | ||
throw new RuntimeException("CloudStorageLateInitializationTest can only be run if gcloud is not configured. This means no GOOGLE_APPLICATION_CREDENTIALS environment variable."); | ||
} | ||
// actually that one's OK so long as it doesn't point to a valid configuration, but it's simpler | ||
// to check that it isn't there. | ||
if (System.getenv().containsKey("CLOUDSDK_CONFIG")) { | ||
throw new RuntimeException("CloudStorageLateInitializationTest can only be run if gcloud is" + | ||
" not configured. This means no CLOUDSDK_CONFIG environment variable."); | ||
} | ||
if (Files.exists(Paths.get(System.getenv("HOME"), ".config/gcloud/properties"))) { | ||
throw new RuntimeException("CloudStorageLateInitializationTest can only be run if gcloud is" + | ||
" not configured. This means no ~/.config/gcloud/properties."); | ||
} | ||
if (Files.exists(Paths.get(System.getenv("HOME"), ".config/gcloud/active_config"))) { | ||
throw new RuntimeException("CloudStorageLateInitializationTest can only be run if gcloud is" + | ||
" not configured. This means no ~/.config/gcloud/active_config."); | ||
} | ||
} | ||
|
||
@Test(expected = java.lang.IllegalArgumentException.class) | ||
public void pathFailsIfNoEnvVariable() throws IOException { | ||
// this should fail if we haven't set GOOGLE_APPLICATION_CREDENTIALS nor CLOUDSDK_CONFIG | ||
// *and* we don't have a ~/.config/gcloud/properties file (or %APPDATA%/gcloud in Windows) | ||
// and we don't have a ~/.config/gcloud/active-config. | ||
// (since we're also not providing credentials in any other way) | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
Path path = Paths.get(URI.create("gs://bucket/wat")); | ||
} | ||
|
||
@Test | ||
public void enumerateFilesystemsIfNoEnvVariable() throws IOException { | ||
// listing available filesystem providers should work even if we can't initialize | ||
// CloudStorageFilesystemProvider. This makes it possible to use other filesystems | ||
// when gcloud-java is in the classpath but gcloud isn't configured. | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
System.out.println("Installed filesystem providers:"); | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
for (FileSystemProvider p : FileSystemProvider.installedProviders()) { | ||
System.out.println(" " + p.getScheme()); | ||
} | ||
} | ||
|
||
This comment was marked as spam.
Sorry, something went wrong. |
||
} |
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.