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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSSpotDatafeedSubscription(t *testing.T) {
    16  	cases := map[string]func(t *testing.T){
    17  		"basic":      testAccAWSSpotDatafeedSubscription_basic,
    18  		"disappears": testAccAWSSpotDatafeedSubscription_disappears,
    19  		"import":     testAccAWSSpotDatafeedSubscription_importBasic,
    20  	}
    21  
    22  	for name, tc := range cases {
    23  		t.Run(name, func(t *testing.T) {
    24  			tc(t)
    25  		})
    26  	}
    27  }
    28  
    29  func testAccAWSSpotDatafeedSubscription_basic(t *testing.T) {
    30  	var subscription ec2.SpotDatafeedSubscription
    31  	ri := acctest.RandInt()
    32  
    33  	resource.Test(t, resource.TestCase{
    34  		PreCheck:     func() { testAccPreCheck(t) },
    35  		Providers:    testAccProviders,
    36  		CheckDestroy: testAccCheckAWSSpotDatafeedSubscriptionDestroy,
    37  		Steps: []resource.TestStep{
    38  			{
    39  				Config: testAccAWSSpotDatafeedSubscription(ri),
    40  				Check: resource.ComposeTestCheckFunc(
    41  					testAccCheckAWSSpotDatafeedSubscriptionExists("aws_spot_datafeed_subscription.default", &subscription),
    42  				),
    43  			},
    44  		},
    45  	})
    46  }
    47  
    48  func testAccCheckAWSSpotDatafeedSubscriptionDisappears(subscription *ec2.SpotDatafeedSubscription) resource.TestCheckFunc {
    49  	return func(s *terraform.State) error {
    50  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    51  
    52  		_, err := conn.DeleteSpotDatafeedSubscription(&ec2.DeleteSpotDatafeedSubscriptionInput{})
    53  		if err != nil {
    54  			return err
    55  		}
    56  
    57  		return resource.Retry(40*time.Minute, func() *resource.RetryError {
    58  			_, err := conn.DescribeSpotDatafeedSubscription(&ec2.DescribeSpotDatafeedSubscriptionInput{})
    59  			if err != nil {
    60  				cgw, ok := err.(awserr.Error)
    61  				if ok && cgw.Code() == "InvalidSpotDatafeed.NotFound" {
    62  					return nil
    63  				}
    64  				return resource.NonRetryableError(
    65  					fmt.Errorf("Error retrieving Spot Datafeed Subscription: %s", err))
    66  			}
    67  			return resource.RetryableError(fmt.Errorf("Waiting for Spot Datafeed Subscription"))
    68  		})
    69  	}
    70  }
    71  
    72  func testAccAWSSpotDatafeedSubscription_disappears(t *testing.T) {
    73  	var subscription ec2.SpotDatafeedSubscription
    74  	ri := acctest.RandInt()
    75  
    76  	resource.Test(t, resource.TestCase{
    77  		PreCheck:     func() { testAccPreCheck(t) },
    78  		Providers:    testAccProviders,
    79  		CheckDestroy: testAccCheckAWSSpotDatafeedSubscriptionDestroy,
    80  		Steps: []resource.TestStep{
    81  			{
    82  				Config: testAccAWSSpotDatafeedSubscription(ri),
    83  				Check: resource.ComposeTestCheckFunc(
    84  					testAccCheckAWSSpotDatafeedSubscriptionExists("aws_spot_datafeed_subscription.default", &subscription),
    85  					testAccCheckAWSSpotDatafeedSubscriptionDisappears(&subscription),
    86  				),
    87  				ExpectNonEmptyPlan: true,
    88  			},
    89  		},
    90  	})
    91  }
    92  
    93  func testAccCheckAWSSpotDatafeedSubscriptionExists(n string, subscription *ec2.SpotDatafeedSubscription) resource.TestCheckFunc {
    94  	return func(s *terraform.State) error {
    95  		rs, ok := s.RootModule().Resources[n]
    96  		if !ok {
    97  			return fmt.Errorf("Not found: %s", n)
    98  		}
    99  
   100  		if rs.Primary.ID == "" {
   101  			return fmt.Errorf("No policy ID is set")
   102  		}
   103  
   104  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   105  
   106  		resp, err := conn.DescribeSpotDatafeedSubscription(&ec2.DescribeSpotDatafeedSubscriptionInput{})
   107  		if err != nil {
   108  			return err
   109  		}
   110  
   111  		*subscription = *resp.SpotDatafeedSubscription
   112  
   113  		return nil
   114  	}
   115  }
   116  
   117  func testAccCheckAWSSpotDatafeedSubscriptionDestroy(s *terraform.State) error {
   118  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   119  
   120  	for _, rs := range s.RootModule().Resources {
   121  		if rs.Type != "aws_spot_datafeed_subscription" {
   122  			continue
   123  		}
   124  
   125  		// Try to get subscription
   126  		_, err := conn.DescribeSpotDatafeedSubscription(&ec2.DescribeSpotDatafeedSubscriptionInput{})
   127  		if err == nil {
   128  			return fmt.Errorf("still exist.")
   129  		}
   130  
   131  		awsErr, ok := err.(awserr.Error)
   132  		if !ok {
   133  			return err
   134  		}
   135  		if awsErr.Code() != "InvalidSpotDatafeed.NotFound" {
   136  			return err
   137  		}
   138  	}
   139  
   140  	return nil
   141  }
   142  
   143  func testAccAWSSpotDatafeedSubscription(randInt int) string {
   144  	return fmt.Sprintf(`
   145  resource "aws_s3_bucket" "default" {
   146  	bucket = "tf-spot-datafeed-%d"
   147  }
   148  
   149  resource "aws_spot_datafeed_subscription" "default" {
   150  	bucket = "${aws_s3_bucket.default.bucket}"
   151  }
   152  `, randInt)
   153  }