From 9b640b53c970d6afbe4f9655dedb095f60c607aa Mon Sep 17 00:00:00 2001 From: Mattia Lavacca Date: Fri, 3 Nov 2023 14:04:54 +0100 Subject: [PATCH] fix: flaky test fixed Signed-off-by: Mattia Lavacca --- pkg/i2gw/providers/kong/converter_test.go | 4 +-- pkg/i2gw/providers/kong/utils_test.go | 40 ++++++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/pkg/i2gw/providers/kong/converter_test.go b/pkg/i2gw/providers/kong/converter_test.go index 6ff01316..6629a5de 100644 --- a/pkg/i2gw/providers/kong/converter_test.go +++ b/pkg/i2gw/providers/kong/converter_test.go @@ -344,9 +344,7 @@ func Test_ToGateway(t *testing.T) { key := types.NamespacedName{Namespace: got.Namespace, Name: got.Name} want := tc.expectedGatewayResources.HTTPRoutes[key] want.SetGroupVersionKind(common.HTTPRouteGVK) - if !apiequality.Semantic.DeepEqual(got, want) { - t.Errorf("Expected HTTPRoute %s to be %+v\n Got: %+v\n Diff: %s", i, want, got, cmp.Diff(want, got)) - } + assertHTTPRoutesEqual(t, i, got, want) } } diff --git a/pkg/i2gw/providers/kong/utils_test.go b/pkg/i2gw/providers/kong/utils_test.go index b500dce9..9f7fdf0b 100644 --- a/pkg/i2gw/providers/kong/utils_test.go +++ b/pkg/i2gw/providers/kong/utils_test.go @@ -16,7 +16,45 @@ limitations under the License. package kong -import gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" +import ( + "testing" + + apiequality "k8s.io/apimachinery/pkg/api/equality" + "k8s.io/apimachinery/pkg/types" + gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" + + "github.com/google/go-cmp/cmp" +) + +func assertHTTPRoutesEqual(t *testing.T, i types.NamespacedName, got, want gatewayv1beta1.HTTPRoute) { + if !apiequality.Semantic.DeepEqual(got.ObjectMeta, want.ObjectMeta) { + t.Errorf("Expected HTTPRoute ObjectMeta %s to be %+v\n Got: %+v\n Diff: %s", i, want.ObjectMeta, got.ObjectMeta, cmp.Diff(want.ObjectMeta, got.ObjectMeta)) + } + if !apiequality.Semantic.DeepEqual(got.Spec.Hostnames, want.Spec.Hostnames) { + t.Errorf("Expected HTTPRoute %s to be %+v\n Got: %+v\n Diff: %s", i, want.Spec.Hostnames, got.Spec.Hostnames, cmp.Diff(want.Spec.Hostnames, got.Spec.Hostnames)) + } + if !apiequality.Semantic.DeepEqual(got.Spec.ParentRefs, want.Spec.ParentRefs) { + t.Errorf("Expected HTTPRoute %s to be %+v\n Got: %+v\n Diff: %s", i, want.Spec.ParentRefs, got.Spec.ParentRefs, cmp.Diff(want.Spec.ParentRefs, got.Spec.ParentRefs)) + } + for _, r := range want.Spec.Rules { + if !httpRouteRulesContainsRule(got.Spec.Rules, r) { + t.Errorf("Expected HTTPRoute %s to have rule %+v\n Got: %+v\n Diff: %s", i, got.Spec.Rules, r, cmp.Diff(want, got)) + } + } +} + +func httpRouteRulesContainsRule(httpRouteRules []gatewayv1beta1.HTTPRouteRule, rule gatewayv1beta1.HTTPRouteRule) bool { + var found bool + for _, r := range httpRouteRules { + if apiequality.Semantic.DeepEqual(r, rule) { + found = true + } + if found { + break + } + } + return found +} func containsMatches(matches []gatewayv1beta1.HTTPRouteMatch, matchesToCheck []gatewayv1beta1.HTTPRouteMatch) bool { if len(matches) != len(matchesToCheck) {