]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - statuscake/resource_statuscaketest_test.go
Fix status & uptime assertions
[github/fretlink/terraform-provider-statuscake.git] / statuscake / resource_statuscaketest_test.go
CommitLineData
b8f05dfc 1package statuscake
2
3import (
4 "fmt"
4eeeab64 5 "strconv"
b8f05dfc 6 "testing"
7
8 "github.com/DreamItGetIT/statuscake"
9 "github.com/hashicorp/terraform/helper/resource"
10 "github.com/hashicorp/terraform/terraform"
11)
12
13func 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{
90db3fb0 21 {
aa4f9726 22 Config: fmt.Sprintf(testAccTestConfig_basic, testContactGroupId),
b8f05dfc 23 Check: resource.ComposeTestCheckFunc(
24 testAccTestCheckExists("statuscake_test.google", &test),
c0701076 25 testAccTestCheckAttributes("statuscake_test.google", &test),
b8f05dfc 26 ),
27 },
28 },
29 })
30}
31
90db3fb0
PS
32func 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 {
aa4f9726 41 Config: fmt.Sprintf(testAccTestConfig_tcp, testContactGroupId),
90db3fb0
PS
42 Check: resource.ComposeTestCheckFunc(
43 testAccTestCheckExists("statuscake_test.google", &test),
44 testAccTestCheckAttributes("statuscake_test.google", &test),
45 ),
46 },
47 },
48 })
49}
50
4eeeab64 51func 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{
90db3fb0 59 {
aa4f9726 60 Config: fmt.Sprintf(testAccTestConfig_basic, testContactGroupId),
4eeeab64 61 Check: resource.ComposeTestCheckFunc(
62 testAccTestCheckExists("statuscake_test.google", &test),
63 ),
64 },
65
90db3fb0 66 {
4eeeab64 67 Config: testAccTestConfig_update,
68 Check: resource.ComposeTestCheckFunc(
69 testAccTestCheckExists("statuscake_test.google", &test),
c0701076 70 testAccTestCheckAttributes("statuscake_test.google", &test),
4eeeab64 71 resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"),
72 resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"),
6ad7af97 73 resource.TestCheckResourceAttr("statuscake_test.google", "timeout", "40"),
c0701076 74 resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"),
b5815405 75 resource.TestCheckResourceAttr("statuscake_test.google", "confirmations", "0"),
0e962b8e 76 resource.TestCheckResourceAttr("statuscake_test.google", "trigger_rate", "20"),
754f1420
AG
77 resource.TestCheckResourceAttr("statuscake_test.google", "custom_header", "{ \"Content-Type\": \"application/x-www-form-urlencoded\" }"),
78 resource.TestCheckResourceAttr("statuscake_test.google", "user_agent", "string9988"),
d01e3cca
MS
79 resource.TestCheckResourceAttr("statuscake_test.google", "status", "Up"),
80 resource.TestCheckResourceAttr("statuscake_test.google", "uptime", "0"),
754f1420
AG
81 resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.#", "3"),
82 resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.0", "string16045"),
83 resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.1", "string19741"),
84 resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.2", "string12122"),
85 resource.TestCheckResourceAttr("statuscake_test.google", "ping_url", "string8410"),
86 resource.TestCheckResourceAttr("statuscake_test.google", "basic_user", "string27052"),
87 resource.TestCheckResourceAttr("statuscake_test.google", "basic_pass", "string5659"),
88 resource.TestCheckResourceAttr("statuscake_test.google", "public", "0"),
89 resource.TestCheckResourceAttr("statuscake_test.google", "logo_image", "string21087"),
90 resource.TestCheckResourceAttr("statuscake_test.google", "branding", "25875"),
91 resource.TestCheckResourceAttr("statuscake_test.google", "website_host", "string32368"),
92 resource.TestCheckResourceAttr("statuscake_test.google", "virus", "1"),
93 resource.TestCheckResourceAttr("statuscake_test.google", "find_string", "string15212"),
94 resource.TestCheckResourceAttr("statuscake_test.google", "do_not_find", "false"),
95 resource.TestCheckResourceAttr("statuscake_test.google", "real_browser", "1"),
96 resource.TestCheckResourceAttr("statuscake_test.google", "test_tags", "string8191"),
97 resource.TestCheckResourceAttr("statuscake_test.google", "status_codes", "string23065"),
98 resource.TestCheckResourceAttr("statuscake_test.google", "use_jar", "1"),
99 resource.TestCheckResourceAttr("statuscake_test.google", "post_raw", "string32096"),
100 resource.TestCheckResourceAttr("statuscake_test.google", "final_endpoint", "string10781"),
101 resource.TestCheckResourceAttr("statuscake_test.google", "follow_redirect", "true"),
4eeeab64 102 ),
103 },
104 },
105 })
106}
107
108func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc {
b8f05dfc 109 return func(s *terraform.State) error {
110 rs, ok := s.RootModule().Resources[rn]
111 if !ok {
112 return fmt.Errorf("resource not found: %s", rn)
113 }
114
115 if rs.Primary.ID == "" {
116 return fmt.Errorf("TestID not set")
117 }
118
4eeeab64 119 client := testAccProvider.Meta().(*statuscake.Client)
120 testId, parseErr := strconv.Atoi(rs.Primary.ID)
121 if parseErr != nil {
122 return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr)
123 }
124
125 gotTest, err := client.Tests().Detail(testId)
126 if err != nil {
c0701076 127 return fmt.Errorf("error getting test: %s", err)
4eeeab64 128 }
129
130 *test = *gotTest
b8f05dfc 131
132 return nil
133 }
134}
135
c0701076
RG
136func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestCheckFunc {
137 return func(s *terraform.State) error {
138 attrs := s.RootModule().Resources[rn].Primary.Attributes
139
140 check := func(key, stateValue, testValue string) error {
141 if testValue != stateValue {
142 return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)",
143 key, stateValue, testValue)
144 }
145 return nil
146 }
147
148 for key, value := range attrs {
149 var err error
150
151 switch key {
152 case "website_name":
153 err = check(key, value, test.WebsiteName)
154 case "website_url":
155 err = check(key, value, test.WebsiteURL)
156 case "check_rate":
157 err = check(key, value, strconv.Itoa(test.CheckRate))
158 case "test_type":
159 err = check(key, value, test.TestType)
160 case "paused":
161 err = check(key, value, strconv.FormatBool(test.Paused))
162 case "timeout":
163 err = check(key, value, strconv.Itoa(test.Timeout))
164 case "contact_id":
c9458806 165 err = check(key, value, strconv.Itoa(test.ContactID))
b5815405
SC
166 case "confirmations":
167 err = check(key, value, strconv.Itoa(test.Confirmation))
0e962b8e
RG
168 case "trigger_rate":
169 err = check(key, value, strconv.Itoa(test.TriggerRate))
754f1420
AG
170 case "custom_header":
171 err = check(key, value, test.CustomHeader)
172 case "user_agent":
173 err = check(key, value, test.UserAgent)
754f1420
AG
174 case "node_locations":
175 for _, tv := range test.NodeLocations {
176 err = check(key, value, tv)
177 if err != nil {
178 return err
179 }
180 }
181 case "ping_url":
182 err = check(key, value, test.PingURL)
183 case "basic_user":
184 err = check(key, value, test.BasicUser)
185 case "basic_pass":
186 err = check(key, value, test.BasicPass)
187 case "public":
188 err = check(key, value, strconv.Itoa(test.Public))
189 case "logo_image":
190 err = check(key, value, test.LogoImage)
191 case "branding":
192 err = check(key, value, strconv.Itoa(test.Branding))
193 case "website_host":
194 err = check(key, value, test.WebsiteHost)
195 case "virus":
196 err = check(key, value, strconv.Itoa(test.Virus))
197 case "find_string":
198 err = check(key, value, test.FindString)
199 case "do_not_find":
200 err = check(key, value, strconv.FormatBool(test.DoNotFind))
201 case "real_browser":
202 err = check(key, value, strconv.Itoa(test.RealBrowser))
203 case "test_tags":
204 err = check(key, value, test.TestTags)
205 case "status_codes":
206 err = check(key, value, test.StatusCodes)
207 case "use_jar":
208 err = check(key, value, strconv.Itoa(test.UseJar))
209 case "post_raw":
210 err = check(key, value, test.PostRaw)
211 case "final_endpoint":
212 err = check(key, value, test.FinalEndpoint)
213 case "follow_redirect":
214 err = check(key, value, strconv.FormatBool(test.FollowRedirect))
c0701076 215 }
c0701076
RG
216 if err != nil {
217 return err
218 }
219 }
220 return nil
221 }
222}
223
4eeeab64 224func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc {
225 return func(s *terraform.State) error {
226 client := testAccProvider.Meta().(*statuscake.Client)
227 err := client.Tests().Delete(test.TestID)
228 if err == nil {
229 return fmt.Errorf("test still exists")
230 }
231
232 return nil
233 }
b8f05dfc 234}
235
236const testAccTestConfig_basic = `
237resource "statuscake_test" "google" {
51688607 238 website_name = "google.com"
239 website_url = "www.google.com"
240 test_type = "HTTP"
241 check_rate = 300
6ad7af97 242 timeout = 10
aa4f9726 243 contact_id = %d
b5815405 244 confirmations = 1
0e962b8e 245 trigger_rate = 10
b8f05dfc 246}
247`
4eeeab64 248
249const testAccTestConfig_update = `
250resource "statuscake_test" "google" {
51688607 251 website_name = "google.com"
252 website_url = "www.google.com"
253 test_type = "HTTP"
254 check_rate = 500
255 paused = true
0e962b8e 256 trigger_rate = 20
754f1420
AG
257 custom_header = "{ \"Content-Type\": \"application/x-www-form-urlencoded\" }"
258 user_agent = "string9988"
754f1420
AG
259 node_locations = [ "string16045", "string19741", "string12122" ]
260 ping_url = "string8410"
261 basic_user = "string27052"
262 basic_pass = "string5659"
263 public = 0
264 logo_image = "string21087"
265 branding = 25875
266 website_host = "string32368"
267 virus = 1
268 find_string = "string15212"
269 do_not_find = false
270 real_browser = 1
271 test_tags = "string8191"
272 status_codes = "string23065"
273 use_jar = 1
274 post_raw = "string32096"
275 final_endpoint = "string10781"
276 follow_redirect = true
4eeeab64 277}
278`
90db3fb0
PS
279
280const testAccTestConfig_tcp = `
281resource "statuscake_test" "google" {
282 website_name = "google.com"
283 website_url = "www.google.com"
284 test_type = "TCP"
285 check_rate = 300
286 timeout = 10
aa4f9726 287 contact_id = %d
90db3fb0
PS
288 confirmations = 1
289 port = 80
290}
291`