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

     1  package spotinst
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/spotinst/spotinst-sdk-go/spotinst"
    10  )
    11  
    12  func TestAccSpotinstSubscription_Basic(t *testing.T) {
    13  	var subscription spotinst.Subscription
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:  func() { testAccPreCheck(t) },
    16  		Providers: testAccProviders,
    17  		//CheckDestroy: testAccCheckSpotinstSubscriptionDestroy,
    18  		Steps: []resource.TestStep{
    19  			{
    20  				Config: testAccCheckSpotinstSubscriptionConfigBasic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckSpotinstSubscriptionExists("spotinst_subscription.foo", &subscription),
    23  					testAccCheckSpotinstSubscriptionAttributes(&subscription),
    24  					resource.TestCheckResourceAttr("spotinst_subscription.foo", "protocol", "http"),
    25  					resource.TestCheckResourceAttr("spotinst_subscription.foo", "endpoint", "http://endpoint.com"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccSpotinstSubscription_Updated(t *testing.T) {
    33  	var subscription spotinst.Subscription
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:  func() { testAccPreCheck(t) },
    36  		Providers: testAccProviders,
    37  		//CheckDestroy: testAccCheckSpotinstSubscriptionDestroy,
    38  		Steps: []resource.TestStep{
    39  			{
    40  				Config: testAccCheckSpotinstSubscriptionConfigBasic,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckSpotinstSubscriptionExists("spotinst_subscription.foo", &subscription),
    43  					testAccCheckSpotinstSubscriptionAttributes(&subscription),
    44  					resource.TestCheckResourceAttr("spotinst_subscription.foo", "protocol", "http"),
    45  					resource.TestCheckResourceAttr("spotinst_subscription.foo", "endpoint", "http://endpoint.com"),
    46  				),
    47  			},
    48  			{
    49  				Config: testAccCheckSpotinstSubscriptionConfigNewValue,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckSpotinstSubscriptionExists("spotinst_subscription.foo", &subscription),
    52  					testAccCheckSpotinstSubscriptionAttributesUpdated(&subscription),
    53  					resource.TestCheckResourceAttr("spotinst_subscription.foo", "protocol", "https"),
    54  					resource.TestCheckResourceAttr("spotinst_subscription.foo", "endpoint", "https://endpoint.com"),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func testAccCheckSpotinstSubscriptionDestroy(s *terraform.State) error {
    62  	client := testAccProvider.Meta().(*spotinst.Client)
    63  	for _, rs := range s.RootModule().Resources {
    64  		if rs.Type != "spotinst_subscription" {
    65  			continue
    66  		}
    67  		input := &spotinst.ReadSubscriptionInput{ID: spotinst.String(rs.Primary.ID)}
    68  		if _, err := client.SubscriptionService.Read(input); err == nil {
    69  			return fmt.Errorf("Subscription still exists")
    70  		}
    71  	}
    72  	return nil
    73  }
    74  
    75  func testAccCheckSpotinstSubscriptionAttributes(subscription *spotinst.Subscription) resource.TestCheckFunc {
    76  	return func(s *terraform.State) error {
    77  		if p := spotinst.StringValue(subscription.Protocol); p != "http" {
    78  			return fmt.Errorf("Bad content: %s", p)
    79  		}
    80  		if e := spotinst.StringValue(subscription.Endpoint); e != "http://endpoint.com" {
    81  			return fmt.Errorf("Bad content: %s", e)
    82  		}
    83  		return nil
    84  	}
    85  }
    86  
    87  func testAccCheckSpotinstSubscriptionAttributesUpdated(subscription *spotinst.Subscription) resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  		if p := spotinst.StringValue(subscription.Protocol); p != "https" {
    90  			return fmt.Errorf("Bad content: %s", p)
    91  		}
    92  		if e := spotinst.StringValue(subscription.Endpoint); e != "https://endpoint.com" {
    93  			return fmt.Errorf("Bad content: %s", e)
    94  		}
    95  		return nil
    96  	}
    97  }
    98  
    99  func testAccCheckSpotinstSubscriptionExists(n string, subscription *spotinst.Subscription) resource.TestCheckFunc {
   100  	return func(s *terraform.State) error {
   101  		rs, ok := s.RootModule().Resources[n]
   102  		if !ok {
   103  			return fmt.Errorf("Not found: %s", n)
   104  		}
   105  		if rs.Primary.ID == "" {
   106  			return fmt.Errorf("No resource ID is set")
   107  		}
   108  		client := testAccProvider.Meta().(*spotinst.Client)
   109  		input := &spotinst.ReadSubscriptionInput{ID: spotinst.String(rs.Primary.ID)}
   110  		resp, err := client.SubscriptionService.Read(input)
   111  		if err != nil {
   112  			return err
   113  		}
   114  		if spotinst.StringValue(resp.Subscription.ID) != rs.Primary.Attributes["id"] {
   115  			return fmt.Errorf("Subscription not found: %+v,\n %+v\n", resp.Subscription, rs.Primary.Attributes)
   116  		}
   117  		*subscription = *resp.Subscription
   118  		return nil
   119  	}
   120  }
   121  
   122  const testAccCheckSpotinstSubscriptionConfigBasic = `
   123  resource "spotinst_subscription" "foo" {
   124  	resource_id = "sig-foo"
   125  	event_type = "aws_ec2_instance_launch"
   126  	protocol = "http"
   127  	endpoint = "http://endpoint.com"
   128  	format = {
   129  		instance_id = "%instance-id%"
   130  		tags = "foo,baz,baz"
   131  	}
   132  }`
   133  
   134  const testAccCheckSpotinstSubscriptionConfigNewValue = `
   135  resource "spotinst_subscription" "foo" {
   136  	resource_id = "sig-foo"
   137  	event_type = "aws_ec2_instance_launch"
   138  	protocol = "https"
   139  	endpoint = "https://endpoint.com"
   140  	format = {
   141  		instance_id = "%instance-id%"
   142  		tags = "foo,baz,baz"
   143  	}
   144  }`