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