Skip to content

S3 tests: use testcontainers #79

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 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
51 changes: 32 additions & 19 deletions backends/s3/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"time"

"github.com/PowerDNS/simpleblob/backends/s3"
"github.com/PowerDNS/simpleblob/backends/s3/s3testing"
"github.com/PowerDNS/simpleblob/tester"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/testcontainers/testcontainers-go"
testcontainersminio "github.com/testcontainers/testcontainers-go/modules/minio"
)

func TestFileSecretsCredentials(t *testing.T) {
testcontainers.SkipIfProviderIsNotHealthy(t)
tempDir := t.TempDir()

access, secret := secretsPaths(tempDir)
Expand All @@ -30,15 +32,20 @@ func TestFileSecretsCredentials(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

// Create server
addr, stop, err := s3testing.ServeMinio(ctx, tempDir)
if errors.Is(err, s3testing.ErrMinioNotFound) {
t.Skip("minio binary not found locally, make sure it is in PATH")
container, err := testcontainersminio.Run(ctx, "quay.io/minio/minio")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := container.Terminate(ctx); err != nil {
t.Log(err)
}
}()

addr, err := container.ConnectionString(ctx)
if err != nil {
t.Fatal(err)
}
defer func() { _ = stop() }()

// Create minio client, using our provider.
creds := credentials.New(provider)
Expand All @@ -65,7 +72,7 @@ func TestFileSecretsCredentials(t *testing.T) {
// First credential files creation.
// Keep them empty for now,
// so that calls to the server will fail.
writeSecrets(t, tempDir, "")
writeSecrets(t, tempDir, "", "")

// The files do not hold the right values,
// so a call to the server should fail.
Expand All @@ -74,31 +81,38 @@ func TestFileSecretsCredentials(t *testing.T) {
// Write the right keys to the files.
// We're not testing expiry here,
// and forcing credentials cache to update.
writeSecrets(t, tempDir, s3testing.AdminUserOrPassword)
writeSecrets(t, tempDir, container.Username, container.Password)
creds.Expire()
assertClientSuccess(true, "after changing files content")

// Change content of the files.
writeSecrets(t, tempDir, "badcredentials")
writeSecrets(t, tempDir, "bad-user", "bad-password")
creds.Expire()
assertClientSuccess(false, "after changing again, to bad credentials")
}

func TestBackendWithSecrets(t *testing.T) {
testcontainers.SkipIfProviderIsNotHealthy(t)
tempDir := t.TempDir()

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

addr, stop, err := s3testing.ServeMinio(ctx, tempDir)
if errors.Is(err, s3testing.ErrMinioNotFound) {
t.Skip("minio binary not found locally, make sure it is in PATH")
container, err := testcontainersminio.Run(ctx, "quay.io/minio/minio")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := container.Terminate(ctx); err != nil {
t.Log(err)
}
}()

addr, err := container.ConnectionString(ctx)
if err != nil {
t.Fatal(err)
}
defer func() { _ = stop() }()

// Prepare backend options to reuse.
// These will not change.
Expand All @@ -119,15 +133,15 @@ func TestBackendWithSecrets(t *testing.T) {
}

// Now write files, but with bad content.
writeSecrets(t, tempDir, "")
writeSecrets(t, tempDir, "", "")
_, err = s3.New(ctx, opt)
if err == nil || err.Error() != "Access Denied." {
t.Fatal("backend should not start with bad credentials")
}

// Write the good content.
// Now the backend should start and be able to perform a request.
writeSecrets(t, tempDir, s3testing.AdminUserOrPassword)
writeSecrets(t, tempDir, container.Username, container.Password)

backend, err := s3.New(ctx, opt)
if err != nil {
Expand All @@ -153,14 +167,13 @@ func secretsPaths(dir string) (access, secret string) {

// writeSecrets writes content to files called "access-key" and "secret-key"
// in dir.
// It returns
func writeSecrets(t testing.TB, dir, content string) {
func writeSecrets(t testing.TB, dir, adminUser, password string) {
access, secret := secretsPaths(dir)
err := os.WriteFile(access, []byte(content), 0666)
err := os.WriteFile(access, []byte(adminUser), 0666)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(secret, []byte(content), 0666)
err = os.WriteFile(secret, []byte(password), 0666)
if err != nil {
t.Fatal(err)
}
Expand Down
65 changes: 27 additions & 38 deletions backends/s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,40 @@

import (
"context"
"os"
"testing"
"time"

"github.com/minio/minio-go/v7"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
"github.com/testcontainers/testcontainers-go"

Check failure on line 11 in backends/s3/s3_test.go

View workflow job for this annotation

GitHub Actions / Go 1.19 tests

github.com/testcontainers/testcontainers-go/modules/[email protected]: missing go.sum entry for go.mod file; to add it:

Check failure on line 11 in backends/s3/s3_test.go

View workflow job for this annotation

GitHub Actions / Go 1.18 tests

github.com/testcontainers/testcontainers-go/modules/[email protected]: missing go.sum entry; to add it:
testcontainersminio "github.com/testcontainers/testcontainers-go/modules/minio"

"github.com/PowerDNS/simpleblob/tester"
)

// TestConfigPathEnv is the path to a YAML file with the Options
// with an S3 bucket configuration that can be used for testing.
// The bucket will be emptied before every run!!!
//
// To run a Minio for this :
//
// env MINIO_ROOT_USER=test MINIO_ROOT_PASSWORD=secret minio server /tmp/test-data/
//
// Example test config:
//
// {
// "access_key": "test",
// "secret_key": "verysecret",
// "region": "us-east-1",
// "bucket": "test-bucket",
// "endpoint_url": "http://127.0.0.1:9000"
// }
const TestConfigPathEnv = "SIMPLEBLOB_TEST_S3_CONFIG"

func getBackend(ctx context.Context, t *testing.T) (b *Backend) {
cfgPath := os.Getenv(TestConfigPathEnv)
if cfgPath == "" {
t.Skipf("S3 tests skipped, set the %s env var to run these", TestConfigPathEnv)
return
testcontainers.SkipIfProviderIsNotHealthy(t)
container, err := testcontainersminio.Run(ctx, "quay.io/minio/minio")
if err != nil {
t.Fatal(err)
}

cfgContents, err := os.ReadFile(cfgPath)
require.NoError(t, err)

var opt Options
err = yaml.Unmarshal(cfgContents, &opt)
require.NoError(t, err)
url, err := container.ConnectionString(ctx)
if err != nil {
t.Fatal(err)
}

b, err = New(ctx, opt)
b, err = New(ctx, Options{
EndpointURL: "http://" + url,
AccessKey: container.Username,
SecretKey: container.Password,
Bucket: "test-bucket",
CreateBucket: true,
})
require.NoError(t, err)

cleanup := func() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

cleanStorage := func(ctx context.Context) {
blobs, err := b.List(ctx, "")
if err != nil {
t.Logf("Blobs list error: %s", err)
Expand All @@ -69,8 +51,15 @@
err = b.client.RemoveObject(ctx, b.opt.Bucket, b.markerName, minio.RemoveObjectOptions{})
require.NoError(t, err)
}
t.Cleanup(cleanup)
cleanup()
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cleanStorage(ctx)
if err := container.Terminate(ctx); err != nil {
t.Log(err)
}
})
cleanStorage(ctx)

return b
}
Expand Down
73 changes: 0 additions & 73 deletions backends/s3/s3testing/minio.go

This file was deleted.

14 changes: 0 additions & 14 deletions backends/s3/s3testing/port.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/minio/minio-go/v7 v7.0.63
github.com/prometheus/client_golang v1.13.0
github.com/stretchr/testify v1.8.1
github.com/testcontainers/testcontainers-go/modules/minio v0.33.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
9 changes: 0 additions & 9 deletions test-minio.json

This file was deleted.

51 changes: 1 addition & 50 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,53 +1,4 @@
#!/usr/bin/env bash

set -e

# Stop all child processes on exit
trap cleanup EXIT

cleanup() {
pkill -P $$
if [ -n "$tmpdir" ] && [ -d "$tmpdir" ]; then
rm -r "$tmpdir"
fi
}

export GOBIN="$PWD/bin"
export PATH="$GOBIN:$PATH"
tmpdir=

mkdir -p "$GOBIN"

if [ -z "$SIMPLEBLOB_TEST_S3_CONFIG" ]; then
echo "* Using MinIO for S3 tests"
export SIMPLEBLOB_TEST_S3_CONFIG="$PWD/test-minio.json"

# Fetch minio if not found
if ! command -v minio >/dev/null; then
dst="$GOBIN/minio"
curl -v -o "$dst" "https://dl.min.io/server/minio/release/$(go env GOOS)-$(go env GOARCH)/minio"
chmod u+x "$dst"
fi

# Start MinIO
echo "* Starting minio at address 127.0.0.1:34730"
tmpdir=$(mktemp -d -t minio.XXXXXX)
minio server --address 127.0.0.1:34730 --console-address 127.0.0.1:34731 --quiet "$tmpdir" &
# Wait for minio server to be ready
i=0
while ! curl -s -I "127.0.0.1:34730/minio/health/ready" | grep '200 OK' >/dev/null; do
i=$((i+1))
if [ "$i" -ge 10 ]; then
# We have been waiting for server to start for 10 seconds
echo "Minio could not start properly"
curl -s -I "127.0.0.1:34730/minio/health/ready"
exit 1
fi
sleep 1
done
fi

echo "* SIMPLEBLOB_TEST_S3_CONFIG=$SIMPLEBLOB_TEST_S3_CONFIG"
#!/usr/bin/env sh

set -ex

Expand Down
Loading