Skip to content

Commit

Permalink
Modifying ExpectKnown<Value|OutputValue|OutputValueAtPath> to handle …
Browse files Browse the repository at this point in the history
…null checking (#266)
  • Loading branch information
bendbennett committed Jan 18, 2024
1 parent c74a9e8 commit 178c2c4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 81 deletions.
30 changes: 7 additions & 23 deletions plancheck/expect_known_output_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package plancheck
import (
"context"
"fmt"
"reflect"

tfjson "github.com/hashicorp/terraform-json"

Expand All @@ -26,6 +25,10 @@ type expectKnownOutputValue struct {
func (e expectKnownOutputValue) CheckPlan(ctx context.Context, req CheckPlanRequest, resp *CheckPlanResponse) {
var change *tfjson.Change

if req.Plan == nil {
resp.Error = fmt.Errorf("plan is nil")
}

for address, oc := range req.Plan.OutputChanges {
if e.outputAddress == address {
change = oc
Expand All @@ -35,7 +38,7 @@ func (e expectKnownOutputValue) CheckPlan(ctx context.Context, req CheckPlanRequ
}

if change == nil {
resp.Error = fmt.Errorf("%s - Output not found in plan OutputChanges", e.outputAddress)
resp.Error = fmt.Errorf("%s - Output not found in plan", e.outputAddress)

return
}
Expand All @@ -48,27 +51,8 @@ func (e expectKnownOutputValue) CheckPlan(ctx context.Context, req CheckPlanRequ
return
}

if result == nil {
resp.Error = fmt.Errorf("value is null for output at path: %s", e.outputAddress)

return
}

switch reflect.TypeOf(result).Kind() {
case reflect.Bool,
reflect.Map,
reflect.Slice,
reflect.String:
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = fmt.Errorf("error checking value for output at path: %s, err: %s", e.outputAddress, err)

return
}
default:
errorStr := fmt.Sprintf("unrecognised output type: %T, known value type is %T", result, e.knownValue)
errorStr += "\n\nThis is an error in plancheck.ExpectKnownOutputValue.\nPlease report this to the maintainers."

resp.Error = fmt.Errorf(errorStr)
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = fmt.Errorf("error checking value for output at path: %s, err: %s", e.outputAddress, err)

return
}
Expand Down
30 changes: 7 additions & 23 deletions plancheck/expect_known_output_value_at_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package plancheck
import (
"context"
"fmt"
"reflect"

tfjson "github.com/hashicorp/terraform-json"

Expand All @@ -27,6 +26,10 @@ type expectKnownOutputValueAtPath struct {
func (e expectKnownOutputValueAtPath) CheckPlan(ctx context.Context, req CheckPlanRequest, resp *CheckPlanResponse) {
var change *tfjson.Change

if req.Plan == nil {
resp.Error = fmt.Errorf("plan is nil")
}

for address, oc := range req.Plan.OutputChanges {
if e.outputAddress == address {
change = oc
Expand All @@ -36,7 +39,7 @@ func (e expectKnownOutputValueAtPath) CheckPlan(ctx context.Context, req CheckPl
}

if change == nil {
resp.Error = fmt.Errorf("%s - Output not found in plan OutputChanges", e.outputAddress)
resp.Error = fmt.Errorf("%s - Output not found in plan", e.outputAddress)

return
}
Expand All @@ -49,27 +52,8 @@ func (e expectKnownOutputValueAtPath) CheckPlan(ctx context.Context, req CheckPl
return
}

if result == nil {
resp.Error = fmt.Errorf("value is null for output at path: %s.%s", e.outputAddress, e.outputPath.String())

return
}

switch reflect.TypeOf(result).Kind() {
case reflect.Bool,
reflect.Map,
reflect.Slice,
reflect.String:
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = fmt.Errorf("error checking value for output at path: %s.%s, err: %s", e.outputAddress, e.outputPath.String(), err)

return
}
default:
errorStr := fmt.Sprintf("unrecognised output type: %T, known value type is %T", result, e.knownValue)
errorStr += "\n\nThis is an error in plancheck.ExpectKnownOutputValueAtPath.\nPlease report this to the maintainers."

resp.Error = fmt.Errorf(errorStr)
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = fmt.Errorf("error checking value for output at path: %s.%s, err: %s", e.outputAddress, e.outputPath.String(), err)

return
}
Expand Down
7 changes: 3 additions & 4 deletions plancheck/expect_known_output_value_at_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestExpectKnownOutputValueAtPath_CheckPlan_ResourceNotFound(t *testing.T) {
),
},
},
ExpectError: regexp.MustCompile("test_resource_two_output - Output not found in plan OutputChanges"),
ExpectError: regexp.MustCompile("test_resource_two_output - Output not found in plan"),
},
},
})
Expand Down Expand Up @@ -91,11 +91,10 @@ func TestExpectKnownOutputValueAtPath_CheckPlan_AttributeValueNull(t *testing.T)
plancheck.ExpectKnownOutputValueAtPath(
"test_resource_one_output",
tfjsonpath.New("bool_attribute"),
knownvalue.BoolExact(true),
knownvalue.NullExact(),
),
},
},
ExpectError: regexp.MustCompile("value is null for output at path: test_resource_one_output.bool_attribute"),
},
},
})
Expand Down Expand Up @@ -1809,7 +1808,7 @@ func TestExpectKnownOutputValueAtPath_CheckPlan_UnknownAttributeType(t *testing.
},
},
},
expectedErr: fmt.Errorf("unrecognised output type: float32, known value type is knownvalue.int64Exact\n\nThis is an error in plancheck.ExpectKnownOutputValueAtPath.\nPlease report this to the maintainers."),
expectedErr: fmt.Errorf("error checking value for output at path: float32_output., err: expected json.Number value for Int64Exact check, got: float32"),
},
}

Expand Down
7 changes: 3 additions & 4 deletions plancheck/expect_known_output_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestExpectKnownOutputValue_CheckPlan_OutputNotFound(t *testing.T) {
),
},
},
ExpectError: regexp.MustCompile("bool_not_found - Output not found in plan OutputChanges"),
ExpectError: regexp.MustCompile("bool_not_found - Output not found in plan"),
},
},
})
Expand All @@ -72,11 +72,10 @@ func TestExpectKnownOutputValue_CheckPlan_AttributeValueNull(t *testing.T) {
PreApply: []plancheck.PlanCheck{
plancheck.ExpectKnownOutputValue(
"bool_output",
knownvalue.BoolExact(true),
knownvalue.NullExact(),
),
},
},
ExpectError: regexp.MustCompile("value is null for output at path: bool_output"),
},
},
})
Expand Down Expand Up @@ -1486,7 +1485,7 @@ func TestExpectKnownOutputValue_CheckPlan_UnknownAttributeType(t *testing.T) {
},
},
},
expectedErr: fmt.Errorf("unrecognised output type: float32, known value type is knownvalue.int64Exact\n\nThis is an error in plancheck.ExpectKnownOutputValue.\nPlease report this to the maintainers."),
expectedErr: fmt.Errorf("error checking value for output at path: float32_output, err: expected json.Number value for Int64Exact check, got: float32"),
},
}

Expand Down
30 changes: 7 additions & 23 deletions plancheck/expect_known_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package plancheck
import (
"context"
"fmt"
"reflect"

tfjson "github.com/hashicorp/terraform-json"

Expand All @@ -27,6 +26,10 @@ type expectKnownValue struct {
func (e expectKnownValue) CheckPlan(ctx context.Context, req CheckPlanRequest, resp *CheckPlanResponse) {
var rc *tfjson.ResourceChange

if req.Plan == nil {
resp.Error = fmt.Errorf("plan is nil")
}

for _, resourceChange := range req.Plan.ResourceChanges {
if e.resourceAddress == resourceChange.Address {
rc = resourceChange
Expand All @@ -36,7 +39,7 @@ func (e expectKnownValue) CheckPlan(ctx context.Context, req CheckPlanRequest, r
}

if rc == nil {
resp.Error = fmt.Errorf("%s - Resource not found in plan ResourceChanges", e.resourceAddress)
resp.Error = fmt.Errorf("%s - Resource not found in plan", e.resourceAddress)

return
}
Expand All @@ -49,27 +52,8 @@ func (e expectKnownValue) CheckPlan(ctx context.Context, req CheckPlanRequest, r
return
}

if result == nil {
resp.Error = fmt.Errorf("value is null for attribute at path: %s.%s", e.resourceAddress, e.attributePath.String())

return
}

switch reflect.TypeOf(result).Kind() {
case reflect.Bool,
reflect.Map,
reflect.Slice,
reflect.String:
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = fmt.Errorf("error checking value for attribute at path: %s.%s, err: %s", e.resourceAddress, e.attributePath.String(), err)

return
}
default:
errorStr := fmt.Sprintf("unrecognised attribute type: %T, known value type is %T", result, e.knownValue)
errorStr += "\n\nThis is an error in plancheck.ExpectKnownValue.\nPlease report this to the maintainers."

resp.Error = fmt.Errorf(errorStr)
if err := e.knownValue.CheckValue(result); err != nil {
resp.Error = fmt.Errorf("error checking value for attribute at path: %s.%s, err: %s", e.resourceAddress, e.attributePath.String(), err)

return
}
Expand Down
7 changes: 3 additions & 4 deletions plancheck/expect_known_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestExpectKnownValue_CheckPlan_ResourceNotFound(t *testing.T) {
),
},
},
ExpectError: regexp.MustCompile("test_resource.two - Resource not found in plan ResourceChanges"),
ExpectError: regexp.MustCompile("test_resource.two - Resource not found in plan"),
},
},
})
Expand All @@ -67,11 +67,10 @@ func TestExpectKnownValue_CheckPlan_AttributeValueNull(t *testing.T) {
plancheck.ExpectKnownValue(
"test_resource.one",
tfjsonpath.New("bool_attribute"),
knownvalue.BoolExact(true),
knownvalue.NullExact(),
),
},
},
ExpectError: regexp.MustCompile("value is null for attribute at path: test_resource.one.bool_attribute"),
},
},
})
Expand Down Expand Up @@ -1372,7 +1371,7 @@ func TestExpectKnownValue_CheckPlan_UnknownAttributeType(t *testing.T) {
},
},
},
expectedErr: fmt.Errorf("unrecognised attribute type: float32, known value type is knownvalue.int64Exact\n\nThis is an error in plancheck.ExpectKnownValue.\nPlease report this to the maintainers."),
expectedErr: fmt.Errorf("error checking value for attribute at path: example_resource.test.attribute, err: expected json.Number value for Int64Exact check, got: float32"),
},
}

Expand Down

0 comments on commit 178c2c4

Please sign in to comment.