2022-03-25 06:55:49 -04:00
|
|
|
//go:build integration
|
|
|
|
|
2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-29 10:13:01 -04:00
|
|
|
package storage
|
2022-03-25 06:55:49 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/api/option"
|
|
|
|
)
|
|
|
|
|
|
|
|
const storageEmulator = "gcr.io/cloud-devrel-public-resources/storage-testbench"
|
|
|
|
|
|
|
|
func TestGoogleCloudStorage(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-03-31 03:25:38 -04:00
|
|
|
containerCtx := context.Background()
|
2022-03-25 06:55:49 -04:00
|
|
|
|
|
|
|
// Set up the Storage Emulator
|
|
|
|
t.Log("Creating storage emulator...")
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
|
|
require.NoError(err)
|
2022-03-31 03:25:38 -04:00
|
|
|
emulator, err := setupEmulator(containerCtx, cli, storageEmulator)
|
2022-03-25 06:55:49 -04:00
|
|
|
require.NoError(err)
|
2022-03-31 03:25:38 -04:00
|
|
|
defer func() { _ = cli.ContainerStop(containerCtx, emulator.ID, nil) }()
|
2022-03-25 06:55:49 -04:00
|
|
|
|
|
|
|
// Run the actual test
|
|
|
|
t.Setenv("STORAGE_EMULATOR_HOST", "localhost:9000")
|
|
|
|
|
|
|
|
bucketName := "test-bucket"
|
|
|
|
projectName := "test-project"
|
|
|
|
|
|
|
|
t.Log("Running test...")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*50)
|
|
|
|
defer cancel()
|
2022-06-29 10:13:01 -04:00
|
|
|
store, err := NewGoogleCloudStorage(ctx, projectName, bucketName, nil, option.WithoutAuthentication())
|
2022-03-25 06:55:49 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
testDEK1 := []byte("test DEK")
|
|
|
|
testDEK2 := []byte("more test DEK")
|
|
|
|
|
|
|
|
// request unset value
|
2022-03-31 03:25:38 -04:00
|
|
|
_, err = store.Get(ctx, "test:input")
|
2022-03-25 06:55:49 -04:00
|
|
|
assert.Error(err)
|
|
|
|
|
|
|
|
// test Put method
|
2022-03-31 03:25:38 -04:00
|
|
|
assert.NoError(store.Put(ctx, "volume01", testDEK1))
|
|
|
|
assert.NoError(store.Put(ctx, "volume02", testDEK2))
|
2022-03-25 06:55:49 -04:00
|
|
|
|
|
|
|
// make sure values have been set
|
2022-03-31 03:25:38 -04:00
|
|
|
val, err := store.Get(ctx, "volume01")
|
2022-03-25 06:55:49 -04:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(testDEK1, val)
|
2022-03-31 03:25:38 -04:00
|
|
|
val, err = store.Get(ctx, "volume02")
|
2022-03-25 06:55:49 -04:00
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(testDEK2, val)
|
|
|
|
|
2022-03-31 03:25:38 -04:00
|
|
|
_, err = store.Get(ctx, "invalid:key")
|
2022-03-25 06:55:49 -04:00
|
|
|
assert.Error(err)
|
2022-06-29 10:13:01 -04:00
|
|
|
assert.ErrorIs(err, ErrDEKUnset)
|
2022-03-25 06:55:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupEmulator(ctx context.Context, cli *client.Client, imageName string) (container.ContainerCreateCreatedBody, error) {
|
|
|
|
reader, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return container.ContainerCreateCreatedBody{}, err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(os.Stdout, reader); err != nil {
|
|
|
|
return container.ContainerCreateCreatedBody{}, err
|
|
|
|
}
|
|
|
|
if err := reader.Close(); err != nil {
|
|
|
|
return container.ContainerCreateCreatedBody{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// the 3 true statements are necessary to attach later to the container log
|
|
|
|
containerConfig := &container.Config{
|
|
|
|
Image: storageEmulator,
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
Tty: true,
|
|
|
|
}
|
|
|
|
emulator, err := cli.ContainerCreate(ctx, containerConfig, &container.HostConfig{NetworkMode: container.NetworkMode("host"), AutoRemove: true}, nil, nil, "google-cloud-storage-test")
|
|
|
|
if err != nil {
|
|
|
|
return emulator, err
|
|
|
|
}
|
2022-03-31 03:25:38 -04:00
|
|
|
if err := cli.ContainerStart(ctx, emulator.ID, types.ContainerStartOptions{}); err != nil {
|
2022-03-25 06:55:49 -04:00
|
|
|
return emulator, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logs, err := cli.ContainerLogs(ctx, emulator.ID, types.ContainerLogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
Follow: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return emulator, err
|
|
|
|
}
|
|
|
|
go func() { _, _ = io.Copy(os.Stdout, logs) }()
|
|
|
|
return emulator, nil
|
|
|
|
}
|