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