github.com/ojiry/terraform@v0.8.2-0.20161218223921-e50cec712c4a/state/remote/gcs_test.go (about)

     1  package remote
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  	"time"
     8  
     9  	storage "google.golang.org/api/storage/v1"
    10  )
    11  
    12  func TestGCSClient_impl(t *testing.T) {
    13  	var _ Client = new(GCSClient)
    14  }
    15  
    16  func TestGCSClient(t *testing.T) {
    17  	// This test creates a bucket in GCS and populates it.
    18  	// It may incur costs, so it will only run if GCS credential environment
    19  	// variables are present.
    20  
    21  	projectID := os.Getenv("GOOGLE_PROJECT")
    22  	if projectID == "" {
    23  		t.Skipf("skipping; GOOGLE_PROJECT must be set")
    24  	}
    25  
    26  	bucketName := fmt.Sprintf("terraform-remote-gcs-test-%x", time.Now().Unix())
    27  	keyName := "testState"
    28  	testData := []byte(`testing data`)
    29  
    30  	config := make(map[string]string)
    31  	config["bucket"] = bucketName
    32  	config["path"] = keyName
    33  
    34  	client, err := gcsFactory(config)
    35  	if err != nil {
    36  		t.Fatalf("Error for valid config: %v", err)
    37  	}
    38  
    39  	gcsClient := client.(*GCSClient)
    40  	nativeClient := gcsClient.clientStorage
    41  
    42  	// Be clear about what we're doing in case the user needs to clean
    43  	// this up later.
    44  	if _, err := nativeClient.Buckets.Get(bucketName).Do(); err == nil {
    45  		fmt.Printf("Bucket %s already exists - skipping buckets.insert call.", bucketName)
    46  	} else {
    47  		// Create a bucket.
    48  		if res, err := nativeClient.Buckets.Insert(projectID, &storage.Bucket{Name: bucketName}).Do(); err == nil {
    49  			fmt.Printf("Created bucket %v at location %v\n\n", res.Name, res.SelfLink)
    50  		} else {
    51  			t.Skipf("Failed to create test GCS bucket, so skipping")
    52  		}
    53  	}
    54  
    55  	// Ensure we can perform a PUT request with the encryption header
    56  	err = gcsClient.Put(testData)
    57  	if err != nil {
    58  		t.Logf("WARNING: Failed to send test data to GCS bucket. (error was %s)", err)
    59  	}
    60  
    61  	defer func() {
    62  		// Delete the test bucket in the project
    63  		if err := gcsClient.clientStorage.Buckets.Delete(bucketName).Do(); err != nil {
    64  			t.Logf("WARNING: Failed to delete the test GCS bucket. It has been left in your GCE account and may incur storage charges. (error was %s)", err)
    65  		}
    66  	}()
    67  
    68  	testClient(t, client)
    69  }