Skip to content

fix: add runtime hints for storage #2001

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

Merged
merged 16 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@
<profile>
<id>spring-native</id>
<modules>
<module>spring-cloud-gcp-storage</module>
<module>spring-cloud-gcp-vision</module>
</modules>

Expand Down Expand Up @@ -380,6 +381,7 @@
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<systemPropertyVariables>
<!--integration tests are not invoked unless the relevant system property is set to true here. -->
<it.storage>true</it.storage>
<it.vision>true</it.vision>
</systemPropertyVariables>
</configuration>
Expand Down
9 changes: 8 additions & 1 deletion spring-cloud-gcp-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
<id>native-sample-config</id>
<modules>
<module>spring-cloud-gcp-logging-sample</module>
<module>spring-cloud-gcp-storage-resource-sample</module>
<module>spring-cloud-gcp-integration-storage-sample</module>
<module>spring-cloud-gcp-vision-api-sample</module>
</modules>
<build>
Expand Down Expand Up @@ -133,7 +135,12 @@
</includes>
<systemPropertyVariables>
<it.logging>true</it.logging>
<it.vision>true</it.vision>
<it.storage>true</it.storage>
<it.vision>true</it.vision>
<gcs-resource-test-bucket>gcp-storage-resource-bucket-sample</gcs-resource-test-bucket>
<gcs-read-bucket>gcp-storage-bucket-sample-input</gcs-read-bucket>
<gcs-write-bucket>gcp-storage-bucket-sample-output</gcs-write-bucket>
<gcs-local-directory>unused</gcs-local-directory>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2023 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
*
* https://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.example;

import com.google.cloud.spring.storage.integration.inbound.GcsInboundFileSynchronizer;
import com.google.cloud.spring.storage.integration.inbound.GcsInboundFileSynchronizingMessageSource;
import java.io.File;
import java.nio.file.Paths;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.core.MessageSource;

@TestConfiguration
public class GcsSpringIntegrationTestConfiguration {

private String uniqueDirectory;

public GcsSpringIntegrationTestConfiguration(
@Value("${gcs-local-directory}") String localDirectory) {
uniqueDirectory = String.format("%s-%s", localDirectory, UUID.randomUUID());
}

@Bean
public String uniqueDirectory() {
return uniqueDirectory;
}

@Bean
@Primary
@InboundChannelAdapter(channel = "new-file-channel", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> synchronizerAdapterOverride(GcsInboundFileSynchronizer synchronizer) {
GcsInboundFileSynchronizingMessageSource syncAdapter =
new GcsInboundFileSynchronizingMessageSource(synchronizer);
syncAdapter.setLocalDirectory(Paths.get(uniqueDirectory).toFile());
return syncAdapter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.google.api.gax.paging.Page;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
Expand All @@ -30,17 +29,22 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;

Expand All @@ -55,12 +59,19 @@
@ExtendWith(SpringExtension.class)
@PropertySource("classpath:application.properties")
@SpringBootTest(classes = {GcsSpringIntegrationApplication.class})
@Import(GcsSpringIntegrationTestConfiguration.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GcsSpringIntegrationTests {

private static final String TEST_FILE_NAME = "test_file";
private static final String TEST_FILE = String.format("test_file_%s", UUID.randomUUID());
private static final Log LOGGER = LogFactory.getLog(GcsSpringIntegrationTests.class);

@Autowired private Storage storage;

@Autowired
@Qualifier("uniqueDirectory")
private String uniqueDirectory;

@Value("${gcs-read-bucket}")
private String cloudInputBucket;

Expand All @@ -70,28 +81,22 @@ class GcsSpringIntegrationTests {
@Value("${gcs-local-directory}")
private String outputFolder;

@BeforeEach
void setupTestEnvironment() {
cleanupCloudStorage();
}

@AfterEach
@AfterAll
void teardownTestEnvironment() throws IOException {
cleanupCloudStorage();
cleanupLocalDirectory();
cleanupLocalDirectories();
}

@Test
void testFilePropagatedToLocalDirectory() {
BlobId blobId = BlobId.of(this.cloudInputBucket, TEST_FILE_NAME);
BlobId blobId = BlobId.of(this.cloudInputBucket, TEST_FILE);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
this.storage.create(blobInfo, "Hello World!".getBytes(StandardCharsets.UTF_8));

Awaitility.await()
.atMost(15, TimeUnit.SECONDS)
.atMost(30, TimeUnit.SECONDS)
.untilAsserted(
() -> {
Path outputFile = Paths.get(this.outputFolder + "/" + TEST_FILE_NAME);
Path outputFile = Paths.get(uniqueDirectory + "/" + TEST_FILE);
assertThat(Files.exists(outputFile)).isTrue();
assertThat(Files.isRegularFile(outputFile)).isTrue();

Expand All @@ -104,23 +109,49 @@ void testFilePropagatedToLocalDirectory() {
.iterateAll()
.forEach(b -> blobNamesInOutputBucket.add(b.getName()));

assertThat(blobNamesInOutputBucket).contains(TEST_FILE_NAME);
assertThat(blobNamesInOutputBucket).contains(TEST_FILE);
});
}

@Test
void testAutomaticGcsLocalDirectoryCreation() {
assertThat(Files.exists(Paths.get(outputFolder))).isTrue();
}

void cleanupCloudStorage() {
Page<Blob> blobs = this.storage.list(this.cloudInputBucket);
for (Blob blob : blobs.iterateAll()) {
blob.delete();
BlobId inputBucketBlobId = BlobId.of(cloudInputBucket, TEST_FILE);
Blob inputBucketBlob = storage.get(inputBucketBlobId);
if (inputBucketBlob != null) {
inputBucketBlob.delete();
}

BlobId outputBucketBlobId = BlobId.of(cloudOutputBucket, TEST_FILE);
Blob outputBucketBlob = storage.get(outputBucketBlobId);
if (outputBucketBlob != null) {
outputBucketBlob.delete();
}
}

void cleanupLocalDirectory() throws IOException {
Path localDirectory = Paths.get(this.outputFolder);
List<Path> files = Files.list(localDirectory).collect(Collectors.toList());
for (Path file : files) {
Files.delete(file);
void cleanupLocalDirectories() throws IOException {
cleanupLocalDirectory(Paths.get(uniqueDirectory));
cleanupLocalDirectory(Paths.get(outputFolder));
}

void cleanupLocalDirectory(Path testDirectory) throws IOException {
if (Files.exists(testDirectory)) {
if (Files.isDirectory(testDirectory)) {
try (Stream<Path> files = Files.list(testDirectory)) {
files.forEach(
path -> {
try {
Files.delete(path);
} catch (IOException ioe) {
LOGGER.info("Error deleting test file.", ioe);
}
});
}
}
Files.delete(testDirectory);
}
Files.deleteIfExists(Paths.get(this.outputFolder));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


/**
* An example Spring Boot application that reads and writes files stored in Google Cloud Storage
* (GCS) using the Spring Resource abstraction and the gs: protocol prefix.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@

package com.example;

import com.google.cloud.spring.storage.GoogleStorageResource;
import com.google.cloud.storage.Storage;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
Expand All @@ -35,19 +39,48 @@
@RestController
public class WebController {

@Value("${gcs-resource-test-bucket}")
private String bucketName;

@Value("gs://${gcs-resource-test-bucket}/my-file.txt")
private Resource gcsFile;

@GetMapping("/")
public String readGcsFile() throws IOException {
private Storage storage;

private WebController(Storage storage) {
this.storage = storage;
}

@GetMapping(value = "/")
public String readGcsFile(@RequestParam("filename") Optional<String> filename)
throws IOException {
if (filename.isPresent()) {
GoogleStorageResource resource = fetchResource(filename);
return StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset()) + "\n";
}
return StreamUtils.copyToString(this.gcsFile.getInputStream(), Charset.defaultCharset()) + "\n";
}

@PostMapping("/")
public String writeGcs(@RequestBody String data) throws IOException {
try (OutputStream os = ((WritableResource) this.gcsFile).getOutputStream()) {
@PostMapping(value = "/")
public String writeGcs(
@RequestBody String data, @RequestParam("filename") Optional<String> filename)
throws IOException {
if (filename.isPresent()) {
GoogleStorageResource resource = fetchResource(filename);
return updateResource(resource, data);
}
return updateResource(this.gcsFile, data);
}

private String updateResource(Resource resource, String data) throws IOException {
try (OutputStream os = ((WritableResource) resource).getOutputStream()) {
os.write(data.getBytes());
}
return "file was updated\n";
}

private GoogleStorageResource fetchResource(Optional<String> filename) {
return new GoogleStorageResource(
this.storage, String.format("gs://%s/%s", this.bucketName, filename.get()));
}
}
Loading