Skip to content

Commit

Permalink
Merge pull request #38258 from hashicorp/td-autoflex-errors-and-logging
Browse files Browse the repository at this point in the history
Adds validation of Diagnostics and logging to AutoFlEx tests
  • Loading branch information
gdavison committed Jul 5, 2024
2 parents 74cd6e1 + 857eb67 commit 48038cc
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 85 deletions.
126 changes: 78 additions & 48 deletions internal/framework/flex/auto_expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@
package flex

import (
"bytes"
"context"
"reflect"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflogtest"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
)

func TestExpand(t *testing.T) {
t.Parallel()

ctx := context.Background()

testString := "test"
testStringResult := "a"

Expand All @@ -34,25 +36,37 @@ func TestExpand(t *testing.T) {
testCases := autoFlexTestCases{
{
TestName: "nil Source and Target",
WantErr: true,
expectedDiags: diag.Diagnostics{
diag.NewErrorDiagnostic("AutoFlEx", "target (<nil>): invalid, want pointer"),
diag.NewErrorDiagnostic("AutoFlEx", "Expand[<nil>, <nil>]"),
},
},
{
TestName: "non-pointer Target",
Source: TestFlex00{},
Target: 0,
WantErr: true,
expectedDiags: diag.Diagnostics{
diag.NewErrorDiagnostic("AutoFlEx", "target (int): int, want pointer"),
diag.NewErrorDiagnostic("AutoFlEx", "Expand[flex.TestFlex00, int]"),
},
},
{
TestName: "non-struct Source",
Source: testString,
Target: &TestFlex00{},
WantErr: true,
expectedDiags: diag.Diagnostics{
diag.NewErrorDiagnostic("AutoFlEx", "does not implement attr.Value: string"),
diag.NewErrorDiagnostic("AutoFlEx", "Expand[string, *flex.TestFlex00]"),
},
},
{
TestName: "non-struct Target",
Source: TestFlex00{},
Target: &testString,
WantErr: true,
expectedDiags: diag.Diagnostics{
diag.NewErrorDiagnostic("AutoFlEx", "does not implement attr.Value: struct"),
diag.NewErrorDiagnostic("AutoFlEx", "Expand[flex.TestFlex00, *string]"),
},
},
{
TestName: "types.String to string",
Expand Down Expand Up @@ -82,7 +96,11 @@ func TestExpand(t *testing.T) {
TestName: "does not implement attr.Value Source",
Source: &TestFlexAWS01{Field1: "a"},
Target: &TestFlexAWS01{},
WantErr: true,
expectedDiags: diag.Diagnostics{
diag.NewErrorDiagnostic("AutoFlEx", "does not implement attr.Value: string"),
diag.NewErrorDiagnostic("AutoFlEx", "convert (Field1)"),
diag.NewErrorDiagnostic("AutoFlEx", "Expand[*flex.TestFlexAWS01, *flex.TestFlexAWS01]"),
},
},
{
TestName: "single string Source and single string Target",
Expand All @@ -101,6 +119,15 @@ func TestExpand(t *testing.T) {
Source: &TestFlexTF01{Field1: types.StringValue("a")},
Target: &TestFlexAWS03{},
WantTarget: &TestFlexAWS03{},
expectedLogLines: []map[string]any{
{
"@level": "info",
"@module": "provider",
"@message": "AutoFlex Expand; incompatible types",
"from": map[string]any{},
"to": float64(reflect.Int64),
},
},
},
{
TestName: "primtive types Source and primtive types Target",
Expand Down Expand Up @@ -247,8 +274,8 @@ func TestExpand(t *testing.T) {
},
},
{
Context: context.WithValue(ctx, ResourcePrefix, "Intent"),
TestName: "resource name prefix",
ContextFn: func(ctx context.Context) context.Context { return context.WithValue(ctx, ResourcePrefix, "Intent") },
TestName: "resource name prefix",
Source: &TestFlexTF16{
Name: types.StringValue("Ovodoghen"),
},
Expand Down Expand Up @@ -303,7 +330,7 @@ func TestExpand(t *testing.T) {
},
}

runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

func TestExpandGeneric(t *testing.T) {
Expand Down Expand Up @@ -655,7 +682,7 @@ func TestExpandGeneric(t *testing.T) {
},
}

runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

func TestExpandSimpleSingleNestedBlock(t *testing.T) {
Expand Down Expand Up @@ -701,7 +728,7 @@ func TestExpandSimpleSingleNestedBlock(t *testing.T) {
WantTarget: &aws03{Field1: aws01{Field1: aws.String("a"), Field2: 1}},
},
}
runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

func TestExpandComplexSingleNestedBlock(t *testing.T) {
Expand Down Expand Up @@ -752,7 +779,7 @@ func TestExpandComplexSingleNestedBlock(t *testing.T) {
WantTarget: &aws03{Field1: &aws02{Field1: &aws01{Field1: true, Field2: []string{"a", "b"}}}},
},
}
runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

func TestExpandStringEnum(t *testing.T) {
Expand All @@ -761,7 +788,6 @@ func TestExpandStringEnum(t *testing.T) {
var testEnum TestEnum
testEnumList := TestEnumList

ctx := context.Background()
testCases := autoFlexTestCases{
{
TestName: "valid value",
Expand All @@ -776,13 +802,12 @@ func TestExpandStringEnum(t *testing.T) {
WantTarget: &testEnum,
},
}
runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

func TestExpandListOfInt64(t *testing.T) {
t.Parallel()

ctx := context.Background()
testCases := autoFlexTestCases{
{
TestName: "valid value []int64",
Expand Down Expand Up @@ -869,13 +894,12 @@ func TestExpandListOfInt64(t *testing.T) {
WantTarget: &[]*int32{},
},
}
runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

func TestExpandSetOfInt64(t *testing.T) {
t.Parallel()

ctx := context.Background()
testCases := autoFlexTestCases{
{
TestName: "valid value []int64",
Expand Down Expand Up @@ -962,7 +986,7 @@ func TestExpandSetOfInt64(t *testing.T) {
WantTarget: &[]*int32{},
},
}
runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

func TestExpandListOfStringEnum(t *testing.T) {
Expand All @@ -972,7 +996,6 @@ func TestExpandListOfStringEnum(t *testing.T) {
var testEnumFoo testEnum = "foo"
var testEnumBar testEnum = "bar"

ctx := context.Background()
testCases := autoFlexTestCases{
{
TestName: "valid value",
Expand All @@ -996,7 +1019,7 @@ func TestExpandListOfStringEnum(t *testing.T) {
WantTarget: &[]testEnum{},
},
}
runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

func TestExpandSetOfStringEnum(t *testing.T) {
Expand All @@ -1006,7 +1029,6 @@ func TestExpandSetOfStringEnum(t *testing.T) {
var testEnumFoo testEnum = "foo"
var testEnumBar testEnum = "bar"

ctx := context.Background()
testCases := autoFlexTestCases{
{
TestName: "valid value",
Expand All @@ -1030,7 +1052,7 @@ func TestExpandSetOfStringEnum(t *testing.T) {
WantTarget: &[]testEnum{},
},
}
runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

func TestExpandSimpleNestedBlockWithStringEnum(t *testing.T) {
Expand All @@ -1045,7 +1067,6 @@ func TestExpandSimpleNestedBlockWithStringEnum(t *testing.T) {
Field2 TestEnum
}

ctx := context.Background()
testCases := autoFlexTestCases{
{
TestName: "single nested valid value",
Expand All @@ -1060,7 +1081,7 @@ func TestExpandSimpleNestedBlockWithStringEnum(t *testing.T) {
WantTarget: &aws01{Field1: 1, Field2: ""},
},
}
runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

func TestExpandComplexNestedBlockWithStringEnum(t *testing.T) {
Expand Down Expand Up @@ -1096,7 +1117,7 @@ func TestExpandComplexNestedBlockWithStringEnum(t *testing.T) {
WantTarget: &aws01{Field1: 1, Field2: &aws02{Field2: ""}},
},
}
runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

func TestExpandOptions(t *testing.T) {
Expand Down Expand Up @@ -1177,48 +1198,57 @@ func TestExpandOptions(t *testing.T) {
},
},
}
runAutoExpandTestCases(ctx, t, testCases)
runAutoExpandTestCases(t, testCases)
}

type autoFlexTestCase struct {
Context context.Context //nolint:containedctx // testing context use
Options []AutoFlexOptionsFunc
TestName string
Source any
Target any
WantErr bool
WantTarget any
WantDiff bool
ContextFn func(context.Context) context.Context
Options []AutoFlexOptionsFunc
TestName string
Source any
Target any
expectedDiags diag.Diagnostics
expectedLogLines []map[string]any
WantTarget any
WantDiff bool
}

type autoFlexTestCases []autoFlexTestCase

func runAutoExpandTestCases(ctx context.Context, t *testing.T, testCases autoFlexTestCases) {
func runAutoExpandTestCases(t *testing.T, testCases autoFlexTestCases) {
t.Helper()

for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.TestName, func(t *testing.T) {
t.Parallel()

testCtx := ctx //nolint:contextcheck // simplify use of testing context
if testCase.Context != nil {
testCtx = testCase.Context
ctx := context.Background()
if testCase.ContextFn != nil {
ctx = testCase.ContextFn(ctx)
}

err := Expand(testCtx, testCase.Source, testCase.Target, testCase.Options...)
gotErr := err != nil
var buf bytes.Buffer
ctx = tflogtest.RootLogger(ctx, &buf)

if gotErr != testCase.WantErr {
t.Errorf("gotErr = %v, wantErr = %v", gotErr, testCase.WantErr)
diags := Expand(ctx, testCase.Source, testCase.Target, testCase.Options...)

if diff := cmp.Diff(diags, testCase.expectedDiags); diff != "" {
t.Errorf("unexpected diagnostics difference: %s", diff)
}

lines, err := tflogtest.MultilineJSONDecode(&buf)
if err != nil {
t.Fatalf("Expand: decoding log lines: %s", err)
}
if diff := cmp.Diff(lines, testCase.expectedLogLines); diff != "" {
t.Errorf("unexpected log lines diff (+wanted, -got): %s", diff)
}

if gotErr {
if !testCase.WantErr {
t.Errorf("err = %q", err)
if !diags.HasError() {
if diff := cmp.Diff(testCase.Target, testCase.WantTarget); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
} else if diff := cmp.Diff(testCase.Target, testCase.WantTarget); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
})
}
Expand Down
Loading

0 comments on commit 48038cc

Please sign in to comment.