backend/remote-state/gcs: Implement an end-to-end test.

The code is loosely based on state/remote/gcs_test.go. If the
GOOGLE_PROJECT environment variable is set, this test will

1) create a new bucket; error out if the bucket already exists.
2) create a new state
3) list states and ensure that the newly created state is listed
4) ensure that an object with the expected name exists
5) rum "state/remote".TestClient()
6) delete the state

The bucket is deleted when the test exits, though this may fail if the
bucket it not empty.
This commit is contained in:
Florian Forster 2017-09-11 15:39:48 +02:00 committed by James Bardin
parent 14bfbf0617
commit 91b1a39a40
1 changed files with 90 additions and 0 deletions

View File

@ -1,7 +1,11 @@
package gcs package gcs
import ( import (
"os"
"testing" "testing"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/state/remote"
) )
func TestStateFile(t *testing.T) { func TestStateFile(t *testing.T) {
@ -34,3 +38,89 @@ func TestStateFile(t *testing.T) {
} }
} }
} }
func TestGCSBackend(t *testing.T) {
// This test creates a bucket in GCS and populates it.
// It may incur costs, so it will only run if the GOOGLE_PROJECT
// environment variable is set.
projectID := os.Getenv("GOOGLE_PROJECT")
if projectID == "" {
t.Skipf("skipping; set GOOGLE_PROJECT to activate")
}
const bucketName = "terraform_remote-state_test"
t.Logf("using bucket %q in project %q", bucketName, projectID)
config := map[string]interface{}{
"bucket": bucketName,
"prefix": "",
}
if creds := os.Getenv("GOOGLE_CREDENTIALS"); creds != "" {
config["credentials"] = creds
t.Logf("using credentials from %q", creds)
} else {
t.Log("using default credentials; set GOOGLE_CREDENTIALS for custom credentials")
}
be := backend.TestBackendConfig(t, New(), config)
gcsBE, ok := be.(*gcsBackend)
if !ok {
t.Fatalf("backend: got %T, want *gcsBackend", be)
}
ctx := gcsBE.storageContext
// create a new bucket and error out if we can't, e.g. because it already exists.
if err := gcsBE.storageClient.Bucket(bucketName).Create(ctx, projectID, nil); err != nil {
t.Fatalf("creating bucket failed: %v", err)
}
t.Log("bucket has been created")
defer func() {
if err := gcsBE.storageClient.Bucket(bucketName).Delete(ctx); err != nil {
t.Errorf("deleting bucket failed: %v", err)
} else {
t.Log("bucket has been deleted")
}
}()
// this should create a new state file
_, err := be.State("TestGCSBackend")
if err != nil {
t.Fatalf("State(\"TestGCSBackend\"): %v", err)
}
states, err := be.States()
if err != nil {
t.Fatalf("States(): %v", err)
}
found := false
for _, st := range states {
if st == "TestGCSBackend" {
found = true
break
}
}
if !found {
t.Errorf("be.States() = %#v, missing \"TestGCSBackend\"", states)
}
// ensure state file exists
if _, err := gcsBE.storageClient.Bucket(bucketName).Object("TestGCSBackend.tfstate").Attrs(ctx); err != nil {
t.Fatalf("Attrs(\"TestGCSBackend.tfstate\"): %v", err)
}
c, err := gcsBE.client("TestGCSBackend_remote_TestClient")
if err != nil {
t.Fatal(err)
}
remote.TestClient(t, c)
if err := be.DeleteState("TestGCSBackend"); err != nil {
t.Errorf("DeleteState(\"TestGCSBackend\"): %v", err)
}
}