Skip to content

Commit

Permalink
refactor: ValidateHierarchicalRules tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KevFan committed Jan 30, 2024
1 parent e61aab4 commit 4ccea81
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
8 changes: 0 additions & 8 deletions pkg/library/reconcilers/target_ref_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ func TestReconcileTargetBackReference(t *testing.T) {
t.Fatal(err)
}

if res == nil {
t.Fatal("res is nil")
}

if len(res.GetAnnotations()) == 0 {
t.Fatal("annotations are empty")
}
Expand Down Expand Up @@ -199,10 +195,6 @@ func TestDeleteTargetBackReference(t *testing.T) {
t.Fatal(err)
}

if res == nil {
t.Fatal("res is nil")
}

if len(res.GetAnnotations()) > 0 {
_, ok := res.GetAnnotations()[annotationName]
if ok {
Expand Down
38 changes: 25 additions & 13 deletions pkg/library/utils/gatewayapi_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"reflect"
"testing"

"gotest.tools/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -925,35 +926,46 @@ func TestGetKuadrantNamespaceFromPolicyTargetRef(t *testing.T) {
func TestValidateHierarchicalRules(t *testing.T) {
hostname := gatewayapiv1.Hostname("*.example.com")
gateway := &gatewayapiv1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Namespace: "cool-namespace",
Name: "cool-gateway",
},
Spec: gatewayapiv1.GatewaySpec{Listeners: []gatewayapiv1.Listener{
{
Hostname: &hostname,
},
}},
}
httpRoute := &gatewayapiv1.HTTPRoute{
Spec: gatewayapiv1.HTTPRouteSpec{
Hostnames: []gatewayapiv1.Hostname{hostname},
},
}

policy1 := FakePolicy{Hosts: []string{"this.example.com", "*.example.com"}}
policy2 := FakePolicy{Hosts: []string{"*.z.com"}}

if err := ValidateHierarchicalRules(&policy1, gateway); err != nil {
t.Fatal(err)
}

expectedError := fmt.Errorf(
"rule host (%s) does not follow any hierarchical constraints, "+
t.Run("gateway - contains host", func(subT *testing.T) {
assert.NilError(subT, ValidateHierarchicalRules(&policy1, gateway))
})

t.Run("gateway error - host has no match", func(subT *testing.T) {
expectedError := fmt.Sprintf("rule host (%s) does not follow any hierarchical constraints, "+
"for the %T to be validated, it must match with at least one of the target network hostnames %+q",
"*.z.com",
&policy2,
[]string{"*.example.com"},
)
"*.z.com",
&policy2,
[]string{"*.example.com"},
)
assert.Error(subT, ValidateHierarchicalRules(&policy2, gateway), expectedError)
})

if err := ValidateHierarchicalRules(&policy2, gateway); err.Error() != expectedError.Error() {
t.Fatal("the error message does not match the expected error one", expectedError.Error(), err.Error())
}
t.Run("gateway - no hosts", func(subT *testing.T) {
assert.NilError(subT, ValidateHierarchicalRules(&policy1, &gatewayapiv1.Gateway{}))
})

t.Run("httpRoute - contains host ", func(subT *testing.T) {
assert.NilError(subT, ValidateHierarchicalRules(&policy1, httpRoute))
})
}

func TestIsHTTPRouteAccepted(t *testing.T) {
Expand Down

0 comments on commit 4ccea81

Please sign in to comment.