Skip to content

Commit

Permalink
r/datadog_monitor: refactoring the tests to match the others
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Feb 4, 2023
1 parent ed9b366 commit 97423af
Showing 1 changed file with 36 additions and 35 deletions.
71 changes: 36 additions & 35 deletions internal/services/datadog/datadog_monitors_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,26 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type DatadogMonitorResource struct{}
type DatadogMonitorResource struct {
datadogApiKey string
datadogApplicationKey string
}

func TestAccDatadogMonitor_basic(t *testing.T) {
if os.Getenv("ARM_TEST_DATADOG_API_KEY") == "" || os.Getenv("ARM_TEST_DATADOG_APPLICATION_KEY") == "" {
t.Skip("Skipping as ARM_TEST_DATADOG_API_KEY and/or ARM_TEST_DATADOG_APPLICATION_KEY are not specified")
return
func (r *DatadogMonitorResource) populateFromEnvironment(t *testing.T) {
if os.Getenv("ARM_TEST_DATADOG_API_KEY") == "" {
t.Skip("Skipping as ARM_TEST_DATADOG_API_KEY is not specified")
}
if os.Getenv("ARM_TEST_DATADOG_APPLICATION_KEY") == "" {
t.Skip("Skipping as ARM_TEST_DATADOG_APPLICATION_KEY is not specified")
}
r.datadogApiKey = os.Getenv("ARM_TEST_DATADOG_API_KEY")
r.datadogApplicationKey = os.Getenv("ARM_TEST_DATADOG_APPLICATION_KEY")
}

func TestAccDatadogMonitor_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_datadog_monitor", "test")
r := DatadogMonitorResource{}
r.populateFromEnvironment(t)
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Expand All @@ -48,12 +59,9 @@ func TestAccDatadogMonitor_basic(t *testing.T) {
}

func TestAccDatadogMonitor_requiresImport(t *testing.T) {
if os.Getenv("ARM_TEST_DATADOG_API_KEY") == "" || os.Getenv("ARM_TEST_DATADOG_APPLICATION_KEY") == "" {
t.Skip("Skipping as ARM_TEST_DATADOG_API_KEY and/or ARM_TEST_DATADOG_APPLICATION_KEY are not specified")
return
}
data := acceptance.BuildTestData(t, "azurerm_datadog_monitor", "test")
r := DatadogMonitorResource{}
r.populateFromEnvironment(t)
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Expand All @@ -66,12 +74,9 @@ func TestAccDatadogMonitor_requiresImport(t *testing.T) {
}

func TestAccDatadogMonitor_complete(t *testing.T) {
if os.Getenv("ARM_TEST_DATADOG_API_KEY") == "" || os.Getenv("ARM_TEST_DATADOG_APPLICATION_KEY") == "" {
t.Skip("Skipping as ARM_TEST_DATADOG_API_KEY and/or ARM_TEST_DATADOG_APPLICATION_KEY are not specified")
return
}
data := acceptance.BuildTestData(t, "azurerm_datadog_monitor", "test")
r := DatadogMonitorResource{}
r.populateFromEnvironment(t)
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Expand All @@ -96,12 +101,9 @@ func TestAccDatadogMonitor_complete(t *testing.T) {
}

func TestAccDatadogMonitor_update(t *testing.T) {
if os.Getenv("ARM_TEST_DATADOG_API_KEY") == "" || os.Getenv("ARM_TEST_DATADOG_APPLICATION_KEY") == "" {
t.Skip("Skipping as ARM_TEST_DATADOG_API_KEY and/or ARM_TEST_DATADOG_APPLICATION_KEY are not specified")
return
}
data := acceptance.BuildTestData(t, "azurerm_datadog_monitor", "test")
r := DatadogMonitorResource{}
r.populateFromEnvironment(t)
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Expand Down Expand Up @@ -180,24 +182,25 @@ func (r DatadogMonitorResource) Exists(ctx context.Context, client *clients.Clie

func (r DatadogMonitorResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctest-datadog-%d"
name = "acctest-datadogrg-%d"
location = "%s"
}
`, data.RandomInteger, data.Locations.Primary)
}

func (r DatadogMonitorResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
provider "azurerm" {
features {}
}
%s
resource "azurerm_datadog_monitor" "test" {
name = "acctest-datadog-%d"
name = "acctest-datadog-%s"
resource_group_name = azurerm_resource_group.test.name
location = "WEST US 2"
location = azurerm_resource_group.test.location
datadog_organization {
api_key = %q
application_key = %q
Expand All @@ -211,7 +214,7 @@ resource "azurerm_datadog_monitor" "test" {
type = "SystemAssigned"
}
}
`, r.template(data), data.RandomInteger%100, os.Getenv("ARM_TEST_DATADOG_API_KEY"), os.Getenv("ARM_TEST_DATADOG_APPLICATION_KEY"))
`, r.template(data), data.RandomString, os.Getenv("ARM_TEST_DATADOG_API_KEY"), os.Getenv("ARM_TEST_DATADOG_APPLICATION_KEY"))
}

func (r DatadogMonitorResource) update(data acceptance.TestData) string {
Expand All @@ -220,14 +223,12 @@ provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctest-datadog-%d"
location = "%s"
}
%s
resource "azurerm_datadog_monitor" "test" {
name = "acctest-datadog-%d"
resource_group_name = azurerm_resource_group.test.name
location = "WEST US 2"
location = azurerm_resource_group.test.location
datadog_organization {
api_key = %q
application_key = %q
Expand All @@ -245,15 +246,15 @@ resource "azurerm_datadog_monitor" "test" {
ENV = "Test"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger%100, os.Getenv("ARM_TEST_DATADOG_API_KEY"), os.Getenv("ARM_TEST_DATADOG_APPLICATION_KEY"))
`, r.template(data), data.RandomString, r.datadogApiKey, r.datadogApplicationKey)
}

func (r DatadogMonitorResource) requiresImport(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_datadog_monitor" "import" {
name = azurerm_datadog_monitor.test.name
resource_group_name = azurerm_resource_group.test.name
resource_group_name = azurerm_datadog_monitor.test.resource_group_name
location = azurerm_datadog_monitor.test.location
datadog_organization {
api_key = %q
Expand All @@ -268,14 +269,14 @@ resource "azurerm_datadog_monitor" "import" {
type = "SystemAssigned"
}
}
`, r.basic(data), os.Getenv("ARM_TEST_DATADOG_API_KEY"), os.Getenv("ARM_TEST_DATADOG_APPLICATION_KEY"))
`, r.basic(data), r.datadogApiKey, r.datadogApplicationKey)
}

func (r DatadogMonitorResource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_datadog_monitor" "test" {
name = "acctest-datadog-%d"
name = "acctest-datadog-%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
datadog_organization {
Expand All @@ -300,5 +301,5 @@ resource "azurerm_datadog_monitor" "test" {
ENV = "Test"
}
}
`, r.template(data), data.RandomInteger%100, os.Getenv("ARM_TEST_DATADOG_API_KEY"), os.Getenv("ARM_TEST_DATADOG_APPLICATION_KEY"))
`, r.template(data), data.RandomString, r.datadogApiKey, r.datadogApplicationKey)
}

0 comments on commit 97423af

Please sign in to comment.