open-consul/api/config_entry_status_test.go
John Maguire c5b7164b16
APIGW Normalize Status Conditions (#16994)
* normalize status conditions for gateways and routes

* Added tests for checking condition status and panic conditions for
validating combinations, added dummy code for fsm store

* get rid of unneeded gateway condition generator struct

* Remove unused file

* run go mod tidy

* Update tests, add conflicted gateway status

* put back removed status for test

* Fix linting violation, remove custom conflicted status

* Update fsm commands oss

* Fix incorrect combination of type/condition/status

* cleaning up from PR review

* Change "invalidCertificate" to be of accepted status

* Move status condition enums into api package

* Update gateways controller and generated code

* Update conditions in fsm oss tests

* run go mod tidy on consul-container module to fix linting

* Fix type for gateway endpoint test

* go mod tidy from changes to api

* go mod tidy on troubleshoot

* Fix route conflicted reason

* fix route conflict reason rename

* Fix text for gateway conflicted status

* Add valid certificate ref condition setting

* Revert change to resolved refs to be handled in future PR
2023-04-24 16:22:55 -04:00

188 lines
5.5 KiB
Go

package api
import "testing"
func TestValidateGatewayConditionReasonWithValidCombinations(t *testing.T) {
testCases := map[string]struct {
status ConditionStatus
reason GatewayConditionReason
condType GatewayConditionType
}{
"accepted": {
status: ConditionStatusTrue,
reason: GatewayReasonAccepted,
condType: GatewayConditionAccepted,
},
"accepted invalid certificates": {
status: ConditionStatusFalse,
reason: GatewayReasonInvalidCertificates,
condType: GatewayConditionAccepted,
},
"conflicted": {
status: ConditionStatusTrue,
reason: GatewayReasonRouteConflict,
condType: GatewayConditionConflicted,
},
"conflicted no conflicts": {
status: ConditionStatusFalse,
reason: GatewayReasonNoConflict,
condType: GatewayConditionConflicted,
},
"resolved refs": {
status: ConditionStatusTrue,
reason: GatewayReasonResolvedRefs,
condType: GatewayConditionResolvedRefs,
},
"resolved refs invalid certificate ref": {
status: ConditionStatusFalse,
reason: GatewayListenerReasonInvalidCertificateRef,
condType: GatewayConditionResolvedRefs,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
err := ValidateGatewayConditionReason(tc.condType, tc.status, tc.reason)
if err != nil {
t.Error("Expected gateway condition reason to be valid but it was not")
}
})
}
}
func TestValidateGatewayConditionReasonWithInvalidCombinationsReturnsError(t *testing.T) {
// This is not an exhaustive list of all invalid combinations, just a few to confirm
testCases := map[string]struct {
status ConditionStatus
reason GatewayConditionReason
condType GatewayConditionType
}{
"reason and condition type are valid but status is not": {
status: ConditionStatusTrue,
reason: GatewayReasonNoConflict,
condType: GatewayConditionConflicted,
},
"reason and status are valid but condition type is not": {
status: ConditionStatusFalse,
reason: GatewayReasonNoConflict,
condType: GatewayConditionResolvedRefs,
},
"condition type and status are valid but status is not": {
status: ConditionStatusTrue,
reason: GatewayReasonNoConflict,
condType: GatewayConditionAccepted,
},
"all are invalid": {
status: ConditionStatusUnknown,
reason: GatewayReasonAccepted,
condType: GatewayConditionResolvedRefs,
},
"pass something other than a condition status": {
status: ConditionStatus("hello"),
reason: GatewayReasonAccepted,
condType: GatewayConditionResolvedRefs,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
err := ValidateGatewayConditionReason(tc.condType, tc.status, tc.reason)
if err == nil {
t.Error("Expected route condition reason to be invalid, but it was valid")
}
})
}
}
func TestValidateRouteConfigReasonWithValidCombinations(t *testing.T) {
testCases := map[string]struct {
status ConditionStatus
reason RouteConditionReason
condType RouteConditionType
}{
"accepted all around": {
status: ConditionStatusTrue,
reason: RouteReasonAccepted,
condType: RouteConditionAccepted,
},
"accepted invalid discovery chain": {
status: ConditionStatusFalse,
reason: RouteReasonInvalidDiscoveryChain,
condType: RouteConditionAccepted,
},
"accepted no upstream services targeted": {
status: ConditionStatusFalse,
reason: RouteReasonNoUpstreamServicesTargeted,
condType: RouteConditionAccepted,
},
"route bound": {
status: ConditionStatusTrue,
reason: RouteReasonBound,
condType: RouteConditionBound,
},
"route bound gateway not found": {
status: ConditionStatusFalse,
reason: RouteReasonGatewayNotFound,
condType: RouteConditionBound,
},
"route bound failed to bind": {
status: ConditionStatusFalse,
reason: RouteReasonFailedToBind,
condType: RouteConditionBound,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
err := ValidateRouteConditionReason(tc.condType, tc.status, tc.reason)
if err != nil {
t.Errorf("Expected route condition reason to be valid, it was not")
}
})
}
}
func TestValidateRouteConditionReasonInvalidCombinationsCausePanic(t *testing.T) {
// This is not an exhaustive list of all invalid combinations, just a few to confirm
testCases := map[string]struct {
status ConditionStatus
reason RouteConditionReason
condType RouteConditionType
}{
"reason and condition type are valid but status is not": {
status: ConditionStatusTrue,
reason: RouteReasonNoUpstreamServicesTargeted,
condType: RouteConditionAccepted,
},
"reason and status are valid but condition type is not": {
status: ConditionStatusFalse,
reason: RouteReasonInvalidDiscoveryChain,
condType: RouteConditionBound,
},
"condition type and status are valid but status is not": {
status: ConditionStatusUnknown,
reason: RouteReasonBound,
condType: RouteConditionBound,
},
"all are invalid": {
status: ConditionStatusUnknown,
reason: RouteReasonGatewayNotFound,
condType: RouteConditionBound,
},
"pass something other than a condition status": {
status: ConditionStatus("hello"),
reason: RouteReasonAccepted,
condType: RouteConditionAccepted,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
err := ValidateRouteConditionReason(tc.condType, tc.status, tc.reason)
if err == nil {
t.Error("Expected route condition reason to be invalid, it was valid")
}
})
}
}