@@ -16,15 +16,15 @@ func TestAccEnvironmentDataSource(t *testing.T) {
16
16
rName := acctest .RandString (8 )
17
17
systemName := fmt .Sprintf ("test-system-%s" , rName )
18
18
envName := fmt .Sprintf ("test-env-%s" , rName )
19
- complexName := fmt .Sprintf ("complex-%s" , envName )
19
+ complexName := fmt .Sprintf ("complex-%s" , rName )
20
20
21
21
resource .Test (t , resource.TestCase {
22
22
PreCheck : func () { testAccPreCheck (t ) },
23
23
ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
24
24
Steps : []resource.TestStep {
25
25
// Test basic configuration
26
26
{
27
- Config : testAccEnvironmentDataSourceConfigSetup (systemName , envName ) +
27
+ Config : testAccEnvironmentDataSourceConfigSetup (systemName , envName , complexName ) +
28
28
testAccEnvironmentDataSourceConfigBasic (envName ),
29
29
Check : resource .ComposeAggregateTestCheckFunc (
30
30
resource .TestCheckResourceAttr ("data.ctrlplane_environment.test" , "name" , envName ),
@@ -35,7 +35,7 @@ func TestAccEnvironmentDataSource(t *testing.T) {
35
35
},
36
36
// Test complex filter
37
37
{
38
- Config : testAccEnvironmentDataSourceConfigSetup (systemName , envName ) +
38
+ Config : testAccEnvironmentDataSourceConfigSetup (systemName , envName , complexName ) +
39
39
testAccEnvironmentDataSourceConfigComplex (complexName ),
40
40
Check : resource .ComposeAggregateTestCheckFunc (
41
41
resource .TestCheckResourceAttr ("data.ctrlplane_environment.test_complex" , "name" , complexName ),
@@ -46,7 +46,7 @@ func TestAccEnvironmentDataSource(t *testing.T) {
46
46
},
47
47
// Test with complex filter
48
48
{
49
- Config : testAccEnvironmentDataSourceConfigWithComplexFilter (systemName , envName ),
49
+ Config : testAccEnvironmentDataSourceConfigWithComplexFilter (systemName , envName , complexName ),
50
50
Check : resource .ComposeAggregateTestCheckFunc (
51
51
resource .TestCheckResourceAttrSet ("data.ctrlplane_environment.test_complex" , "id" ),
52
52
resource .TestCheckResourceAttr ("data.ctrlplane_environment.test_complex" , "name" , complexName ),
@@ -60,27 +60,28 @@ func TestAccEnvironmentDataSource(t *testing.T) {
60
60
}
61
61
62
62
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
+
64
66
resource .Test (t , resource.TestCase {
65
67
PreCheck : func () { testAccPreCheck (t ) },
66
68
ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
67
69
Steps : []resource.TestStep {
70
+ // Test with missing name (should fail)
68
71
{
69
- Config : testAccEnvironmentDataSourceConfigMissingName (),
70
- // Test for missing required field (name)
72
+ Config : testAccEnvironmentDataSourceConfigMissingName (),
71
73
ExpectError : regexp .MustCompile (`The argument "name" is required` ),
72
74
},
75
+ // Test with a non-existent environment name (should fail)
73
76
{
74
- // Test for non-existent environment
75
- Config : testAccEnvironmentDataSourceConfigNonExistentEnv (),
77
+ Config : testAccEnvironmentDataSourceConfigNonExistentEnv (nonExistentEnvName ),
76
78
ExpectError : regexp .MustCompile (`Environment Not Found` ),
77
79
},
78
80
},
79
81
})
80
82
}
81
83
82
- func testAccEnvironmentDataSourceConfigSetup (systemName , envName string ) string {
83
- complexName := fmt .Sprintf ("complex-%s" , envName )
84
+ func testAccEnvironmentDataSourceConfigSetup (systemName , envName , complexName string ) string {
84
85
return fmt .Sprintf (`
85
86
resource "ctrlplane_system" "test" {
86
87
name = "%[1]s"
@@ -93,38 +94,40 @@ resource "ctrlplane_environment" "test" {
93
94
system_id = ctrlplane_system.test.id
94
95
description = "Test environment"
95
96
metadata = {
96
- key1 = "value1"
97
- key2 = "value2"
97
+ "key1" = "value1"
98
+ "key2" = "value2"
99
+ "test" = "true"
98
100
}
99
101
resource_filter = {
100
- type = "metadata"
101
- key = "environment"
102
+ type = "metadata"
103
+ key = "environment"
102
104
operator = "equals"
103
- value = "staging "
105
+ value = "production "
104
106
}
105
107
}
106
108
107
109
resource "ctrlplane_environment" "test_complex" {
108
110
name = "%[3]s"
109
111
system_id = ctrlplane_system.test.id
110
- description = "Test environment with complex filter "
112
+ description = "Complex environment"
111
113
metadata = {
112
- test = "true"
114
+ "key1" = "complex_value"
115
+ "complex" = "true"
113
116
}
114
117
resource_filter = {
115
- type = "comparison"
116
- operator = "and"
118
+ type = "comparison"
119
+ operator = "and"
117
120
conditions = [
118
121
{
119
122
type = "metadata"
120
123
key = "environment"
121
124
operator = "equals"
122
- value = "staging "
125
+ value = "production "
123
126
},
124
127
{
125
- type = "kind "
126
- operator = "equals "
127
- value = "Deployment "
128
+ type = "name "
129
+ operator = "contains "
130
+ value = "api "
128
131
}
129
132
]
130
133
}
@@ -141,39 +144,45 @@ data "ctrlplane_environment" "test" {
141
144
` , envName )
142
145
}
143
146
144
- func testAccEnvironmentDataSourceConfigWithComplexFilter (systemName , envName string ) string {
147
+ func testAccEnvironmentDataSourceConfigWithComplexFilter (systemName , envName , complexName string ) string {
145
148
return fmt .Sprintf (`
146
149
%s
147
150
148
151
data "ctrlplane_environment" "test_complex" {
149
- name = ctrlplane_environment.test_complex.name
152
+ name = "%[2]s"
150
153
system_id = ctrlplane_system.test.id
151
154
}
152
- ` , testAccEnvironmentDataSourceConfigSetup (systemName , envName ) )
155
+ ` , testAccEnvironmentDataSourceConfigSetup (systemName , envName , complexName ), complexName )
153
156
}
154
157
155
158
func testAccEnvironmentDataSourceConfigMissingName () string {
156
159
return `
160
+ resource "ctrlplane_system" "test" {
161
+ name = "test-system-missing-name"
162
+ slug = "test-system-missing-name"
163
+ description = "Test system"
164
+ }
165
+
157
166
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
160
169
}
161
170
`
162
171
}
163
172
164
- func testAccEnvironmentDataSourceConfigNonExistentEnv () string {
165
- return `
173
+ func testAccEnvironmentDataSourceConfigNonExistentEnv (envName string ) string {
174
+ return fmt . Sprintf ( `
166
175
resource "ctrlplane_system" "test" {
167
176
name = "test-system-nonexistent"
177
+ slug = "test-system-nonexistent"
168
178
description = "Test system"
169
- slug = "test-system-nonexistent"
170
179
}
171
180
172
181
data "ctrlplane_environment" "test" {
173
- name = "non-existent-environment "
182
+ name = "%[1]s "
174
183
system_id = ctrlplane_system.test.id
175
184
}
176
- `
185
+ ` , envName )
177
186
}
178
187
179
188
func testAccEnvironmentDataSourceConfigComplex (complexName string ) string {
0 commit comments