github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/state/remote/s3_test.go (about)

     1  package remote
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/awslabs/aws-sdk-go/service/s3"
    10  )
    11  
    12  func TestS3Client_impl(t *testing.T) {
    13  	var _ Client = new(S3Client)
    14  }
    15  
    16  func TestS3Factory(t *testing.T) {
    17  	// This test just instantiates the client. Shouldn't make any actual
    18  	// requests nor incur any costs.
    19  
    20  	config := make(map[string]string)
    21  
    22  	// Empty config is an error
    23  	_, err := s3Factory(config)
    24  	if err == nil {
    25  		t.Fatalf("Empty config should be error")
    26  	}
    27  
    28  	config["region"] = "us-west-1"
    29  	config["bucket"] = "foo"
    30  	config["key"] = "bar"
    31  	// For this test we'll provide the credentials as config. The
    32  	// acceptance tests implicitly test passing credentials as
    33  	// environment variables.
    34  	config["access_key"] = "bazkey"
    35  	config["secret_key"] = "bazsecret"
    36  
    37  	client, err := s3Factory(config)
    38  	if err != nil {
    39  		t.Fatalf("Error for valid config")
    40  	}
    41  
    42  	s3Client := client.(*S3Client)
    43  
    44  	if s3Client.nativeClient.Config.Region != "us-west-1" {
    45  		t.Fatalf("Incorrect region was populated")
    46  	}
    47  	if s3Client.bucketName != "foo" {
    48  		t.Fatalf("Incorrect bucketName was populated")
    49  	}
    50  	if s3Client.keyName != "bar" {
    51  		t.Fatalf("Incorrect keyName was populated")
    52  	}
    53  
    54  	credentials, err := s3Client.nativeClient.Config.Credentials.Get()
    55  	if err != nil {
    56  		t.Fatalf("Error when requesting credentials")
    57  	}
    58  	if credentials.AccessKeyID != "bazkey" {
    59  		t.Fatalf("Incorrect Access Key Id was populated")
    60  	}
    61  	if credentials.SecretAccessKey != "bazsecret" {
    62  		t.Fatalf("Incorrect Secret Access Key was populated")
    63  	}
    64  }
    65  
    66  func TestS3Client(t *testing.T) {
    67  	// This test creates a bucket in S3 and populates it.
    68  	// It may incur costs, so it will only run if AWS credential environment
    69  	// variables are present.
    70  
    71  	accessKeyId := os.Getenv("AWS_ACCESS_KEY_ID")
    72  	if accessKeyId == "" {
    73  		t.Skipf("skipping; AWS_ACCESS_KEY_ID must be set")
    74  	}
    75  
    76  	regionName := os.Getenv("AWS_DEFAULT_REGION")
    77  	if regionName == "" {
    78  		regionName = "us-west-2"
    79  	}
    80  
    81  	bucketName := fmt.Sprintf("terraform-remote-s3-test-%x", time.Now().Unix())
    82  	keyName := "testState"
    83  
    84  	config := make(map[string]string)
    85  	config["region"] = regionName
    86  	config["bucket"] = bucketName
    87  	config["key"] = keyName
    88  
    89  	client, err := s3Factory(config)
    90  	if err != nil {
    91  		t.Fatalf("Error for valid config")
    92  	}
    93  
    94  	s3Client := client.(*S3Client)
    95  	nativeClient := s3Client.nativeClient
    96  
    97  	createBucketReq := &s3.CreateBucketInput{
    98  		Bucket: &bucketName,
    99  	}
   100  
   101  	// Be clear about what we're doing in case the user needs to clean
   102  	// this up later.
   103  	t.Logf("Creating S3 bucket %s in %s", bucketName, regionName)
   104  	_, err = nativeClient.CreateBucket(createBucketReq)
   105  	if err != nil {
   106  		t.Skipf("Failed to create test S3 bucket, so skipping")
   107  	}
   108  	defer func() {
   109  		deleteBucketReq := &s3.DeleteBucketInput{
   110  			Bucket: &bucketName,
   111  		}
   112  
   113  		_, err := nativeClient.DeleteBucket(deleteBucketReq)
   114  		if err != nil {
   115  			t.Logf("WARNING: Failed to delete the test S3 bucket. It has been left in your AWS account and may incur storage charges. (error was %s)", err)
   116  		}
   117  	}()
   118  
   119  	testClient(t, client)
   120  }