github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 testAccCheckCloudWatchLogGroupExists(n string, lg *cloudwatchlogs.LogGroup) resource.TestCheckFunc {
    81  	return func(s *terraform.State) error {
    82  		rs, ok := s.RootModule().Resources[n]
    83  		if !ok {
    84  			return fmt.Errorf("Not found: %s", n)
    85  		}
    86  
    87  		conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
    88  		logGroup, err := lookupCloudWatchLogGroup(conn, rs.Primary.ID, nil)
    89  		if err != nil {
    90  			return err
    91  		}
    92  
    93  		*lg = *logGroup
    94  
    95  		return nil
    96  	}
    97  }
    98  
    99  func testAccCheckAWSCloudWatchLogGroupDestroy(s *terraform.State) error {
   100  	conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
   101  
   102  	for _, rs := range s.RootModule().Resources {
   103  		if rs.Type != "aws_cloudwatch_log_group" {
   104  			continue
   105  		}
   106  
   107  		_, err := lookupCloudWatchLogGroup(conn, rs.Primary.ID, nil)
   108  		if err == nil {
   109  			return fmt.Errorf("LogGroup Still Exists: %s", rs.Primary.ID)
   110  		}
   111  	}
   112  
   113  	return nil
   114  }
   115  
   116  var testAccAWSCloudWatchLogGroupConfig = `
   117  resource "aws_cloudwatch_log_group" "foobar" {
   118      name = "foo-bar"
   119  }
   120  `
   121  
   122  var testAccAWSCloudWatchLogGroupConfig_withRetention = `
   123  resource "aws_cloudwatch_log_group" "foobar" {
   124      name = "foo-bang"
   125      retention_in_days = 365
   126  }
   127  `
   128  
   129  var testAccAWSCloudWatchLogGroupConfigModified_withRetention = `
   130  resource "aws_cloudwatch_log_group" "foobar" {
   131      name = "foo-bang"
   132  }
   133  `
   134  
   135  var testAccAWSCloudWatchLogGroupConfig_multiple = `
   136  resource "aws_cloudwatch_log_group" "alpha" {
   137      name = "foo-bar"
   138      retention_in_days = 14
   139  }
   140  resource "aws_cloudwatch_log_group" "beta" {
   141      name = "foo-bara"
   142  }
   143  resource "aws_cloudwatch_log_group" "charlie" {
   144      name = "foo-baraa"
   145      retention_in_days = 3653
   146  }
   147  `