Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit e8b0bb3

Browse files
committed
Add acceptance tests for monitor data source
1 parent bf944f3 commit e8b0bb3

File tree

1 file changed

+332
-0
lines changed

1 file changed

+332
-0
lines changed
Lines changed: 332 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,332 @@
1+
package uptimerobot
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8+
)
9+
10+
func TestUptimeRobotDataSourceMonitor_http_monitor(t *testing.T) {
11+
friendlyName := "TF Test: http monitor"
12+
monitorType := "http"
13+
monitorURL := "https://google.com"
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
Providers: testAccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testAccUptimeRobotMonitorHTTPMonitor(friendlyName, monitorType, monitorURL),
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "friendly_name", friendlyName),
23+
resource.TestCheckResourceAttrSet("data.uptimerobot_monitor.test", "id"),
24+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "url", monitorURL),
25+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "type", monitorType),
26+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "status", "not checked yet"),
27+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "interval", "300"),
28+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "port", "0"),
29+
),
30+
},
31+
},
32+
})
33+
}
34+
35+
func TestUptimeRobotDataSourceMonitor_keyword_monitor(t *testing.T) {
36+
friendlyName := "TF Test: keyword"
37+
monitorType := "keyword"
38+
monitorURL := "https://google.com"
39+
keywordType := "not exists"
40+
keywordValue := "google"
41+
42+
resource.Test(t, resource.TestCase{
43+
PreCheck: func() { testAccPreCheck(t) },
44+
Providers: testAccProviders,
45+
Steps: []resource.TestStep{
46+
{
47+
Config: testAccUptimeRobotMonitorKeywordMonitor(friendlyName, monitorType, monitorURL, keywordType, keywordValue),
48+
Check: resource.ComposeTestCheckFunc(
49+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "friendly_name", friendlyName),
50+
resource.TestCheckResourceAttrSet("data.uptimerobot_monitor.test", "id"),
51+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "url", monitorURL),
52+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "type", monitorType),
53+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "status", "not checked yet"),
54+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "interval", "300"),
55+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "port", "0"),
56+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "keyword_type", keywordType),
57+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "keyword_value", keywordValue),
58+
),
59+
},
60+
},
61+
})
62+
}
63+
64+
func TestUptimeRobotDataSourceMonitor_http_port_monitor(t *testing.T) {
65+
friendlyName := "TF Test: http port monitor"
66+
monitorType := "port"
67+
monitorURL := "google.com"
68+
subType := "http"
69+
70+
resource.Test(t, resource.TestCase{
71+
PreCheck: func() { testAccPreCheck(t) },
72+
Providers: testAccProviders,
73+
Steps: []resource.TestStep{
74+
{
75+
Config: testAccUptimeRobotMonitorHTTPPortMonitor(friendlyName, monitorType, monitorURL, subType),
76+
Check: resource.ComposeTestCheckFunc(
77+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "friendly_name", friendlyName),
78+
resource.TestCheckResourceAttrSet("data.uptimerobot_monitor.test", "id"),
79+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "url", monitorURL),
80+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "type", monitorType),
81+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "status", "not checked yet"),
82+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "interval", "300"),
83+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "port", "0"),
84+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "sub_type", subType),
85+
),
86+
},
87+
},
88+
})
89+
}
90+
91+
func TestUptimeRobotDataSourceMonitor_custom_port_monitor(t *testing.T) {
92+
friendlyName := "TF Test: custom port monitor"
93+
monitorType := "port"
94+
monitorURL := "google.com"
95+
subType := "custom"
96+
port := 8080
97+
98+
resource.Test(t, resource.TestCase{
99+
PreCheck: func() { testAccPreCheck(t) },
100+
Providers: testAccProviders,
101+
Steps: []resource.TestStep{
102+
{
103+
Config: testAccUptimeRobotMonitorCustomPortMonitor(friendlyName, monitorType, monitorURL, subType, port),
104+
Check: resource.ComposeTestCheckFunc(
105+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "friendly_name", friendlyName),
106+
resource.TestCheckResourceAttrSet("data.uptimerobot_monitor.test", "id"),
107+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "url", monitorURL),
108+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "type", monitorType),
109+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "status", "not checked yet"),
110+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "interval", "300"),
111+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "port", fmt.Sprintf("%d", port)),
112+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "sub_type", subType),
113+
),
114+
},
115+
},
116+
})
117+
}
118+
119+
func TestUptimeRobotDataSourceMonitor_alert_contact(t *testing.T) {
120+
friendlyName := "TF Test: alert contact"
121+
monitorType := "http"
122+
monitorURL := "https://google.com"
123+
124+
resource.Test(t, resource.TestCase{
125+
PreCheck: func() { testAccPreCheck(t) },
126+
Providers: testAccProviders,
127+
Steps: []resource.TestStep{
128+
{
129+
Config: testAccUptimeRobotMonitorAlertContact(friendlyName, monitorType, monitorURL),
130+
Check: resource.ComposeTestCheckFunc(
131+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "friendly_name", friendlyName),
132+
resource.TestCheckResourceAttrSet("data.uptimerobot_monitor.test", "id"),
133+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "url", monitorURL),
134+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "type", monitorType),
135+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "status", "not checked yet"),
136+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "interval", "300"),
137+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "port", "0"),
138+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "alert_contact.#", "2"),
139+
resource.TestCheckResourceAttrSet("data.uptimerobot_monitor.test", "alert_contact.0.id"),
140+
resource.TestCheckResourceAttrSet("data.uptimerobot_monitor.test", "alert_contact.0.recurrence"),
141+
resource.TestCheckResourceAttrSet("data.uptimerobot_monitor.test", "alert_contact.0.threshold"),
142+
),
143+
},
144+
},
145+
})
146+
}
147+
148+
func TestUptimeRobotDataSourceMonitor_custom_http_headers(t *testing.T) {
149+
friendlyName := "TF Test: custom http headers"
150+
monitorType := "http"
151+
monitorURL := "https://google.com"
152+
153+
resource.Test(t, resource.TestCase{
154+
PreCheck: func() { testAccPreCheck(t) },
155+
Providers: testAccProviders,
156+
Steps: []resource.TestStep{
157+
{
158+
Config: testAccUptimeRobotMonitorCustomHTTPHeaders(friendlyName, monitorType, monitorURL),
159+
Check: resource.ComposeTestCheckFunc(
160+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "friendly_name", friendlyName),
161+
resource.TestCheckResourceAttrSet("data.uptimerobot_monitor.test", "id"),
162+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "url", monitorURL),
163+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "type", monitorType),
164+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "status", "not checked yet"),
165+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "interval", "300"),
166+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "port", "0"),
167+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "custom_http_headers.%", "0"),
168+
),
169+
},
170+
},
171+
})
172+
}
173+
174+
func TestUptimeRobotDataSourceMonitor_http_auth_monitor(t *testing.T) {
175+
friendlyName := "TF Test: http auth monitor"
176+
monitorType := "http"
177+
username := "tester"
178+
password := "secret"
179+
authType := "basic"
180+
monitorURL := fmt.Sprintf("https://httpbin.org/basic-auth/%s/%s", username, password)
181+
182+
resource.Test(t, resource.TestCase{
183+
PreCheck: func() { testAccPreCheck(t) },
184+
Providers: testAccProviders,
185+
Steps: []resource.TestStep{
186+
{
187+
Config: testAccUptimeRobotMonitorHTTPAuthMonitor(friendlyName, monitorType, monitorURL, username, password, authType),
188+
Check: resource.ComposeTestCheckFunc(
189+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "friendly_name", friendlyName),
190+
resource.TestCheckResourceAttrSet("data.uptimerobot_monitor.test", "id"),
191+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "url", monitorURL),
192+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "type", monitorType),
193+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "status", "not checked yet"),
194+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "interval", "300"),
195+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "port", "0"),
196+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "http_username", username),
197+
resource.TestCheckResourceAttr("data.uptimerobot_monitor.test", "http_password", password),
198+
),
199+
},
200+
},
201+
})
202+
}
203+
204+
func testAccUptimeRobotMonitorHTTPMonitor(friendlyName, monitorType, monitorURL string) string {
205+
return fmt.Sprintf(`
206+
resource "uptimerobot_monitor" "monitor" {
207+
friendly_name = "%s"
208+
type = "%s"
209+
url = "%s"
210+
}
211+
212+
data "uptimerobot_monitor" "test" {
213+
friendly_name = uptimerobot_monitor.monitor.friendly_name
214+
}
215+
`, friendlyName, monitorType, monitorURL)
216+
}
217+
218+
func testAccUptimeRobotMonitorKeywordMonitor(friendlyName, monitorType, monitorURL, keywordType, keywordValue string) string {
219+
return fmt.Sprintf(`
220+
resource "uptimerobot_monitor" "monitor" {
221+
friendly_name = "%s"
222+
type = "%s"
223+
url = "%s"
224+
keyword_type = "%s"
225+
keyword_value = "%s"
226+
}
227+
228+
data "uptimerobot_monitor" "test" {
229+
friendly_name = uptimerobot_monitor.monitor.friendly_name
230+
}
231+
`, friendlyName, monitorType, monitorURL, keywordType, keywordValue)
232+
}
233+
234+
func testAccUptimeRobotMonitorHTTPPortMonitor(friendlyName, monitorType, monitorURL, subType string) string {
235+
return fmt.Sprintf(`
236+
resource "uptimerobot_monitor" "monitor" {
237+
friendly_name = "%s"
238+
type = "%s"
239+
url = "%s"
240+
sub_type = "%s"
241+
}
242+
243+
data "uptimerobot_monitor" "test" {
244+
friendly_name = uptimerobot_monitor.monitor.friendly_name
245+
}
246+
`, friendlyName, monitorType, monitorURL, subType)
247+
}
248+
249+
func testAccUptimeRobotMonitorCustomPortMonitor(friendlyName, monitorType, monitorURL, subType string, port int) string {
250+
return fmt.Sprintf(`
251+
resource "uptimerobot_monitor" "monitor" {
252+
friendly_name = "%s"
253+
type = "%s"
254+
url = "%s"
255+
sub_type = "%s"
256+
port = %d
257+
}
258+
259+
data "uptimerobot_monitor" "test" {
260+
friendly_name = uptimerobot_monitor.monitor.friendly_name
261+
}
262+
`, friendlyName, monitorType, monitorURL, subType, port)
263+
}
264+
265+
func testAccUptimeRobotMonitorAlertContact(friendlyName, monitorType, monitorURL string) string {
266+
return fmt.Sprintf(`
267+
resource "uptimerobot_alert_contact" "slack" {
268+
friendly_name = "TF Test: Slack"
269+
type = "slack"
270+
value = "https://slack.com/services/xxxx"
271+
}
272+
273+
resource "uptimerobot_alert_contact" "email" {
274+
friendly_name = "TF Test: Email"
275+
type = "email"
276+
277+
}
278+
279+
resource "uptimerobot_monitor" "monitor" {
280+
friendly_name = "%s"
281+
type = "%s"
282+
url = "%s"
283+
284+
alert_contact {
285+
id = uptimerobot_alert_contact.slack.id
286+
}
287+
288+
alert_contact {
289+
id = uptimerobot_alert_contact.email.id
290+
}
291+
}
292+
293+
data "uptimerobot_monitor" "test" {
294+
friendly_name = uptimerobot_monitor.monitor.friendly_name
295+
}
296+
`, friendlyName, monitorType, monitorURL)
297+
}
298+
299+
func testAccUptimeRobotMonitorCustomHTTPHeaders(friendlyName, monitorType, monitorURL string) string {
300+
return fmt.Sprintf(`
301+
resource "uptimerobot_monitor" "monitor" {
302+
friendly_name = "%s"
303+
type = "%s"
304+
url = "%s"
305+
306+
custom_http_headers = {
307+
// Accept-Language = "en"
308+
}
309+
}
310+
311+
data "uptimerobot_monitor" "test" {
312+
friendly_name = uptimerobot_monitor.monitor.friendly_name
313+
}
314+
`, friendlyName, monitorType, monitorURL)
315+
}
316+
317+
func testAccUptimeRobotMonitorHTTPAuthMonitor(friendlyName, monitorType, monitorURL, username, password, authType string) string {
318+
return fmt.Sprintf(`
319+
resource "uptimerobot_monitor" "monitor" {
320+
friendly_name = "%s"
321+
type = "%s"
322+
url = "%s"
323+
http_username = "%s"
324+
http_password = "%s"
325+
http_auth_type = "%s"
326+
}
327+
328+
data "uptimerobot_monitor" "test" {
329+
friendly_name = uptimerobot_monitor.monitor.friendly_name
330+
}
331+
`, friendlyName, monitorType, monitorURL, username, password, authType)
332+
}

0 commit comments

Comments
 (0)