Skip to content

Commit

Permalink
Fixing synthetics alert condition acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgsiebers committed Feb 20, 2019
1 parent e3a9a16 commit 649908c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 22 deletions.
63 changes: 63 additions & 0 deletions newrelic/data_source_newrelic_synthetics_monitor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package newrelic

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

var (
expectedMonitorName string = fmt.Sprintf("tf-test-synthetic-%s", acctest.RandString(5))
)

func TestAccNewRelicSyntheticsMonitorDataSource_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckNewRelicSyntheticsDataSourceConfig(expectedMonitorName),
Check: resource.ComposeTestCheckFunc(
testAccNewRelicSyntheticsDataSource("data.newrelic_synthetics_monitor.bar"),
),
},
},
})
}

func testAccNewRelicSyntheticsDataSource(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r := s.RootModule().Resources[n]
a := r.Primary.Attributes

if a["id"] == "" {
return fmt.Errorf("Expected to read synthetics monitor data from New Relic")
}

if a["name"] != expectedMonitorName {
return fmt.Errorf("Expected the synthetics monitor name to be: %s, but got: %s", expectedMonitorName, a["name"])
}
return nil
}
}

func testAccCheckNewRelicSyntheticsDataSourceConfig(name string) string {
return fmt.Sprintf(`
resource "newrelic_synthetics_monitor" "foo" {
name = "%[1]s"
type = "SIMPLE"
frequency = 15
status = "DISABLED"
locations = ["AWS_US_EAST_1"]
uri = "https://google.com"
}
data "newrelic_synthetics_monitor" "bar" {
name = "${newrelic_synthetics_monitor.foo.name}"
}
`, name)
}
62 changes: 40 additions & 22 deletions newrelic/resource_newrelic_synthetics_alert_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ func TestAccNewRelicSyntheticsAlertCondition_Basic(t *testing.T) {
testAccCheckNewRelicSyntheticsAlertConditionExists("newrelic_synthetics_alert_condition.foo"),
resource.TestCheckResourceAttr(
"newrelic_synthetics_alert_condition.foo", "name", fmt.Sprintf("tf-test-%s", rName)),
resource.TestCheckResourceAttr(
"newrelic_synthetics_alert_condition.foo", "policy_id", "0"),
resource.TestCheckResourceAttr(
"newrelic_synthetics_alert_condition.foo", "monitor_id", "derp"),
resource.TestCheckResourceAttr(
"newrelic_synthetics_alert_condition.foo", "runbook_url", "www.example.com"),
resource.TestCheckResourceAttr(
"newrelic_synthetics_alert_condition.foo", "enabled", "true"),
),
},
{
Config: testAccCheckNewRelicSyntheticsAlertConditionUpdated(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckNewRelicSyntheticsAlertConditionExists("newrelic_synthetics_alert_condition.foo"),
resource.TestCheckResourceAttr(
"newrelic_synthetics_alert_condition.foo", "name", fmt.Sprintf("tf-test-%s", rName)),
resource.TestCheckResourceAttr(
"newrelic_synthetics_alert_condition.foo", "runbook_url", "www.example2.com"),
resource.TestCheckResourceAttr(
"newrelic_synthetics_alert_condition.foo", "enabled", "false"),
),
},
},
})
}
Expand Down Expand Up @@ -96,41 +104,51 @@ func testAccCheckNewRelicSyntheticsAlertConditionExists(n string) resource.TestC
func testAccCheckNewRelicSyntheticsAlertConditionConfig(rName string) string {
return fmt.Sprintf(`
data "newrelic_synthetics_monitor" "bar" {
name = "%[2]s"
resource "newrelic_synthetics_monitor" "bar" {
name = "tf-test-synthetic-%[1]s"
type = "SIMPLE"
frequency = 15
status = "DISABLED"
locations = ["AWS_US_EAST_1"]
uri = "https://google.com"
}
resource "newrelic_alert_policy" "foo" {
name = "tf-test-%[1]s"
name = "tf-test-%[1]s"
}
resource "newrelic_synthetics_alert_condition" "foo" {
policy_id = "${newrelic_alert_policy.foo.id}"
policy_id = "${newrelic_alert_policy.foo.id}"
name = "tf-test-%[1]s"
monitor_id = "${data.newrelic_synthetics_monitor.bar.id}"
runbook_url = "https://foo.example.com"
monitor_id = "${newrelic_synthetics_monitor.bar.id}"
runbook_url = "www.example.com"
enabled = "true"
}
`, rName, testAccExpectedApplicationName)
`, rName)
}

func testAccCheckNewRelicSyntheticsAlertConditionConfigUpdated(rName string) string {
func testAccCheckNewRelicSyntheticsAlertConditionUpdated(rName string) string {
return fmt.Sprintf(`
data "newrelic_synthetics_monitor" "bar" {
name = "%[2]s"
resource "newrelic_synthetics_monitor" "bar" {
name = "tf-test-synthetic-%[1]s"
type = "SIMPLE"
frequency = 15
status = "DISABLED"
locations = ["AWS_US_EAST_1"]
uri = "https://google.com"
}
resource "newrelic_alert_policy" "foo" {
name = "tf-test-%[1]s"
name = "tf-test-%[1]s"
}
resource "newrelic_synthetics_alert_condition" "foo" {
policy_id = "${newrelic_alert_policy.foo.id}"
name = "tf-test-updated-%[1]s"
monitor_id = "${data.newrelic_synthetics_monitor.bar.id}"
runbook_url = "https://bar.example.com"
policy_id = "${newrelic_alert_policy.foo.id}"
name = "tf-test-%[1]s"
monitor_id = "${newrelic_synthetics_monitor.bar.id}"
runbook_url = "www.example2.com"
enabled = "false"
}
`, rName, testAccExpectedApplicationName)
`, rName)
}

0 comments on commit 649908c

Please sign in to comment.