Skip to content

Commit 0acf236

Browse files
fix: makes the tests idempotent
1 parent 823016a commit 0acf236

File tree

3 files changed

+140
-112
lines changed

3 files changed

+140
-112
lines changed

internal/provider/environment_data_source_test.go

+43-34
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ func TestAccEnvironmentDataSource(t *testing.T) {
1616
rName := acctest.RandString(8)
1717
systemName := fmt.Sprintf("test-system-%s", rName)
1818
envName := fmt.Sprintf("test-env-%s", rName)
19-
complexName := fmt.Sprintf("complex-%s", envName)
19+
complexName := fmt.Sprintf("complex-%s", rName)
2020

2121
resource.Test(t, resource.TestCase{
2222
PreCheck: func() { testAccPreCheck(t) },
2323
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
2424
Steps: []resource.TestStep{
2525
// Test basic configuration
2626
{
27-
Config: testAccEnvironmentDataSourceConfigSetup(systemName, envName) +
27+
Config: testAccEnvironmentDataSourceConfigSetup(systemName, envName, complexName) +
2828
testAccEnvironmentDataSourceConfigBasic(envName),
2929
Check: resource.ComposeAggregateTestCheckFunc(
3030
resource.TestCheckResourceAttr("data.ctrlplane_environment.test", "name", envName),
@@ -35,7 +35,7 @@ func TestAccEnvironmentDataSource(t *testing.T) {
3535
},
3636
// Test complex filter
3737
{
38-
Config: testAccEnvironmentDataSourceConfigSetup(systemName, envName) +
38+
Config: testAccEnvironmentDataSourceConfigSetup(systemName, envName, complexName) +
3939
testAccEnvironmentDataSourceConfigComplex(complexName),
4040
Check: resource.ComposeAggregateTestCheckFunc(
4141
resource.TestCheckResourceAttr("data.ctrlplane_environment.test_complex", "name", complexName),
@@ -46,7 +46,7 @@ func TestAccEnvironmentDataSource(t *testing.T) {
4646
},
4747
// Test with complex filter
4848
{
49-
Config: testAccEnvironmentDataSourceConfigWithComplexFilter(systemName, envName),
49+
Config: testAccEnvironmentDataSourceConfigWithComplexFilter(systemName, envName, complexName),
5050
Check: resource.ComposeAggregateTestCheckFunc(
5151
resource.TestCheckResourceAttrSet("data.ctrlplane_environment.test_complex", "id"),
5252
resource.TestCheckResourceAttr("data.ctrlplane_environment.test_complex", "name", complexName),
@@ -60,27 +60,28 @@ func TestAccEnvironmentDataSource(t *testing.T) {
6060
}
6161

6262
func TestAccEnvironmentDataSourceErrorHandling(t *testing.T) {
63-
// Define the tests for error handling
63+
rName := acctest.RandString(8)
64+
nonExistentEnvName := fmt.Sprintf("non-existent-env-%s", rName)
65+
6466
resource.Test(t, resource.TestCase{
6567
PreCheck: func() { testAccPreCheck(t) },
6668
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
6769
Steps: []resource.TestStep{
70+
// Test with missing name (should fail)
6871
{
69-
Config: testAccEnvironmentDataSourceConfigMissingName(),
70-
// Test for missing required field (name)
72+
Config: testAccEnvironmentDataSourceConfigMissingName(),
7173
ExpectError: regexp.MustCompile(`The argument "name" is required`),
7274
},
75+
// Test with a non-existent environment name (should fail)
7376
{
74-
// Test for non-existent environment
75-
Config: testAccEnvironmentDataSourceConfigNonExistentEnv(),
77+
Config: testAccEnvironmentDataSourceConfigNonExistentEnv(nonExistentEnvName),
7678
ExpectError: regexp.MustCompile(`Environment Not Found`),
7779
},
7880
},
7981
})
8082
}
8183

82-
func testAccEnvironmentDataSourceConfigSetup(systemName, envName string) string {
83-
complexName := fmt.Sprintf("complex-%s", envName)
84+
func testAccEnvironmentDataSourceConfigSetup(systemName, envName, complexName string) string {
8485
return fmt.Sprintf(`
8586
resource "ctrlplane_system" "test" {
8687
name = "%[1]s"
@@ -93,38 +94,40 @@ resource "ctrlplane_environment" "test" {
9394
system_id = ctrlplane_system.test.id
9495
description = "Test environment"
9596
metadata = {
96-
key1 = "value1"
97-
key2 = "value2"
97+
"key1" = "value1"
98+
"key2" = "value2"
99+
"test" = "true"
98100
}
99101
resource_filter = {
100-
type = "metadata"
101-
key = "environment"
102+
type = "metadata"
103+
key = "environment"
102104
operator = "equals"
103-
value = "staging"
105+
value = "production"
104106
}
105107
}
106108
107109
resource "ctrlplane_environment" "test_complex" {
108110
name = "%[3]s"
109111
system_id = ctrlplane_system.test.id
110-
description = "Test environment with complex filter"
112+
description = "Complex environment"
111113
metadata = {
112-
test = "true"
114+
"key1" = "complex_value"
115+
"complex" = "true"
113116
}
114117
resource_filter = {
115-
type = "comparison"
116-
operator = "and"
118+
type = "comparison"
119+
operator = "and"
117120
conditions = [
118121
{
119122
type = "metadata"
120123
key = "environment"
121124
operator = "equals"
122-
value = "staging"
125+
value = "production"
123126
},
124127
{
125-
type = "kind"
126-
operator = "equals"
127-
value = "Deployment"
128+
type = "name"
129+
operator = "contains"
130+
value = "api"
128131
}
129132
]
130133
}
@@ -141,39 +144,45 @@ data "ctrlplane_environment" "test" {
141144
`, envName)
142145
}
143146

144-
func testAccEnvironmentDataSourceConfigWithComplexFilter(systemName, envName string) string {
147+
func testAccEnvironmentDataSourceConfigWithComplexFilter(systemName, envName, complexName string) string {
145148
return fmt.Sprintf(`
146149
%s
147150
148151
data "ctrlplane_environment" "test_complex" {
149-
name = ctrlplane_environment.test_complex.name
152+
name = "%[2]s"
150153
system_id = ctrlplane_system.test.id
151154
}
152-
`, testAccEnvironmentDataSourceConfigSetup(systemName, envName))
155+
`, testAccEnvironmentDataSourceConfigSetup(systemName, envName, complexName), complexName)
153156
}
154157

155158
func testAccEnvironmentDataSourceConfigMissingName() string {
156159
return `
160+
resource "ctrlplane_system" "test" {
161+
name = "test-system-missing-name"
162+
slug = "test-system-missing-name"
163+
description = "Test system"
164+
}
165+
157166
data "ctrlplane_environment" "test" {
158-
# Missing name
159-
system_id = "00000000-0000-0000-0000-000000000000"
167+
# Missing name field
168+
system_id = ctrlplane_system.test.id
160169
}
161170
`
162171
}
163172

164-
func testAccEnvironmentDataSourceConfigNonExistentEnv() string {
165-
return `
173+
func testAccEnvironmentDataSourceConfigNonExistentEnv(envName string) string {
174+
return fmt.Sprintf(`
166175
resource "ctrlplane_system" "test" {
167176
name = "test-system-nonexistent"
177+
slug = "test-system-nonexistent"
168178
description = "Test system"
169-
slug = "test-system-nonexistent"
170179
}
171180
172181
data "ctrlplane_environment" "test" {
173-
name = "non-existent-environment"
182+
name = "%[1]s"
174183
system_id = ctrlplane_system.test.id
175184
}
176-
`
185+
`, envName)
177186
}
178187

179188
func testAccEnvironmentDataSourceConfigComplex(complexName string) string {

0 commit comments

Comments
 (0)