github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_kms_alias_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccAWSKmsAlias_basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 CheckDestroy: testAccCheckAWSKmsAliasDestroy, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccAWSKmsSingleAlias, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckAWSKmsAliasExists("aws_kms_alias.single"), 22 ), 23 }, 24 resource.TestStep{ 25 Config: testAccAWSKmsSingleAlias_modified, 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckAWSKmsAliasExists("aws_kms_alias.single"), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func TestAccAWSKmsAlias_name_prefix(t *testing.T) { 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testAccCheckAWSKmsAliasDestroy, 39 Steps: []resource.TestStep{ 40 resource.TestStep{ 41 Config: testAccAWSKmsSingleAlias, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckAWSKmsAliasExists("aws_kms_alias.name_prefix"), 44 ), 45 }, 46 }, 47 }) 48 } 49 50 func TestAccAWSKmsAlias_no_name(t *testing.T) { 51 resource.Test(t, resource.TestCase{ 52 PreCheck: func() { testAccPreCheck(t) }, 53 Providers: testAccProviders, 54 CheckDestroy: testAccCheckAWSKmsAliasDestroy, 55 Steps: []resource.TestStep{ 56 resource.TestStep{ 57 Config: testAccAWSKmsSingleAlias, 58 Check: resource.ComposeTestCheckFunc( 59 testAccCheckAWSKmsAliasExists("aws_kms_alias.nothing"), 60 ), 61 }, 62 }, 63 }) 64 } 65 66 func TestAccAWSKmsAlias_multiple(t *testing.T) { 67 resource.Test(t, resource.TestCase{ 68 PreCheck: func() { testAccPreCheck(t) }, 69 Providers: testAccProviders, 70 CheckDestroy: testAccCheckAWSKmsAliasDestroy, 71 Steps: []resource.TestStep{ 72 resource.TestStep{ 73 Config: testAccAWSKmsMultipleAliases, 74 Check: resource.ComposeTestCheckFunc( 75 testAccCheckAWSKmsAliasExists("aws_kms_alias.one"), 76 testAccCheckAWSKmsAliasExists("aws_kms_alias.two"), 77 ), 78 }, 79 }, 80 }) 81 } 82 83 func testAccCheckAWSKmsAliasDestroy(s *terraform.State) error { 84 conn := testAccProvider.Meta().(*AWSClient).kmsconn 85 86 for _, rs := range s.RootModule().Resources { 87 if rs.Type != "aws_kms_alias" { 88 continue 89 } 90 91 entry, err := findKmsAliasByName(conn, rs.Primary.ID, nil) 92 if err != nil { 93 return err 94 } 95 if entry != nil { 96 return fmt.Errorf("KMS alias still exists:\n%#v", entry) 97 } 98 99 return nil 100 } 101 102 return nil 103 } 104 105 func testAccCheckAWSKmsAliasExists(name string) resource.TestCheckFunc { 106 return func(s *terraform.State) error { 107 _, ok := s.RootModule().Resources[name] 108 if !ok { 109 return fmt.Errorf("Not found: %s", name) 110 } 111 112 return nil 113 } 114 } 115 116 var kmsAliasTimestamp = time.Now().Format(time.RFC1123) 117 var testAccAWSKmsSingleAlias = fmt.Sprintf(` 118 resource "aws_kms_key" "one" { 119 description = "Terraform acc test One %s" 120 deletion_window_in_days = 7 121 } 122 resource "aws_kms_key" "two" { 123 description = "Terraform acc test Two %s" 124 deletion_window_in_days = 7 125 } 126 127 resource "aws_kms_alias" "name_prefix" { 128 name_prefix = "alias/tf-acc-key-alias" 129 target_key_id = "${aws_kms_key.one.key_id}" 130 } 131 132 resource "aws_kms_alias" "nothing" { 133 target_key_id = "${aws_kms_key.one.key_id}" 134 } 135 136 resource "aws_kms_alias" "single" { 137 name = "alias/tf-acc-key-alias" 138 target_key_id = "${aws_kms_key.one.key_id}" 139 }`, kmsAliasTimestamp, kmsAliasTimestamp) 140 141 var testAccAWSKmsSingleAlias_modified = fmt.Sprintf(` 142 resource "aws_kms_key" "one" { 143 description = "Terraform acc test One %s" 144 deletion_window_in_days = 7 145 } 146 resource "aws_kms_key" "two" { 147 description = "Terraform acc test Two %s" 148 deletion_window_in_days = 7 149 } 150 151 resource "aws_kms_alias" "single" { 152 name = "alias/tf-acc-key-alias" 153 target_key_id = "${aws_kms_key.two.key_id}" 154 }`, kmsAliasTimestamp, kmsAliasTimestamp) 155 156 var testAccAWSKmsMultipleAliases = fmt.Sprintf(` 157 resource "aws_kms_key" "single" { 158 description = "Terraform acc test One %s" 159 deletion_window_in_days = 7 160 } 161 162 resource "aws_kms_alias" "one" { 163 name = "alias/tf-acc-key-alias-one" 164 target_key_id = "${aws_kms_key.single.key_id}" 165 } 166 resource "aws_kms_alias" "two" { 167 name = "alias/tf-acc-key-alias-two" 168 target_key_id = "${aws_kms_key.single.key_id}" 169 }`, kmsAliasTimestamp)