diff options
Diffstat (limited to 'statuscake/resource_statuscakessl_test.go')
-rw-r--r-- | statuscake/resource_statuscakessl_test.go | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/statuscake/resource_statuscakessl_test.go b/statuscake/resource_statuscakessl_test.go new file mode 100644 index 0000000..fa48bad --- /dev/null +++ b/statuscake/resource_statuscakessl_test.go | |||
@@ -0,0 +1,224 @@ | |||
1 | package statuscake | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "github.com/DreamItGetIT/statuscake" | ||
6 | "github.com/hashicorp/terraform/helper/resource" | ||
7 | "github.com/hashicorp/terraform/terraform" | ||
8 | "os" | ||
9 | "strconv" | ||
10 | "testing" | ||
11 | ) | ||
12 | |||
13 | func TestAccStatusCakeSsl_basic(t *testing.T) { | ||
14 | var ssl statuscake.Ssl | ||
15 | |||
16 | resource.Test(t, resource.TestCase{ | ||
17 | PreCheck: func() { testAccPreCheck(t) }, | ||
18 | Providers: testAccProviders, | ||
19 | CheckDestroy: testAccSslCheckDestroy(&ssl), | ||
20 | Steps: []resource.TestStep{ | ||
21 | { | ||
22 | Config: interpolateTerraformTemplateSsl(testAccSslConfig_basic), | ||
23 | Check: resource.ComposeTestCheckFunc( | ||
24 | testAccSslCheckExists("statuscake_ssl.exemple", &ssl), | ||
25 | testAccSslCheckAttributes("statuscake_ssl.exemple", &ssl), | ||
26 | ), | ||
27 | }, | ||
28 | }, | ||
29 | }) | ||
30 | } | ||
31 | |||
32 | func TestAccStatusCakeSsl_withUpdate(t *testing.T) { | ||
33 | var ssl statuscake.Ssl | ||
34 | |||
35 | resource.Test(t, resource.TestCase{ | ||
36 | PreCheck: func() { testAccPreCheck(t) }, | ||
37 | Providers: testAccProviders, | ||
38 | CheckDestroy: testAccSslCheckDestroy(&ssl), | ||
39 | Steps: []resource.TestStep{ | ||
40 | { | ||
41 | Config: interpolateTerraformTemplateSsl(testAccSslConfig_basic), | ||
42 | Check: resource.ComposeTestCheckFunc( | ||
43 | testAccSslCheckExists("statuscake_ssl.exemple", &ssl), | ||
44 | testAccSslCheckAttributes("statuscake_ssl.exemple", &ssl), | ||
45 | ), | ||
46 | }, | ||
47 | |||
48 | { | ||
49 | Config: testAccSslConfig_update, | ||
50 | Check: resource.ComposeTestCheckFunc( | ||
51 | testAccSslCheckExists("statuscake_ssl.exemple", &ssl), | ||
52 | testAccSslCheckAttributes("statuscake_ssl.exemple", &ssl), | ||
53 | resource.TestCheckResourceAttr("statuscake_ssl.exemple", "checkrate", "86400"), | ||
54 | resource.TestCheckResourceAttr("statuscake_ssl.exemple", "domain", "https://www.exemple.com"), | ||
55 | resource.TestCheckResourceAttr("statuscake_ssl.exemple", "contact_groups_c", ""), | ||
56 | resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_at", "18,81,2019"), | ||
57 | resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_reminder", "false"), | ||
58 | resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_expiry", "false"), | ||
59 | resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_broken", "true"), | ||
60 | resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_mixed", "false"), | ||
61 | ), | ||
62 | }, | ||
63 | }, | ||
64 | }) | ||
65 | } | ||
66 | |||
67 | func testAccSslCheckExists(rn string, ssl *statuscake.Ssl) resource.TestCheckFunc { | ||
68 | return func(s *terraform.State) error { | ||
69 | rs, ok := s.RootModule().Resources[rn] | ||
70 | if !ok { | ||
71 | return fmt.Errorf("resource not found: %s", rn) | ||
72 | } | ||
73 | |||
74 | if rs.Primary.ID == "" { | ||
75 | return fmt.Errorf("SslID not set") | ||
76 | } | ||
77 | |||
78 | client := testAccProvider.Meta().(*statuscake.Client) | ||
79 | sslId := rs.Primary.ID | ||
80 | |||
81 | gotSsl, err := statuscake.NewSsls(client).Detail(sslId) | ||
82 | if err != nil { | ||
83 | return fmt.Errorf("error getting ssl: %s", err) | ||
84 | } | ||
85 | gotSsl.LastUpdatedUtc = "0000-00-00 00:00:00" // quick fix to avoid issue with it because the state is updated before the value change but it is changed when gotSsl is created | ||
86 | *ssl = *gotSsl | ||
87 | |||
88 | return nil | ||
89 | } | ||
90 | } | ||
91 | |||
92 | func testAccSslCheckAttributes(rn string, ssl *statuscake.Ssl) resource.TestCheckFunc { | ||
93 | return func(s *terraform.State) error { | ||
94 | attrs := s.RootModule().Resources[rn].Primary.Attributes | ||
95 | |||
96 | check := func(key, stateValue, sslValue string) error { | ||
97 | if sslValue != stateValue { | ||
98 | return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)", | ||
99 | key, stateValue, sslValue) | ||
100 | } | ||
101 | return nil | ||
102 | } | ||
103 | |||
104 | for key, value := range attrs { | ||
105 | var err error | ||
106 | |||
107 | switch key { | ||
108 | case "domain": | ||
109 | err = check(key, value, ssl.Domain) | ||
110 | case "contact_groups_c": | ||
111 | err = check(key, value, ssl.ContactGroupsC) | ||
112 | case "checkrate": | ||
113 | err = check(key, value, strconv.Itoa(ssl.Checkrate)) | ||
114 | case "alert_at": | ||
115 | err = check(key, value, ssl.AlertAt) | ||
116 | case "alert_reminder": | ||
117 | err = check(key, value, strconv.FormatBool(ssl.AlertReminder)) | ||
118 | case "alert_expiry": | ||
119 | err = check(key, value, strconv.FormatBool(ssl.AlertExpiry)) | ||
120 | case "alert_broken": | ||
121 | err = check(key, value, strconv.FormatBool(ssl.AlertBroken)) | ||
122 | case "alert_mixed": | ||
123 | err = check(key, value, strconv.FormatBool(ssl.AlertMixed)) | ||
124 | case "last_updated_utc": | ||
125 | err = check(key, value, ssl.LastUpdatedUtc) | ||
126 | case "paused": | ||
127 | err = check(key, value, strconv.FormatBool(ssl.Paused)) | ||
128 | case "issuer_cn": | ||
129 | err = check(key, value, ssl.IssuerCn) | ||
130 | case "contact_groups": | ||
131 | for _, tv := range ssl.ContactGroups { | ||
132 | err = check(key, value, tv) | ||
133 | if err != nil { | ||
134 | return err | ||
135 | } | ||
136 | } | ||
137 | case "cert_score": | ||
138 | err = check(key, value, ssl.CertScore) | ||
139 | case "cert_status": | ||
140 | err = check(key, value, ssl.CertStatus) | ||
141 | case "cipher": | ||
142 | err = check(key, value, ssl.Cipher) | ||
143 | case "valid_from_utc": | ||
144 | err = check(key, value, ssl.ValidFromUtc) | ||
145 | case "valid_until_utc": | ||
146 | err = check(key, value, ssl.ValidUntilUtc) | ||
147 | case "last_reminder": | ||
148 | err = check(key, value, strconv.Itoa(ssl.LastReminder)) | ||
149 | case "flags": | ||
150 | for _, tv := range ssl.Flags { | ||
151 | err = check(key, value, strconv.FormatBool(tv)) | ||
152 | if err != nil { | ||
153 | return err | ||
154 | } | ||
155 | } | ||
156 | |||
157 | case "mixed_content": | ||
158 | for _, tv := range ssl.MixedContent { | ||
159 | for _, tv2 := range tv { | ||
160 | err = check(key, value, tv2) | ||
161 | if err != nil { | ||
162 | return err | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | } | ||
167 | if err != nil { | ||
168 | return err | ||
169 | } | ||
170 | } | ||
171 | return nil | ||
172 | } | ||
173 | } | ||
174 | |||
175 | func testAccSslCheckDestroy(ssl *statuscake.Ssl) resource.TestCheckFunc { | ||
176 | return func(s *terraform.State) error { | ||
177 | client := testAccProvider.Meta().(*statuscake.Client) | ||
178 | _, err := statuscake.NewSsls(client).Detail(ssl.ID) | ||
179 | if err == nil { | ||
180 | return fmt.Errorf("ssl still exists") | ||
181 | } | ||
182 | |||
183 | return nil | ||
184 | } | ||
185 | } | ||
186 | |||
187 | func interpolateTerraformTemplateSsl(template string) string { | ||
188 | sslContactGroupId := "43402" | ||
189 | |||
190 | if v := os.Getenv("STATUSCAKE_SSL_CONTACT_GROUP_ID"); v != "" { | ||
191 | sslContactGroupId = v | ||
192 | } | ||
193 | if sslContactGroupId == "-1" { | ||
194 | sslContactGroupId = "" | ||
195 | } | ||
196 | |||
197 | return fmt.Sprintf(template, sslContactGroupId) | ||
198 | } | ||
199 | |||
200 | const testAccSslConfig_basic = ` | ||
201 | resource "statuscake_ssl" "exemple" { | ||
202 | domain = "https://www.exemple.com" | ||
203 | contact_groups_c = "%s" | ||
204 | checkrate = 3600 | ||
205 | alert_at = "18,71,2019" | ||
206 | alert_reminder = true | ||
207 | alert_expiry = true | ||
208 | alert_broken = false | ||
209 | alert_mixed = true | ||
210 | } | ||
211 | ` | ||
212 | |||
213 | const testAccSslConfig_update = ` | ||
214 | resource "statuscake_ssl" "exemple" { | ||
215 | domain = "https://www.exemple.com" | ||
216 | contact_groups_c = "" | ||
217 | checkrate = 86400 | ||
218 | alert_at = "18,81,2019" | ||
219 | alert_reminder = false | ||
220 | alert_expiry = false | ||
221 | alert_broken = true | ||
222 | alert_mixed = false | ||
223 | } | ||
224 | ` | ||