@@ -2,6 +2,7 @@ package spacelift
22
33import (
44 "fmt"
5+ "regexp"
56 "testing"
67
78 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -141,3 +142,139 @@ func TestEnvironmentVariableResource(t *testing.T) {
141142 })
142143 })
143144}
145+
146+ func TestEnvironmentVariableResourceNonsensitiveValue (t * testing.T ) {
147+ const resourceName = "spacelift_environment_variable.test"
148+
149+ t .Run ("with a context" , func (t * testing.T ) {
150+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
151+
152+ config := func (description string ) string {
153+ return fmt .Sprintf (`
154+ resource "spacelift_context" "test" {
155+ name = "My first context %s"
156+ }
157+
158+ resource "spacelift_environment_variable" "test" {
159+ context_id = spacelift_context.test.id
160+ name = "BACON"
161+ value_nonsensitive = "is tasty"
162+ write_only = false
163+ description = %s
164+ }
165+ ` , randomID , description )
166+ }
167+
168+ testSteps (t , []resource.TestStep {
169+ {
170+ Config : config (`"Bacon is tasty"` ),
171+ Check : Resource (
172+ resourceName ,
173+ Attribute ("id" , IsNotEmpty ()),
174+ Attribute ("checksum" , Equals ("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154" )),
175+ Attribute ("context_id" , Contains (randomID )),
176+ Attribute ("name" , Equals ("BACON" )),
177+ Attribute ("value_nonsensitive" , Equals ("is tasty" )),
178+ Attribute ("write_only" , Equals ("false" )),
179+ Attribute ("description" , Equals ("Bacon is tasty" )),
180+ AttributeNotPresent ("value" ),
181+ AttributeNotPresent ("module_id" ),
182+ AttributeNotPresent ("stack_id" ),
183+ ),
184+ },
185+ })
186+ })
187+
188+ t .Run ("with a module" , func (t * testing.T ) {
189+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
190+
191+ testSteps (t , []resource.TestStep {
192+ {
193+ Config : fmt .Sprintf (`
194+ resource "spacelift_module" "test" {
195+ name = "test-module-%s"
196+ branch = "master"
197+ repository = "terraform-bacon-tasty"
198+ }
199+
200+ resource "spacelift_environment_variable" "test" {
201+ module_id = spacelift_module.test.id
202+ name = "BACON"
203+ value_nonsensitive = "is tasty"
204+ write_only = false
205+ description = "Bacon is tasty"
206+ }
207+ ` , randomID ),
208+ Check : Resource (
209+ resourceName ,
210+ Attribute ("module_id" , Equals (fmt .Sprintf ("terraform-default-test-module-%s" , randomID ))),
211+ Attribute ("value_nonsensitive" , Equals ("is tasty" )),
212+ Attribute ("write_only" , Equals ("false" )),
213+ Attribute ("description" , Equals ("Bacon is tasty" )),
214+ AttributeNotPresent ("value" ),
215+ AttributeNotPresent ("context_id" ),
216+ AttributeNotPresent ("stack_id" ),
217+ ),
218+ },
219+ })
220+ })
221+
222+ t .Run ("with a stack" , func (t * testing.T ) {
223+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
224+
225+ testSteps (t , []resource.TestStep {
226+ {
227+ Config : fmt .Sprintf (`
228+ resource "spacelift_stack" "test" {
229+ branch = "master"
230+ repository = "demo"
231+ name = "Test stack %s"
232+ }
233+
234+ resource "spacelift_environment_variable" "test" {
235+ stack_id = spacelift_stack.test.id
236+ value_nonsensitive = "is tasty"
237+ write_only = false
238+ name = "BACON"
239+ description = "Bacon is tasty"
240+ }
241+ ` , randomID ),
242+ Check : Resource (
243+ resourceName ,
244+ Attribute ("stack_id" , StartsWith ("test-stack-" )),
245+ Attribute ("stack_id" , Contains (randomID )),
246+ Attribute ("value_nonsensitive" , Equals ("is tasty" )),
247+ Attribute ("description" , Equals ("Bacon is tasty" )),
248+ AttributeNotPresent ("value" ),
249+ AttributeNotPresent ("context_id" ),
250+ AttributeNotPresent ("module_id" ),
251+ ),
252+ },
253+ })
254+ })
255+
256+ t .Run ("write only is not allowed" , func (t * testing.T ) {
257+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
258+
259+ testSteps (t , []resource.TestStep {
260+ {
261+ Config : fmt .Sprintf (`
262+ resource "spacelift_stack" "test" {
263+ branch = "master"
264+ repository = "demo"
265+ name = "Test stack %s"
266+ }
267+
268+ resource "spacelift_environment_variable" "test" {
269+ stack_id = spacelift_stack.test.id
270+ value_nonsensitive = "is tasty"
271+ write_only = true
272+ name = "BACON"
273+ description = "Bacon is tasty"
274+ }
275+ ` , randomID ),
276+ ExpectError : regexp .MustCompile ("a non-sensitive environment variable cannot be write-only" ),
277+ },
278+ })
279+ })
280+ }
0 commit comments