github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/builtin/providers/statuscake/resource_statuscaketest_test.go (about)

     1  package statuscake
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/DreamItGetIT/statuscake"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccStatusCake_basic(t *testing.T) {
    14  	var test statuscake.Test
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccTestCheckDestroy(&test),
    20  		Steps: []resource.TestStep{
    21  			{
    22  				Config: testAccTestConfig_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccTestCheckExists("statuscake_test.google", &test),
    25  					testAccTestCheckAttributes("statuscake_test.google", &test),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccStatusCake_tcp(t *testing.T) {
    33  	var test statuscake.Test
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccTestCheckDestroy(&test),
    39  		Steps: []resource.TestStep{
    40  			{
    41  				Config: testAccTestConfig_tcp,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccTestCheckExists("statuscake_test.google", &test),
    44  					testAccTestCheckAttributes("statuscake_test.google", &test),
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func TestAccStatusCake_withUpdate(t *testing.T) {
    52  	var test statuscake.Test
    53  
    54  	resource.Test(t, resource.TestCase{
    55  		PreCheck:     func() { testAccPreCheck(t) },
    56  		Providers:    testAccProviders,
    57  		CheckDestroy: testAccTestCheckDestroy(&test),
    58  		Steps: []resource.TestStep{
    59  			{
    60  				Config: testAccTestConfig_basic,
    61  				Check: resource.ComposeTestCheckFunc(
    62  					testAccTestCheckExists("statuscake_test.google", &test),
    63  				),
    64  			},
    65  
    66  			{
    67  				Config: testAccTestConfig_update,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccTestCheckExists("statuscake_test.google", &test),
    70  					testAccTestCheckAttributes("statuscake_test.google", &test),
    71  					resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"),
    72  					resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"),
    73  					resource.TestCheckResourceAttr("statuscake_test.google", "timeout", "40"),
    74  					resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"),
    75  					resource.TestCheckResourceAttr("statuscake_test.google", "confirmations", "0"),
    76  				),
    77  			},
    78  		},
    79  	})
    80  }
    81  
    82  func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc {
    83  	return func(s *terraform.State) error {
    84  		rs, ok := s.RootModule().Resources[rn]
    85  		if !ok {
    86  			return fmt.Errorf("resource not found: %s", rn)
    87  		}
    88  
    89  		if rs.Primary.ID == "" {
    90  			return fmt.Errorf("TestID not set")
    91  		}
    92  
    93  		client := testAccProvider.Meta().(*statuscake.Client)
    94  		testId, parseErr := strconv.Atoi(rs.Primary.ID)
    95  		if parseErr != nil {
    96  			return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr)
    97  		}
    98  
    99  		gotTest, err := client.Tests().Detail(testId)
   100  		if err != nil {
   101  			return fmt.Errorf("error getting test: %s", err)
   102  		}
   103  
   104  		*test = *gotTest
   105  
   106  		return nil
   107  	}
   108  }
   109  
   110  func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestCheckFunc {
   111  	return func(s *terraform.State) error {
   112  		attrs := s.RootModule().Resources[rn].Primary.Attributes
   113  
   114  		check := func(key, stateValue, testValue string) error {
   115  			if testValue != stateValue {
   116  				return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)",
   117  					key, stateValue, testValue)
   118  			}
   119  			return nil
   120  		}
   121  
   122  		for key, value := range attrs {
   123  			var err error
   124  
   125  			switch key {
   126  			case "website_name":
   127  				err = check(key, value, test.WebsiteName)
   128  			case "website_url":
   129  				err = check(key, value, test.WebsiteURL)
   130  			case "check_rate":
   131  				err = check(key, value, strconv.Itoa(test.CheckRate))
   132  			case "test_type":
   133  				err = check(key, value, test.TestType)
   134  			case "paused":
   135  				err = check(key, value, strconv.FormatBool(test.Paused))
   136  			case "timeout":
   137  				err = check(key, value, strconv.Itoa(test.Timeout))
   138  			case "contact_id":
   139  				err = check(key, value, strconv.Itoa(test.ContactID))
   140  			case "confirmations":
   141  				err = check(key, value, strconv.Itoa(test.Confirmation))
   142  			}
   143  
   144  			if err != nil {
   145  				return err
   146  			}
   147  		}
   148  		return nil
   149  	}
   150  }
   151  
   152  func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc {
   153  	return func(s *terraform.State) error {
   154  		client := testAccProvider.Meta().(*statuscake.Client)
   155  		err := client.Tests().Delete(test.TestID)
   156  		if err == nil {
   157  			return fmt.Errorf("test still exists")
   158  		}
   159  
   160  		return nil
   161  	}
   162  }
   163  
   164  const testAccTestConfig_basic = `
   165  resource "statuscake_test" "google" {
   166  	website_name = "google.com"
   167  	website_url = "www.google.com"
   168  	test_type = "HTTP"
   169  	check_rate = 300
   170  	timeout = 10
   171  	contact_id = 43402
   172  	confirmations = 1
   173  }
   174  `
   175  
   176  const testAccTestConfig_update = `
   177  resource "statuscake_test" "google" {
   178  	website_name = "google.com"
   179  	website_url = "www.google.com"
   180  	test_type = "HTTP"
   181  	check_rate = 500
   182  	paused = true
   183  }
   184  `
   185  
   186  const testAccTestConfig_tcp = `
   187  resource "statuscake_test" "google" {
   188  	website_name = "google.com"
   189  	website_url = "www.google.com"
   190  	test_type = "TCP"
   191  	check_rate = 300
   192  	timeout = 10
   193  	contact_id = 43402
   194  	confirmations = 1
   195  	port = 80
   196  }
   197  `