Skip to content

Commit

Permalink
Simplify test for CredentialsValidator in plugin-framework code (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
SarahFrench authored Oct 28, 2024
1 parent eeb0e6e commit cca64da
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions mmv1/third_party/terraform/fwprovider/framework_validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,48 @@ package fwprovider_test

import (
"context"
"io/ioutil"
"testing"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/fwprovider"

transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func TestFrameworkProvider_CredentialsValidator(t *testing.T) {
cases := map[string]struct {
ConfigValue func(t *testing.T) types.String
ConfigValue types.String
ExpectedWarningCount int
ExpectedErrorCount int
}{
"configuring credentials as a path to a credentials JSON file is valid": {
ConfigValue: func(t *testing.T) types.String {
return types.StringValue(transport_tpg.TestFakeCredentialsPath) // Path to a test fixture
},
ConfigValue: types.StringValue(transport_tpg.TestFakeCredentialsPath), // Path to a test fixture
},
"configuring credentials as a path to a non-existant file is NOT valid": {
ConfigValue: func(t *testing.T) types.String {
return types.StringValue("./this/path/doesnt/exist.json") // Doesn't exist
},
ConfigValue: types.StringValue("./this/path/doesnt/exist.json"), // Doesn't exist
ExpectedErrorCount: 1,
},
"configuring credentials as a credentials JSON string is valid": {
ConfigValue: func(t *testing.T) types.String {
contents, err := ioutil.ReadFile(transport_tpg.TestFakeCredentialsPath)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
stringContents := string(contents)
return types.StringValue(stringContents)
},
ConfigValue: types.StringValue(acctest.GenerateFakeCredentialsJson("CredentialsValidator")),
},
"configuring credentials as an empty string is not valid": {
ConfigValue: func(t *testing.T) types.String {
return types.StringValue("")
},
ConfigValue: types.StringValue(""),
ExpectedErrorCount: 1,
},
"leaving credentials unconfigured is valid": {
ConfigValue: func(t *testing.T) types.String {
return types.StringNull()
},
ConfigValue: types.StringNull(),
},
}

for tn, tc := range cases {
t.Run(tn, func(t *testing.T) {
// Arrange
req := validator.StringRequest{
ConfigValue: tc.ConfigValue(t),
ConfigValue: tc.ConfigValue,
}

resp := validator.StringResponse{
Expand Down

0 comments on commit cca64da

Please sign in to comment.