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