Skip to content

Commit

Permalink
Alerting Contact Points: Use OpenAPI for tests (#1248)
Browse files Browse the repository at this point in the history
* Alerting Contact Points: Use OpenAPI for tests
Depends on #1247
When the ID is the contact point's name, it makes it easy to write the test helper

* Rename badly named func
  • Loading branch information
julienduchesne authored Jan 11, 2024
1 parent a935506 commit d08b00e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 101 deletions.
15 changes: 15 additions & 0 deletions internal/resources/grafana/common_check_exists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/go-openapi/runtime"
goapi "github.com/grafana/grafana-openapi-client-go/client"
"github.com/grafana/grafana-openapi-client-go/client/api_keys"
"github.com/grafana/grafana-openapi-client-go/client/provisioning"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/grafana/terraform-provider-grafana/internal/resources/grafana"
Expand All @@ -19,6 +20,20 @@ import (
// Helpers that check if a resource exists or doesn't. To define a new one, use the newCheckExistsHelper function.
// A function that gets a resource by their Terraform ID is required.
var (
alertingContactPointCheckExists = newCheckExistsHelper(
func(p *models.ContactPoints) string { return (*p)[0].Name },
func(client *goapi.GrafanaHTTPAPI, id string) (*models.ContactPoints, error) {
params := provisioning.NewGetContactpointsParams().WithName(&id)
resp, err := client.Provisioning.GetContactpoints(params)
if err != nil {
return nil, err
}
if len(resp.Payload) == 0 {
return nil, &runtime.APIError{Code: 404}
}
return &resp.Payload, nil
},
)
alertingMessageTemplateCheckExists = newCheckExistsHelper(
func(t *models.NotificationTemplate) string { return t.Name },
func(client *goapi.GrafanaHTTPAPI, id string) (*models.NotificationTemplate, error) {
Expand Down
138 changes: 37 additions & 101 deletions internal/resources/grafana/resource_alerting_contact_point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"testing"

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

Expand All @@ -17,18 +17,18 @@ import (
func TestAccContactPoint_basic(t *testing.T) {
testutils.CheckOSSTestsEnabled(t, ">=9.0.0")

var points []gapi.ContactPoint
var points models.ContactPoints

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
// Implicitly tests deletion.
CheckDestroy: testContactPointCheckDestroy(points),
CheckDestroy: alertingContactPointCheckExists.destroyed(&points, nil),
Steps: []resource.TestStep{
// Test creation.
{
Config: testutils.TestAccExample(t, "resources/grafana_contact_point/resource.tf"),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.my_contact_point", &points, 1),
checkAlertingContactPointExistsWithLength("grafana_contact_point.my_contact_point", &points, 1),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "name", "My Contact Point"),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "email.#", "1"),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "email.0.disable_resolve_message", "false"),
Expand All @@ -49,7 +49,7 @@ func TestAccContactPoint_basic(t *testing.T) {
"company.org": "user.net",
}),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.my_contact_point", &points, 1),
checkAlertingContactPointExistsWithLength("grafana_contact_point.my_contact_point", &points, 1),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "email.#", "1"),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "email.0.addresses.0", "one@user.net"),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "email.0.addresses.1", "two@user.net"),
Expand All @@ -61,10 +61,9 @@ func TestAccContactPoint_basic(t *testing.T) {
"My Contact Point": "A Different Contact Point",
}),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.my_contact_point", &points, 1),
checkAlertingContactPointExistsWithLength("grafana_contact_point.my_contact_point", &points, 1),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "name", "A Different Contact Point"),
resource.TestCheckResourceAttr("grafana_contact_point.my_contact_point", "email.#", "1"),
testContactPointCheckAllDestroy("My Contact Point"),
),
},
},
Expand All @@ -74,19 +73,19 @@ func TestAccContactPoint_basic(t *testing.T) {
func TestAccContactPoint_compound(t *testing.T) {
testutils.CheckOSSTestsEnabled(t, ">=9.0.0")

var points []gapi.ContactPoint
var points models.ContactPoints

// TODO: Make parallelizable
resource.Test(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
// Implicitly tests deletion.
CheckDestroy: testContactPointCheckDestroy(points),
CheckDestroy: alertingContactPointCheckExists.destroyed(&points, nil),
Steps: []resource.TestStep{
// Test creation.
{
Config: testutils.TestAccExample(t, "resources/grafana_contact_point/_acc_compound_receiver.tf"),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.compound_contact_point", &points, 2),
checkAlertingContactPointExistsWithLength("grafana_contact_point.compound_contact_point", &points, 2),
resource.TestCheckResourceAttr("grafana_contact_point.compound_contact_point", "name", "Compound Contact Point"),
resource.TestCheckResourceAttr("grafana_contact_point.compound_contact_point", "email.#", "2"),
),
Expand Down Expand Up @@ -125,7 +124,7 @@ func TestAccContactPoint_compound(t *testing.T) {
"one": "asdf",
}),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.compound_contact_point", &points, 2),
checkAlertingContactPointExistsWithLength("grafana_contact_point.compound_contact_point", &points, 2),
resource.TestCheckResourceAttr("grafana_contact_point.compound_contact_point", "email.#", "2"),
resource.TestCheckResourceAttr("grafana_contact_point.compound_contact_point", "email.0.addresses.0", "asdf@company.org"),
),
Expand All @@ -134,7 +133,7 @@ func TestAccContactPoint_compound(t *testing.T) {
{
Config: testutils.TestAccExample(t, "resources/grafana_contact_point/_acc_compound_receiver_added.tf"),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.compound_contact_point", &points, 3),
checkAlertingContactPointExistsWithLength("grafana_contact_point.compound_contact_point", &points, 3),
resource.TestCheckResourceAttr("grafana_contact_point.compound_contact_point", "email.#", "3"),
resource.TestCheckResourceAttr("grafana_contact_point.compound_contact_point", "email.0.addresses.0", "five@company.org"),
resource.TestCheckResourceAttr("grafana_contact_point.compound_contact_point", "email.1.addresses.0", "one@company.org"),
Expand All @@ -145,7 +144,7 @@ func TestAccContactPoint_compound(t *testing.T) {
{
Config: testutils.TestAccExample(t, "resources/grafana_contact_point/_acc_compound_receiver_subtracted.tf"),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.compound_contact_point", &points, 1),
checkAlertingContactPointExistsWithLength("grafana_contact_point.compound_contact_point", &points, 1),
resource.TestCheckResourceAttr("grafana_contact_point.compound_contact_point", "email.#", "1"),
resource.TestCheckResourceAttr("grafana_contact_point.compound_contact_point", "email.0.addresses.0", "one@company.org"),
),
Expand All @@ -156,10 +155,9 @@ func TestAccContactPoint_compound(t *testing.T) {
"Compound Contact Point": "A Different Contact Point",
}),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.compound_contact_point", &points, 2),
checkAlertingContactPointExistsWithLength("grafana_contact_point.compound_contact_point", &points, 2),
resource.TestCheckResourceAttr("grafana_contact_point.compound_contact_point", "name", "A Different Contact Point"),
resource.TestCheckResourceAttr("grafana_contact_point.compound_contact_point", "email.#", "2"),
testContactPointCheckAllDestroy("Compound Contact Point"),
),
},
},
Expand All @@ -169,18 +167,18 @@ func TestAccContactPoint_compound(t *testing.T) {
func TestAccContactPoint_notifiers(t *testing.T) {
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

var points []gapi.ContactPoint
var points models.ContactPoints

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
// Implicitly tests deletion.
CheckDestroy: testContactPointCheckDestroy(points),
CheckDestroy: alertingContactPointCheckExists.destroyed(&points, nil),
Steps: []resource.TestStep{
// Test creation.
{
Config: testutils.TestAccExample(t, "resources/grafana_contact_point/_acc_receiver_types.tf"),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.receiver_types", &points, 19),
checkAlertingContactPointExistsWithLength("grafana_contact_point.receiver_types", &points, 19),
// alertmanager
resource.TestCheckResourceAttr("grafana_contact_point.receiver_types", "alertmanager.#", "1"),
resource.TestCheckResourceAttr("grafana_contact_point.receiver_types", "alertmanager.0.url", "http://my-am"),
Expand Down Expand Up @@ -339,7 +337,7 @@ func TestAccContactPoint_notifiers(t *testing.T) {
{
Config: testutils.TestAccExample(t, "resources/grafana_contact_point/_acc_default_settings.tf"),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.default_settings", &points, 1),
checkAlertingContactPointExistsWithLength("grafana_contact_point.default_settings", &points, 1),
resource.TestCheckResourceAttr("grafana_contact_point.default_settings", "slack.#", "1"),
resource.TestCheckNoResourceAttr("grafana_contact_point.default_settings", "slack.endpoint_url"),
func(s *terraform.State) error {
Expand Down Expand Up @@ -371,18 +369,18 @@ func TestAccContactPoint_notifiers(t *testing.T) {
func TestAccContactPoint_notifiers10_2(t *testing.T) {
testutils.CheckOSSTestsEnabled(t, ">=10.2.0")

var points []gapi.ContactPoint
var points models.ContactPoints

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
// Implicitly tests deletion.
CheckDestroy: testContactPointCheckDestroy(points),
CheckDestroy: alertingContactPointCheckExists.destroyed(&points, nil),
Steps: []resource.TestStep{
// Test creation.
{
Config: testutils.TestAccExample(t, "resources/grafana_contact_point/_acc_receiver_types_10_2.tf"),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.receiver_types", &points, 1),
checkAlertingContactPointExistsWithLength("grafana_contact_point.receiver_types", &points, 1),
// oncall
resource.TestCheckResourceAttr("grafana_contact_point.receiver_types", "oncall.#", "1"),
resource.TestCheckResourceAttr("grafana_contact_point.receiver_types", "oncall.0.url", "http://my-url"),
Expand All @@ -401,18 +399,18 @@ func TestAccContactPoint_notifiers10_2(t *testing.T) {
func TestAccContactPoint_notifiers10_3(t *testing.T) {
testutils.CheckCloudInstanceTestsEnabled(t) // TODO: Switch to `testutils.CheckOSSTestsEnabled(t, ">=10.3.0")` once 10.3 is released.

var points []gapi.ContactPoint
var points models.ContactPoints

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
// Implicitly tests deletion.
CheckDestroy: testContactPointCheckDestroy(points),
CheckDestroy: alertingContactPointCheckExists.destroyed(&points, nil),
Steps: []resource.TestStep{
// Test creation.
{
Config: testutils.TestAccExample(t, "resources/grafana_contact_point/_acc_receiver_types_10_3.tf"),
Check: resource.ComposeTestCheckFunc(
testContactPointCheckExists("grafana_contact_point.receiver_types", &points, 1),
checkAlertingContactPointExistsWithLength("grafana_contact_point.receiver_types", &points, 1),
// opsgenie
resource.TestCheckResourceAttr("grafana_contact_point.receiver_types", "opsgenie.#", "1"),
resource.TestCheckResourceAttr("grafana_contact_point.receiver_types", "opsgenie.0.url", "http://opsgenie-api"),
Expand Down Expand Up @@ -440,87 +438,25 @@ func TestAccContactPoint_empty(t *testing.T) {
Steps: []resource.TestStep{
// Test creation.
{
Config: testAccEmptyContactPoint,
Config: `
resource "grafana_contact_point" "dev_null" {
name = "empty-test"
}
`,
ExpectError: regexp.MustCompile(`Missing required argument`),
},
},
})
}

func testContactPointCheckExists(rname string, pts *[]gapi.ContactPoint, expCount int) resource.TestCheckFunc {
return func(s *terraform.State) error {
resource, ok := s.RootModule().Resources[rname]
if !ok {
return fmt.Errorf("resource not found: %s, resources: %#v", rname, s.RootModule().Resources)
}

name, ok := resource.Primary.Attributes["name"]
if !ok {
return fmt.Errorf("resource name not set")
}

client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
points, err := client.ContactPointsByName(name)
if err != nil {
return fmt.Errorf("error getting resource: %w", err)
}

// Work around query parameters not being supported in some older patch versions.
filtered := make([]gapi.ContactPoint, 0, len(points))
for i := range points {
if points[i].Name == name {
filtered = append(filtered, points[i])
}
}

if len(filtered) != expCount {
return fmt.Errorf("wrong number of contact points on the server, expected %d but got %#v", expCount, filtered)
}

*pts = points
return nil
}
}

func testContactPointCheckDestroy(points []gapi.ContactPoint) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
for _, p := range points {
_, err := client.ContactPoint(p.UID)
if err == nil {
return fmt.Errorf("contact point still exists on the server")
func checkAlertingContactPointExistsWithLength(rn string, v *models.ContactPoints, expectedLength int) resource.TestCheckFunc {
return resource.ComposeTestCheckFunc(
alertingContactPointCheckExists.exists(rn, v),
func(s *terraform.State) error {
if len(*v) != expectedLength {
return fmt.Errorf("expected %d contact points, got %d", expectedLength, len(*v))
}
}
points = []gapi.ContactPoint{}
return nil
}
}

func testContactPointCheckAllDestroy(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
points, err := client.ContactPointsByName(name)
if err != nil {
return fmt.Errorf("error getting resource: %w", err)
}

// Work around query parameters not being supported in some older patch versions.
filtered := make([]gapi.ContactPoint, 0, len(points))
for i := range points {
if points[i].Name == name {
filtered = append(filtered, points[i])
}
}

if len(filtered) > 0 {
return fmt.Errorf("contact points still exist on the server: %#v", filtered)
}
return nil
}
}

const testAccEmptyContactPoint = `
resource "grafana_contact_point" "dev_null" {
name = "empty-test"
return nil
},
)
}
`

0 comments on commit d08b00e

Please sign in to comment.