github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 resource.TestStep{ 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_withUpdate(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 resource.TestStep{ 41 Config: testAccTestConfig_basic, 42 Check: resource.ComposeTestCheckFunc( 43 testAccTestCheckExists("statuscake_test.google", &test), 44 ), 45 }, 46 47 resource.TestStep{ 48 Config: testAccTestConfig_update, 49 Check: resource.ComposeTestCheckFunc( 50 testAccTestCheckExists("statuscake_test.google", &test), 51 testAccTestCheckAttributes("statuscake_test.google", &test), 52 resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"), 53 resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"), 54 resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"), 55 resource.TestCheckResourceAttr("statuscake_test.google", "confirmations", "0"), 56 ), 57 }, 58 }, 59 }) 60 } 61 62 func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc { 63 return func(s *terraform.State) error { 64 rs, ok := s.RootModule().Resources[rn] 65 if !ok { 66 return fmt.Errorf("resource not found: %s", rn) 67 } 68 69 if rs.Primary.ID == "" { 70 return fmt.Errorf("TestID not set") 71 } 72 73 client := testAccProvider.Meta().(*statuscake.Client) 74 testId, parseErr := strconv.Atoi(rs.Primary.ID) 75 if parseErr != nil { 76 return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr) 77 } 78 79 gotTest, err := client.Tests().Detail(testId) 80 if err != nil { 81 return fmt.Errorf("error getting test: %s", err) 82 } 83 84 *test = *gotTest 85 86 return nil 87 } 88 } 89 90 func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestCheckFunc { 91 return func(s *terraform.State) error { 92 attrs := s.RootModule().Resources[rn].Primary.Attributes 93 94 check := func(key, stateValue, testValue string) error { 95 if testValue != stateValue { 96 return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)", 97 key, stateValue, testValue) 98 } 99 return nil 100 } 101 102 for key, value := range attrs { 103 var err error 104 105 switch key { 106 case "website_name": 107 err = check(key, value, test.WebsiteName) 108 case "website_url": 109 err = check(key, value, test.WebsiteURL) 110 case "check_rate": 111 err = check(key, value, strconv.Itoa(test.CheckRate)) 112 case "test_type": 113 err = check(key, value, test.TestType) 114 case "paused": 115 err = check(key, value, strconv.FormatBool(test.Paused)) 116 case "timeout": 117 err = check(key, value, strconv.Itoa(test.Timeout)) 118 case "contact_id": 119 err = check(key, value, strconv.Itoa(test.ContactID)) 120 case "confirmations": 121 err = check(key, value, strconv.Itoa(test.Confirmation)) 122 } 123 124 if err != nil { 125 return err 126 } 127 } 128 return nil 129 } 130 } 131 132 func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc { 133 return func(s *terraform.State) error { 134 client := testAccProvider.Meta().(*statuscake.Client) 135 err := client.Tests().Delete(test.TestID) 136 if err == nil { 137 return fmt.Errorf("test still exists") 138 } 139 140 return nil 141 } 142 } 143 144 const testAccTestConfig_basic = ` 145 resource "statuscake_test" "google" { 146 website_name = "google.com" 147 website_url = "www.google.com" 148 test_type = "HTTP" 149 check_rate = 300 150 contact_id = 12345 151 confirmations = 1 152 } 153 ` 154 155 const testAccTestConfig_update = ` 156 resource "statuscake_test" "google" { 157 website_name = "google.com" 158 website_url = "www.google.com" 159 test_type = "HTTP" 160 check_rate = 500 161 paused = true 162 } 163 `