aws: kms_key + kms_alias - Add acceptance tests

This commit is contained in:
Radek Simko 2015-11-14 17:54:48 +00:00 committed by Radek Simko
parent ccedb36a86
commit ff2d040d28
2 changed files with 228 additions and 0 deletions

View File

@ -0,0 +1,131 @@
package aws
import (
"fmt"
"testing"
"time"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSKmsAlias_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSKmsAliasDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSKmsSingleAlias,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKmsAliasExists("aws_kms_alias.single"),
),
},
resource.TestStep{
Config: testAccAWSKmsSingleAlias_modified,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKmsAliasExists("aws_kms_alias.single"),
),
},
},
})
}
func TestAccAWSKmsAlias_multiple(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSKmsAliasDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSKmsMultipleAliases,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKmsAliasExists("aws_kms_alias.one"),
testAccCheckAWSKmsAliasExists("aws_kms_alias.two"),
),
},
},
})
}
func testAccCheckAWSKmsAliasDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).kmsconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_kms_alias" {
continue
}
resp, err := conn.ListAliases(&kms.ListAliasesInput{})
if err != nil {
return err
}
for _, e := range resp.Aliases {
if *e.AliasName == rs.Primary.ID {
return fmt.Errorf("KMS alias still exists:\n%#v", e)
}
}
return nil
}
return nil
}
func testAccCheckAWSKmsAliasExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
return nil
}
}
var kmsAliasTimestamp = time.Now().Format(time.RFC1123)
var testAccAWSKmsSingleAlias = fmt.Sprintf(`
resource "aws_kms_key" "one" {
description = "Terraform acc test One %s"
deletion_window_in_days = 7
}
resource "aws_kms_key" "two" {
description = "Terraform acc test Two %s"
deletion_window_in_days = 7
}
resource "aws_kms_alias" "single" {
name = "alias/tf-acc-key-alias"
target_key_id = "${aws_kms_key.one.key_id}"
}`, kmsAliasTimestamp, kmsAliasTimestamp)
var testAccAWSKmsSingleAlias_modified = fmt.Sprintf(`
resource "aws_kms_key" "one" {
description = "Terraform acc test One %s"
deletion_window_in_days = 7
}
resource "aws_kms_key" "two" {
description = "Terraform acc test Two %s"
deletion_window_in_days = 7
}
resource "aws_kms_alias" "single" {
name = "alias/tf-acc-key-alias"
target_key_id = "${aws_kms_key.two.key_id}"
}`, kmsAliasTimestamp, kmsAliasTimestamp)
var testAccAWSKmsMultipleAliases = fmt.Sprintf(`
resource "aws_kms_key" "single" {
description = "Terraform acc test One %s"
deletion_window_in_days = 7
}
resource "aws_kms_alias" "one" {
name = "alias/tf-acc-key-alias-one"
target_key_id = "${aws_kms_key.single.key_id}"
}
resource "aws_kms_alias" "two" {
name = "alias/tf-acc-key-alias-two"
target_key_id = "${aws_kms_key.single.key_id}"
}`, kmsAliasTimestamp)

View File

@ -0,0 +1,97 @@
package aws
import (
"fmt"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSKmsKey_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSKmsKeyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSKmsKey,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKmsKeyExists("aws_kms_key.foo"),
),
},
resource.TestStep{
Config: testAccAWSKmsKey_removedPolicy,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKmsKeyExists("aws_kms_key.foo"),
),
},
},
})
}
func testAccCheckAWSKmsKeyDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).kmsconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_kms_key" {
continue
}
out, err := conn.DescribeKey(&kms.DescribeKeyInput{
KeyId: aws.String(rs.Primary.ID),
})
if err == nil {
return fmt.Errorf("KMS key still exists:\n%#v", out.KeyMetadata)
}
return err
}
return nil
}
func testAccCheckAWSKmsKeyExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
return nil
}
}
var kmsTimestamp = time.Now().Format(time.RFC1123)
var testAccAWSKmsKey = fmt.Sprintf(`
resource "aws_kms_key" "foo" {
description = "Terraform acc test %s"
deletion_window_in_days = 7
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "kms-tf-1",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "kms:*",
"Resource": "*"
}
]
}
POLICY
}`, kmsTimestamp)
var testAccAWSKmsKey_removedPolicy = fmt.Sprintf(`
resource "aws_kms_key" "foo" {
description = "Terraform acc test %s"
deletion_window_in_days = 7
}`, kmsTimestamp)