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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccAWSCloudWatchLogGroup_basic(t *testing.T) {
    13  	var lg cloudwatchlogs.LogGroup
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAWSCloudWatchLogGroupConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
    24  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "retention_in_days", "0"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccAWSCloudWatchLogGroup_retentionPolicy(t *testing.T) {
    32  	var lg cloudwatchlogs.LogGroup
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccAWSCloudWatchLogGroupConfig_withRetention,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
    43  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "retention_in_days", "365"),
    44  				),
    45  			},
    46  			resource.TestStep{
    47  				Config: testAccAWSCloudWatchLogGroupConfigModified_withRetention,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
    50  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "retention_in_days", "0"),
    51  				),
    52  			},
    53  		},
    54  	})
    55  }
    56  
    57  func TestAccAWSCloudWatchLogGroup_multiple(t *testing.T) {
    58  	var lg cloudwatchlogs.LogGroup
    59  
    60  	resource.Test(t, resource.TestCase{
    61  		PreCheck:     func() { testAccPreCheck(t) },
    62  		Providers:    testAccProviders,
    63  		CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
    64  		Steps: []resource.TestStep{
    65  			resource.TestStep{
    66  				Config: testAccAWSCloudWatchLogGroupConfig_multiple,
    67  				Check: resource.ComposeTestCheckFunc(
    68  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.alpha", &lg),
    69  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.alpha", "retention_in_days", "14"),
    70  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.beta", &lg),
    71  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.beta", "retention_in_days", "0"),
    72  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.charlie", &lg),
    73  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.charlie", "retention_in_days", "3653"),
    74  				),
    75  			},
    76  		},
    77  	})
    78  }
    79  
    80  func TestAccAWSCloudWatchLogGroup_disappears(t *testing.T) {
    81  	var lg cloudwatchlogs.LogGroup
    82  
    83  	resource.Test(t, resource.TestCase{
    84  		PreCheck:     func() { testAccPreCheck(t) },
    85  		Providers:    testAccProviders,
    86  		CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
    87  		Steps: []resource.TestStep{
    88  			resource.TestStep{
    89  				Config: testAccAWSCloudWatchLogGroupConfig,
    90  				Check: resource.ComposeTestCheckFunc(
    91  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
    92  					testAccCheckCloudWatchLogGroupDisappears(&lg),
    93  				),
    94  				ExpectNonEmptyPlan: true,
    95  			},
    96  		},
    97  	})
    98  }
    99  
   100  func testAccCheckCloudWatchLogGroupDisappears(lg *cloudwatchlogs.LogGroup) resource.TestCheckFunc {
   101  	return func(s *terraform.State) error {
   102  		conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
   103  		opts := &cloudwatchlogs.DeleteLogGroupInput{
   104  			LogGroupName: lg.LogGroupName,
   105  		}
   106  		if _, err := conn.DeleteLogGroup(opts); err != nil {
   107  			return err
   108  		}
   109  		return nil
   110  	}
   111  }
   112  
   113  func testAccCheckCloudWatchLogGroupExists(n string, lg *cloudwatchlogs.LogGroup) resource.TestCheckFunc {
   114  	return func(s *terraform.State) error {
   115  		rs, ok := s.RootModule().Resources[n]
   116  		if !ok {
   117  			return fmt.Errorf("Not found: %s", n)
   118  		}
   119  
   120  		conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
   121  		logGroup, exists, err := lookupCloudWatchLogGroup(conn, rs.Primary.ID, nil)
   122  		if err != nil {
   123  			return err
   124  		}
   125  		if !exists {
   126  			return fmt.Errorf("Bad: LogGroup %q does not exist", rs.Primary.ID)
   127  		}
   128  
   129  		*lg = *logGroup
   130  
   131  		return nil
   132  	}
   133  }
   134  
   135  func testAccCheckAWSCloudWatchLogGroupDestroy(s *terraform.State) error {
   136  	conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
   137  
   138  	for _, rs := range s.RootModule().Resources {
   139  		if rs.Type != "aws_cloudwatch_log_group" {
   140  			continue
   141  		}
   142  		_, exists, err := lookupCloudWatchLogGroup(conn, rs.Primary.ID, nil)
   143  		if err != nil {
   144  			return nil
   145  		}
   146  
   147  		if exists {
   148  			return fmt.Errorf("Bad: LogGroup still exists: %q", rs.Primary.ID)
   149  		}
   150  
   151  	}
   152  
   153  	return nil
   154  }
   155  
   156  var testAccAWSCloudWatchLogGroupConfig = `
   157  resource "aws_cloudwatch_log_group" "foobar" {
   158      name = "foo-bar"
   159  }
   160  `
   161  
   162  var testAccAWSCloudWatchLogGroupConfig_withRetention = `
   163  resource "aws_cloudwatch_log_group" "foobar" {
   164      name = "foo-bang"
   165      retention_in_days = 365
   166  }
   167  `
   168  
   169  var testAccAWSCloudWatchLogGroupConfigModified_withRetention = `
   170  resource "aws_cloudwatch_log_group" "foobar" {
   171      name = "foo-bang"
   172  }
   173  `
   174  
   175  var testAccAWSCloudWatchLogGroupConfig_multiple = `
   176  resource "aws_cloudwatch_log_group" "alpha" {
   177      name = "foo-bar"
   178      retention_in_days = 14
   179  }
   180  resource "aws_cloudwatch_log_group" "beta" {
   181      name = "foo-bara"
   182  }
   183  resource "aws_cloudwatch_log_group" "charlie" {
   184      name = "foo-baraa"
   185      retention_in_days = 3653
   186  }
   187  `