Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions pkg/scenario/stage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package scenario

import (
"fmt"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/stretchr/testify/assert"
"reflect"
"strconv"
"testing"
Expand All @@ -13,6 +16,22 @@ import (

type StageClient struct{}

type TestType int

const (
ShouldContain = iota
ShouldNOTContain
ShouldBeEqual
)

type JSONPathTestCases struct {
TestName string
ExpectedValue interface{}
JSONPathToCompare string
AllowDifferentType bool
TestType TestType
}

type Stage interface {
DestroyStage(t *testing.T, options *terraform.Options)
PlanStage(t *testing.T, options *terraform.Options)
Expand All @@ -26,6 +45,7 @@ type Stage interface {
PlanWithResourcesExpectedToBeDeleted(t *testing.T, options *terraform.Options, resources []string)
PlanWithResourcesExpectedToBeUpdated(t *testing.T, options *terraform.Options, resources []string)
PlanWithSpecificVariableValueToExpect(t *testing.T, options *terraform.Options, variable, value string)
PlanAndAssertJSONWithJSONPath(t *testing.T, options *terraform.Options, testCases []JSONPathTestCases)
}

func (c *StageClient) DestroyStage(t *testing.T, options *terraform.Options) {
Expand Down Expand Up @@ -166,6 +186,50 @@ func (c *StageClient) PlanWithSpecificVariableValueToExpect(t *testing.T, option
compareValues(t, actualValue, expectedValue, variable)
}

// PlanAndAssertJSONWithJSONPath performs JSON path planning and assertion in Go testing.
//
// t *testing.T: Testing object
// options *terraform.Options: Terraform options
// testCases []JSONPathTestCases: Array of JSON path test cases
func (c *StageClient) PlanAndAssertJSONWithJSONPath(t *testing.T, options *terraform.Options, testCases []JSONPathTestCases) {
jsonPlan := terraform.InitAndPlanAndShow(t, options)

for _, testCase := range testCases {
t.Run(testCase.TestName, func(t *testing.T) {
var result []interface{}
k8s.UnmarshalJSONPath(t, []byte(jsonPlan), testCase.JSONPathToCompare, &result)
assert.NotNil(t, result)

// if expected is slice then it's ok to compare entire slice
if reflect.TypeOf(testCase.ExpectedValue).Kind() == reflect.Slice {
assert.ObjectsAreEqual(testCase.ExpectedValue, result)
} else {
// work on the result[0]
t.Logf("result returned raw: %v", result)
v := result[0]
if !testCase.AllowDifferentType {
assert.Equal(t, reflect.TypeOf(testCase.ExpectedValue).Kind(), reflect.TypeOf(v).Kind())
}
applyTestType(t, testCase.TestType, v, testCase.ExpectedValue, fmt.Sprintf("JSONPATH query: %s", testCase.JSONPathToCompare))
}
})
}
}

func applyTestType(t *testing.T, testType TestType, actual, expected interface{}, additionalMessage string) {
switch testType {
case ShouldContain:
msg := fmt.Sprintf("Output did not contains the expected value. Expected: %v, Actual: %v, %s", expected, actual, additionalMessage)
assert.Contains(t, actual, expected, msg)
case ShouldNOTContain:
msg := fmt.Sprintf("Output is expected NOT to contains the value. Expected: %v, Actual: %v, %s", expected, actual, additionalMessage)
assert.NotContains(t, actual, expected, msg)
case ShouldBeEqual:
msg := fmt.Sprintf("Output did not match with expected value. Expected: %v, Actual: %v, %s", expected, actual, additionalMessage)
assert.EqualValuesf(t, actual, expected, msg)
}
}

func compareValues(t *testing.T, actual interface{}, expected, variableName string) {
actualType := reflect.TypeOf(actual)
if actualType == nil {
Expand Down