Skip to content

Commit

Permalink
tests: add envtests unit test for HTTPRouteReconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Mar 21, 2023
1 parent 33959c1 commit bdb14e7
Show file tree
Hide file tree
Showing 4 changed files with 386 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func startKongAdminAPIServiceReconciler(ctx context.Context, t *testing.T, clien

mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Logger: logrusr.New(logrus.New()),
Scheme: scheme.Scheme,
Scheme: client.Scheme(),
SyncPeriod: lo.ToPtr(2 * time.Second),
MetricsBindAddress: "0",
})
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestKongAdminAPIController(t *testing.T) {
// In tests below we use a deferred cancel to stop the manager and not wait
// for its timeout.

cfg := envtest.Setup(t)
cfg := envtest.Setup(t, scheme.Scheme)
client, err := ctrlclient.New(cfg, ctrlclient.Options{})
require.NoError(t, err)

Expand Down
29 changes: 29 additions & 0 deletions internal/controllers/gateway/dataplane_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gateway

import (
"sigs.k8s.io/controller-runtime/pkg/client"

k8sobj "github.com/kong/kubernetes-ingress-controller/v2/internal/util/kubernetes/object"
)

type DataplaneMock struct {
KubernetesObjectReportsEnabled bool
// Mapping namespace to name to status
ObjectsStatuses map[string]map[string]k8sobj.ConfigurationStatus
}

func (d DataplaneMock) UpdateObject(obj client.Object) error {
return nil
}

func (d DataplaneMock) DeleteObject(obj client.Object) error {
return nil
}

func (d DataplaneMock) AreKubernetesObjectReportsEnabled() bool {
return d.KubernetesObjectReportsEnabled
}

func (d DataplaneMock) KubernetesObjectConfigurationStatus(obj client.Object) k8sobj.ConfigurationStatus {
return d.ObjectsStatuses[obj.GetNamespace()][obj.GetName()]
}
12 changes: 10 additions & 2 deletions internal/controllers/gateway/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/source"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

"github.com/kong/kubernetes-ingress-controller/v2/internal/dataplane"
"github.com/kong/kubernetes-ingress-controller/v2/internal/util"
k8sobj "github.com/kong/kubernetes-ingress-controller/v2/internal/util/kubernetes/object"
)
Expand All @@ -32,13 +31,22 @@ import (
// HTTPRoute Controller - HTTPRouteReconciler
// -----------------------------------------------------------------------------

// TODO: This can probably be useful in other reconcilers as well.
type DataPlane interface {
UpdateObject(obj client.Object) error
DeleteObject(obj client.Object) error

AreKubernetesObjectReportsEnabled() bool
KubernetesObjectConfigurationStatus(obj client.Object) k8sobj.ConfigurationStatus
}

// HTTPRouteReconciler reconciles an HTTPRoute object.
type HTTPRouteReconciler struct {
client.Client

Log logr.Logger
Scheme *runtime.Scheme
DataplaneClient *dataplane.KongClient
DataplaneClient DataPlane
// If EnableReferenceGrant is true, we will check for ReferenceGrant if backend in another
// namespace is in backendRefs.
// If it is false, referencing backend in different namespace will be rejected.
Expand Down

0 comments on commit bdb14e7

Please sign in to comment.