### Testcontainers Setup for Fake GCS Server
Source: https://github.com/fsouza/fake-gcs-server/blob/main/examples/java/README.md
Configures and starts a fake GCS server using Testcontainers. This example demonstrates how to dynamically update the server's external URL after container startup, which is crucial for resumable uploads in containerized environments.
```xml
org.testcontainers
testcontainers
${testcontainers.version}
test
org.testcontainers
junit-jupiter
${testcontainers.version}
test
```
```java
@Testcontainers
class FakeGcsServerTest {
@Container
static final GenericContainer> fakeGcs = new GenericContainer<>("fsouza/fake-gcs-server")
.withExposedPorts(4443)
.withCreateContainerCmdModifier(cmd -> cmd.withEntrypoint(
"/bin/fake-gcs-server",
"-scheme", "http"
));
@BeforeAll
static void setUpFakeGcs() throws Exception {
String fakeGcsExternalUrl = "http://" + fakeGcs.getHost() + ":" + fakeGcs.getFirstMappedPort();
updateExternalUrlWithContainerUrl(fakeGcsExternalUrl);
storageClient = StorageOptions.newBuilder()
.setHost(fakeGcsExternalUrl)
.setProjectId("test-project")
.setCredentials(NoCredentials.getInstance())
.build()
.getService();
}
private static void updateExternalUrlWithContainerUrl(String fakeGcsExternalUrl) throws Exception {
String modifyExternalUrlRequestUri = fakeGcsExternalUrl + "/_internal/config";
String updateExternalUrlJson = "{"
+ "\"externalUrl\": \"" + fakeGcsExternalUrl + "\""
+ "}";
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create(modifyExternalUrlRequestUri))
.header("Content-Type", "application/json")
.PUT(BodyPublishers.ofString(updateExternalUrlJson))
.build();
HttpResponse response = HttpClient.newBuilder().build()
.send(req, BodyHandlers.discarding());
if (response.statusCode() != 200) {
throw new RuntimeException(
"error updating fake-gcs-server with external url, response status code " + response.statusCode() + " != 200");
}
}
@Test
void shouldUploadFileByWriterChannel() throws IOException {
storageClient.create(BucketInfo.newBuilder("sample-bucket2").build());
WriteChannel channel = storageClient.writer(BlobInfo.newBuilder("sample-bucket2", "some_file2.txt").build());
channel.write(ByteBuffer.wrap("line1\n".getBytes()));
channel.write(ByteBuffer.wrap("line2\n".getBytes()));
channel.close();
Blob someFile2 = storageClient.get("sample-bucket2", "some_file2.txt");
String fileContent = new String(someFile2.getContent());
assertEquals("line1\nline2\n", fileContent);
}
}
```
--------------------------------
### Setup Storage Client for Fake GCS Server
Source: https://github.com/fsouza/fake-gcs-server/blob/main/examples/java/README.md
Instantiates a Storage client pointing to the fake GCS server. Use this for basic integration tests.
```java
Storage storageClient = StorageOptions.newBuilder()
.setHost(fakeGcsExternalUrl)
.setProjectId("test-project")
.setCredentials(NoCredentials.getInstance())
.build()
.getService();
```
--------------------------------
### CMake Minimum Requirements and Project Setup
Source: https://github.com/fsouza/fake-gcs-server/blob/main/examples/cpp/CMakeLists.txt
Sets the minimum CMake version, C++ standard, and project name. This is a standard starting point for any CMake project.
```cmake
cmake_minimum_required (VERSION 3.2)
set(CMAKE_CXX_STANDARD 11)
project (fake-gcs-server-cpp-examples)
```
--------------------------------
### Start Fake GCS Server with Testcontainers
Source: https://github.com/fsouza/fake-gcs-server/blob/main/examples/node/README.md
Initialize and start a fake-gcs-server container using Testcontainers. Configure the server to use HTTP and expose the necessary port. This setup is ideal for integration tests.
```typescript
import {
Storage
} from "@google-cloud/storage";
import ky from "ky";
import { GenericContainer } from "testcontainers";
const PORT = 4443;
const CONTAINER = await new GenericContainer("fsouza/fake-gcs-server:1.49.0")
.withEntrypoint(["/bin/fake-gcs-server", "-scheme", "http"])
.withExposedPorts(PORT)
.start();
const API_ENDPOINT = `http://${CONTAINER.getHost()}:${CONTAINER.getMappedPort(
PORT
)}`;
await ky.put(`${API_ENDPOINT}/_internal/config`, {
json: { externalUrl: API_ENDPOINT },
});
const STORAGE = new Storage({ apiEndpoint: API_ENDPOINT });
// ...
```
--------------------------------
### Run fake-gcs-server (Default HTTPS)
Source: https://github.com/fsouza/fake-gcs-server/blob/main/CLAUDE.md
Start the fake-gcs-server using the default HTTPS configuration on port 4443.
```bash
./fake-gcs-server
```
--------------------------------
### Run fake-gcs-server with HTTP
Source: https://github.com/fsouza/fake-gcs-server/blob/main/README.md
Starts the fake-gcs-server using HTTP protocol by specifying the -scheme http flag. The binding port can be set with -port.
```shell
docker run -d --name fake-gcs-server -p 4443:4443 -v ${PWD}/examples/data:/data fsouza/fake-gcs-server -scheme http
```
--------------------------------
### Run fake-gcs-server in Docker
Source: https://github.com/fsouza/fake-gcs-server/blob/main/README.md
Starts the fake-gcs-server in detached mode, mapping port 4443.
```shell
docker run -d --name fake-gcs-server -p 4443:4443 fsouza/fake-gcs-server
```
--------------------------------
### Run fake-gcs-server with both HTTPS and HTTP
Source: https://github.com/fsouza/fake-gcs-server/blob/main/README.md
Starts both HTTPS and HTTP servers. HTTPS binds to -port (default 4443) and HTTP binds to -port-http (default 8000).
```shell
docker run -d --name fake-gcs-server -p 4443:4443 -p 8000:8000 -v ${PWD}/examples/data:/data fsouza/fake-gcs-server -scheme both
```
--------------------------------
### Run fake-gcs-server with Seed Data
Source: https://github.com/fsouza/fake-gcs-server/blob/main/CLAUDE.md
Initialize the fake-gcs-server with predefined data by specifying a seed data path.
```bash
./fake-gcs-server -data /path/to/seed/data
```
--------------------------------
### Executable and Library Linking
Source: https://github.com/fsouza/fake-gcs-server/blob/main/examples/cpp/CMakeLists.txt
Defines the main executable 'cpp-example' and links it against the Google Cloud Storage library and Google Test. It also sets public include directories.
```cmake
add_executable(cpp-example cpp-examples.cpp)
target_include_directories(cpp-example PUBLIC ${google-cloud-cpp_INCLUDE_DIRS})
target_link_libraries(cpp-example PUBLIC google-cloud-cpp::storage gtest)
gtest_add_tests(TARGET cpp-example)
```
--------------------------------
### Finding and Fetching Dependencies
Source: https://github.com/fsouza/fake-gcs-server/blob/main/examples/cpp/CMakeLists.txt
Configures the build to find Google Test and Abseil, and fetches Google Cloud Storage client library and crc32c. Ensure these packages are available or can be downloaded.
```cmake
include(FetchContent)
find_package(GTest REQUIRED)
find_package(absl CONFIG REQUIRED)
set(GOOGLE_CLOUD_CPP_ENABLE storage CACHE INTERNAL storage-api)
set(GOOGLE_CLOUD_CPP_ENABLE_MACOS_OPENSSL_CHECK OFF CACHE INTERNAL macos-openssl-check)
set(BUILD_TESTING OFF CACHE INTERNAL testing-off)
set(CRC32C_USE_GLOG OFF CACHE INTERNAL crc32c-glog-off)
set(CRC32C_BUILD_TESTS OFF CACHE INTERNAL crc32c-gtest-off)
set(CRC32C_BUILD_BENCHMARKS OFF CACHE INTERNAL crc32-benchmarks-off)
set(CRC32C_INSTALL ON CACHE INTERNAL crc32-install-on)
FetchContent_Declare(
crc32c
URL https://github.com/google/crc32c/archive/refs/tags/1.1.2.tar.gz
URL_HASH SHA256=ac07840513072b7fcebda6e821068aa04889018f24e10e46181068fb214d7e56
)
FetchContent_MakeAvailable(crc32c)
add_library(Crc32c::crc32c ALIAS crc32c)
FetchContent_Declare(google-cloud-cpp
URL https://github.com/googleapis/google-cloud-cpp/archive/refs/tags/v1.40.2.tar.gz
URL_HASH SHA256=394595d8ce0f17ad9f1a1e3efa1c54118a1246df617a7388da7111dfb299b05b)
FetchContent_MakeAvailable(google-cloud-cpp)
```
--------------------------------
### Build fake-gcs-server
Source: https://github.com/fsouza/fake-gcs-server/blob/main/CLAUDE.md
Use this command to build the fake-gcs-server executable.
```bash
go build
```
--------------------------------
### Display fake-gcs-server Help
Source: https://github.com/fsouza/fake-gcs-server/blob/main/CLAUDE.md
View all available command-line flags and options for configuring the fake-gcs-server.
```bash
./fake-gcs-server -help
```
--------------------------------
### Run fake-gcs-server (HTTP and HTTPS)
Source: https://github.com/fsouza/fake-gcs-server/blob/main/CLAUDE.md
Configure the fake-gcs-server to listen on both HTTP and HTTPS ports simultaneously.
```bash
./fake-gcs-server -scheme both
```
--------------------------------
### List all available server flags
Source: https://github.com/fsouza/fake-gcs-server/blob/main/README.md
Displays all configurable flags and their usage instructions for the fake-gcs-server.
```shell
docker run --rm fsouza/fake-gcs-server -help
```
--------------------------------
### Run fake-gcs-server (HTTP Mode)
Source: https://github.com/fsouza/fake-gcs-server/blob/main/CLAUDE.md
Launch the fake-gcs-server in HTTP mode for testing applications that do not require HTTPS.
```bash
./fake-gcs-server -scheme http
```
--------------------------------
### Run All Tests with Race Detection
Source: https://github.com/fsouza/fake-gcs-server/blob/main/CLAUDE.md
Execute all tests in the project with race detection enabled for thorough testing.
```bash
go test -race -vet all -mod readonly ./...
```
--------------------------------
### Lint Code with golangci-lint
Source: https://github.com/fsouza/fake-gcs-server/blob/main/CLAUDE.md
Run the golangci-lint tool to check for code quality and style issues.
```bash
golangci-lint run
```
--------------------------------
### Verify preloaded data with curl (HTTP)
Source: https://github.com/fsouza/fake-gcs-server/blob/main/README.md
Uses curl to list buckets and objects from the fake-gcs-server running over HTTP. No --insecure flag is needed.
```shell
curl http://0.0.0.0:4443/storage/v1/b
{"kind":"storage#buckets","items":[{"kind":"storage#bucket","id":"sample-bucket","name":"sample-bucket"}],"prefixes":null}
```
```shell
curl http://0.0.0.0:4443/storage/v1/b/sample-bucket/o
{"kind":"storage#objects","items":[{"kind":"storage#object","name":"some_file.txt","id":"sample-bucket/some_file.txt","bucket":"sample-bucket","size":"33"}],"prefixes":[]}
```
--------------------------------
### Run Fake GCS Server with Environment Variables
Source: https://github.com/fsouza/fake-gcs-server/blob/main/README.md
Use this command to run the fake-gcs-server in detached mode, configuring the scheme to HTTP via an environment variable. Ensure the port is correctly mapped.
```shell
docker run -d --name fake-gcs-server -e FAKE_GCS_SCHEME=http -p 4443:4443 fsouza/fake-gcs-server
```
--------------------------------
### Run a Single Test
Source: https://github.com/fsouza/fake-gcs-server/blob/main/CLAUDE.md
Focus on testing a specific test case by running it individually with race detection.
```bash
go test -race -vet all -mod readonly ./fakestorage -run TestName
```
--------------------------------
### Build Fake GCS Server Docker Image Locally
Source: https://github.com/fsouza/fake-gcs-server/blob/main/README.md
Build the fake-gcs-server Docker image locally using the provided Dockerfile. This command tags the image for easy use with Docker.
```shell
docker build -t fsouza/fake-gcs-server .
```
--------------------------------
### Verify preloaded data with curl (HTTPS)
Source: https://github.com/fsouza/fake-gcs-server/blob/main/README.md
Uses curl to list buckets and objects from the fake-gcs-server running over HTTPS. Requires the --insecure flag due to self-signed certificates.
```shell
curl --insecure https://0.0.0.0:4443/storage/v1/b
{"kind":"storage#buckets","items":[{"kind":"storage#bucket","id":"sample-bucket","name":"sample-bucket"}],"prefixes":null}
```
```shell
curl --insecure https://0.0.0.0:4443/storage/v1/b/sample-bucket/o
{"kind":"storage#objects","items":[{"kind":"storage#object","name":"some_file.txt","id":"sample-bucket/some_file.txt","bucket":"sample-bucket","size":"33"}],"prefixes":[]}
```
--------------------------------
### Preload data in fake-gcs-server Docker container
Source: https://github.com/fsouza/fake-gcs-server/blob/main/README.md
Mounts a local directory to /data inside the container to preload data. Ensure the local directory structure matches expected bucket and object names.
```shell
docker run -d --name fake-gcs-server -p 4443:4443 -v ${PWD}/examples/data:/data fsouza/fake-gcs-server
```
--------------------------------
### Lint Code with staticcheck
Source: https://github.com/fsouza/fake-gcs-server/blob/main/CLAUDE.md
Use staticcheck to perform static analysis on the code, identifying potential errors.
```bash
staticcheck ./...
```
--------------------------------
### Run Tests in a Specific Package
Source: https://github.com/fsouza/fake-gcs-server/blob/main/CLAUDE.md
Execute tests only within a designated package, useful for targeted testing.
```bash
go test -race -vet all -mod readonly ./internal/backend
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.