Skip to content

Commit

Permalink
test: increase fetcher coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
KevFan committed Jan 30, 2024
1 parent 4ccea81 commit 1b0bfa7
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 52 deletions.
169 changes: 120 additions & 49 deletions pkg/library/reconcilers/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pkg/library/reconcilers/gateway_diffs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,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{
Expand Down
4 changes: 2 additions & 2 deletions pkg/library/reconcilers/target_ref_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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{
Expand Down Expand Up @@ -137,7 +137,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{
Expand Down

0 comments on commit 1b0bfa7

Please sign in to comment.