Skip to content
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

fix: use projectId from CloudStorageConfig #429

Merged
merged 1 commit into from Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -103,15 +103,24 @@ boolean seemsLikeADirectoryAndUsePseudoDirectories(Storage storage) {
if (!prefix.endsWith("/")) {
prefix += "/";
}
Page<Blob> list =
storage.list(
this.bucket(),
Storage.BlobListOption.prefix(prefix),
// we only look at the first result, so no need for a bigger page.
Storage.BlobListOption.pageSize(1),
fileSystem.provider().getProject() == null
? null
: Storage.BlobListOption.userProject(fileSystem.provider().getProject()));
String userProject = fileSystem.config().userProject();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I understanding correctly, the bug was reading projectId from the ServiceOptions instead of from the CloudStorageConfig? If so, can we update the commit comment to explain this fact?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, that's much clearer.

Page<Blob> list = null;
if (userProject != null) {
list =
storage.list(
this.bucket(),
Storage.BlobListOption.prefix(prefix),
// we only look at the first result, so no need for a bigger page.
Storage.BlobListOption.pageSize(1),
Storage.BlobListOption.userProject(userProject));
} else {
list =
storage.list(
this.bucket(),
Storage.BlobListOption.prefix(prefix),
// we only look at the first result, so no need for a bigger page.
Storage.BlobListOption.pageSize(1));
}
for (Blob b : list.getValues()) {
// if this blob starts with our prefix and then a slash, then prefix is indeed a folder!
if (b.getBlobId() == null) {
Expand Down
@@ -0,0 +1,98 @@
/*
* Copyright 2021 Google LLC
*
* 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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.api.gax.paging.Page;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
import com.google.common.collect.Lists;
import java.nio.file.Files;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@code Files.isDirectory()}. */
@RunWith(JUnit4.class)
public class CloudStorageIsDirectoryTest {
@Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);

private StorageOptions mockOptions;
private Storage mockStorage;

@Before
public void before() {
mockOptions = mock(StorageOptions.class);
mockStorage = mock(Storage.class);
when(mockOptions.getService()).thenReturn(mockStorage);
CloudStorageFileSystemProvider.setStorageOptions(mockOptions);
}

@Test
public void testIsDirectoryNoUserProject() {
CloudStorageFileSystem fs =
CloudStorageFileSystem.forBucket("bucket", CloudStorageConfiguration.DEFAULT);
when(mockStorage.get(BlobId.of("bucket", "test", null)))
.thenThrow(new IllegalArgumentException());
Page<Blob> pages = mock(Page.class);
Blob blob = mock(Blob.class);
when(blob.getBlobId()).thenReturn(BlobId.of("bucket", "test/hello.txt"));
when(pages.getValues()).thenReturn(Lists.newArrayList(blob));
when(mockStorage.list(
"bucket", Storage.BlobListOption.prefix("test/"), Storage.BlobListOption.pageSize(1)))
.thenReturn(pages);

Files.isDirectory(fs.getPath("test"));
verify(mockStorage, times(1))
.list("bucket", Storage.BlobListOption.prefix("test/"), Storage.BlobListOption.pageSize(1));
}

@Test
public void testIsDirectoryWithUserProject() {
CloudStorageFileSystem fs =
CloudStorageFileSystem.forBucket(
"bucket", CloudStorageConfiguration.builder().userProject("project-id").build());
when(mockStorage.get(BlobId.of("bucket", "test", null)))
.thenThrow(new IllegalArgumentException());
Page<Blob> pages = mock(Page.class);
Blob blob = mock(Blob.class);
when(blob.getBlobId()).thenReturn(BlobId.of("bucket", "test/hello.txt"));
when(pages.getValues()).thenReturn(Lists.newArrayList(blob));
when(mockStorage.list(
"bucket",
Storage.BlobListOption.prefix("test/"),
Storage.BlobListOption.pageSize(1),
Storage.BlobListOption.userProject("project-id")))
.thenReturn(pages);
Files.isDirectory(fs.getPath("test"));
verify(mockStorage, times(1))
.list(
"bucket",
Storage.BlobListOption.prefix("test/"),
Storage.BlobListOption.pageSize(1),
Storage.BlobListOption.userProject("project-id"));
}
}