-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We have to test that resources can be destroyed. However, for org tests, since we destroy everything (including the org), the test is bad since destroying the org destroys everything within To help with this, this new helper will allow to add a step in org tests where we keep the org, but remove the actual resource on which we want to test deletion To test the behavior, I made the role resource tests use this instead of a `if` and I added a test step for the team resource
- Loading branch information
1 parent
5abae1f
commit 2eb8186
Showing
4 changed files
with
103 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package testutils | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
) | ||
|
||
// WithoutResource removes a resource from a Terraform configuration. | ||
func WithoutResource(t *testing.T, tfCode string, resourceName string) string { | ||
tfConfig, err := hclwrite.ParseConfig([]byte(tfCode), "", hcl.Pos{Line: 1, Column: 1}) | ||
if err != nil { | ||
t.Fatalf("failed to parse HCL: %v", err) | ||
} | ||
|
||
block := tfConfig.Body().FirstMatchingBlock("resource", strings.Split(resourceName, ".")) | ||
if block == nil { | ||
t.Fatalf("failed to find resource %q", resourceName) | ||
} | ||
tfConfig.Body().RemoveBlock(block) | ||
|
||
return string(tfConfig.Bytes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package testutils | ||
|
||
import "testing" | ||
|
||
func TestWithoutResource(t *testing.T) { | ||
t.Parallel() | ||
|
||
input := `resource "grafana_organization" "test" { | ||
name = "test" | ||
} | ||
resource "grafana_folder" "test" { | ||
org_id = grafana_organization.test.id | ||
title = "test" | ||
} | ||
resource "grafana_rule_group" "test" { | ||
org_id = grafana_organization.test.id | ||
name = "test" | ||
folder_uid = grafana_folder.test.uid | ||
interval_seconds = 360 | ||
rule { | ||
name = "My Alert Rule 1" | ||
for = "2m" | ||
condition = "B" | ||
no_data_state = "NoData" | ||
exec_err_state = "Alerting" | ||
is_paused = false | ||
data { | ||
ref_id = "A" | ||
query_type = "" | ||
relative_time_range { | ||
from = 600 | ||
to = 0 | ||
} | ||
datasource_uid = "PD8C576611E62080A" | ||
model = jsonencode({ | ||
hide = false | ||
intervalMs = 1000 | ||
maxDataPoints = 43200 | ||
refId = "A" | ||
}) | ||
} | ||
} | ||
}` | ||
|
||
expected := `resource "grafana_organization" "test" { | ||
name = "test" | ||
} | ||
resource "grafana_folder" "test" { | ||
org_id = grafana_organization.test.id | ||
title = "test" | ||
} | ||
` | ||
|
||
actual := WithoutResource(t, input, "grafana_rule_group.test") | ||
|
||
if actual != expected { | ||
t.Errorf("expected:\n%q\n\nactual:\n%q", expected, actual) | ||
} | ||
} |