2015-10-02 00:09:13 +02:00
|
|
|
package statuscake
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-10-08 16:36:11 +02:00
|
|
|
"strconv"
|
2015-10-02 00:09:13 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/DreamItGetIT/statuscake"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccStatusCake_basic(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),
|
2016-12-11 20:41:49 +01:00
|
|
|
testAccTestCheckAttributes("statuscake_test.google", &test),
|
2015-10-02 00:09:13 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-08 16:36:11 +02:00
|
|
|
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),
|
2016-12-11 20:41:49 +01:00
|
|
|
testAccTestCheckAttributes("statuscake_test.google", &test),
|
2015-10-08 16:36:11 +02:00
|
|
|
resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"),
|
|
|
|
resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"),
|
2016-12-11 20:41:49 +01:00
|
|
|
resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"),
|
2015-10-08 16:36:11 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc {
|
2015-10-02 00:09:13 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[rn]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("resource not found: %s", rn)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("TestID not set")
|
|
|
|
}
|
|
|
|
|
2015-10-08 16:36:11 +02:00
|
|
|
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 {
|
2016-12-11 20:41:49 +01:00
|
|
|
return fmt.Errorf("error getting test: %s", err)
|
2015-10-08 16:36:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*test = *gotTest
|
2015-10-02 00:09:13 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 20:41:49 +01:00
|
|
|
func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
attrs := s.RootModule().Resources[rn].Primary.Attributes
|
|
|
|
|
|
|
|
check := func(key, stateValue, testValue string) error {
|
|
|
|
if testValue != stateValue {
|
|
|
|
return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)",
|
|
|
|
key, stateValue, testValue)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range attrs {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "website_name":
|
|
|
|
err = check(key, value, test.WebsiteName)
|
|
|
|
case "website_url":
|
|
|
|
err = check(key, value, test.WebsiteURL)
|
|
|
|
case "check_rate":
|
|
|
|
err = check(key, value, strconv.Itoa(test.CheckRate))
|
|
|
|
case "test_type":
|
|
|
|
err = check(key, value, test.TestType)
|
|
|
|
case "paused":
|
|
|
|
err = check(key, value, strconv.FormatBool(test.Paused))
|
|
|
|
case "timeout":
|
|
|
|
err = check(key, value, strconv.Itoa(test.Timeout))
|
|
|
|
case "contact_id":
|
|
|
|
err = check(key, value, strconv.Itoa(test.ContactID))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 16:36:11 +02:00
|
|
|
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
|
|
|
|
}
|
2015-10-02 00:09:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const testAccTestConfig_basic = `
|
|
|
|
resource "statuscake_test" "google" {
|
2016-08-23 16:43:31 +02:00
|
|
|
website_name = "google.com"
|
|
|
|
website_url = "www.google.com"
|
|
|
|
test_type = "HTTP"
|
|
|
|
check_rate = 300
|
|
|
|
contact_id = 12345
|
2015-10-02 00:09:13 +02:00
|
|
|
}
|
|
|
|
`
|
2015-10-08 16:36:11 +02:00
|
|
|
|
|
|
|
const testAccTestConfig_update = `
|
|
|
|
resource "statuscake_test" "google" {
|
2016-08-23 16:43:31 +02:00
|
|
|
website_name = "google.com"
|
|
|
|
website_url = "www.google.com"
|
|
|
|
test_type = "HTTP"
|
|
|
|
check_rate = 500
|
|
|
|
paused = true
|
2015-10-08 16:36:11 +02:00
|
|
|
}
|
|
|
|
`
|