Skip to content

Commit

Permalink
Add tests for TrafficSplit
Browse files Browse the repository at this point in the history
Signed-off-by: Nic Jackson <[email protected]>
  • Loading branch information
nicholasjackson committed Jul 7, 2021
1 parent 85b6375 commit 786b351
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 9 deletions.
47 changes: 47 additions & 0 deletions controllers/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package helpers

import (
"testing"
"time"

"github.com/niemeyer/pretty"
"github.com/stretchr/testify/mock"
)

// Helper functions to check and remove string from a slice of strings.
func ContainsString(slice []string, s string) bool {
for _, item := range slice {
Expand All @@ -19,3 +27,42 @@ func RemoveString(slice []string, s string) (result []string) {
}
return
}

func AssertCalledEventually(t *testing.T, m *MockAPI, method string, d time.Duration, args ...interface{}) {
done := make(chan struct{})

timeout := time.After(d)

go func() {
for {
called := false

for _, call := range m.Calls {
if call.Method == method {
_, differences := mock.Arguments(args).Diff(call.Arguments)

if differences == 0 {
// found the expected call
called = true
break
}
}
}

if called {
done <- struct{}{}
break
}

time.Sleep(100 * time.Millisecond)
}
}()

select {
case <-done:
return
case <-timeout:
t.Errorf("Expected method %s to be called with arguments %s", method, pretty.Sprint(args))
t.Fail()
}
}
89 changes: 87 additions & 2 deletions controllers/helpers/mockAPI.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/go-logr/logr"
accessv1alpha3 "github.com/servicemeshinterface/smi-controller-sdk/apis/access/v1alpha3"
specsv1alpha4 "github.com/servicemeshinterface/smi-controller-sdk/apis/specs/v1alpha4"
splitv1alpha4 "github.com/servicemeshinterface/smi-controller-sdk/apis/split/v1alpha4"
"github.com/stretchr/testify/mock"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -38,7 +39,7 @@ func (ms *MockAPI) DeleteTrafficTarget(

args := ms.Called(ctx, c, log, tt)

log.Info("Upsert TrafficTarget called")
log.Info("Delete TrafficTarget called")

return args.Get(0).(ctrl.Result), args.Error(1)
}
Expand Down Expand Up @@ -66,7 +67,91 @@ func (ms *MockAPI) DeleteTrafficSplit(

args := ms.Called(ctx, c, log, ts)

log.Info("Upsert TrafficSplit called")
log.Info("Delete TrafficSplit called")

return args.Get(0).(ctrl.Result), args.Error(1)
}

func (ms *MockAPI) UpsertHTTPRouteGroup(
ctx context.Context,
c client.Client,
log logr.Logger,
ts *specsv1alpha4.HTTPRouteGroup,
) (ctrl.Result, error) {

args := ms.Called(ctx, c, log, ts)

log.Info("Upsert HTTPRouteGroup called")

return args.Get(0).(ctrl.Result), args.Error(1)
}

func (ms *MockAPI) DeleteHTTPRouteGroup(
ctx context.Context,
c client.Client,
log logr.Logger,
ts *specsv1alpha4.HTTPRouteGroup,
) (ctrl.Result, error) {

args := ms.Called(ctx, c, log, ts)

log.Info("Delete HTTPRouteGroup called")

return args.Get(0).(ctrl.Result), args.Error(1)
}

func (ms *MockAPI) UpsertTCPRoute(
ctx context.Context,
c client.Client,
log logr.Logger,
ts *specsv1alpha4.TCPRoute,
) (ctrl.Result, error) {

args := ms.Called(ctx, c, log, ts)

log.Info("Upsert TCPRoute called")

return args.Get(0).(ctrl.Result), args.Error(1)
}

func (ms *MockAPI) DeleteTCPRoute(
ctx context.Context,
c client.Client,
log logr.Logger,
ts *specsv1alpha4.TCPRoute,
) (ctrl.Result, error) {

args := ms.Called(ctx, c, log, ts)

log.Info("Delete TCPRoute called")

return args.Get(0).(ctrl.Result), args.Error(1)
}

func (ms *MockAPI) UpsertUDPRoute(
ctx context.Context,
c client.Client,
log logr.Logger,
ts *specsv1alpha4.UDPRoute,
) (ctrl.Result, error) {

args := ms.Called(ctx, c, log, ts)

log.Info("Upsert UDPRoute called")

return args.Get(0).(ctrl.Result), args.Error(1)
}

func (ms *MockAPI) DeleteUDPRoute(
ctx context.Context,
c client.Client,
log logr.Logger,
ts *specsv1alpha4.UDPRoute,
) (ctrl.Result, error) {

args := ms.Called(ctx, c, log, ts)

log.Info("Delete UDPRoute called")

return args.Get(0).(ctrl.Result), args.Error(1)
}
6 changes: 6 additions & 0 deletions controllers/specs/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"path/filepath"
"testing"

"github.com/servicemeshinterface/smi-controller-sdk/controllers/helpers"
"github.com/servicemeshinterface/smi-controller-sdk/sdk"
"github.com/stretchr/testify/require"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -54,4 +56,8 @@ func setupSuite(t *testing.T) {
ErrorIfCRDPathMissing: true,
AttachControlPlaneOutput: false,
}

// create the mocks and register it with the SDK
mock := &helpers.MockAPI{}
sdk.API().RegisterV1Alpha(mock)
}
29 changes: 28 additions & 1 deletion controllers/split/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"

accessv1alpha1 "github.com/servicemeshinterface/smi-controller-sdk/apis/access/v1alpha1"
accessv1alpha2 "github.com/servicemeshinterface/smi-controller-sdk/apis/access/v1alpha2"
"github.com/servicemeshinterface/smi-controller-sdk/controllers/helpers"
"github.com/servicemeshinterface/smi-controller-sdk/sdk"

splitv1alpha1 "github.com/servicemeshinterface/smi-controller-sdk/apis/split/v1alpha1"
splitv1alpha2 "github.com/servicemeshinterface/smi-controller-sdk/apis/split/v1alpha2"
Expand All @@ -39,6 +43,7 @@ import (
var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var mockAPI *helpers.MockAPI

func TestAPIs(t *testing.T) {
t.Cleanup(func() {
Expand All @@ -54,7 +59,6 @@ func TestAPIs(t *testing.T) {
}

func setupSuite(t *testing.T) {
//logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,
Expand Down Expand Up @@ -85,7 +89,30 @@ func setupSuite(t *testing.T) {
require.NoError(t, err)

// +kubebuilder:scaffold:scheme

k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
})
require.NoError(t, err)

err = (&TrafficSplitReconciler{
Client: k8sManager.GetClient(),
}).SetupWithManager(k8sManager)
require.NoError(t, err)

go func() {
err = k8sManager.Start(ctrl.SetupSignalHandler())
require.NoError(t, err)
}()

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
require.NoError(t, err)
require.NotNil(t, k8sClient)

// create the mocks and register it with the SDK
mockAPI = &helpers.MockAPI{}
sdk.API().RegisterV1Alpha(mockAPI)

mockAPI.On("UpsertTrafficSplit", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(ctrl.Result{}, nil)
mockAPI.On("DeleteTrafficSplit", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(ctrl.Result{}, nil)
}
20 changes: 17 additions & 3 deletions controllers/split/trafficsplit_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"testing"
"time"

"github.com/niemeyer/pretty"
splitv1alpha4 "github.com/servicemeshinterface/smi-controller-sdk/apis/split/v1alpha4"
"github.com/servicemeshinterface/smi-controller-sdk/controllers/helpers"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -17,6 +20,9 @@ const (
SplitNamespace = "default"
ServiceName = "test-service"

APIVersion = "split.smi-spec.io/v1alpha4"
Kind = "TrafficSplit"

timeout = time.Second * 10
duration = time.Second * 10
interval = time.Millisecond * 250
Expand All @@ -25,8 +31,8 @@ const (
func testCreateTrafficSplit(t *testing.T) {
split := &splitv1alpha4.TrafficSplit{
TypeMeta: metav1.TypeMeta{
APIVersion: "split.smi-spec.io/v1alpha",
Kind: "TrafficSplit",
APIVersion: APIVersion,
Kind: Kind,
},
ObjectMeta: metav1.ObjectMeta{
Name: SplitName,
Expand All @@ -43,15 +49,20 @@ func testCreateTrafficSplit(t *testing.T) {
},
}

pretty.Print(split)

ctx := context.Background()
err := k8sClient.Create(ctx, split)
require.NoError(t, err)

// check that the create method on the API was called
helpers.AssertCalledEventually(t, mockAPI, "UpsertTrafficSplit", timeout, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
}

func testDeleteTrafficSplit(t *testing.T) {
split := &splitv1alpha4.TrafficSplit{
TypeMeta: metav1.TypeMeta{
APIVersion: "split.smi-spec.io/v1alpha",
APIVersion: "split.smi-spec.io/v1alpha4",
Kind: "TrafficSplit",
},
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -63,4 +74,7 @@ func testDeleteTrafficSplit(t *testing.T) {
ctx := context.Background()
err := k8sClient.Delete(ctx, split)
require.NoError(t, err)

// check that the create method on the API was called
helpers.AssertCalledEventually(t, mockAPI, "DeleteTrafficSplit", timeout, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ require (
github.com/cucumber/godog v0.11.0
github.com/cucumber/messages-go/v10 v10.0.3
github.com/go-logr/logr v0.4.0
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.13.0
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e
github.com/stretchr/testify v1.7.0
k8s.io/api v0.21.2
k8s.io/apimachinery v0.21.2
Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
Expand Down Expand Up @@ -550,7 +551,6 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449 h1:xUIPaMhvROX9dhPvRCenIJtU78+lbEenGbgqB5hfHCQ=
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down

0 comments on commit 786b351

Please sign in to comment.