diff --git a/pkg/library/reconcilers/fetcher_test.go b/pkg/library/reconcilers/fetcher_test.go index 6efc75d97..09541a92e 100644 --- a/pkg/library/reconcilers/fetcher_test.go +++ b/pkg/library/reconcilers/fetcher_test.go @@ -2,10 +2,13 @@ package reconcilers import ( "context" + "fmt" "reflect" "testing" "github.com/go-logr/logr" + "github.com/google/go-cmp/cmp" + "gotest.tools/assert" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -19,8 +22,9 @@ import ( func TestFetchTargetRefObject(t *testing.T) { var ( - namespace = "operator-unittest" - routeName = "my-route" + namespace = "operator-unittest" + routeName = "my-route" + gatewayName = "my-gw" ) baseCtx := context.Background() ctx := logr.NewContext(baseCtx, log.Log) @@ -35,72 +39,139 @@ func TestFetchTargetRefObject(t *testing.T) { t.Fatal(err) } - targetRef := gatewayapiv1alpha2.PolicyTargetReference{ + routeTargetRef := gatewayapiv1alpha2.PolicyTargetReference{ Group: "gateway.networking.k8s.io", Kind: "HTTPRoute", Name: gatewayapiv1.ObjectName(routeName), } - existingRoute := &gatewayapiv1.HTTPRoute{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "gateway.networking.k8s.io/v1beta1", - Kind: "HTTPRoute", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: routeName, - Namespace: namespace, - }, - Spec: gatewayapiv1.HTTPRouteSpec{ - CommonRouteSpec: gatewayapiv1.CommonRouteSpec{ - ParentRefs: []gatewayapiv1.ParentReference{ - { - Name: "gwName", - }, - }, + gatewayTargetRef := gatewayapiv1alpha2.PolicyTargetReference{ + Group: "gateway.networking.k8s.io", + Kind: "Gateway", + Name: gatewayapiv1.ObjectName(gatewayName), + } + + routeFactory := func(status metav1.ConditionStatus) *gatewayapiv1.HTTPRoute { + return &gatewayapiv1.HTTPRoute{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "gateway.networking.k8s.io/v1", + Kind: "HTTPRoute", }, - }, - Status: gatewayapiv1.HTTPRouteStatus{ - RouteStatus: gatewayapiv1.RouteStatus{ - Parents: []gatewayapiv1.RouteParentStatus{ - { - ParentRef: gatewayapiv1.ParentReference{ + ObjectMeta: metav1.ObjectMeta{ + Name: routeName, + Namespace: namespace, + }, + Spec: gatewayapiv1.HTTPRouteSpec{ + CommonRouteSpec: gatewayapiv1.CommonRouteSpec{ + ParentRefs: []gatewayapiv1.ParentReference{ + { Name: "gwName", }, - Conditions: []metav1.Condition{ - { - Type: "Accepted", - Status: metav1.ConditionTrue, + }, + }, + }, + Status: gatewayapiv1.HTTPRouteStatus{ + RouteStatus: gatewayapiv1.RouteStatus{ + Parents: []gatewayapiv1.RouteParentStatus{ + { + ParentRef: gatewayapiv1.ParentReference{ + Name: "gwName", + }, + Conditions: []metav1.Condition{ + { + Type: "Accepted", + Status: status, + }, }, }, }, }, }, - }, + } } - // Objects to track in the fake client. - objs := []runtime.Object{existingRoute} - - // Create a fake client to mock API calls. - clientAPIReader := fake.NewClientBuilder().WithRuntimeObjects(objs...).Build() - - res, err := FetchTargetRefObject(ctx, clientAPIReader, targetRef, namespace) - if err != nil { - t.Fatal(err) + gatewayFactory := func(status metav1.ConditionStatus) *gatewayapiv1.Gateway { + return &gatewayapiv1.Gateway{ + TypeMeta: metav1.TypeMeta{ + Kind: "Gateway", + APIVersion: "gateway.networking.k8s.io/v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: gatewayName, + Namespace: namespace, + }, + Status: gatewayapiv1.GatewayStatus{ + Conditions: []metav1.Condition{ + { + Type: string(gatewayapiv1.GatewayConditionProgrammed), + Status: status, + }, + }, + }, + } } - if res == nil { - t.Fatal("res is nil") + clientFactory := func(objs ...runtime.Object) client.WithWatch { + return fake.NewClientBuilder().WithRuntimeObjects(objs...).Build() } - switch obj := res.(type) { - case *gatewayapiv1.HTTPRoute: - if !reflect.DeepEqual(obj.Spec, existingRoute.Spec) { - t.Fatal("res spec not as expected") + assertion := func(res, existing client.Object) { + switch obj := res.(type) { + case *gatewayapiv1.HTTPRoute: + if !reflect.DeepEqual(obj, existing) { + t.Fatal("res spec not as expected", cmp.Diff(obj, existing)) + } + case *gatewayapiv1.Gateway: + if !reflect.DeepEqual(obj, existing) { + t.Fatal("res spec not as expected", cmp.Diff(obj, existing)) + } + default: + t.Fatal("res type not known") } - default: - t.Fatal("res type not known") } + + t.Run("fetch http route", func(subT *testing.T) { + existingRoute := routeFactory(metav1.ConditionTrue) + clientAPIReader := clientFactory(existingRoute) + res, err := FetchTargetRefObject(ctx, clientAPIReader, routeTargetRef, namespace) + assert.NilError(subT, err) + assert.Equal(subT, res == nil, false) + assertion(res, existingRoute) + }) + + t.Run("fetch http route - not accepted", func(subT *testing.T) { + existingRoute := routeFactory(metav1.ConditionFalse) + clientAPIReader := clientFactory(existingRoute) + res, err := FetchTargetRefObject(ctx, clientAPIReader, routeTargetRef, namespace) + assert.Error(subT, err, fmt.Sprintf("httproute (%s/%s) not accepted", namespace, routeName)) + assert.DeepEqual(subT, res, (*gatewayapiv1.HTTPRoute)(nil)) + }) + + t.Run("fetch gateway", func(subT *testing.T) { + existingGateway := gatewayFactory(metav1.ConditionTrue) + clientAPIReader := clientFactory(existingGateway) + res, err := FetchTargetRefObject(ctx, clientAPIReader, gatewayTargetRef, namespace) + assert.NilError(subT, err) + assert.Equal(subT, res == nil, false) + assertion(res, existingGateway) + }) + + t.Run("fetch gateway - not ready", func(subT *testing.T) { + existingGateway := gatewayFactory(metav1.ConditionFalse) + clientAPIReader := clientFactory(existingGateway) + res, err := FetchTargetRefObject(ctx, clientAPIReader, gatewayTargetRef, namespace) + assert.Error(subT, err, fmt.Sprintf("gateway (%s/%s) not ready", namespace, gatewayName)) + assert.DeepEqual(subT, res, (*gatewayapiv1.Gateway)(nil)) + }) + + t.Run("unknown network resource", func(subT *testing.T) { + ns := gatewayapiv1.Namespace(namespace) + targetRef := gatewayapiv1alpha2.PolicyTargetReference{Kind: "Service", Name: "my-sv", Namespace: &ns} + clientAPIReader := clientFactory() + res, err := FetchTargetRefObject(ctx, clientAPIReader, targetRef, namespace) + assert.Error(subT, err, fmt.Sprintf("FetchValidTargetRef: targetRef (%v) to unknown network resource", targetRef)) + assert.DeepEqual(subT, res, nil) + }) } func TestFetchGateway(t *testing.T) { @@ -123,7 +194,7 @@ func TestFetchGateway(t *testing.T) { existingGateway := &gatewayapiv1.Gateway{ TypeMeta: metav1.TypeMeta{ - APIVersion: "gateway.networking.k8s.io/v1beta1", + APIVersion: "gateway.networking.k8s.io/v1", Kind: "Gateway", }, ObjectMeta: metav1.ObjectMeta{ @@ -185,7 +256,7 @@ func TestFetchHTTPRoute(t *testing.T) { existingRoute := &gatewayapiv1.HTTPRoute{ TypeMeta: metav1.TypeMeta{ - APIVersion: "gateway.networking.k8s.io/v1beta1", + APIVersion: "gateway.networking.k8s.io/v1", Kind: "HTTPRoute", }, ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/library/reconcilers/gateway_diffs_test.go b/pkg/library/reconcilers/gateway_diffs_test.go index 122d7ec79..a04f63605 100644 --- a/pkg/library/reconcilers/gateway_diffs_test.go +++ b/pkg/library/reconcilers/gateway_diffs_test.go @@ -256,7 +256,7 @@ func TestTargetedGatewayKeys(t *testing.T) { httpRoute := &gatewayapiv1.HTTPRoute{ TypeMeta: metav1.TypeMeta{ - APIVersion: "gateway.networking.k8s.io/v1beta1", + APIVersion: "gateway.networking.k8s.io/v1", Kind: "HTTPRoute", }, ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/library/reconcilers/target_ref_reconciler_test.go b/pkg/library/reconcilers/target_ref_reconciler_test.go index fedbede50..595b075c4 100644 --- a/pkg/library/reconcilers/target_ref_reconciler_test.go +++ b/pkg/library/reconcilers/target_ref_reconciler_test.go @@ -47,7 +47,7 @@ func TestReconcileTargetBackReference(t *testing.T) { existingRoute := &gatewayapiv1.HTTPRoute{ TypeMeta: metav1.TypeMeta{ - APIVersion: "gateway.networking.k8s.io/v1beta1", + APIVersion: "gateway.networking.k8s.io/v1", Kind: "HTTPRoute", }, ObjectMeta: metav1.ObjectMeta{ @@ -138,7 +138,7 @@ func TestDeleteTargetBackReference(t *testing.T) { existingRoute := &gatewayapiv1.HTTPRoute{ TypeMeta: metav1.TypeMeta{ - APIVersion: "gateway.networking.k8s.io/v1beta1", + APIVersion: "gateway.networking.k8s.io/v1", Kind: "HTTPRoute", }, ObjectMeta: metav1.ObjectMeta{