github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_cloudwatch_event_rule_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	events "github.com/aws/aws-sdk-go/service/cloudwatchevents"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSCloudWatchEventRule_basic(t *testing.T) {
    15  	var rule events.DescribeRuleOutput
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSCloudWatchEventRuleDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSCloudWatchEventRuleConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.foo", &rule),
    26  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.foo", "name", "tf-acc-cw-event-rule"),
    27  				),
    28  			},
    29  			resource.TestStep{
    30  				Config: testAccAWSCloudWatchEventRuleConfigModified,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.foo", &rule),
    33  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.foo", "name", "tf-acc-cw-event-rule-mod"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func TestAccAWSCloudWatchEventRule_full(t *testing.T) {
    41  	var rule events.DescribeRuleOutput
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckAWSCloudWatchEventRuleDestroy,
    47  		Steps: []resource.TestStep{
    48  			resource.TestStep{
    49  				Config: testAccAWSCloudWatchEventRuleConfig_full,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moobar", &rule),
    52  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "name", "tf-acc-cw-event-rule-full"),
    53  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "schedule_expression", "rate(5 minutes)"),
    54  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "event_pattern", "{\"source\":[\"aws.ec2\"]}"),
    55  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "description", "He's not dead, he's just resting!"),
    56  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "role_arn", ""),
    57  					testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moobar", "DISABLED", &rule),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestAccAWSCloudWatchEventRule_enable(t *testing.T) {
    65  	var rule events.DescribeRuleOutput
    66  
    67  	resource.Test(t, resource.TestCase{
    68  		PreCheck:     func() { testAccPreCheck(t) },
    69  		Providers:    testAccProviders,
    70  		CheckDestroy: testAccCheckAWSCloudWatchEventRuleDestroy,
    71  		Steps: []resource.TestStep{
    72  			resource.TestStep{
    73  				Config: testAccAWSCloudWatchEventRuleConfigEnabled,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moo", &rule),
    76  					testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moo", "ENABLED", &rule),
    77  				),
    78  			},
    79  			resource.TestStep{
    80  				Config: testAccAWSCloudWatchEventRuleConfigDisabled,
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moo", &rule),
    83  					testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moo", "DISABLED", &rule),
    84  				),
    85  			},
    86  			resource.TestStep{
    87  				Config: testAccAWSCloudWatchEventRuleConfigEnabled,
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moo", &rule),
    90  					testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moo", "ENABLED", &rule),
    91  				),
    92  			},
    93  		},
    94  	})
    95  }
    96  
    97  func testAccCheckCloudWatchEventRuleExists(n string, rule *events.DescribeRuleOutput) resource.TestCheckFunc {
    98  	return func(s *terraform.State) error {
    99  		rs, ok := s.RootModule().Resources[n]
   100  		if !ok {
   101  			return fmt.Errorf("Not found: %s", n)
   102  		}
   103  
   104  		conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn
   105  		params := events.DescribeRuleInput{
   106  			Name: aws.String(rs.Primary.ID),
   107  		}
   108  		resp, err := conn.DescribeRule(&params)
   109  		if err != nil {
   110  			return err
   111  		}
   112  		if resp == nil {
   113  			return fmt.Errorf("Rule not found")
   114  		}
   115  
   116  		*rule = *resp
   117  
   118  		return nil
   119  	}
   120  }
   121  
   122  func testAccCheckCloudWatchEventRuleEnabled(n string, desired string, rule *events.DescribeRuleOutput) resource.TestCheckFunc {
   123  	return func(s *terraform.State) error {
   124  		rs, ok := s.RootModule().Resources[n]
   125  		if !ok {
   126  			return fmt.Errorf("Not found: %s", n)
   127  		}
   128  
   129  		conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn
   130  		params := events.DescribeRuleInput{
   131  			Name: aws.String(rs.Primary.ID),
   132  		}
   133  		resp, err := conn.DescribeRule(&params)
   134  
   135  		if err != nil {
   136  			return err
   137  		}
   138  		if *resp.State != desired {
   139  			return fmt.Errorf("Expected state %q, given %q", desired, *resp.State)
   140  		}
   141  
   142  		return nil
   143  	}
   144  }
   145  
   146  func testAccCheckAWSCloudWatchEventRuleDestroy(s *terraform.State) error {
   147  	conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn
   148  
   149  	for _, rs := range s.RootModule().Resources {
   150  		if rs.Type != "aws_cloudwatch_event_rule" {
   151  			continue
   152  		}
   153  
   154  		params := events.DescribeRuleInput{
   155  			Name: aws.String(rs.Primary.ID),
   156  		}
   157  
   158  		resp, err := conn.DescribeRule(&params)
   159  
   160  		if err == nil {
   161  			return fmt.Errorf("CloudWatch Event Rule %q still exists: %s",
   162  				rs.Primary.ID, resp)
   163  		}
   164  	}
   165  
   166  	return nil
   167  }
   168  
   169  func TestResourceAWSCloudWatchEventRule_validateEventPatternValue(t *testing.T) {
   170  	type testCases struct {
   171  		Length   int
   172  		Value    string
   173  		ErrCount int
   174  	}
   175  
   176  	invalidCases := []testCases{
   177  		{
   178  			Length:   8,
   179  			Value:    acctest.RandString(16),
   180  			ErrCount: 1,
   181  		},
   182  		{
   183  			Length:   123,
   184  			Value:    `{"abc":}`,
   185  			ErrCount: 1,
   186  		},
   187  		{
   188  			Length:   1,
   189  			Value:    `{"abc":["1","2"]}`,
   190  			ErrCount: 1,
   191  		},
   192  	}
   193  
   194  	for _, tc := range invalidCases {
   195  		_, errors := validateEventPatternValue(tc.Length)(tc.Value, "event_pattern")
   196  		if len(errors) != tc.ErrCount {
   197  			t.Fatalf("Expected %q to trigger a validation error.", tc.Value)
   198  		}
   199  	}
   200  
   201  	validCases := []testCases{
   202  		{
   203  			Length:   0,
   204  			Value:    ``,
   205  			ErrCount: 0,
   206  		},
   207  		{
   208  			Length:   2,
   209  			Value:    `{}`,
   210  			ErrCount: 0,
   211  		},
   212  		{
   213  			Length:   18,
   214  			Value:    `{"abc":["1","2"]}`,
   215  			ErrCount: 0,
   216  		},
   217  	}
   218  
   219  	for _, tc := range validCases {
   220  		_, errors := validateEventPatternValue(tc.Length)(tc.Value, "event_pattern")
   221  		if len(errors) != tc.ErrCount {
   222  			t.Fatalf("Expected %q not to trigger a validation error.", tc.Value)
   223  		}
   224  	}
   225  }
   226  
   227  var testAccAWSCloudWatchEventRuleConfig = `
   228  resource "aws_cloudwatch_event_rule" "foo" {
   229      name = "tf-acc-cw-event-rule"
   230      schedule_expression = "rate(1 hour)"
   231  }
   232  `
   233  
   234  var testAccAWSCloudWatchEventRuleConfigEnabled = `
   235  resource "aws_cloudwatch_event_rule" "moo" {
   236      name = "tf-acc-cw-event-rule-state"
   237      schedule_expression = "rate(1 hour)"
   238  }
   239  `
   240  var testAccAWSCloudWatchEventRuleConfigDisabled = `
   241  resource "aws_cloudwatch_event_rule" "moo" {
   242      name = "tf-acc-cw-event-rule-state"
   243      schedule_expression = "rate(1 hour)"
   244      is_enabled = false
   245  }
   246  `
   247  
   248  var testAccAWSCloudWatchEventRuleConfigModified = `
   249  resource "aws_cloudwatch_event_rule" "foo" {
   250      name = "tf-acc-cw-event-rule-mod"
   251      schedule_expression = "rate(1 hour)"
   252  }
   253  `
   254  
   255  var testAccAWSCloudWatchEventRuleConfig_full = `
   256  resource "aws_cloudwatch_event_rule" "moobar" {
   257      name = "tf-acc-cw-event-rule-full"
   258      schedule_expression = "rate(5 minutes)"
   259  	event_pattern = <<PATTERN
   260  { "source": ["aws.ec2"] }
   261  PATTERN
   262  	description = "He's not dead, he's just resting!"
   263  	is_enabled = false
   264  }
   265  `
   266  
   267  // TODO: Figure out example with IAM Role