Skip to content

Commit

Permalink
Cleanup flaky tests (#2166)
Browse files Browse the repository at this point in the history
* make tests less flaky

* cassettes
  • Loading branch information
skarimo authored Oct 31, 2023
1 parent e4c4ad5 commit eb1beae
Show file tree
Hide file tree
Showing 21 changed files with 481 additions and 444 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-06-16T12:00:53.754-04:00
2023-10-31T14:17:04.187542-04:00
272 changes: 144 additions & 128 deletions datadog/tests/cassettes/TestAccDatadogMonitorDatasource.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-06-16T11:56:44.238284-04:00
2023-10-31T14:16:21.62046-04:00

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-06-16T12:01:14.985149-04:00
2023-10-31T14:16:39.088182-04:00
270 changes: 143 additions & 127 deletions datadog/tests/cassettes/TestAccDatadogMonitorsDatasource.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-08-17T10:19:08.476225+02:00
2023-10-31T14:16:01.698388-04:00

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccDatadogSyntheticsGlobalVariable(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testSyntheticsResourceIsDestroyed(accProvider),
CheckDestroy: testSyntheticsGlobalVariableResourceIsDestroyed(accProvider),
Steps: []resource.TestStep{
{
Config: testAccCheckDatadogSyntheticsGlobalVariableConfig(uniq),
Expand Down
4 changes: 2 additions & 2 deletions datadog/tests/data_source_datadog_synthetics_test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccDatadogSyntheticsTest(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testSyntheticsResourceIsDestroyed(accProvider),
CheckDestroy: testSyntheticsTestIsDestroyed(accProvider),
Steps: []resource.TestStep{
{
Config: testAccCheckDatadogSyntheticsTestConfig(uniq),
Expand All @@ -38,7 +38,7 @@ func TestAccDatadogSyntheticsTestWithUrl(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testSyntheticsResourceIsDestroyed(accProvider),
CheckDestroy: testSyntheticsTestIsDestroyed(accProvider),
Steps: []resource.TestStep{
{
Config: testAccCheckDatadogSyntheticsTestConfigWithUrl(uniq),
Expand Down
2 changes: 1 addition & 1 deletion datadog/tests/resource_datadog_dashboard_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func testAccCheckDatadogDashListDestroyWithFw(accProvider *fwprovider.FrameworkP

func datadogDashListDestroyHelper(ctx context.Context, s *terraform.State, apiInstances *utils.ApiInstances) error {
for _, r := range s.RootModule().Resources {
if !strings.Contains(r.Primary.Attributes["name"], "List") {
if r.Type != "datadog_dashboard_list" {
continue
}
id, _ := strconv.Atoi(r.Primary.ID)
Expand Down
22 changes: 15 additions & 7 deletions datadog/tests/resource_datadog_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ func testAccCheckDatadogMonitorDestroy(accProvider func() (*schema.Provider, err
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth

if err := destroyHelper(auth, s, apiInstances); err != nil {
if err := destroyMonitorHelper(auth, s, apiInstances); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -1568,9 +1568,13 @@ resource "datadog_monitor" "foo" {
}`, uniq)
}

func destroyHelper(ctx context.Context, s *terraform.State, apiInstances *utils.ApiInstances) error {
err := utils.Retry(2, 10, func() error {
for _, r := range s.RootModule().Resources {
func destroyMonitorHelper(ctx context.Context, s *terraform.State, apiInstances *utils.ApiInstances) error {
for _, r := range s.RootModule().Resources {
if r.Type != "datadog_monitor" {
continue
}

err := utils.Retry(2, 10, func() error {
i, _ := strconv.ParseInt(r.Primary.ID, 10, 64)
_, httpresp, err := apiInstances.GetMonitorsApiV1().GetMonitor(ctx, i)
if err != nil {
Expand All @@ -1580,10 +1584,14 @@ func destroyHelper(ctx context.Context, s *terraform.State, apiInstances *utils.
return &utils.RetryableError{Prob: fmt.Sprintf("received an error retrieving Monitor %s", err)}
}
return &utils.RetryableError{Prob: "Monitor still exists"}
})

if err != nil {
return err
}
return nil
})
return err
}

return nil
}

func existsHelper(ctx context.Context, s *terraform.State, apiInstances *utils.ApiInstances) error {
Expand Down
31 changes: 19 additions & 12 deletions datadog/tests/resource_datadog_service_level_objective_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,28 @@ func testAccCheckDatadogServiceLevelObjectiveDestroy(accProvider func() (*schema
}

func destroyServiceLevelObjectiveHelper(ctx context.Context, s *terraform.State, apiInstances *utils.ApiInstances) error {
err := utils.Retry(2, 5, func() error {
for _, r := range s.RootModule().Resources {
if r.Primary.ID != "" && r.Type == "datadog_service_level_objective" {
if _, httpResp, err := apiInstances.GetServiceLevelObjectivesApiV1().GetSLO(ctx, r.Primary.ID); err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
continue
}
return &utils.FatalError{Prob: fmt.Sprintf("received an error retrieving service level objective %s", err)}
for _, r := range s.RootModule().Resources {
if r.Type != "datadog_service_level_objective" {
continue
}

err := utils.Retry(2, 5, func() error {
if _, httpResp, err := apiInstances.GetServiceLevelObjectivesApiV1().GetSLO(ctx, r.Primary.ID); err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
return nil
}
return &utils.RetryableError{Prob: "service Level Objective still exists"}
return &utils.FatalError{Prob: fmt.Sprintf("received an error retrieving service level objective %s", err)}
}
return &utils.RetryableError{Prob: "service Level Objective still exists"}
})

if err != nil {
return nil
}
return nil
})
return err

}

return nil
}

func existsServiceLevelObjectiveHelper(ctx context.Context, s *terraform.State, apiInstances *utils.ApiInstances) error {
Expand Down
34 changes: 9 additions & 25 deletions datadog/tests/resource_datadog_synthetics_global_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestAccDatadogSyntheticsGlobalVariable_importBasic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testSyntheticsResourceIsDestroyed(accProvider),
CheckDestroy: testSyntheticsGlobalVariableResourceIsDestroyed(accProvider),
Steps: []resource.TestStep{
{
Config: createSyntheticsGlobalVariableConfig(variableName, roleName),
Expand All @@ -49,7 +49,7 @@ func TestAccDatadogSyntheticsGlobalVariable_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testSyntheticsResourceIsDestroyed(accProvider),
CheckDestroy: testSyntheticsGlobalVariableResourceIsDestroyed(accProvider),
Steps: []resource.TestStep{
createSyntheticsGlobalVariableStep(ctx, accProvider, t),
},
Expand All @@ -64,7 +64,7 @@ func TestAccDatadogSyntheticsGlobalVariableSecure_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testSyntheticsResourceIsDestroyed(accProvider),
CheckDestroy: testSyntheticsGlobalVariableResourceIsDestroyed(accProvider),
Steps: []resource.TestStep{
createSyntheticsGlobalVariableSecureStep(ctx, accProvider, t),
},
Expand All @@ -79,7 +79,7 @@ func TestAccDatadogSyntheticsGlobalVariable_Updated(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testSyntheticsResourceIsDestroyed(accProvider),
CheckDestroy: testSyntheticsGlobalVariableResourceIsDestroyed(accProvider),
Steps: []resource.TestStep{
createSyntheticsGlobalVariableStep(ctx, accProvider, t),
updateSyntheticsGlobalVariableStep(ctx, accProvider, t),
Expand All @@ -95,7 +95,7 @@ func TestAccDatadogSyntheticsGlobalVariableSecure_Updated(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testSyntheticsResourceIsDestroyed(accProvider),
CheckDestroy: testSyntheticsGlobalVariableResourceIsDestroyed(accProvider),
Steps: []resource.TestStep{
createSyntheticsGlobalVariableSecureStep(ctx, accProvider, t),
updateSyntheticsGlobalVariableSecureStep(ctx, accProvider, t),
Expand All @@ -111,7 +111,7 @@ func TestAccDatadogSyntheticsGlobalVariableFromTest_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testSyntheticsResourceIsDestroyed(accProvider),
CheckDestroy: testSyntheticsGlobalVariableResourceIsDestroyed(accProvider),
Steps: []resource.TestStep{
createSyntheticsGlobalVariableFromTestStep(ctx, accProvider, t),
},
Expand All @@ -126,7 +126,7 @@ func TestAccDatadogSyntheticsGlobalVariableFromTest_LocalVariable(t *testing.T)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testSyntheticsResourceIsDestroyed(accProvider),
CheckDestroy: testSyntheticsGlobalVariableResourceIsDestroyed(accProvider),
Steps: []resource.TestStep{
createSyntheticsGlobalVariableFromTestLocalVariableStep(ctx, accProvider, t),
},
Expand Down Expand Up @@ -494,12 +494,6 @@ func testSyntheticsResourceExists(accProvider func() (*schema.Provider, error))
auth := providerConf.Auth

for _, r := range s.RootModule().Resources {
if r.Type == "datadog_synthetics_test" {
if _, _, err := apiInstances.GetSyntheticsApiV1().GetTest(auth, r.Primary.ID); err != nil {
return fmt.Errorf("received an error retrieving synthetics test %s", err)
}
}

if r.Type == "datadog_synthetics_global_variable" {
if _, _, err := apiInstances.GetSyntheticsApiV1().GetGlobalVariable(auth, r.Primary.ID); err != nil {
return fmt.Errorf("received an error retrieving synthetics global variable %s", err)
Expand All @@ -510,28 +504,18 @@ func testSyntheticsResourceExists(accProvider func() (*schema.Provider, error))
}
}

func testSyntheticsResourceIsDestroyed(accProvider func() (*schema.Provider, error)) resource.TestCheckFunc {
func testSyntheticsGlobalVariableResourceIsDestroyed(accProvider func() (*schema.Provider, error)) resource.TestCheckFunc {
return func(s *terraform.State) error {
provider, _ := accProvider()
providerConf := provider.Meta().(*datadog.ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth

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

if r.Type == "datadog_synthetics_test" {
if _, _, err := apiInstances.GetSyntheticsApiV1().GetTest(auth, r.Primary.ID); err != nil {
if strings.Contains(err.Error(), "404 Not Found") {
continue
}
return fmt.Errorf("received an error retrieving synthetics test %s", err)
}
return fmt.Errorf("synthetics test still exists")
}

if _, _, err := apiInstances.GetSyntheticsApiV1().GetGlobalVariable(auth, r.Primary.ID); err != nil {
if strings.Contains(err.Error(), "404 Not Found") {
continue
Expand Down
6 changes: 5 additions & 1 deletion datadog/tests/resource_datadog_synthetics_test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func TestAccDatadogSyntheticsTestMultistepApi_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: accProviders,
CheckDestroy: testSyntheticsResourceIsDestroyed(accProvider),
CheckDestroy: testSyntheticsTestIsDestroyed(accProvider),
Steps: []resource.TestStep{
createSyntheticsMultistepAPITest(ctx, accProvider, t),
},
Expand Down Expand Up @@ -4457,6 +4457,10 @@ func testSyntheticsTestIsDestroyed(accProvider func() (*schema.Provider, error))
auth := providerConf.Auth

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

if _, _, err := apiInstances.GetSyntheticsApiV1().GetTest(auth, r.Primary.ID); err != nil {
if strings.Contains(err.Error(), "404 Not Found") {
continue
Expand Down
26 changes: 14 additions & 12 deletions datadog/tests/resource_datadog_team_link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,22 @@ func testAccCheckDatadogTeamLinkDestroy(accProvider *fwprovider.FrameworkProvide
apiInstances := accProvider.DatadogApiInstances
auth := accProvider.Auth

if err := TeamLinkDestroyHelper(auth, s, apiInstances); err != nil {
if err := teamLinkDestroyHelper(auth, s, apiInstances); err != nil {
return err
}
return nil
}
}

func TeamLinkDestroyHelper(auth context.Context, s *terraform.State, apiInstances *utils.ApiInstances) error {
err := utils.Retry(2, 10, func() error {
for _, r := range s.RootModule().Resources {
if r.Type != "resource_datadog_team_link" {
continue
}
teamId := r.Primary.Attributes["team_id"]
id := r.Primary.ID
func teamLinkDestroyHelper(auth context.Context, s *terraform.State, apiInstances *utils.ApiInstances) error {
for _, r := range s.RootModule().Resources {
if r.Type != "resource_datadog_team_link" {
continue
}
teamId := r.Primary.Attributes["team_id"]
id := r.Primary.ID

err := utils.Retry(2, 10, func() error {
_, httpResp, err := apiInstances.GetTeamsApiV2().GetTeamLink(auth, teamId, id)
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
Expand All @@ -129,10 +129,12 @@ func TeamLinkDestroyHelper(auth context.Context, s *terraform.State, apiInstance
return &utils.RetryableError{Prob: fmt.Sprintf("received an error retrieving TeamLink %s", err)}
}
return &utils.RetryableError{Prob: "TeamLink still exists"}
})
if err != nil {
return nil
}
return nil
})
return err
}
return nil
}

func testAccCheckDatadogTeamLinkExists(accProvider *fwprovider.FrameworkProvider) resource.TestCheckFunc {
Expand Down
26 changes: 15 additions & 11 deletions datadog/tests/resource_datadog_team_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,21 @@ func testAccCheckDatadogTeamMembershipDestroy(accProvider *fwprovider.FrameworkP
apiInstances := accProvider.DatadogApiInstances
auth := accProvider.Auth

if err := TeamMembershipDestroyHelper(auth, s, apiInstances); err != nil {
if err := teamMembershipDestroyHelper(auth, s, apiInstances); err != nil {
return err
}
return nil
}
}

func TeamMembershipDestroyHelper(auth context.Context, s *terraform.State, apiInstances *utils.ApiInstances) error {
err := utils.Retry(2, 10, func() error {
for _, r := range s.RootModule().Resources {
if r.Type != "resource_datadog_team_membership" {
continue
}
teamId := r.Primary.ID
func teamMembershipDestroyHelper(auth context.Context, s *terraform.State, apiInstances *utils.ApiInstances) error {
for _, r := range s.RootModule().Resources {
if r.Type != "resource_datadog_team_membership" {
continue
}
teamId := r.Primary.ID

err := utils.Retry(2, 10, func() error {
r, httpResp, err := apiInstances.GetTeamsApiV2().GetTeamMemberships(auth, teamId)
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
Expand All @@ -88,10 +89,13 @@ func TeamMembershipDestroyHelper(auth context.Context, s *terraform.State, apiIn
return &utils.RetryableError{Prob: "TeamMembership still exists"}
}
}
return nil
})
if err != nil {
return err
}
return nil
})
return err
}
return nil
}

func testAccCheckDatadogTeamMembershipExists(accProvider *fwprovider.FrameworkProvider) resource.TestCheckFunc {
Expand Down
21 changes: 12 additions & 9 deletions datadog/tests/resource_datadog_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ func testAccCheckDatadogTeamDestroy(accProvider *fwprovider.FrameworkProvider) f
}

func TeamDestroyHelper(auth context.Context, s *terraform.State, apiInstances *utils.ApiInstances) error {
err := utils.Retry(2, 10, func() error {
for _, r := range s.RootModule().Resources {
if r.Type != "resource_datadog_team" {
continue
}
id := r.Primary.ID
for _, r := range s.RootModule().Resources {
if r.Type != "resource_datadog_team" {
continue
}
id := r.Primary.ID

err := utils.Retry(2, 10, func() error {
_, httpResp, err := apiInstances.GetTeamsApiV2().GetTeam(auth, id)
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 {
Expand All @@ -96,10 +96,13 @@ func TeamDestroyHelper(auth context.Context, s *terraform.State, apiInstances *u
return &utils.RetryableError{Prob: fmt.Sprintf("received an error retrieving Team %s", err)}
}
return &utils.RetryableError{Prob: "Team still exists"}
})

if err != nil {
return err
}
return nil
})
return err
}
return nil
}

func testAccCheckDatadogTeamExists(accProvider *fwprovider.FrameworkProvider) resource.TestCheckFunc {
Expand Down
Loading

0 comments on commit eb1beae

Please sign in to comment.