From ee4d29ce97e0ec3f6dbd332b63e0f6f5c61f86de Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 8 Oct 2015 15:36:11 +0100 Subject: [PATCH] Added some acceptance tests for the statuscake provider --- builtin/providers/statuscake/provider.go | 6 +- .../statuscake/resource_statuscaketest.go | 16 ++-- .../resource_statuscaketest_test.go | 82 ++++++++++++++----- 3 files changed, 72 insertions(+), 32 deletions(-) diff --git a/builtin/providers/statuscake/provider.go b/builtin/providers/statuscake/provider.go index 7d96e0ebf..abca37698 100644 --- a/builtin/providers/statuscake/provider.go +++ b/builtin/providers/statuscake/provider.go @@ -1,7 +1,7 @@ package statuscake import ( - wtf "github.com/DreamItGetIT/statuscake" + "github.com/DreamItGetIT/statuscake" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" ) @@ -32,9 +32,9 @@ func Provider() terraform.ResourceProvider { } func providerConfigure(d *schema.ResourceData) (interface{}, error) { - auth := wtf.Auth{ + auth := statuscake.Auth{ Username: d.Get("username").(string), Apikey: d.Get("apikey").(string), } - return wtf.New(auth) + return statuscake.New(auth) } diff --git a/builtin/providers/statuscake/resource_statuscaketest.go b/builtin/providers/statuscake/resource_statuscaketest.go index 17488a565..7f5ff67bb 100644 --- a/builtin/providers/statuscake/resource_statuscaketest.go +++ b/builtin/providers/statuscake/resource_statuscaketest.go @@ -6,7 +6,7 @@ import ( "log" - wtf "github.com/DreamItGetIT/statuscake" + "github.com/DreamItGetIT/statuscake" "github.com/hashicorp/terraform/helper/schema" ) @@ -58,9 +58,9 @@ func resourceStatusCakeTest() *schema.Resource { } func CreateTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*wtf.Client) + client := meta.(*statuscake.Client) - newTest := &wtf.Test{ + newTest := &statuscake.Test{ WebsiteName: d.Get("website_name").(string), WebsiteURL: d.Get("website_url").(string), TestType: d.Get("test_type").(string), @@ -81,7 +81,7 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error { } func UpdateTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*wtf.Client) + client := meta.(*statuscake.Client) params := getStatusCakeTestInput(d) @@ -94,7 +94,7 @@ func UpdateTest(d *schema.ResourceData, meta interface{}) error { } func DeleteTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*wtf.Client) + client := meta.(*statuscake.Client) testId, parseErr := strconv.Atoi(d.Id()) if parseErr != nil { @@ -110,7 +110,7 @@ func DeleteTest(d *schema.ResourceData, meta interface{}) error { } func ReadTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*wtf.Client) + client := meta.(*statuscake.Client) testId, parseErr := strconv.Atoi(d.Id()) if parseErr != nil { @@ -125,12 +125,12 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error { return nil } -func getStatusCakeTestInput(d *schema.ResourceData) *wtf.Test { +func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { testId, parseErr := strconv.Atoi(d.Id()) if parseErr != nil { log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) } - test := &wtf.Test{ + test := &statuscake.Test{ TestID: testId, } if v, ok := d.GetOk("website_name"); ok { diff --git a/builtin/providers/statuscake/resource_statuscaketest_test.go b/builtin/providers/statuscake/resource_statuscaketest_test.go index 8bbd4b5ba..f5b47e470 100644 --- a/builtin/providers/statuscake/resource_statuscaketest_test.go +++ b/builtin/providers/statuscake/resource_statuscaketest_test.go @@ -2,6 +2,7 @@ package statuscake import ( "fmt" + "strconv" "testing" "github.com/DreamItGetIT/statuscake" @@ -27,7 +28,34 @@ func TestAccStatusCake_basic(t *testing.T) { }) } -func testAccTestCheckExists(rn string, project *statuscake.Test) resource.TestCheckFunc { +func TestAccStatusCake_withUpdate(t *testing.T) { + var test statuscake.Test + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccTestCheckDestroy(&test), + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccTestConfig_basic, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + ), + }, + + resource.TestStep{ + Config: testAccTestConfig_update, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"), + resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"), + ), + }, + }, + }) +} + +func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[rn] if !ok { @@ -38,31 +66,33 @@ func testAccTestCheckExists(rn string, project *statuscake.Test) resource.TestCh return fmt.Errorf("TestID not set") } - // client := testAccProvider.Meta().(*statuscake.Client) - // gotProject, err := client.GetProject(rs.Primary.ID) - // if err != nil { - // return fmt.Errorf("error getting project: %s", err) - // } - // - // *project = *gotProject + client := testAccProvider.Meta().(*statuscake.Client) + testId, parseErr := strconv.Atoi(rs.Primary.ID) + if parseErr != nil { + return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr) + } + + gotTest, err := client.Tests().Detail(testId) + if err != nil { + return fmt.Errorf("error getting project: %s", err) + } + + *test = *gotTest return nil } } -func testAccTestCheckDestroy(project *statuscake.Test) resource.TestCheckFunc { - // return func(s *terraform.State) error { - // client := testAccProvider.Meta().(*statuscake.Client) - // // _, err := client.Tests().All() - // // if err == nil { - // // return fmt.Errorf("test still exists") - // // } - // // if _, ok := err.(*statuscake.NotFoundError); !ok { - // // return fmt.Errorf("got something other than NotFoundError (%v) when getting test", err) - // // } - // - // return nil - // } +func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc { + return func(s *terraform.State) error { + client := testAccProvider.Meta().(*statuscake.Client) + err := client.Tests().Delete(test.TestID) + if err == nil { + return fmt.Errorf("test still exists") + } + + return nil + } return nil } @@ -74,3 +104,13 @@ resource "statuscake_test" "google" { check_rate = 300 } ` + +const testAccTestConfig_update = ` +resource "statuscake_test" "google" { + website_name = "google.com" + website_url = "www.google.com" + test_type = "HTTP" + check_rate = 500 + paused = true +} +`