Skip to content

Commit

Permalink
Proposed refactoring of test mocks
Browse files Browse the repository at this point in the history
The changes one of the test client mocks from
using a function pointer based model to a derived
type model.

Signed-off-by: Daniel Hiltgen <[email protected]>
  • Loading branch information
Daniel Hiltgen committed Aug 14, 2018
1 parent d282c38 commit 9d73826
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 208 deletions.
43 changes: 9 additions & 34 deletions internal/containerizedengine/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,8 @@ import (
)

type (
fakeContainerdClient struct {
containersFunc func(ctx context.Context, filters ...string) ([]containerd.Container, error)
newContainerFunc func(ctx context.Context, id string, opts ...containerd.NewContainerOpts) (containerd.Container, error)
pullFunc func(ctx context.Context, ref string, opts ...containerd.RemoteOpt) (containerd.Image, error)
getImageFunc func(ctx context.Context, ref string) (containerd.Image, error)
contentStoreFunc func() content.Store
containerServiceFunc func() containers.Store
}
fakeContainer struct {
testContainerdClient struct{}
fakeContainer struct {
idFunc func() string
infoFunc func(context.Context) (containers.Container, error)
deleteFunc func(context.Context, ...containerd.DeleteOpts) error
Expand Down Expand Up @@ -75,43 +68,25 @@ type (
}
)

func (w *fakeContainerdClient) Containers(ctx context.Context, filters ...string) ([]containerd.Container, error) {
if w.containersFunc != nil {
return w.containersFunc(ctx, filters...)
}
func (w *testContainerdClient) Containers(ctx context.Context, filters ...string) ([]containerd.Container, error) {
return []containerd.Container{}, nil
}
func (w *fakeContainerdClient) NewContainer(ctx context.Context, id string, opts ...containerd.NewContainerOpts) (containerd.Container, error) {
if w.newContainerFunc != nil {
return w.newContainerFunc(ctx, id, opts...)
}
func (w *testContainerdClient) NewContainer(ctx context.Context, id string, opts ...containerd.NewContainerOpts) (containerd.Container, error) {
return nil, nil
}
func (w *fakeContainerdClient) Pull(ctx context.Context, ref string, opts ...containerd.RemoteOpt) (containerd.Image, error) {
if w.pullFunc != nil {
return w.pullFunc(ctx, ref, opts...)
}
func (w *testContainerdClient) Pull(ctx context.Context, ref string, opts ...containerd.RemoteOpt) (containerd.Image, error) {
return nil, nil
}
func (w *fakeContainerdClient) GetImage(ctx context.Context, ref string) (containerd.Image, error) {
if w.getImageFunc != nil {
return w.getImageFunc(ctx, ref)
}
func (w *testContainerdClient) GetImage(ctx context.Context, ref string) (containerd.Image, error) {
return nil, nil
}
func (w *fakeContainerdClient) ContentStore() content.Store {
if w.contentStoreFunc != nil {
return w.contentStoreFunc()
}
func (w *testContainerdClient) ContentStore() content.Store {
return nil
}
func (w *fakeContainerdClient) ContainerService() containers.Store {
if w.containerServiceFunc != nil {
return w.containerServiceFunc()
}
func (w *testContainerdClient) ContainerService() containers.Store {
return nil
}
func (w *fakeContainerdClient) Close() error {
func (w *testContainerdClient) Close() error {
return nil
}

Expand Down
23 changes: 11 additions & 12 deletions internal/containerizedengine/containerd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ import (
"gotest.tools/assert"
)

type localTestPullWithAuthPullFailClient struct {
testContainerdClient
}

func (c *localTestPullWithAuthPullFailClient) Pull(ctx context.Context, ref string, opts ...containerd.RemoteOpt) (containerd.Image, error) {
return nil, fmt.Errorf("pull failure")
}

func TestPullWithAuthPullFail(t *testing.T) {
ctx := context.Background()
client := baseClient{
cclient: &fakeContainerdClient{
pullFunc: func(ctx context.Context, ref string, opts ...containerd.RemoteOpt) (containerd.Image, error) {
return nil, fmt.Errorf("pull failure")

},
},
client := baseClient{
cclient: &localTestPullWithAuthPullFailClient{},
}
imageName := "testnamegoeshere"

Expand All @@ -29,12 +33,7 @@ func TestPullWithAuthPullFail(t *testing.T) {
func TestPullWithAuthPullPass(t *testing.T) {
ctx := context.Background()
client := baseClient{
cclient: &fakeContainerdClient{
pullFunc: func(ctx context.Context, ref string, opts ...containerd.RemoteOpt) (containerd.Image, error) {
return nil, nil

},
},
cclient: &testContainerdClient{},
}
imageName := "testnamegoeshere"

Expand Down
Loading

0 comments on commit 9d73826

Please sign in to comment.