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