Commit 79c52a64 authored by Boris Glimcher's avatar Boris Glimcher Committed by Tomasz Zawadzki
Browse files

go/rpc: add mockery for Client

this help for users of this package
to mock this interface for unit testing

See more documentation here:
https://github.com/vektra/mockery



Example:

```
import (
    "log"
	"errors"
	"testing"
	"github.com/stretchr/testify/mock"
	"github.com/spdk/spdk/go/rpc/client"
	"github.com/spdk/spdk/go/rpc/client/mocks"
)

func SpdkGetVersion(c *client.IClient) error {
    // a lot of logic before the call
	resp1, err1 := c.Call("spdk_get_version", nil)
    // a lot of logic after the call
	log.Print(resp1)
	resp2, err2 := c.Call("bdev_get_bdevs", nil)
    // a lot of logic after the call
	log.Print(resp2)
	return err
}

func Test_Example(t *testing.T) {
	mockSpdk := mocks.NewIClient(t)
	mockSpdk.EXPECT().Call("spdk_get_version", mock.Anything).Return(errors.New("my error")).Once()
	mockSpdk.EXPECT().Call("bdev_get_bdevs", mock.Anything).Return(errors.New("my error")).Once()

    err := SpdkGetVersion(mockSpdk)
	if err != nil {
		t.Error("unexpected error received")
	}
}
```

Change-Id: I2747472def0cd73075f52ec678ca45e981bdf0b8
Signed-off-by: default avatarBoris Glimcher <Boris.Glimcher@emc.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/20143


Reviewed-by: default avatarJim Harris <jim.harris@samsung.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarArtek Koltun <artsiom.koltun@intel.com>
Reviewed-by: default avatarMaciej Miś <maciej.mis@intel.com>
Reviewed-by: default avatarBen Walker <ben@nvidia.com>
Community-CI: Mellanox Build Bot
parent 85eff555
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
CLIENT_BUILD_DIR = $(SPDK_ROOT_DIR)/build/go/rpc
CLIENT_SRC := $(wildcard $(CURDIR)/*.go $(CURDIR)/client/*.go)

.PHONY: all clean
.PHONY: all clean mock-generate

CGOFLAGS=-trimpath -mod=readonly -gcflags="all=-spectre=all -N -l" -asmflags="all=-spectre=all" -buildmode=c-shared -ldflags="all=-s -w"

@@ -17,3 +17,6 @@ all: $(CLIENT_SRC)

clean:
	$(Q)rm -rf $(CLIENT_BUILD_DIR)

mock-generate:
	$(Q)mockery --name=IClient --dir client --output mocks --with-expecter --boilerplate-file mocks/boilerplate.txt
+9 −0
Original line number Diff line number Diff line
module github.com/spdk/spdk/go/rpc

go 1.21

require github.com/stretchr/testify v1.8.4

require (
	github.com/davecgh/go-spew v1.1.1 // indirect
	github.com/pmezard/go-difflib v1.0.0 // indirect
	github.com/stretchr/objx v0.5.0 // indirect
	gopkg.in/yaml.v3 v3.0.1 // indirect
)

go/rpc/go.sum

0 → 100644
+18 −0
Original line number Diff line number Diff line
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=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
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/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+93 −0
Original line number Diff line number Diff line
/*   SPDX-License-Identifier: BSD-3-Clause
 */

// Code generated by mockery v2.34.2. DO NOT EDIT.

package mocks

import (
	client "github.com/spdk/spdk/go/rpc/client"
	mock "github.com/stretchr/testify/mock"
)

// IClient is an autogenerated mock type for the IClient type
type IClient struct {
	mock.Mock
}

type IClient_Expecter struct {
	mock *mock.Mock
}

func (_m *IClient) EXPECT() *IClient_Expecter {
	return &IClient_Expecter{mock: &_m.Mock}
}

// Call provides a mock function with given fields: method, params
func (_m *IClient) Call(method string, params interface{}) (*client.Response, error) {
	ret := _m.Called(method, params)

	var r0 *client.Response
	var r1 error
	if rf, ok := ret.Get(0).(func(string, interface{}) (*client.Response, error)); ok {
		return rf(method, params)
	}
	if rf, ok := ret.Get(0).(func(string, interface{}) *client.Response); ok {
		r0 = rf(method, params)
	} else {
		if ret.Get(0) != nil {
			r0 = ret.Get(0).(*client.Response)
		}
	}

	if rf, ok := ret.Get(1).(func(string, interface{}) error); ok {
		r1 = rf(method, params)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

// IClient_Call_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Call'
type IClient_Call_Call struct {
	*mock.Call
}

// Call is a helper method to define mock.On call
//   - method string
//   - params interface{}
func (_e *IClient_Expecter) Call(method interface{}, params interface{}) *IClient_Call_Call {
	return &IClient_Call_Call{Call: _e.mock.On("Call", method, params)}
}

func (_c *IClient_Call_Call) Run(run func(method string, params interface{})) *IClient_Call_Call {
	_c.Call.Run(func(args mock.Arguments) {
		run(args[0].(string), args[1].(interface{}))
	})
	return _c
}

func (_c *IClient_Call_Call) Return(_a0 *client.Response, _a1 error) *IClient_Call_Call {
	_c.Call.Return(_a0, _a1)
	return _c
}

func (_c *IClient_Call_Call) RunAndReturn(run func(string, interface{}) (*client.Response, error)) *IClient_Call_Call {
	_c.Call.Return(run)
	return _c
}

// NewIClient creates a new instance of IClient. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewIClient(t interface {
	mock.TestingT
	Cleanup(func())
}) *IClient {
	mock := &IClient{}
	mock.Mock.Test(t)

	t.Cleanup(func() { mock.AssertExpectations(t) })

	return mock
}
+2 −0
Original line number Diff line number Diff line
/*   SPDX-License-Identifier: BSD-3-Clause
 */