Skip to content

Commit

Permalink
feat: convert environment variable resource tests and fix behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
sorccu committed Dec 6, 2024
1 parent f01b286 commit c4c8f40
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 59 deletions.
58 changes: 0 additions & 58 deletions checkly/resource_environment_variable_test.go

This file was deleted.

8 changes: 7 additions & 1 deletion internal/provider/resources/environment_variable_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,16 @@ func (m *EnvironmentVariableResourceModel) Refresh(ctx context.Context, from *ch
}

m.Key = types.StringValue(from.Key)
m.Value = types.StringValue(from.Value)
m.Locked = types.BoolValue(from.Locked)
m.Secret = types.BoolValue(from.Secret)

// We can never receive a secret value back from the server. Just assume
// the value is still unchanged and only update state if we're not dealing
// with a secret.
if !from.Secret {
m.Value = types.StringValue(from.Value)
}

return nil
}

Expand Down
70 changes: 70 additions & 0 deletions internal/provider/resources/environment_variable_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package resources_test

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccEnvVarCheckRequiredFields(t *testing.T) {
config := `resource "checkly_environment_variable" "test" {}`
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),

Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile(`The argument "key" is required`),
},
{
Config: config,
ExpectError: regexp.MustCompile(`The argument "value" is required`),
},
},
})
}

func TestAccEnvVarSuccess(t *testing.T) {
config := `resource "checkly_environment_variable" "test" {
key = "API_URL"
value = "https://api.checklyhq.com"
}`
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),

Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"checkly_environment_variable.test",
"key",
"API_URL",
),
resource.TestCheckResourceAttr(
"checkly_environment_variable.test",
"value",
"https://api.checklyhq.com",
),
),
},
},
})
}

func TestAccSecretEnvVarSuccess(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),

Steps: []resource.TestStep{
{
Config: `resource "checkly_environment_variable" "test" {
key = "SECRET"
value = "https://api.checklyhq.com"
secret = true
}`,
},
},
})
}

0 comments on commit c4c8f40

Please sign in to comment.