diff --git a/spacelift/resource_environment_variable_test.go b/spacelift/resource_environment_variable_test.go index 88c0633d..d6d5a806 100644 --- a/spacelift/resource_environment_variable_test.go +++ b/spacelift/resource_environment_variable_test.go @@ -11,10 +11,10 @@ import ( ) func TestEnvironmentVariableResource(t *testing.T) { - t.Run("with a context", func(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + const resourceName = "spacelift_environment_variable.test" - config := func(writeOnly bool) string { + t.Run("with a context", func(t *testing.T) { + config := func(randomID string, writeOnly bool, description string) string { return fmt.Sprintf(` resource "spacelift_context" "test" { name = "My first context %s" @@ -25,26 +25,110 @@ func TestEnvironmentVariableResource(t *testing.T) { name = "BACON" value = "is tasty" write_only = %t - description = "Bacon is tasty" + description = %s } - `, randomID, writeOnly) + `, randomID, writeOnly, description) } - const resourceName = "spacelift_environment_variable.test" + t.Run("with description", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + + testSteps(t, []resource.TestStep{ + { + Config: config(randomID, true, "\"Bacon is tasty\""), + Check: Resource( + resourceName, + Attribute("id", IsNotEmpty()), + Attribute("checksum", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")), + Attribute("context_id", Contains(randomID)), + Attribute("name", Equals("BACON")), + Attribute("value", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")), + Attribute("write_only", Equals("true")), + Attribute("description", Equals("Bacon is tasty")), + AttributeNotPresent("module_id"), + AttributeNotPresent("stack_id"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: config(randomID, false, "\"Bacon is tasty\""), + Check: Resource( + resourceName, + Attribute("value", Equals("is tasty")), + Attribute("write_only", Equals("false")), + Attribute("description", Equals("Bacon is tasty")), + ), + }, + }) + }) + + t.Run("null description", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + + testSteps(t, []resource.TestStep{ + { + Config: config(randomID, true, "null"), + Check: Resource( + resourceName, + Attribute("id", IsNotEmpty()), + Attribute("checksum", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")), + Attribute("context_id", Contains(randomID)), + Attribute("name", Equals("BACON")), + Attribute("value", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")), + Attribute("write_only", Equals("true")), + Attribute("description", IsEmpty()), + AttributeNotPresent("module_id"), + AttributeNotPresent("stack_id"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: config(randomID, false, "\"Bacon is tasty\""), + Check: Resource( + resourceName, + Attribute("value", Equals("is tasty")), + Attribute("write_only", Equals("false")), + Attribute("description", IsEmpty()), + ), + }, + }) + }) + }) + + t.Run("with a module", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) testSteps(t, []resource.TestStep{ { - Config: config(true), + Config: fmt.Sprintf(` + resource "spacelift_module" "test" { + name = "test-module-%s" + branch = "master" + repository = "terraform-bacon-tasty" + } + + resource "spacelift_environment_variable" "test" { + module_id = spacelift_module.test.id + name = "BACON" + value = "is tasty" + description = "Bacon is tasty" + } + `, randomID), Check: Resource( - "spacelift_environment_variable.test", - Attribute("id", IsNotEmpty()), - Attribute("checksum", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")), - Attribute("context_id", Contains(randomID)), - Attribute("name", Equals("BACON")), + resourceName, + Attribute("module_id", Equals(fmt.Sprintf("terraform-default-test-module-%s", randomID))), Attribute("value", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")), Attribute("write_only", Equals("true")), Attribute("description", Equals("Bacon is tasty")), - AttributeNotPresent("module_id"), + AttributeNotPresent("context_id"), AttributeNotPresent("stack_id"), ), }, @@ -53,23 +137,9 @@ func TestEnvironmentVariableResource(t *testing.T) { ImportState: true, ImportStateVerify: true, }, - { - Config: config(false), - Check: Resource( - "spacelift_environment_variable.test", - Attribute("value", Equals("is tasty")), - Attribute("write_only", Equals("false")), - Attribute("description", Equals("Bacon is tasty")), - ), - }, }) - }) - - t.Run("with a module", func(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - - const resourceName = "spacelift_environment_variable.test" + // Without description testSteps(t, []resource.TestStep{ { Config: fmt.Sprintf(` @@ -83,7 +153,6 @@ func TestEnvironmentVariableResource(t *testing.T) { module_id = spacelift_module.test.id name = "BACON" value = "is tasty" - description = "Bacon is tasty" } `, randomID), Check: Resource( @@ -91,7 +160,7 @@ func TestEnvironmentVariableResource(t *testing.T) { Attribute("module_id", Equals(fmt.Sprintf("terraform-default-test-module-%s", randomID))), Attribute("value", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")), Attribute("write_only", Equals("true")), - Attribute("description", Equals("Bacon is tasty")), + Attribute("description", IsEmpty()), AttributeNotPresent("context_id"), AttributeNotPresent("stack_id"), ), @@ -105,8 +174,6 @@ func TestEnvironmentVariableResource(t *testing.T) { }) t.Run("with a stack", func(t *testing.T) { - const resourceName = "spacelift_environment_variable.test" - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) testSteps(t, []resource.TestStep{ @@ -126,7 +193,7 @@ func TestEnvironmentVariableResource(t *testing.T) { } `, randomID), Check: Resource( - "spacelift_environment_variable.test", + resourceName, Attribute("stack_id", StartsWith("test-stack-")), Attribute("stack_id", Contains(randomID)), Attribute("value", Equals("4d5d01ea427b10dd483e8fce5b5149fb5a9814e9ee614176b756ca4a65c8f154")),