Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor and add tests + cassettes #539

Merged
merged 12 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
445 changes: 445 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardAlertGraph.yaml

Large diffs are not rendered by default.

545 changes: 545 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardAlertGraph_import.yaml

Large diffs are not rendered by default.

445 changes: 445 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardAlertValue.yaml

Large diffs are not rendered by default.

545 changes: 545 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardAlertValue_import.yaml

Large diffs are not rendered by default.

459 changes: 459 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardChange.yaml

Large diffs are not rendered by default.

563 changes: 563 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardChange_import.yaml

Large diffs are not rendered by default.

445 changes: 445 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardCheckStatus.yaml

Large diffs are not rendered by default.

545 changes: 545 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardCheckStatus_import.yaml

Large diffs are not rendered by default.

452 changes: 452 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardDistribution.yaml

Large diffs are not rendered by default.

554 changes: 554 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardDistribution_import.yaml

Large diffs are not rendered by default.

445 changes: 445 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardEventStream.yaml

Large diffs are not rendered by default.

545 changes: 545 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardEventStream_import.yaml

Large diffs are not rendered by default.

445 changes: 445 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardEventTimeline.yaml

Large diffs are not rendered by default.

545 changes: 545 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardEventTimeline_import.yaml

Large diffs are not rendered by default.

445 changes: 445 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardFreeText.yaml

Large diffs are not rendered by default.

545 changes: 545 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardFreeText_import.yaml

Large diffs are not rendered by default.

452 changes: 452 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardHeatMap.yaml

Large diffs are not rendered by default.

554 changes: 554 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardHeatMap_import.yaml

Large diffs are not rendered by default.

452 changes: 452 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardHostMap.yaml

Large diffs are not rendered by default.

554 changes: 554 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardHostMap_import.yaml

Large diffs are not rendered by default.

438 changes: 438 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardIFrame.yaml

Large diffs are not rendered by default.

536 changes: 536 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardIFrame_import.yaml

Large diffs are not rendered by default.

438 changes: 438 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardImage.yaml

Large diffs are not rendered by default.

536 changes: 536 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardImage_import.yaml

Large diffs are not rendered by default.

452 changes: 452 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardLogStream.yaml

Large diffs are not rendered by default.

554 changes: 554 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardLogStream_import.yaml

Large diffs are not rendered by default.

445 changes: 445 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardManageStatus.yaml

Large diffs are not rendered by default.

545 changes: 545 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardManageStatus_import.yaml

Large diffs are not rendered by default.

445 changes: 445 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardNote.yaml

Large diffs are not rendered by default.

545 changes: 545 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardNote_import.yaml

Large diffs are not rendered by default.

466 changes: 466 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardQueryTable.yaml

Large diffs are not rendered by default.

572 changes: 572 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardQueryTable_import.yaml

Large diffs are not rendered by default.

445 changes: 445 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardQueryValue.yaml

Large diffs are not rendered by default.

545 changes: 545 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardQueryValue_import.yaml

Large diffs are not rendered by default.

438 changes: 438 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardSLO.yaml

Large diffs are not rendered by default.

536 changes: 536 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardSLO_import.yaml

Large diffs are not rendered by default.

473 changes: 473 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardScatterplot.yaml

Large diffs are not rendered by default.

581 changes: 581 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardScatterplot_import.yaml

Large diffs are not rendered by default.

466 changes: 466 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardTimeseries.yaml

Large diffs are not rendered by default.

572 changes: 572 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardTimeseries_import.yaml

Large diffs are not rendered by default.

452 changes: 452 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardTopList.yaml

Large diffs are not rendered by default.

554 changes: 554 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardTopList_import.yaml

Large diffs are not rendered by default.

445 changes: 445 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardTraceService.yaml

Large diffs are not rendered by default.

545 changes: 545 additions & 0 deletions datadog/cassettes/TestAccDatadogDashboardTraceService_import.yaml

Large diffs are not rendered by default.

896 changes: 427 additions & 469 deletions datadog/cassettes/TestAccDatadogDashboard_import.yaml

Large diffs are not rendered by default.

1,118 changes: 0 additions & 1,118 deletions datadog/cassettes/TestAccDatadogDashboard_update.yaml

This file was deleted.

473 changes: 473 additions & 0 deletions datadog/cassettes/TestAccDatadogFreeDashboard.yaml

Large diffs are not rendered by default.

648 changes: 648 additions & 0 deletions datadog/cassettes/TestAccDatadogOrderedDashboard.yaml

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions datadog/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"runtime"
"strings"
"testing"
"time"

Expand All @@ -18,6 +19,7 @@ import (
"github.com/dnaeon/go-vcr/recorder"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform-plugin-sdk/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/jonboulle/clockwork"
Expand Down Expand Up @@ -268,3 +270,70 @@ func testAccPreCheck(t *testing.T) {
t.Fatal("DD_APP_KEY must be set for acceptance tests")
}
}

func testCheckResourceAttrs(name string, checkExists resource.TestCheckFunc, assertions []string) []resource.TestCheckFunc {
funcs := []resource.TestCheckFunc{}
funcs = append(funcs, checkExists)
for _, assertion := range assertions {
assertionPair := strings.Split(assertion, " = ")
if len(assertionPair) == 1 {
assertionPair = strings.Split(assertion, " =")
}
key := assertionPair[0]
value := ""
if len(assertionPair) > 1 {
value = assertionPair[1]
}
funcs = append(funcs, resource.TestCheckResourceAttr(name, key, value))
// Use utility method below, instead of the above one, to print out all state keys/values during test debugging
//funcs = append(funcs, CheckResourceAttr(name, key, value))
gzussa marked this conversation as resolved.
Show resolved Hide resolved
}
return funcs
}

/* Utility method for Debugging purpose. This method helps list assertions as well */
func CheckResourceAttr(name, key, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ms := s.RootModule()
rs, ok := ms.Resources[name]
if !ok {
return nil
}

is := rs.Primary
if is == nil {
return nil
}

for k, val := range is.Attributes {
fmt.Println(fmt.Sprintf("%v = %v", k, val))
}

// Empty containers may be elided from the state.
// If the intent here is to check for an empty container, allow the key to
// also be non-existent.
emptyCheck := false
if value == "0" && (strings.HasSuffix(key, ".#") || strings.HasSuffix(key, ".%")) {
emptyCheck = true
}
gzussa marked this conversation as resolved.
Show resolved Hide resolved

if v, ok := is.Attributes[key]; !ok || v != value {

if emptyCheck && !ok {
return nil
}

if !ok {
return fmt.Errorf("%s: Attribute '%s' not found", name, key)
}

return fmt.Errorf(
"%s: Attribute '%s' expected %#v, got %#v",
name,
key,
value,
v)
}
return nil
}
}
3 changes: 1 addition & 2 deletions datadog/resource_datadog_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,6 @@ func getNonGroupWidgetSchema() map[string]*schema.Schema {
Schema: getManageStatusDefinitionSchema(),
},
},

"note_definition": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1944,7 +1943,7 @@ func buildDatadogHeatmapDefinition(terraformDefinition map[string]interface{}) *
datadogDefinition.SetTitleSize(v)
}
if v, ok := terraformDefinition["title_align"].(string); ok && len(v) != 0 {
datadogDefinition.SetTitle(v)
datadogDefinition.SetTitleAlign(datadogV1.WidgetTextAlign(v))
gzussa marked this conversation as resolved.
Show resolved Hide resolved
}
if v, ok := terraformDefinition["time"].(map[string]interface{}); ok && len(v) > 0 {
datadogDefinition.Time = buildDatadogWidgetTime(v)
Expand Down
95 changes: 95 additions & 0 deletions datadog/resource_datadog_dashboard_alert_graph_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package datadog

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

const datadogDashboardAlertGraphConfig = `
resource "datadog_dashboard" "alert_graph_dashboard" {
title = "Acceptance Test Alert Graph Widget Dashboard"
description = "Created using the Datadog provider in Terraform"
layout_type = "ordered"
is_read_only = true
widget {
alert_graph_definition {
alert_id = "895605"
viz_type = "timeseries"
}
}
widget {
alert_graph_definition {
alert_id = "895606"
viz_type = "toplist"
title = "Widget Title"
title_align = "right"
gzussa marked this conversation as resolved.
Show resolved Hide resolved
title_size = "16"
time = {
live_span = "1h"
}
}
}
}
`

var datadogDashboardAlertGraphAsserts = []string{
"title = Acceptance Test Alert Graph Widget Dashboard",
"widget.0.alert_graph_definition.0.alert_id = 895605",
"widget.1.alert_graph_definition.0.time.% = 1",
"widget.1.alert_graph_definition.0.title = Widget Title",
"is_read_only = true",
"widget.1.alert_graph_definition.0.title_size = 16",
"widget.1.alert_graph_definition.0.viz_type = toplist",
"widget.1.alert_graph_definition.0.time.live_span = 1h",
"widget.1.alert_graph_definition.0.alert_id = 895606",
"widget.0.alert_graph_definition.0.title_size =",
"description = Created using the Datadog provider in Terraform",
"widget.0.alert_graph_definition.0.title_align =",
"widget.0.alert_graph_definition.0.title =",
"widget.1.alert_graph_definition.0.title_align = right",
"layout_type = ordered",
"widget.0.alert_graph_definition.0.viz_type = timeseries",
}

func TestAccDatadogDashboardAlertGraph(t *testing.T) {
accProviders, cleanup := testAccProviders(t)
defer cleanup(t)
accProvider := testAccProvider(t, accProviders)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: accProviders,
CheckDestroy: checkDashboardDestroy(accProvider),
Steps: []resource.TestStep{
{
Config: datadogDashboardAlertGraphConfig,
Check: resource.ComposeTestCheckFunc(
testCheckResourceAttrs("datadog_dashboard.alert_graph_dashboard", checkDashboardExists(accProvider), datadogDashboardAlertGraphAsserts)...,
),
},
},
})
}

func TestAccDatadogDashboardAlertGraph_import(t *testing.T) {
accProviders, cleanup := testAccProviders(t)
defer cleanup(t)
accProvider := testAccProvider(t, accProviders)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: accProviders,
CheckDestroy: checkDashboardDestroy(accProvider),
Steps: []resource.TestStep{
{
Config: datadogDashboardAlertGraphConfig,
},
{
ResourceName: "datadog_dashboard.alert_graph_dashboard",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
96 changes: 96 additions & 0 deletions datadog/resource_datadog_dashboard_alert_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package datadog

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

const datadogDashboardAlertValueConfig = `
resource "datadog_dashboard" "alert_value_dashboard" {
title = "Acceptance Test Alert Value Widget Dashboard"
description = "Created using the Datadog provider in Terraform"
layout_type = "ordered"
is_read_only = true
widget {
alert_value_definition {
alert_id = "895605"
}
}
widget {
alert_value_definition {
alert_id = "895606"
precision = 1
unit = "b"
title_size = "16"
gzussa marked this conversation as resolved.
Show resolved Hide resolved
title_align = "center"
title = "Widget Title"
text_align = "center"
}
}
}
`

var datadogDashboardAlertValueAsserts = []string{
"widget.0.alert_value_definition.0.title_align =",
"widget.1.alert_value_definition.0.title_align = center",
"widget.1.alert_value_definition.0.text_align = center",
"widget.1.layout.% = 0",
"title = Acceptance Test Alert Value Widget Dashboard",
"description = Created using the Datadog provider in Terraform",
"widget.0.alert_value_definition.0.precision = 0",
"widget.1.alert_value_definition.0.title_size = 16",
"widget.1.alert_value_definition.0.precision = 1",
"widget.0.alert_value_definition.0.title_size =",
"widget.1.alert_value_definition.0.alert_id = 895606",
"widget.0.alert_value_definition.0.text_align =",
"layout_type = ordered",
"widget.0.alert_value_definition.0.title =",
"widget.0.alert_value_definition.0.unit =",
"widget.1.alert_value_definition.0.title = Widget Title",
"widget.0.alert_value_definition.0.alert_id = 895605",
"widget.1.alert_value_definition.0.unit = b",
"is_read_only = true",
}

func TestAccDatadogDashboardAlertValue(t *testing.T) {
accProviders, cleanup := testAccProviders(t)
defer cleanup(t)
accProvider := testAccProvider(t, accProviders)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: accProviders,
CheckDestroy: checkDashboardDestroy(accProvider),
Steps: []resource.TestStep{
{
Config: datadogDashboardAlertValueConfig,
Check: resource.ComposeTestCheckFunc(
testCheckResourceAttrs("datadog_dashboard.alert_value_dashboard", checkDashboardExists(accProvider), datadogDashboardAlertValueAsserts)...,
),
},
},
})
}

func TestAccDatadogDashboardAlertValue_import(t *testing.T) {
accProviders, cleanup := testAccProviders(t)
defer cleanup(t)
accProvider := testAccProvider(t, accProviders)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: accProviders,
CheckDestroy: checkDashboardDestroy(accProvider),
Steps: []resource.TestStep{
{
Config: datadogDashboardAlertValueConfig,
},
{
ResourceName: "datadog_dashboard.alert_value_dashboard",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Loading