github.com/aznashwan/terraform@v0.4.3-0.20151118032030-21f93ca4558d/state/remote/s3_test.go (about) 1 package remote 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 "time" 8 9 "github.com/aws/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 config["encrypt"] = "1" 32 33 // For this test we'll provide the credentials as config. The 34 // acceptance tests implicitly test passing credentials as 35 // environment variables. 36 config["access_key"] = "bazkey" 37 config["secret_key"] = "bazsecret" 38 39 client, err := s3Factory(config) 40 if err != nil { 41 t.Fatalf("Error for valid config") 42 } 43 44 s3Client := client.(*S3Client) 45 46 if *s3Client.nativeClient.Config.Region != "us-west-1" { 47 t.Fatalf("Incorrect region was populated") 48 } 49 if s3Client.bucketName != "foo" { 50 t.Fatalf("Incorrect bucketName was populated") 51 } 52 if s3Client.keyName != "bar" { 53 t.Fatalf("Incorrect keyName was populated") 54 } 55 56 credentials, err := s3Client.nativeClient.Config.Credentials.Get() 57 if err != nil { 58 t.Fatalf("Error when requesting credentials") 59 } 60 if credentials.AccessKeyID != "bazkey" { 61 t.Fatalf("Incorrect Access Key Id was populated") 62 } 63 if credentials.SecretAccessKey != "bazsecret" { 64 t.Fatalf("Incorrect Secret Access Key was populated") 65 } 66 } 67 68 func TestS3Client(t *testing.T) { 69 // This test creates a bucket in S3 and populates it. 70 // It may incur costs, so it will only run if AWS credential environment 71 // variables are present. 72 73 accessKeyId := os.Getenv("AWS_ACCESS_KEY_ID") 74 if accessKeyId == "" { 75 t.Skipf("skipping; AWS_ACCESS_KEY_ID must be set") 76 } 77 78 regionName := os.Getenv("AWS_DEFAULT_REGION") 79 if regionName == "" { 80 regionName = "us-west-2" 81 } 82 83 bucketName := fmt.Sprintf("terraform-remote-s3-test-%x", time.Now().Unix()) 84 keyName := "testState" 85 testData := []byte(`testing data`) 86 87 config := make(map[string]string) 88 config["region"] = regionName 89 config["bucket"] = bucketName 90 config["key"] = keyName 91 config["encrypt"] = "1" 92 93 client, err := s3Factory(config) 94 if err != nil { 95 t.Fatalf("Error for valid config") 96 } 97 98 s3Client := client.(*S3Client) 99 nativeClient := s3Client.nativeClient 100 101 createBucketReq := &s3.CreateBucketInput{ 102 Bucket: &bucketName, 103 } 104 105 // Be clear about what we're doing in case the user needs to clean 106 // this up later. 107 t.Logf("Creating S3 bucket %s in %s", bucketName, regionName) 108 _, err = nativeClient.CreateBucket(createBucketReq) 109 if err != nil { 110 t.Skipf("Failed to create test S3 bucket, so skipping") 111 } 112 113 // Ensure we can perform a PUT request with the encryption header 114 err = s3Client.Put(testData) 115 if err != nil { 116 t.Logf("WARNING: Failed to send test data to S3 bucket. (error was %s)", err) 117 } 118 119 defer func() { 120 deleteBucketReq := &s3.DeleteBucketInput{ 121 Bucket: &bucketName, 122 } 123 124 _, err := nativeClient.DeleteBucket(deleteBucketReq) 125 if err != nil { 126 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) 127 } 128 }() 129 130 testClient(t, client) 131 }