github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/data_source_aws_kms_alias_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccDataSourceAwsKmsAlias(t *testing.T) {
    13  	rInt := acctest.RandInt()
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:  func() { testAccPreCheck(t) },
    16  		Providers: testAccProviders,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccDataSourceAwsKmsAlias(rInt),
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccDataSourceAwsKmsAliasCheck("data.aws_kms_alias.by_name"),
    22  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func testAccDataSourceAwsKmsAliasCheck(name string) resource.TestCheckFunc {
    29  	return func(s *terraform.State) error {
    30  		rs, ok := s.RootModule().Resources[name]
    31  		if !ok {
    32  			return fmt.Errorf("root module has no resource called %s", name)
    33  		}
    34  
    35  		kmsKeyRs, ok := s.RootModule().Resources["aws_kms_alias.single"]
    36  		if !ok {
    37  			return fmt.Errorf("can't find aws_kms_alias.single in state")
    38  		}
    39  
    40  		attr := rs.Primary.Attributes
    41  
    42  		if attr["arn"] != kmsKeyRs.Primary.Attributes["arn"] {
    43  			return fmt.Errorf(
    44  				"arn is %s; want %s",
    45  				attr["arn"],
    46  				kmsKeyRs.Primary.Attributes["arn"],
    47  			)
    48  		}
    49  
    50  		if attr["target_key_id"] != kmsKeyRs.Primary.Attributes["target_key_id"] {
    51  			return fmt.Errorf(
    52  				"target_key_id is %s; want %s",
    53  				attr["target_key_id"],
    54  				kmsKeyRs.Primary.Attributes["target_key_id"],
    55  			)
    56  		}
    57  
    58  		return nil
    59  	}
    60  }
    61  
    62  func testAccDataSourceAwsKmsAlias(rInt int) string {
    63  	return fmt.Sprintf(`
    64  resource "aws_kms_key" "one" {
    65      description = "Terraform acc test"
    66      deletion_window_in_days = 7
    67  }
    68  
    69  resource "aws_kms_alias" "single" {
    70      name = "alias/tf-acc-key-alias-%d"
    71      target_key_id = "${aws_kms_key.one.key_id}"
    72  }
    73  
    74  data "aws_kms_alias" "by_name" {
    75    name = "${aws_kms_alias.single.name}"
    76  }`, rInt)
    77  }