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 Oct 4, 2023
1 parent f8ab8a3 commit 1385b9d
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 49 deletions.
4 changes: 2 additions & 2 deletions controllers/gateway_kuadrant_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var _ = Describe("Kuadrant Gateway controller", func() {
return false
}

if meta.IsStatusConditionFalse(existingGateway.Status.Conditions, common.GatewayProgrammedConditionType) {
if meta.IsStatusConditionFalse(existingGateway.Status.Conditions, string(gatewayapiv1beta1.GatewayConditionProgrammed)) {
logf.Log.V(1).Info("[WARN] Gateway not ready")
return false
}
Expand Down Expand Up @@ -105,7 +105,7 @@ var _ = Describe("Kuadrant Gateway controller", func() {
return false
}

if meta.IsStatusConditionFalse(existingGateway.Status.Conditions, common.GatewayProgrammedConditionType) {
if meta.IsStatusConditionFalse(existingGateway.Status.Conditions, string(gatewayapiv1beta1.GatewayConditionProgrammed)) {
logf.Log.V(1).Info("[WARN] Gateway not ready")
return false
}
Expand Down
164 changes: 117 additions & 47 deletions pkg/library/reconcilers/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package reconcilers

import (
"context"
"fmt"
"reflect"
"testing"

"github.com/go-logr/logr"
"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 +21,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 +38,139 @@ func TestFetchTargetRefObject(t *testing.T) {
t.Fatal(err)
}

targetRef := gatewayapiv1alpha2.PolicyTargetReference{
routeTargetRef := gatewayapiv1alpha2.PolicyTargetReference{
Group: "gateway.networking.k8s.io",
Kind: "HTTPRoute",
Name: gatewayapiv1beta1.ObjectName(routeName),
}

existingRoute := &gatewayapiv1beta1.HTTPRoute{
TypeMeta: metav1.TypeMeta{
APIVersion: "gateway.networking.k8s.io/v1beta1",
Kind: "HTTPRoute",
},
ObjectMeta: metav1.ObjectMeta{
Name: routeName,
Namespace: namespace,
},
Spec: gatewayapiv1beta1.HTTPRouteSpec{
CommonRouteSpec: gatewayapiv1beta1.CommonRouteSpec{
ParentRefs: []gatewayapiv1beta1.ParentReference{
{
Name: "gwName",
},
},
gatewayTargetRef := gatewayapiv1alpha2.PolicyTargetReference{
Group: "gateway.networking.k8s.io",
Kind: "Gateway",
Name: gatewayapiv1beta1.ObjectName(gatewayName),
}

routeFactory := func(status metav1.ConditionStatus) *gatewayapiv1beta1.HTTPRoute {
return &gatewayapiv1beta1.HTTPRoute{
TypeMeta: metav1.TypeMeta{
APIVersion: "gateway.networking.k8s.io/v1beta1",
Kind: "HTTPRoute",
},
},
Status: gatewayapiv1beta1.HTTPRouteStatus{
RouteStatus: gatewayapiv1beta1.RouteStatus{
Parents: []gatewayapiv1beta1.RouteParentStatus{
{
ParentRef: gatewayapiv1beta1.ParentReference{
ObjectMeta: metav1.ObjectMeta{
Name: routeName,
Namespace: namespace,
},
Spec: gatewayapiv1beta1.HTTPRouteSpec{
CommonRouteSpec: gatewayapiv1beta1.CommonRouteSpec{
ParentRefs: []gatewayapiv1beta1.ParentReference{
{
Name: "gwName",
},
Conditions: []metav1.Condition{
{
Type: "Accepted",
Status: metav1.ConditionTrue,
},
},
},
Status: gatewayapiv1beta1.HTTPRouteStatus{
RouteStatus: gatewayapiv1beta1.RouteStatus{
Parents: []gatewayapiv1beta1.RouteParentStatus{
{
ParentRef: gatewayapiv1beta1.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) *gatewayapiv1beta1.Gateway {
return &gatewayapiv1beta1.Gateway{
TypeMeta: metav1.TypeMeta{
Kind: "Gateway",
APIVersion: "gateway.networking.k8s.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: gatewayName,
Namespace: namespace,
},
Status: gatewayapiv1beta1.GatewayStatus{
Conditions: []metav1.Condition{
{
Type: string(gatewayapiv1beta1.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 *gatewayapiv1beta1.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 *gatewayapiv1beta1.HTTPRoute:
if !reflect.DeepEqual(obj, existing) {
t.Fatal("res spec not as expected")
}
case *gatewayapiv1beta1.Gateway:
if !reflect.DeepEqual(obj, existing) {
t.Fatal("res spec not as expected")
}
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, (*gatewayapiv1beta1.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, (*gatewayapiv1beta1.Gateway)(nil))
})

t.Run("unknown network resource", func(subT *testing.T) {
ns := gatewayapiv1beta1.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 Down

0 comments on commit 1385b9d

Please sign in to comment.