github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_ses_receipt_rule_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/ses"
    11  	"github.com/hashicorp/terraform/helper/acctest"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccAWSSESReceiptRule_basic(t *testing.T) {
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck: func() {
    19  			testAccPreCheck(t)
    20  		},
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckSESReceiptRuleDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccAWSSESReceiptRuleBasicConfig,
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckAwsSESReceiptRuleExists("aws_ses_receipt_rule.basic"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAWSSESReceiptRule_order(t *testing.T) {
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck: func() {
    37  			testAccPreCheck(t)
    38  		},
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckSESReceiptRuleDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccAWSSESReceiptRuleOrderConfig,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckAwsSESReceiptRuleOrder("aws_ses_receipt_rule.second"),
    46  				),
    47  			},
    48  		},
    49  	})
    50  }
    51  
    52  func TestAccAWSSESReceiptRule_actions(t *testing.T) {
    53  	resource.Test(t, resource.TestCase{
    54  		PreCheck: func() {
    55  			testAccPreCheck(t)
    56  		},
    57  		Providers:    testAccProviders,
    58  		CheckDestroy: testAccCheckSESReceiptRuleDestroy,
    59  		Steps: []resource.TestStep{
    60  			resource.TestStep{
    61  				Config: testAccAWSSESReceiptRuleActionsConfig,
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testAccCheckAwsSESReceiptRuleActions("aws_ses_receipt_rule.actions"),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func testAccCheckSESReceiptRuleDestroy(s *terraform.State) error {
    71  	conn := testAccProvider.Meta().(*AWSClient).sesConn
    72  
    73  	for _, rs := range s.RootModule().Resources {
    74  		if rs.Type != "aws_ses_receipt_rule" {
    75  			continue
    76  		}
    77  
    78  		params := &ses.DescribeReceiptRuleInput{
    79  			RuleName:    aws.String(rs.Primary.Attributes["name"]),
    80  			RuleSetName: aws.String(rs.Primary.Attributes["rule_set_name"]),
    81  		}
    82  
    83  		_, err := conn.DescribeReceiptRule(params)
    84  		if err == nil {
    85  			return fmt.Errorf("Receipt rule %s still exists. Failing!", rs.Primary.ID)
    86  		}
    87  
    88  		// Verify the error is what we want
    89  		_, ok := err.(awserr.Error)
    90  		if !ok {
    91  			return err
    92  		}
    93  
    94  	}
    95  
    96  	return nil
    97  
    98  }
    99  
   100  func testAccCheckAwsSESReceiptRuleExists(n string) resource.TestCheckFunc {
   101  	return func(s *terraform.State) error {
   102  		rs, ok := s.RootModule().Resources[n]
   103  		if !ok {
   104  			return fmt.Errorf("SES Receipt Rule not found: %s", n)
   105  		}
   106  
   107  		if rs.Primary.ID == "" {
   108  			return fmt.Errorf("SES Receipt Rule name not set")
   109  		}
   110  
   111  		conn := testAccProvider.Meta().(*AWSClient).sesConn
   112  
   113  		params := &ses.DescribeReceiptRuleInput{
   114  			RuleName:    aws.String("basic"),
   115  			RuleSetName: aws.String(fmt.Sprintf("test-me-%d", srrsRandomInt)),
   116  		}
   117  
   118  		response, err := conn.DescribeReceiptRule(params)
   119  		if err != nil {
   120  			return err
   121  		}
   122  
   123  		if !*response.Rule.Enabled {
   124  			return fmt.Errorf("Enabled (%v) was not set to true", *response.Rule.Enabled)
   125  		}
   126  
   127  		if !reflect.DeepEqual(response.Rule.Recipients, []*string{aws.String("test@example.com")}) {
   128  			return fmt.Errorf("Recipients (%v) was not set to [test@example.com]", response.Rule.Recipients)
   129  		}
   130  
   131  		if !*response.Rule.ScanEnabled {
   132  			return fmt.Errorf("ScanEnabled (%v) was not set to true", *response.Rule.ScanEnabled)
   133  		}
   134  
   135  		if *response.Rule.TlsPolicy != "Require" {
   136  			return fmt.Errorf("TLS Policy (%s) was not set to Require", *response.Rule.TlsPolicy)
   137  		}
   138  
   139  		return nil
   140  	}
   141  }
   142  
   143  func testAccCheckAwsSESReceiptRuleOrder(n string) resource.TestCheckFunc {
   144  	return func(s *terraform.State) error {
   145  		rs, ok := s.RootModule().Resources[n]
   146  		if !ok {
   147  			return fmt.Errorf("SES Receipt Rule not found: %s", n)
   148  		}
   149  
   150  		if rs.Primary.ID == "" {
   151  			return fmt.Errorf("SES Receipt Rule name not set")
   152  		}
   153  
   154  		conn := testAccProvider.Meta().(*AWSClient).sesConn
   155  
   156  		params := &ses.DescribeReceiptRuleSetInput{
   157  			RuleSetName: aws.String(fmt.Sprintf("test-me-%d", srrsRandomInt)),
   158  		}
   159  
   160  		response, err := conn.DescribeReceiptRuleSet(params)
   161  		if err != nil {
   162  			return err
   163  		}
   164  
   165  		if len(response.Rules) != 2 {
   166  			return fmt.Errorf("Number of rules (%d) was not equal to 2", len(response.Rules))
   167  		} else if *response.Rules[0].Name != "first" || *response.Rules[1].Name != "second" {
   168  			return fmt.Errorf("Order of rules (%v) was incorrect", response.Rules)
   169  		}
   170  
   171  		return nil
   172  	}
   173  }
   174  
   175  func testAccCheckAwsSESReceiptRuleActions(n string) resource.TestCheckFunc {
   176  	return func(s *terraform.State) error {
   177  		rs, ok := s.RootModule().Resources[n]
   178  		if !ok {
   179  			return fmt.Errorf("SES Receipt Rule not found: %s", n)
   180  		}
   181  
   182  		if rs.Primary.ID == "" {
   183  			return fmt.Errorf("SES Receipt Rule name not set")
   184  		}
   185  
   186  		conn := testAccProvider.Meta().(*AWSClient).sesConn
   187  
   188  		params := &ses.DescribeReceiptRuleInput{
   189  			RuleName:    aws.String("actions4"),
   190  			RuleSetName: aws.String(fmt.Sprintf("test-me-%d", srrsRandomInt)),
   191  		}
   192  
   193  		response, err := conn.DescribeReceiptRule(params)
   194  		if err != nil {
   195  			return err
   196  		}
   197  
   198  		actions := response.Rule.Actions
   199  
   200  		if len(actions) != 3 {
   201  			return fmt.Errorf("Number of rules (%d) was not equal to 3", len(actions))
   202  		}
   203  
   204  		addHeaderAction := actions[0].AddHeaderAction
   205  		if *addHeaderAction.HeaderName != "Another-Header" {
   206  			return fmt.Errorf("Header Name (%s) was not equal to Another-Header", *addHeaderAction.HeaderName)
   207  		}
   208  
   209  		if *addHeaderAction.HeaderValue != "First" {
   210  			return fmt.Errorf("Header Value (%s) was not equal to First", *addHeaderAction.HeaderValue)
   211  		}
   212  
   213  		secondAddHeaderAction := actions[1].AddHeaderAction
   214  		if *secondAddHeaderAction.HeaderName != "Added-By" {
   215  			return fmt.Errorf("Header Name (%s) was not equal to Added-By", *secondAddHeaderAction.HeaderName)
   216  		}
   217  
   218  		if *secondAddHeaderAction.HeaderValue != "Terraform" {
   219  			return fmt.Errorf("Header Value (%s) was not equal to Terraform", *secondAddHeaderAction.HeaderValue)
   220  		}
   221  
   222  		stopAction := actions[2].StopAction
   223  		if *stopAction.Scope != "RuleSet" {
   224  			return fmt.Errorf("Scope (%s) was not equal to RuleSet", *stopAction.Scope)
   225  		}
   226  
   227  		return nil
   228  	}
   229  }
   230  
   231  var srrsRandomInt = acctest.RandInt()
   232  var testAccAWSSESReceiptRuleBasicConfig = fmt.Sprintf(`
   233  resource "aws_ses_receipt_rule_set" "test" {
   234      rule_set_name = "test-me-%d"
   235  }
   236  
   237  resource "aws_ses_receipt_rule" "basic" {
   238      name = "basic"
   239      rule_set_name = "${aws_ses_receipt_rule_set.test.rule_set_name}"
   240      recipients = ["test@example.com"]
   241      enabled = true
   242      scan_enabled = true
   243      tls_policy = "Require"
   244  }
   245  `, srrsRandomInt)
   246  
   247  var testAccAWSSESReceiptRuleOrderConfig = fmt.Sprintf(`
   248  resource "aws_ses_receipt_rule_set" "test" {
   249      rule_set_name = "test-me-%d"
   250  }
   251  
   252  resource "aws_ses_receipt_rule" "second" {
   253      name = "second"
   254      rule_set_name = "${aws_ses_receipt_rule_set.test.rule_set_name}"
   255      after = "${aws_ses_receipt_rule.first.name}"
   256  }
   257  
   258  resource "aws_ses_receipt_rule" "first" {
   259      name = "first"
   260      rule_set_name = "${aws_ses_receipt_rule_set.test.rule_set_name}"
   261  }
   262  `, srrsRandomInt)
   263  
   264  var testAccAWSSESReceiptRuleActionsConfig = fmt.Sprintf(`
   265  resource "aws_s3_bucket" "emails" {
   266      bucket = "ses-terraform-emails"
   267  }
   268  
   269  resource "aws_ses_receipt_rule_set" "test" {
   270      rule_set_name = "test-me-%d"
   271  }
   272  
   273  resource "aws_ses_receipt_rule" "actions" {
   274      name = "actions4"
   275      rule_set_name = "${aws_ses_receipt_rule_set.test.rule_set_name}"
   276  
   277      add_header_action {
   278  			header_name = "Added-By"
   279  			header_value = "Terraform"
   280  			position = 1
   281      }
   282  
   283      add_header_action {
   284  			header_name = "Another-Header"
   285  			header_value = "First"
   286  			position = 0
   287      }
   288  
   289      stop_action {
   290  			scope = "RuleSet"
   291  			position = 2
   292      }
   293  }
   294  `, srrsRandomInt)