From 43189689e472b6fd7a984922e507ab3d4b5a020e Mon Sep 17 00:00:00 2001 From: Ben Lilley <114938118+bdlilley@users.noreply.github.com> Date: Thu, 20 Jul 2023 15:34:49 -0400 Subject: [PATCH] use mock client for unit test (#3) * use mock client for unit test Signed-off-by: bdlilley --------- Signed-off-by: bdlilley --- pkg/mocks/plugin.go | 84 ++++++++++------------- pkg/plugin/plugin.go | 40 +++++------ pkg/plugin/plugin_test.go | 9 ++- pkg/plugin/testfiles/10-basic-canary.yaml | 4 -- 4 files changed, 60 insertions(+), 77 deletions(-) diff --git a/pkg/mocks/plugin.go b/pkg/mocks/plugin.go index 7468df5..271af26 100644 --- a/pkg/mocks/plugin.go +++ b/pkg/mocks/plugin.go @@ -1,55 +1,45 @@ package mocks import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "context" + "fmt" - commonv2 "github.com/solo-io/solo-apis/client-go/common.gloo.solo.io/v2" - networkv2 "github.com/solo-io/solo-apis/client-go/networking.gloo.solo.io/v2" + "github.com/argoproj-labs/rollouts-plugin-trafficrouter-glooplatform/pkg/gloo" + gloov2 "github.com/solo-io/solo-apis/client-go/networking.gloo.solo.io/v2" + k8sclient "sigs.k8s.io/controller-runtime/pkg/client" ) -const ( - RouteTableName = "mock" - RouteTableNamespace = "mock" - DestinationKind = "SERVICE" - DestinationNamespace = "mock" - StableService = "stable" - CanaryService = "canary" - RolloutNamespace = "mock" - RolloutName = "mock" -) - -var RouteTable = networkv2.RouteTable{ - ObjectMeta: metav1.ObjectMeta{ - Name: RouteTableName, - Namespace: RouteTableNamespace, - }, - Spec: networkv2.RouteTableSpec{ - Hosts: []string{"*"}, - - Http: []*networkv2.HTTPRoute{ - { - Name: RouteTableName, - ActionType: &networkv2.HTTPRoute_ForwardTo{ - ForwardTo: &networkv2.ForwardToAction{ - Destinations: []*commonv2.DestinationReference{ - { - Kind: commonv2.DestinationKind_SERVICE, - Port: &commonv2.PortSelector{ - Specifier: &commonv2.PortSelector_Number{ - Number: 8000, - }, - }, - RefKind: &commonv2.DestinationReference_Ref{ - Ref: &commonv2.ObjectReference{ - Name: StableService, - Namespace: DestinationNamespace, - }, - }, - }, - }, - }, - }, - }, +func NewGlooMockClient(routeTables []*gloov2.RouteTable) gloo.NetworkV2ClientSet { + return &GlooMockClient{ + rtClient: &glooMockRouteTableClient{ + routeTables: routeTables, }, - }, + } +} + +type GlooMockClient struct { + rtClient *glooMockRouteTableClient +} + +func (c GlooMockClient) RouteTables() gloo.RouteTableClient { + return c.rtClient +} + +type glooMockRouteTableClient struct { + routeTables []*gloov2.RouteTable +} + +func (c glooMockRouteTableClient) GetRouteTable(ctx context.Context, name string, namespace string) (*gloov2.RouteTable, error) { + if len(c.routeTables) > 0 { + return c.routeTables[0], nil + } + return nil, fmt.Errorf("routeTable not found: %s:%s", namespace, name) +} + +func (c glooMockRouteTableClient) PatchRouteTable(ctx context.Context, obj *gloov2.RouteTable, patch k8sclient.Patch, opts ...k8sclient.PatchOption) error { + return nil +} + +func (c glooMockRouteTableClient) ListRouteTable(ctx context.Context, opts ...k8sclient.ListOption) ([]*gloov2.RouteTable, error) { + return c.routeTables, nil } diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index 4008c36..133b36e 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -25,9 +25,9 @@ const ( type RpcPlugin struct { IsTest bool // temporary hack until mock clienset is fixed (missing some interface methods) - TestRouteTable *networkv2.RouteTable - LogCtx *logrus.Entry - Client gloo.NetworkV2ClientSet + // TestRouteTable *networkv2.RouteTable + LogCtx *logrus.Entry + Client gloo.NetworkV2ClientSet } type GlooPlatformAPITrafficRouting struct { @@ -176,7 +176,7 @@ func (r *RpcPlugin) getRouteTables(ctx context.Context, rollout *v1alpha1.Rollou var rts []*networkv2.RouteTable - if !r.IsTest && !strings.EqualFold(glooPluginConfig.RouteTableSelector.Name, "") { + if !strings.EqualFold(glooPluginConfig.RouteTableSelector.Name, "") { r.LogCtx.Debugf("getRouteTables using ns:name ref %s:%s to get single table", glooPluginConfig.RouteTableSelector.Name, glooPluginConfig.RouteTableSelector.Namespace) result, err := r.Client.RouteTables().GetRouteTable(ctx, glooPluginConfig.RouteTableSelector.Name, glooPluginConfig.RouteTableSelector.Namespace) if err != nil { @@ -184,35 +184,29 @@ func (r *RpcPlugin) getRouteTables(ctx context.Context, rollout *v1alpha1.Rollou } r.LogCtx.Debugf("getRouteTables using ns:name ref %s:%s found 1 table", glooPluginConfig.RouteTableSelector.Name, glooPluginConfig.RouteTableSelector.Namespace) - rts = []*networkv2.RouteTable{result} - } - - matched := []*GlooMatchedRouteTable{} + rts = append(rts, result) + } else { + opts := &k8sclient.ListOptions{} - opts := &k8sclient.ListOptions{} - - if glooPluginConfig.RouteTableSelector.Labels != nil { - opts.LabelSelector = labels.SelectorFromSet(glooPluginConfig.RouteTableSelector.Labels) - } - if !strings.EqualFold(glooPluginConfig.RouteTableSelector.Namespace, "") { - opts.Namespace = glooPluginConfig.RouteTableSelector.Namespace - } + if glooPluginConfig.RouteTableSelector.Labels != nil { + opts.LabelSelector = labels.SelectorFromSet(glooPluginConfig.RouteTableSelector.Labels) + } + if !strings.EqualFold(glooPluginConfig.RouteTableSelector.Namespace, "") { + opts.Namespace = glooPluginConfig.RouteTableSelector.Namespace + } - r.LogCtx.Debugf("getRouteTables listing tables with opts %+v", opts) - var err error - if !r.IsTest { + r.LogCtx.Debugf("getRouteTables listing tables with opts %+v", opts) + var err error rts, err = r.Client.RouteTables().ListRouteTable(ctx, opts) if err != nil { return nil, err } + r.LogCtx.Debugf("getRouteTables listing tables with opts %+v; found %d routeTables", opts, len(rts)) } - if r.IsTest { - rts = []*networkv2.RouteTable{r.TestRouteTable} - } + matched := []*GlooMatchedRouteTable{} - r.LogCtx.Debugf("getRouteTables listing tables with opts %+v; found %d routeTables", opts, len(rts)) for _, rt := range rts { matchedRt := &GlooMatchedRouteTable{ RouteTable: rt, diff --git a/pkg/plugin/plugin_test.go b/pkg/plugin/plugin_test.go index 78ea01f..468a649 100644 --- a/pkg/plugin/plugin_test.go +++ b/pkg/plugin/plugin_test.go @@ -12,6 +12,7 @@ import ( "github.com/PaesslerAG/gval" "github.com/PaesslerAG/jsonpath" + "github.com/argoproj-labs/rollouts-plugin-trafficrouter-glooplatform/pkg/mocks" "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1" rolloutsPlugin "github.com/argoproj/argo-rollouts/rollout/trafficrouting/plugin/rpc" "github.com/ghodss/yaml" @@ -63,10 +64,12 @@ func (tc *TestCase) Test(t *testing.T) error { ctx, cancel := context.WithCancel(context.Background()) defer cancel() + mockClient := mocks.NewGlooMockClient([]*networkv2.RouteTable{tc.RouteTable}) + rpcPluginImp := &RpcPlugin{ - LogCtx: logCtx, - IsTest: true, - TestRouteTable: tc.RouteTable, + LogCtx: logCtx, + IsTest: true, + Client: mockClient, } var pluginMap = map[string]goPlugin.Plugin{ diff --git a/pkg/plugin/testfiles/10-basic-canary.yaml b/pkg/plugin/testfiles/10-basic-canary.yaml index b8b84d7..421b1a9 100644 --- a/pkg/plugin/testfiles/10-basic-canary.yaml +++ b/pkg/plugin/testfiles/10-basic-canary.yaml @@ -20,10 +20,6 @@ rollout: name: demo ports: - containerPort: 8080 - # resources: - # limits: - # cpu: 100m - # memory: 100Mi strategy: canary: canaryService: canary