github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_iam_access_key_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/iam"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSAccessKey_basic(t *testing.T) {
    15  	var conf iam.AccessKeyMetadata
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSAccessKeyDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSAccessKeyConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSAccessKeyExists("aws_iam_access_key.a_key", &conf),
    26  					testAccCheckAWSAccessKeyAttributes(&conf),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func testAccCheckAWSAccessKeyDestroy(s *terraform.State) error {
    34  	iamconn := testAccProvider.Meta().(*AWSClient).iamconn
    35  
    36  	for _, rs := range s.RootModule().Resources {
    37  		if rs.Type != "aws_access_key" {
    38  			continue
    39  		}
    40  
    41  		// Try to get access key
    42  		resp, err := iamconn.ListAccessKeys(&iam.ListAccessKeysInput{
    43  			UserName: aws.String(rs.Primary.ID),
    44  		})
    45  		if err == nil {
    46  			if len(resp.AccessKeyMetadata) > 0 {
    47  				return fmt.Errorf("still exist.")
    48  			}
    49  			return nil
    50  		}
    51  
    52  		// Verify the error is what we want
    53  		ec2err, ok := err.(awserr.Error)
    54  		if !ok {
    55  			return err
    56  		}
    57  		if ec2err.Code() != "NoSuchEntity" {
    58  			return err
    59  		}
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func testAccCheckAWSAccessKeyExists(n string, res *iam.AccessKeyMetadata) resource.TestCheckFunc {
    66  	return func(s *terraform.State) error {
    67  		rs, ok := s.RootModule().Resources[n]
    68  		if !ok {
    69  			return fmt.Errorf("Not found: %s", n)
    70  		}
    71  
    72  		if rs.Primary.ID == "" {
    73  			return fmt.Errorf("No Role name is set")
    74  		}
    75  
    76  		iamconn := testAccProvider.Meta().(*AWSClient).iamconn
    77  
    78  		resp, err := iamconn.ListAccessKeys(&iam.ListAccessKeysInput{
    79  			UserName: aws.String("testuser"),
    80  		})
    81  		if err != nil {
    82  			return err
    83  		}
    84  
    85  		if len(resp.AccessKeyMetadata) != 1 ||
    86  			*resp.AccessKeyMetadata[0].UserName != "testuser" {
    87  			return fmt.Errorf("User not found not found")
    88  		}
    89  
    90  		*res = *resp.AccessKeyMetadata[0]
    91  
    92  		return nil
    93  	}
    94  }
    95  
    96  func testAccCheckAWSAccessKeyAttributes(accessKeyMetadata *iam.AccessKeyMetadata) resource.TestCheckFunc {
    97  	return func(s *terraform.State) error {
    98  		if *accessKeyMetadata.UserName != "testuser" {
    99  			return fmt.Errorf("Bad username: %s", *accessKeyMetadata.UserName)
   100  		}
   101  
   102  		if *accessKeyMetadata.Status != "Active" {
   103  			return fmt.Errorf("Bad status: %s", *accessKeyMetadata.Status)
   104  		}
   105  
   106  		return nil
   107  	}
   108  }
   109  
   110  const testAccAWSAccessKeyConfig = `
   111  resource "aws_iam_user" "a_user" {
   112  	name = "testuser"
   113  }
   114  
   115  resource "aws_iam_access_key" "a_key" {
   116  	user = "${aws_iam_user.a_user.name}"
   117  }
   118  `
   119  
   120  func TestSesSmtpPasswordFromSecretKey(t *testing.T) {
   121  	cases := []struct {
   122  		Input    string
   123  		Expected string
   124  	}{
   125  		{"some+secret+key", "AnkqhOiWEcszZZzTMCQbOY1sPGoLFgMH9zhp4eNgSjo4"},
   126  		{"another+secret+key", "Akwqr0Giwi8FsQFgW3DXWCC2DiiQ/jZjqLDWK8TeTBgL"},
   127  	}
   128  
   129  	for _, tc := range cases {
   130  		actual := sesSmtpPasswordFromSecretKey(&tc.Input)
   131  		if actual != tc.Expected {
   132  			t.Fatalf("%q: expected %q, got %q", tc.Input, tc.Expected, actual)
   133  		}
   134  	}
   135  }