github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/spotinst/resource_spotinst_healthcheck_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 TestAccSpotinstHealthCheck_Basic(t *testing.T) {
    13  	var healthCheck spotinst.HealthCheck
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckSpotinstHealthCheckDestroy,
    18  		Steps: []resource.TestStep{
    19  			{
    20  				Config: testAccCheckSpotinstHealthCheckConfigBasic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckSpotinstHealthCheckExists("spotinst_healthcheck.foo", &healthCheck),
    23  					testAccCheckSpotinstHealthCheckAttributes(&healthCheck),
    24  					resource.TestCheckResourceAttr("spotinst_healthcheck.foo", "name", "hc-foo"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccSpotinstHealthCheck_Updated(t *testing.T) {
    32  	var healthCheck spotinst.HealthCheck
    33  	resource.Test(t, resource.TestCase{
    34  		PreCheck:     func() { testAccPreCheck(t) },
    35  		Providers:    testAccProviders,
    36  		CheckDestroy: testAccCheckSpotinstHealthCheckDestroy,
    37  		Steps: []resource.TestStep{
    38  			{
    39  				Config: testAccCheckSpotinstHealthCheckConfigBasic,
    40  				Check: resource.ComposeTestCheckFunc(
    41  					testAccCheckSpotinstHealthCheckExists("spotinst_healthcheck.foo", &healthCheck),
    42  					testAccCheckSpotinstHealthCheckAttributes(&healthCheck),
    43  					resource.TestCheckResourceAttr("spotinst_healthcheck.foo", "name", "hc-foo"),
    44  				),
    45  			},
    46  			{
    47  				Config: testAccCheckSpotinstHealthCheckConfigNewValue,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckSpotinstHealthCheckExists("spotinst_healthcheck.foo", &healthCheck),
    50  					testAccCheckSpotinstHealthCheckAttributesUpdated(&healthCheck),
    51  					resource.TestCheckResourceAttr("spotinst_healthcheck.foo", "name", "hc-bar"),
    52  				),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func testAccCheckSpotinstHealthCheckDestroy(s *terraform.State) error {
    59  	client := testAccProvider.Meta().(*spotinst.Client)
    60  	for _, rs := range s.RootModule().Resources {
    61  		if rs.Type != "spotinst_healthcheck" {
    62  			continue
    63  		}
    64  		input := &spotinst.ReadHealthCheckInput{ID: spotinst.String(rs.Primary.ID)}
    65  		resp, err := client.HealthCheckService.Read(input)
    66  		if err == nil && resp != nil && resp.HealthCheck != nil {
    67  			return fmt.Errorf("HealthCheck still exists")
    68  		}
    69  	}
    70  	return nil
    71  }
    72  
    73  func testAccCheckSpotinstHealthCheckAttributes(healthCheck *spotinst.HealthCheck) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		if p := spotinst.StringValue(healthCheck.Check.Protocol); p != "http" {
    76  			return fmt.Errorf("Bad content: %s", p)
    77  		}
    78  		if e := spotinst.StringValue(healthCheck.Check.Endpoint); e != "http://endpoint.com" {
    79  			return fmt.Errorf("Bad content: %s", e)
    80  		}
    81  		return nil
    82  	}
    83  }
    84  
    85  func testAccCheckSpotinstHealthCheckAttributesUpdated(healthCheck *spotinst.HealthCheck) resource.TestCheckFunc {
    86  	return func(s *terraform.State) error {
    87  		if p := spotinst.StringValue(healthCheck.Check.Protocol); p != "https" {
    88  			return fmt.Errorf("Bad content: %s", p)
    89  		}
    90  		if e := spotinst.StringValue(healthCheck.Check.Endpoint); e != "https://endpoint.com" {
    91  			return fmt.Errorf("Bad content: %s", e)
    92  		}
    93  		return nil
    94  	}
    95  }
    96  
    97  func testAccCheckSpotinstHealthCheckExists(n string, healthCheck *spotinst.HealthCheck) resource.TestCheckFunc {
    98  	return func(s *terraform.State) error {
    99  		rs, ok := s.RootModule().Resources[n]
   100  		if !ok {
   101  			return fmt.Errorf("Not found: %s", n)
   102  		}
   103  		if rs.Primary.ID == "" {
   104  			return fmt.Errorf("No resource ID is set")
   105  		}
   106  		client := testAccProvider.Meta().(*spotinst.Client)
   107  		input := &spotinst.ReadHealthCheckInput{ID: spotinst.String(rs.Primary.ID)}
   108  		resp, err := client.HealthCheckService.Read(input)
   109  		if err != nil {
   110  			return err
   111  		}
   112  		if spotinst.StringValue(resp.HealthCheck.ID) != rs.Primary.Attributes["id"] {
   113  			return fmt.Errorf("HealthCheck not found: %+v,\n %+v\n", resp.HealthCheck, rs.Primary.Attributes)
   114  		}
   115  		*healthCheck = *resp.HealthCheck
   116  		return nil
   117  	}
   118  }
   119  
   120  const testAccCheckSpotinstHealthCheckConfigBasic = `
   121  resource "spotinst_healthcheck" "foo" {
   122  	name = "hc-foo"
   123  	resource_id = "sig-foo"
   124  	check {
   125  		protocol = "http"
   126  		endpoint = "http://endpoint.com"
   127  		port = 1337
   128  		interval = 10
   129  		timeout = 10
   130  	}
   131  	threshold {
   132  		healthy = 1
   133  		unhealthy = 1
   134  	}
   135  	proxy {
   136  		addr = "http://proxy.com"
   137  		port = 80
   138  	}
   139  }`
   140  
   141  const testAccCheckSpotinstHealthCheckConfigNewValue = `
   142  resource "spotinst_healthcheck" "foo" {
   143  	name = "hc-bar"
   144  	resource_id = "sig-foo"
   145  	check {
   146  		protocol = "https"
   147  		endpoint = "https://endpoint.com"
   148  		port = 3000
   149  		interval = 10
   150  		timeout = 10
   151  	}
   152  	threshold {
   153  		healthy = 2
   154  		unhealthy = 2
   155  	}
   156  	proxy {
   157  		addr = "http://proxy.com"
   158  		port = 8080
   159  	}
   160  }`