From aa4f9726b4fd3cc624fe5784b033840a6c92c84c Mon Sep 17 00:00:00 2001 From: Mat Schaffer Date: Thu, 1 Feb 2018 16:07:31 +0900 Subject: Parameterize the contact ID used for acceptance testing This allowed me to run acceptance tests on my own account. --- statuscake/provider_test.go | 15 +++++++++++++++ statuscake/resource_statuscaketest_test.go | 10 +++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/statuscake/provider_test.go b/statuscake/provider_test.go index 83045d0..81d5888 100644 --- a/statuscake/provider_test.go +++ b/statuscake/provider_test.go @@ -1,7 +1,9 @@ package statuscake import ( + "fmt" "os" + "strconv" "testing" "github.com/hashicorp/terraform/helper/schema" @@ -10,12 +12,25 @@ import ( var testAccProviders map[string]terraform.ResourceProvider var testAccProvider *schema.Provider +var testContactGroupId int func init() { testAccProvider = Provider().(*schema.Provider) testAccProviders = map[string]terraform.ResourceProvider{ "statuscake": testAccProvider, } + + if v := os.Getenv("STATUSCAKE_TEST_CONTACT_GROUP_ID"); v == "" { + fmt.Println("STATUSCAKE_TEST_CONTACT_GROUP_ID must be set for acceptance tests") + os.Exit(1) + } else { + id, err := strconv.Atoi(v) + if err != nil { + fmt.Println("STATUSCAKE_TEST_CONTACT_GROUP_ID must be a valid int") + os.Exit(1) + } + testContactGroupId = id + } } func TestProvider(t *testing.T) { diff --git a/statuscake/resource_statuscaketest_test.go b/statuscake/resource_statuscaketest_test.go index b727e92..ea69756 100644 --- a/statuscake/resource_statuscaketest_test.go +++ b/statuscake/resource_statuscaketest_test.go @@ -19,7 +19,7 @@ func TestAccStatusCake_basic(t *testing.T) { CheckDestroy: testAccTestCheckDestroy(&test), Steps: []resource.TestStep{ { - Config: testAccTestConfig_basic, + Config: fmt.Sprintf(testAccTestConfig_basic, testContactGroupId), Check: resource.ComposeTestCheckFunc( testAccTestCheckExists("statuscake_test.google", &test), testAccTestCheckAttributes("statuscake_test.google", &test), @@ -38,7 +38,7 @@ func TestAccStatusCake_tcp(t *testing.T) { CheckDestroy: testAccTestCheckDestroy(&test), Steps: []resource.TestStep{ { - Config: testAccTestConfig_tcp, + Config: fmt.Sprintf(testAccTestConfig_tcp, testContactGroupId), Check: resource.ComposeTestCheckFunc( testAccTestCheckExists("statuscake_test.google", &test), testAccTestCheckAttributes("statuscake_test.google", &test), @@ -57,7 +57,7 @@ func TestAccStatusCake_withUpdate(t *testing.T) { CheckDestroy: testAccTestCheckDestroy(&test), Steps: []resource.TestStep{ { - Config: testAccTestConfig_basic, + Config: fmt.Sprintf(testAccTestConfig_basic, testContactGroupId), Check: resource.ComposeTestCheckFunc( testAccTestCheckExists("statuscake_test.google", &test), ), @@ -240,7 +240,7 @@ resource "statuscake_test" "google" { test_type = "HTTP" check_rate = 300 timeout = 10 - contact_id = 43402 + contact_id = %d confirmations = 1 trigger_rate = 10 } @@ -284,7 +284,7 @@ resource "statuscake_test" "google" { test_type = "TCP" check_rate = 300 timeout = 10 - contact_id = 43402 + contact_id = %d confirmations = 1 port = 80 } -- cgit v1.2.3 From ed5e20e8578d3a01b41fecf3f075887bd3e24584 Mon Sep 17 00:00:00 2001 From: Mat Schaffer Date: Thu, 1 Feb 2018 16:08:10 +0900 Subject: Mark basic_pass sensitive Avoids showing the password in plan output --- statuscake/resource_statuscaketest.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/statuscake/resource_statuscaketest.go b/statuscake/resource_statuscaketest.go index 459c71a..e824955 100644 --- a/statuscake/resource_statuscaketest.go +++ b/statuscake/resource_statuscaketest.go @@ -123,8 +123,9 @@ func resourceStatusCakeTest() *schema.Resource { }, "basic_pass": { - Type: schema.TypeString, - Optional: true, + Type: schema.TypeString, + Optional: true, + Sensitive: true, }, "public": { -- cgit v1.2.3 From 5c7efdcb8c0a6d3d21f1fb1481bb95e402296d77 Mon Sep 17 00:00:00 2001 From: Mat Schaffer Date: Thu, 1 Feb 2018 16:10:15 +0900 Subject: Remove status codes default In my testing, the default response was `[""]` and having the default provided cause test assertion failures --- statuscake/resource_statuscaketest.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/statuscake/resource_statuscaketest.go b/statuscake/resource_statuscaketest.go index e824955..8e67bcd 100644 --- a/statuscake/resource_statuscaketest.go +++ b/statuscake/resource_statuscaketest.go @@ -176,10 +176,6 @@ func resourceStatusCakeTest() *schema.Resource { "status_codes": { Type: schema.TypeString, Optional: true, - Default: "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " + - "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " + - "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " + - "598, 599", }, "use_jar": { -- cgit v1.2.3 From d01e3cca165df6d7d4a9d2a6cf30ed5a7bed9a4c Mon Sep 17 00:00:00 2001 From: Mat Schaffer Date: Thu, 1 Feb 2018 16:10:53 +0900 Subject: Fix status & uptime assertions They were present in the details but the values were different during my testing. Hopefully this proves true for all future testing. --- statuscake/resource_statuscaketest_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/statuscake/resource_statuscaketest_test.go b/statuscake/resource_statuscaketest_test.go index ea69756..62d6bf9 100644 --- a/statuscake/resource_statuscaketest_test.go +++ b/statuscake/resource_statuscaketest_test.go @@ -76,8 +76,8 @@ func TestAccStatusCake_withUpdate(t *testing.T) { resource.TestCheckResourceAttr("statuscake_test.google", "trigger_rate", "20"), resource.TestCheckResourceAttr("statuscake_test.google", "custom_header", "{ \"Content-Type\": \"application/x-www-form-urlencoded\" }"), resource.TestCheckResourceAttr("statuscake_test.google", "user_agent", "string9988"), - resource.TestCheckResourceAttr("statuscake_test.google", "status", "string22117"), - resource.TestCheckResourceAttr("statuscake_test.google", "uptime", "3498.27"), + resource.TestCheckResourceAttr("statuscake_test.google", "status", "Up"), + resource.TestCheckResourceAttr("statuscake_test.google", "uptime", "0"), resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.#", "3"), resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.0", "string16045"), resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.1", "string19741"), -- cgit v1.2.3 From 89027b6ac2e02ed5097e2aed6e2a5e0b0476d5f9 Mon Sep 17 00:00:00 2001 From: Mat Schaffer Date: Thu, 1 Feb 2018 16:14:33 +0900 Subject: Switch node locations to a set and consider empty string to be empty set Note that we also don't check the individual locations anymore. The key numbers will be randomized and not testing content seems to be the method employed by the AWS provider as well https://github.com/terraform-providers/terraform-provider-aws/blob/81bba6b1f567aed561c6a6a30916504ee0886c68/aws/resource_aws_autoscaling_group_test.go#L404 --- statuscake/resource_statuscaketest.go | 25 +++++++++++++++++-------- statuscake/resource_statuscaketest_test.go | 3 --- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/statuscake/resource_statuscaketest.go b/statuscake/resource_statuscaketest.go index 8e67bcd..b2b0c68 100644 --- a/statuscake/resource_statuscaketest.go +++ b/statuscake/resource_statuscaketest.go @@ -10,16 +10,24 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func castInterfaceToSliceStrings(in interface{}) []string { - input := in.([]interface{}) - res := make([]string, len(input)) +func castSetToSliceStrings(configured []interface{}) []string { + res := make([]string, len(configured)) - for i, element := range input { + for i, element := range configured { res[i] = element.(string) } return res } +// Special handling for node_locations since statuscake will return `[""]` for the empty case +func considerEmptyStringAsEmptyArray(in []string) []string { + if len(in) == 1 && in[0] == "" { + return []string{} + } else { + return in + } +} + func resourceStatusCakeTest() *schema.Resource { return &schema.Resource{ Create: CreateTest, @@ -107,9 +115,10 @@ func resourceStatusCakeTest() *schema.Resource { }, "node_locations": { - Type: schema.TypeList, + Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeString}, Optional: true, + Set: schema.HashString, }, "ping_url": { @@ -219,7 +228,7 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error { UserAgent: d.Get("user_agent").(string), Status: d.Get("status").(string), Uptime: d.Get("uptime").(float64), - NodeLocations: castInterfaceToSliceStrings(d.Get("node_locations")), + NodeLocations: castSetToSliceStrings(d.Get("node_locations").(*schema.Set).List()), PingURL: d.Get("ping_url").(string), BasicUser: d.Get("basic_user").(string), BasicPass: d.Get("basic_pass").(string), @@ -308,7 +317,7 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error { d.Set("user_agent", testResp.UserAgent) d.Set("status", testResp.Status) d.Set("uptime", testResp.Uptime) - if err := d.Set("node_locations", testResp.NodeLocations); err != nil { + if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil { return fmt.Errorf("[WARN] Error setting node locations: %s", err) } d.Set("ping_url", testResp.PingURL) @@ -377,7 +386,7 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { test.UserAgent = v.(string) } if v, ok := d.GetOk("node_locations"); ok { - test.NodeLocations = castInterfaceToSliceStrings(v) + test.NodeLocations = castSetToSliceStrings(v.(*schema.Set).List()) } if v, ok := d.GetOk("ping_url"); ok { test.PingURL = v.(string) diff --git a/statuscake/resource_statuscaketest_test.go b/statuscake/resource_statuscaketest_test.go index 62d6bf9..555cff1 100644 --- a/statuscake/resource_statuscaketest_test.go +++ b/statuscake/resource_statuscaketest_test.go @@ -79,9 +79,6 @@ func TestAccStatusCake_withUpdate(t *testing.T) { resource.TestCheckResourceAttr("statuscake_test.google", "status", "Up"), resource.TestCheckResourceAttr("statuscake_test.google", "uptime", "0"), resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.#", "3"), - resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.0", "string16045"), - resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.1", "string19741"), - resource.TestCheckResourceAttr("statuscake_test.google", "node_locations.2", "string12122"), resource.TestCheckResourceAttr("statuscake_test.google", "ping_url", "string8410"), resource.TestCheckResourceAttr("statuscake_test.google", "basic_user", "string27052"), resource.TestCheckResourceAttr("statuscake_test.google", "basic_pass", "string5659"), -- cgit v1.2.3 From ef20d8d74891bfcfbf17088c4cd9c39cd1e347dd Mon Sep 17 00:00:00 2001 From: Mat Schaffer Date: Thu, 1 Feb 2018 16:15:21 +0900 Subject: Don't attempt to set or verify values which aren't present in the details response The `TestCheckResourceAttr` tests can stay since they rely on the state from the previous apply. --- statuscake/resource_statuscaketest.go | 15 +++++---------- statuscake/resource_statuscaketest_test.go | 18 ------------------ 2 files changed, 5 insertions(+), 28 deletions(-) diff --git a/statuscake/resource_statuscaketest.go b/statuscake/resource_statuscaketest.go index b2b0c68..d7d3f36 100644 --- a/statuscake/resource_statuscaketest.go +++ b/statuscake/resource_statuscaketest.go @@ -314,24 +314,19 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error { d.Set("port", testResp.Port) d.Set("trigger_rate", testResp.TriggerRate) d.Set("custom_header", testResp.CustomHeader) - d.Set("user_agent", testResp.UserAgent) d.Set("status", testResp.Status) d.Set("uptime", testResp.Uptime) if err := d.Set("node_locations", considerEmptyStringAsEmptyArray(testResp.NodeLocations)); err != nil { return fmt.Errorf("[WARN] Error setting node locations: %s", err) } - d.Set("ping_url", testResp.PingURL) - d.Set("basic_user", testResp.BasicUser) - d.Set("basic_pass", testResp.BasicPass) - d.Set("public", testResp.Public) d.Set("logo_image", testResp.LogoImage) - d.Set("branding", testResp.Branding) - d.Set("website_host", testResp.WebsiteHost) - d.Set("virus", testResp.Virus) + // Even after WebsiteHost is set, the API returns "" + // API docs aren't clear on usage will only override state if we get a non-empty value back + if testResp.WebsiteHost != "" { + d.Set("website_host", testResp.WebsiteHost) + } d.Set("find_string", testResp.FindString) d.Set("do_not_find", testResp.DoNotFind) - d.Set("real_browser", testResp.RealBrowser) - d.Set("test_tags", testResp.TestTags) d.Set("status_codes", testResp.StatusCodes) d.Set("use_jar", testResp.UseJar) d.Set("post_raw", testResp.PostRaw) diff --git a/statuscake/resource_statuscaketest_test.go b/statuscake/resource_statuscaketest_test.go index 555cff1..da3a349 100644 --- a/statuscake/resource_statuscaketest_test.go +++ b/statuscake/resource_statuscaketest_test.go @@ -166,8 +166,6 @@ func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestC err = check(key, value, strconv.Itoa(test.TriggerRate)) case "custom_header": err = check(key, value, test.CustomHeader) - case "user_agent": - err = check(key, value, test.UserAgent) case "node_locations": for _, tv := range test.NodeLocations { err = check(key, value, tv) @@ -175,30 +173,14 @@ func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestC return err } } - case "ping_url": - err = check(key, value, test.PingURL) - case "basic_user": - err = check(key, value, test.BasicUser) - case "basic_pass": - err = check(key, value, test.BasicPass) case "public": err = check(key, value, strconv.Itoa(test.Public)) case "logo_image": err = check(key, value, test.LogoImage) - case "branding": - err = check(key, value, strconv.Itoa(test.Branding)) - case "website_host": - err = check(key, value, test.WebsiteHost) - case "virus": - err = check(key, value, strconv.Itoa(test.Virus)) case "find_string": err = check(key, value, test.FindString) case "do_not_find": err = check(key, value, strconv.FormatBool(test.DoNotFind)) - case "real_browser": - err = check(key, value, strconv.Itoa(test.RealBrowser)) - case "test_tags": - err = check(key, value, test.TestTags) case "status_codes": err = check(key, value, test.StatusCodes) case "use_jar": -- cgit v1.2.3