Skip to content

Commit

Permalink
backend/remote-state/gcs: Implement an end-to-end test.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
octo authored and pbzdyl committed Sep 11, 2017
1 parent 52edb50 commit 9b2ba2e
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions backend/remote-state/gcs/backend_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package gcs

import (
"os"
"testing"

"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/state/remote"
)

func TestStateFile(t *testing.T) {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 9b2ba2e

Please sign in to comment.