Skip to content

Commit

Permalink
use mock client for unit test (#3)
Browse files Browse the repository at this point in the history
* use mock client for unit test


Signed-off-by: bdlilley <[email protected]>

---------

Signed-off-by: bdlilley <[email protected]>
  • Loading branch information
bdlilley authored Jul 20, 2023
1 parent f5ff42a commit 4318968
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 77 deletions.
84 changes: 37 additions & 47 deletions pkg/mocks/plugin.go
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 17 additions & 23 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -176,43 +176,37 @@ 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 {
return nil, err
}

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,
Expand Down
9 changes: 6 additions & 3 deletions pkg/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
4 changes: 0 additions & 4 deletions pkg/plugin/testfiles/10-basic-canary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ rollout:
name: demo
ports:
- containerPort: 8080
# resources:
# limits:
# cpu: 100m
# memory: 100Mi
strategy:
canary:
canaryService: canary
Expand Down

0 comments on commit 4318968

Please sign in to comment.