Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel committed Sep 16, 2023
1 parent 2ae8c6d commit 9922923
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 24 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -43,6 +44,7 @@ require (
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
Expand Down
11 changes: 10 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -39,6 +40,12 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
Expand All @@ -56,6 +63,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
Expand Down Expand Up @@ -127,8 +135,9 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
Expand Down
58 changes: 58 additions & 0 deletions internal/gardener/automock/ResourceInterface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions internal/gardener/automock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions internal/gardener/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ func newClusterConfig(kubeconfigPath string) (*rest.Config, error) {
return gardenerClusterConfig, nil
}

//go:generate mockery --name=ResourceInterface --output=automock --outpkg=automock
type ResourceInterface interface {
List(context.Context, v1.ListOptions) (*unstructured.UnstructuredList, error)
}

type shootClient struct {
resourceInterface dynamic.NamespaceableResourceInterface
namespace string
resourceInterface ResourceInterface
}

func (sc *shootClient) List(ctx context.Context, opts v1.ListOptions) (*types.ShootList, error) {
resources, err := sc.resourceInterface.Namespace(sc.namespace).List(ctx, opts)
func (sc *shootClient) List(ctx context.Context) (*types.ShootList, error) {
resources, err := sc.resourceInterface.List(ctx, v1.ListOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -81,7 +85,6 @@ func newShootClient(config *rest.Config, namespace string) (Client, error) {
}

return &shootClient{
resourceInterface: client.Resource(shootResource),
namespace: namespace,
resourceInterface: client.Resource(shootResource).Namespace(namespace),
}, nil
}
133 changes: 133 additions & 0 deletions internal/gardener/client_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package gardener

import (
"context"
"errors"
"path/filepath"
"reflect"
"runtime"
"testing"

"github.com/pPrecel/cloudagent/internal/gardener/automock"
"github.com/pPrecel/cloudagent/pkg/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
Expand Down Expand Up @@ -133,3 +139,130 @@ func createValidTestConfig() *api.Config {

return config
}

func Test_shootClient_List(t *testing.T) {
type fields struct {
resourceInterface ResourceInterface
}
type args struct {
ctx context.Context
}
tests := []struct {
name string
fields fields
args args
want *types.ShootList
wantErr bool
}{
{
name: "no resources",
fields: fields{
resourceInterface: func() ResourceInterface {
ri := automock.NewResourceInterface(t)
ri.On("List", mock.Anything, mock.Anything).Return(&unstructured.UnstructuredList{}, nil)
return ri
}(),
},
args: args{
ctx: context.Background(),
},
want: &types.ShootList{},
wantErr: false,
},
{
name: "list resources",
fields: fields{
resourceInterface: func() ResourceInterface {
ri := automock.NewResourceInterface(t)
ri.On("List", mock.Anything, mock.Anything).Return(&unstructured.UnstructuredList{
Items: []unstructured.Unstructured{
{}, {},
},
}, nil)
return ri
}(),
},
args: args{
ctx: context.Background(),
},
want: &types.ShootList{
Items: []types.Shoot{
{}, {},
},
},
wantErr: false,
},
{
name: "client error",
fields: fields{
resourceInterface: func() ResourceInterface {
ri := automock.NewResourceInterface(t)
ri.On("List", mock.Anything, mock.Anything).Return(nil, errors.New("test error"))
return ri
}(),
},
args: args{
ctx: context.Background(),
},
want: nil,
wantErr: true,
},
{
name: "client unexpected list response",
fields: fields{
resourceInterface: func() ResourceInterface {
ri := automock.NewResourceInterface(t)
ri.On("List", mock.Anything, mock.Anything).Return(&unstructured.UnstructuredList{
Object: map[string]interface{}{
"metadata": "wrong value",
},
}, nil)
return ri
}(),
},
args: args{
ctx: context.Background(),
},
want: nil,
wantErr: true,
},
{
name: "client unexpected item list",
fields: fields{
resourceInterface: func() ResourceInterface {
ri := automock.NewResourceInterface(t)
ri.On("List", mock.Anything, mock.Anything).Return(&unstructured.UnstructuredList{
Items: []unstructured.Unstructured{
{
Object: map[string]interface{}{
"metadata": "wrong value",
},
},
},
}, nil)
return ri
}(),
},
args: args{
ctx: context.Background(),
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sc := &shootClient{
resourceInterface: tt.fields.resourceInterface,
}
got, err := sc.List(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("shootClient.List() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("shootClient.List() = %v, want %v", got, tt.want)
}
})
}
}
5 changes: 2 additions & 3 deletions internal/gardener/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"github.com/pPrecel/cloudagent/pkg/cache"
"github.com/pPrecel/cloudagent/pkg/types"
"github.com/sirupsen/logrus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
)

//go:generate mockery --name=Client --output=automock --outpkg=automock
type Client interface {
List(context.Context, v1.ListOptions) (*types.ShootList, error)
List(context.Context) (*types.ShootList, error)
}

func NewWatchFunc(l *logrus.Entry, r cache.GardenerRegisteredResource, namespace, kubeconfig string) agent.WatchFn {
Expand All @@ -37,7 +36,7 @@ func newWatchFunc(l *logrus.Entry, r cache.GardenerRegisteredResource, clientBui
}
}

list, err := c.List(context, v1.ListOptions{})
list, err := c.List(context)
r.Set(list, err)
if err != nil {
l.Errorf("when watching for shoots: %s", err.Error())
Expand Down
6 changes: 3 additions & 3 deletions internal/gardener/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func Test_newWatchFunc(t *testing.T) {
r: cache.NewGardenerCache().Register("test"),
clientBuilder: func() (Client, error) {
c := automock.NewClient(t)
c.On("List", mock.Anything, v1.ListOptions{}).Return(shootList, nil).Once()
c.On("List", mock.Anything).Return(shootList, nil).Once()

return c, nil
},
Expand All @@ -83,7 +83,7 @@ func Test_newWatchFunc(t *testing.T) {
r: cache.NewGardenerCache().Register("test"),
clientBuilder: func() (Client, error) {
c := automock.NewClient(t)
c.On("List", mock.Anything, v1.ListOptions{}).Return(nil, errors.New("test error")).Once()
c.On("List", mock.Anything).Return(nil, errors.New("test error")).Once()

return c, nil
},
Expand All @@ -97,7 +97,7 @@ func Test_newWatchFunc(t *testing.T) {
r: cache.NewGardenerCache().Register("test"),
clientBuilder: func() (Client, error) {
c := automock.NewClient(t)
c.On("List", mock.Anything, v1.ListOptions{}).Return(nil, errors.New("test error")).Once()
c.On("List", mock.Anything).Return(nil, errors.New("test error")).Once()

return c, nil
},
Expand Down

0 comments on commit 9922923

Please sign in to comment.