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