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