2017-09-08 16:11:41 +02:00
|
|
|
package gcs
|
2017-09-08 14:58:19 +02:00
|
|
|
|
|
|
|
import (
|
2017-09-26 09:20:43 +02:00
|
|
|
"fmt"
|
2017-12-06 22:49:29 +01:00
|
|
|
"log"
|
2017-09-11 15:39:48 +02:00
|
|
|
"os"
|
2017-10-05 09:54:41 +02:00
|
|
|
"strings"
|
2017-09-08 14:58:19 +02:00
|
|
|
"testing"
|
2017-12-06 22:49:29 +01:00
|
|
|
"time"
|
2017-09-11 15:39:48 +02:00
|
|
|
|
2017-12-06 22:49:29 +01:00
|
|
|
"cloud.google.com/go/storage"
|
2017-09-11 15:39:48 +02:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
2017-09-08 14:58:19 +02:00
|
|
|
)
|
|
|
|
|
2017-12-18 01:59:10 +01:00
|
|
|
const (
|
|
|
|
noPrefix = ""
|
|
|
|
noEncryptionKey = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
// See https://cloud.google.com/storage/docs/using-encryption-keys#generating_your_own_encryption_key
|
|
|
|
var encryptionKey = "yRyCOikXi1ZDNE0xN3yiFsJjg7LGimoLrGFcLZgQoVk="
|
2017-11-08 20:24:36 +01:00
|
|
|
|
2017-09-08 14:58:19 +02:00
|
|
|
func TestStateFile(t *testing.T) {
|
2017-09-26 09:20:43 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-09-08 14:58:19 +02:00
|
|
|
cases := []struct {
|
2017-09-11 08:25:42 +02:00
|
|
|
prefix string
|
2017-09-08 14:58:19 +02:00
|
|
|
defaultStateFile string
|
|
|
|
name string
|
|
|
|
wantStateFile string
|
|
|
|
wantLockFile string
|
|
|
|
}{
|
|
|
|
{"state", "", "default", "state/default.tfstate", "state/default.tflock"},
|
|
|
|
{"state", "", "test", "state/test.tfstate", "state/test.tflock"},
|
|
|
|
{"state", "legacy.tfstate", "default", "legacy.tfstate", "legacy.tflock"},
|
|
|
|
{"state", "legacy.tfstate", "test", "state/test.tfstate", "state/test.tflock"},
|
|
|
|
{"state", "legacy.state", "default", "legacy.state", "legacy.state.tflock"},
|
|
|
|
{"state", "legacy.state", "test", "state/test.tfstate", "state/test.tflock"},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2018-07-04 12:11:35 +02:00
|
|
|
b := &Backend{
|
2017-09-11 08:25:42 +02:00
|
|
|
prefix: c.prefix,
|
2017-09-08 14:58:19 +02:00
|
|
|
defaultStateFile: c.defaultStateFile,
|
|
|
|
}
|
|
|
|
|
|
|
|
if got := b.stateFile(c.name); got != c.wantStateFile {
|
|
|
|
t.Errorf("stateFile(%q) = %q, want %q", c.name, got, c.wantStateFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got := b.lockFile(c.name); got != c.wantLockFile {
|
|
|
|
t.Errorf("lockFile(%q) = %q, want %q", c.name, got, c.wantLockFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-11 15:39:48 +02:00
|
|
|
|
2017-09-26 09:20:43 +02:00
|
|
|
func TestRemoteClient(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-09-11 15:39:48 +02:00
|
|
|
|
2017-12-06 22:49:29 +01:00
|
|
|
bucket := bucketName(t)
|
2017-12-18 01:59:10 +01:00
|
|
|
be := setupBackend(t, bucket, noPrefix, noEncryptionKey)
|
|
|
|
defer teardownBackend(t, be, noPrefix)
|
|
|
|
|
2018-10-01 23:35:23 +02:00
|
|
|
ss, err := be.StateMgr(backend.DefaultStateName)
|
2017-12-18 01:59:10 +01:00
|
|
|
if err != nil {
|
2018-10-01 23:35:23 +02:00
|
|
|
t.Fatalf("be.StateMgr(%q) = %v", backend.DefaultStateName, err)
|
2017-12-18 01:59:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rs, ok := ss.(*remote.State)
|
|
|
|
if !ok {
|
2018-10-01 23:35:23 +02:00
|
|
|
t.Fatalf("be.StateMgr(): got a %T, want a *remote.State", ss)
|
2017-12-18 01:59:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
remote.TestClient(t, rs.Client)
|
|
|
|
}
|
|
|
|
func TestRemoteClientWithEncryption(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := bucketName(t)
|
|
|
|
be := setupBackend(t, bucket, noPrefix, encryptionKey)
|
2017-11-08 20:24:36 +01:00
|
|
|
defer teardownBackend(t, be, noPrefix)
|
2017-09-11 15:39:48 +02:00
|
|
|
|
2018-10-01 23:35:23 +02:00
|
|
|
ss, err := be.StateMgr(backend.DefaultStateName)
|
2017-09-26 09:20:43 +02:00
|
|
|
if err != nil {
|
2018-10-01 23:35:23 +02:00
|
|
|
t.Fatalf("be.StateMgr(%q) = %v", backend.DefaultStateName, err)
|
2017-09-11 15:39:48 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 09:20:43 +02:00
|
|
|
rs, ok := ss.(*remote.State)
|
|
|
|
if !ok {
|
2018-10-01 23:35:23 +02:00
|
|
|
t.Fatalf("be.StateMgr(): got a %T, want a *remote.State", ss)
|
2017-09-11 15:39:48 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 09:20:43 +02:00
|
|
|
remote.TestClient(t, rs.Client)
|
|
|
|
}
|
2017-09-11 15:39:48 +02:00
|
|
|
|
2017-09-26 09:20:43 +02:00
|
|
|
func TestRemoteLocks(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-09-11 15:39:48 +02:00
|
|
|
|
2017-12-06 22:49:29 +01:00
|
|
|
bucket := bucketName(t)
|
2017-12-18 01:59:10 +01:00
|
|
|
be := setupBackend(t, bucket, noPrefix, noEncryptionKey)
|
2017-11-08 20:24:36 +01:00
|
|
|
defer teardownBackend(t, be, noPrefix)
|
2017-09-11 15:39:48 +02:00
|
|
|
|
2017-09-26 09:20:43 +02:00
|
|
|
remoteClient := func() (remote.Client, error) {
|
2018-10-01 23:35:23 +02:00
|
|
|
ss, err := be.StateMgr(backend.DefaultStateName)
|
2017-09-26 09:20:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-09-11 15:39:48 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 09:20:43 +02:00
|
|
|
rs, ok := ss.(*remote.State)
|
|
|
|
if !ok {
|
2018-10-01 23:35:23 +02:00
|
|
|
return nil, fmt.Errorf("be.StateMgr(): got a %T, want a *remote.State", ss)
|
2017-09-26 09:20:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rs.Client, nil
|
2017-09-11 15:39:48 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 09:20:43 +02:00
|
|
|
c0, err := remoteClient()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("remoteClient(0) = %v", err)
|
|
|
|
}
|
|
|
|
c1, err := remoteClient()
|
2017-09-11 15:39:48 +02:00
|
|
|
if err != nil {
|
2017-09-26 09:20:43 +02:00
|
|
|
t.Fatalf("remoteClient(1) = %v", err)
|
2017-09-11 15:39:48 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 09:20:43 +02:00
|
|
|
remote.TestRemoteLocks(t, c0, c1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2017-12-06 22:49:29 +01:00
|
|
|
bucket := bucketName(t)
|
|
|
|
|
2017-12-18 01:59:10 +01:00
|
|
|
be0 := setupBackend(t, bucket, noPrefix, noEncryptionKey)
|
2017-11-08 20:24:36 +01:00
|
|
|
defer teardownBackend(t, be0, noPrefix)
|
2017-09-26 09:20:43 +02:00
|
|
|
|
2017-12-18 01:59:10 +01:00
|
|
|
be1 := setupBackend(t, bucket, noPrefix, noEncryptionKey)
|
2017-11-08 20:24:36 +01:00
|
|
|
|
2018-02-21 02:36:08 +01:00
|
|
|
backend.TestBackendStates(t, be0)
|
|
|
|
backend.TestBackendStateLocks(t, be0, be1)
|
|
|
|
backend.TestBackendStateForceUnlock(t, be0, be1)
|
2017-11-08 20:24:36 +01:00
|
|
|
}
|
2018-02-21 02:36:08 +01:00
|
|
|
|
2017-11-08 20:24:36 +01:00
|
|
|
func TestBackendWithPrefix(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
prefix := "test/prefix"
|
2017-12-06 22:49:29 +01:00
|
|
|
bucket := bucketName(t)
|
2017-11-08 20:24:36 +01:00
|
|
|
|
2017-12-18 01:59:10 +01:00
|
|
|
be0 := setupBackend(t, bucket, prefix, noEncryptionKey)
|
2017-11-08 20:24:36 +01:00
|
|
|
defer teardownBackend(t, be0, prefix)
|
|
|
|
|
2017-12-18 01:59:10 +01:00
|
|
|
be1 := setupBackend(t, bucket, prefix+"/", noEncryptionKey)
|
|
|
|
|
2018-02-21 02:36:08 +01:00
|
|
|
backend.TestBackendStates(t, be0)
|
|
|
|
backend.TestBackendStateLocks(t, be0, be1)
|
2017-12-18 01:59:10 +01:00
|
|
|
}
|
|
|
|
func TestBackendWithEncryption(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := bucketName(t)
|
|
|
|
|
|
|
|
be0 := setupBackend(t, bucket, noPrefix, encryptionKey)
|
|
|
|
defer teardownBackend(t, be0, noPrefix)
|
|
|
|
|
|
|
|
be1 := setupBackend(t, bucket, noPrefix, encryptionKey)
|
2017-09-26 09:20:43 +02:00
|
|
|
|
2018-02-21 02:36:08 +01:00
|
|
|
backend.TestBackendStates(t, be0)
|
|
|
|
backend.TestBackendStateLocks(t, be0, be1)
|
2017-09-26 09:20:43 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 11:17:20 +02:00
|
|
|
// setupBackend returns a new GCS backend.
|
2017-12-18 01:59:10 +01:00
|
|
|
func setupBackend(t *testing.T, bucket, prefix, key string) backend.Backend {
|
2017-09-26 09:20:43 +02:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
projectID := os.Getenv("GOOGLE_PROJECT")
|
2017-10-04 08:50:58 +02:00
|
|
|
if projectID == "" || os.Getenv("TF_ACC") == "" {
|
|
|
|
t.Skip("This test creates a bucket in GCS and populates it. " +
|
|
|
|
"Since this may incur costs, it will only run if " +
|
|
|
|
"the TF_ACC and GOOGLE_PROJECT environment variables are set.")
|
2017-09-11 15:39:48 +02:00
|
|
|
}
|
2017-09-26 09:20:43 +02:00
|
|
|
|
|
|
|
config := map[string]interface{}{
|
2017-12-18 01:59:10 +01:00
|
|
|
"project": projectID,
|
|
|
|
"bucket": bucket,
|
|
|
|
"prefix": prefix,
|
|
|
|
"encryption_key": key,
|
2017-09-11 15:39:48 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config))
|
2018-07-04 12:11:35 +02:00
|
|
|
be := b.(*Backend)
|
2017-09-26 09:20:43 +02:00
|
|
|
|
2017-12-06 22:49:29 +01:00
|
|
|
// create the bucket if it doesn't exist
|
|
|
|
bkt := be.storageClient.Bucket(bucket)
|
|
|
|
_, err := bkt.Attrs(be.storageContext)
|
2017-09-11 15:39:48 +02:00
|
|
|
if err != nil {
|
2017-12-06 22:49:29 +01:00
|
|
|
if err != storage.ErrBucketNotExist {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attrs := &storage.BucketAttrs{
|
|
|
|
Location: be.region,
|
2017-09-26 09:20:43 +02:00
|
|
|
}
|
2017-12-06 22:49:29 +01:00
|
|
|
err := bkt.Create(be.storageContext, be.projectID, attrs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2017-09-26 09:20:43 +02:00
|
|
|
}
|
2017-09-11 15:39:48 +02:00
|
|
|
}
|
2017-10-04 11:17:20 +02:00
|
|
|
|
2017-12-06 22:49:29 +01:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// teardownBackend deletes all states from be except the default state.
|
|
|
|
func teardownBackend(t *testing.T, be backend.Backend, prefix string) {
|
|
|
|
t.Helper()
|
2018-07-04 12:11:35 +02:00
|
|
|
gcsBE, ok := be.(*Backend)
|
2017-10-04 11:17:20 +02:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("be is a %T, want a *gcsBackend", be)
|
|
|
|
}
|
|
|
|
ctx := gcsBE.storageContext
|
|
|
|
|
2017-12-06 22:49:29 +01:00
|
|
|
bucket := gcsBE.storageClient.Bucket(gcsBE.bucketName)
|
|
|
|
objs := bucket.Objects(ctx, nil)
|
|
|
|
|
|
|
|
for o, err := objs.Next(); err == nil; o, err = objs.Next() {
|
|
|
|
if err := bucket.Object(o.Name).Delete(ctx); err != nil {
|
|
|
|
log.Printf("Error trying to delete object: %s %s\n\n", o.Name, err)
|
|
|
|
} else {
|
|
|
|
log.Printf("Object deleted: %s", o.Name)
|
|
|
|
}
|
2017-10-04 11:17:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the bucket itself.
|
2017-12-06 22:49:29 +01:00
|
|
|
if err := bucket.Delete(ctx); err != nil {
|
|
|
|
t.Errorf("deleting bucket %q failed, manual cleanup may be required: %v", gcsBE.bucketName, err)
|
2017-10-04 11:17:20 +02:00
|
|
|
}
|
2017-09-11 15:39:48 +02:00
|
|
|
}
|
2017-10-05 09:54:41 +02:00
|
|
|
|
2017-12-06 22:49:29 +01:00
|
|
|
// bucketName returns a valid bucket name for this test.
|
|
|
|
func bucketName(t *testing.T) string {
|
|
|
|
name := fmt.Sprintf("tf-%x-%s", time.Now().UnixNano(), t.Name())
|
2017-10-05 09:54:41 +02:00
|
|
|
|
|
|
|
// Bucket names must contain 3 to 63 characters.
|
2017-12-06 22:49:29 +01:00
|
|
|
if len(name) > 63 {
|
|
|
|
name = name[:63]
|
2017-10-05 09:54:41 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 22:49:29 +01:00
|
|
|
return strings.ToLower(name)
|
2017-10-05 09:54:41 +02:00
|
|
|
}
|