Skip to content

Commit

Permalink
chore: correct test cases and update error message
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-new-relic committed Apr 23, 2024
1 parent e6c4c87 commit cb46bcb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 57 deletions.
4 changes: 1 addition & 3 deletions newrelic/resource_helpers_synthetics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package newrelic

import (
"encoding/base64"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -456,15 +455,14 @@ func getRuntimeValuesFromEntityTags(tags []entities.EntityTag) (runtimeType stri
}

func getMonitorID(monitorGUID string) (string, error) {

decodedGUID, err := base64.RawStdEncoding.DecodeString(monitorGUID)
if err != nil {
return "", err
}
splitGUID := strings.Split(string(decodedGUID), "|")

if len(splitGUID) < 4 {
return "", errors.New("invalid monitor ID")
return "", fmt.Errorf("invalid monitor GUID '%s'", monitorGUID)
}
monitorID := splitGUID[3]
return monitorID, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ func TestAccNewRelicSyntheticsMultiLocationAlertCondition_Basic(t *testing.T) {
})
}

func TestAccNewRelicSyntheticsMultiLocationAlertCondition_InvalidEntities(t *testing.T) {
rName := generateNameForIntegrationTestResource()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicMultiLocationAlertConditionDestroy,
Steps: []resource.TestStep{
{
Config: testAccNewRelicSyntheticsMultiLocationConditionConfigInvalidEntities(rName, "1", "2"),
ExpectError: regexp.MustCompile(`invalid monitor GUID`),
},
},
})
}

func testAccCheckNewRelicSyntheticsMultiLocationAlertConditionExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
providerConfig := testAccProvider.Meta().(*ProviderConfig)
Expand Down Expand Up @@ -82,6 +98,33 @@ func testAccCheckNewRelicSyntheticsMultiLocationAlertConditionExists(n string) r
}
}

func testAccCheckNewRelicMultiLocationAlertConditionDestroy(s *terraform.State) error {
providerConfig := testAccProvider.Meta().(*ProviderConfig)
client := providerConfig.NewClient

for _, r := range s.RootModule().Resources {
if r.Type != "newrelic_nrql_alert_condition" {
continue
}

var err error

ids, err := parseHashedIDs(r.Primary.ID)
if err != nil {
return err
}

conditionID := ids[1]
policyID := ids[0]

if _, err = client.Alerts.GetMultiLocationSyntheticsCondition(policyID, conditionID); err == nil {
return fmt.Errorf("Synthetics multi-location condition still exists") //nolint:golint
}
}

return nil
}

func testAccNewRelicSyntheticsMultiLocationConditionConfigBasic(
name string,
criticalThreshold string,
Expand Down Expand Up @@ -127,52 +170,10 @@ resource "newrelic_synthetics_multilocation_alert_condition" "foo" {
`, name, criticalThreshold, warningThreshold, conditionalAttrs)
}

func testAccCheckNewRelicMultiLocationAlertConditionDestroy(s *terraform.State) error {
providerConfig := testAccProvider.Meta().(*ProviderConfig)
client := providerConfig.NewClient

for _, r := range s.RootModule().Resources {
if r.Type != "newrelic_nrql_alert_condition" {
continue
}

var err error

ids, err := parseHashedIDs(r.Primary.ID)
if err != nil {
return err
}

conditionID := ids[1]
policyID := ids[0]

if _, err = client.Alerts.GetMultiLocationSyntheticsCondition(policyID, conditionID); err == nil {
return fmt.Errorf("Synthetics multi-location condition still exists") //nolint:golint
}
}

return nil
}

func TestAccNewRelicSyntheticsMultiLocationAlertCondition_InvalidEntities(t *testing.T) {
resourceName := "newrelic_synthetics_multilocation_alert_condition.foo"
rName := generateNameForIntegrationTestResource()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNewRelicMultiLocationAlertConditionDestroy,
Steps: []resource.TestStep{
{
Config: testAccNewRelicSyntheticsMultiLocationConditionConfigInvalidEntities(rName),
ExpectError: regexp.MustCompile(`invalid monitor ID`),
},
},
})
}

func testAccNewRelicSyntheticsMultiLocationConditionConfigInvalidEntities(
name string,
criticalThreshold string,
warningThreshold string,
) string {
return fmt.Sprintf(`
resource "newrelic_alert_policy" "policy" {
Expand All @@ -189,22 +190,22 @@ resource "newrelic_synthetics_monitor" "monitor" {
}
resource "newrelic_synthetics_multilocation_alert_condition" "foo" {
policy_id = newrelic_alert_policy.policy.id
policy_id = newrelic_alert_policy.policy.id
name = "tf-test-%[1]s"
runbook_url = "https://foo.example.com"
enabled = true
violation_time_limit_seconds = "3600"
entities = "1234" // Providing an invalid value here
entities = ["1234"]
// Providing an invalid value here
critical {
threshold = "1.0"
}
critical {
threshold = %[2]s
}
warning {
threshold = "0.5"
}
warning {
threshold = %[3]s
}
}
`, name)
`, name, criticalThreshold, warningThreshold)
}

0 comments on commit cb46bcb

Please sign in to comment.