diff --git a/go.mod b/go.mod index d6f574e..af878f3 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,15 @@ module github.com/cloud-club/Aviator-service go 1.21.1 require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 // indirect github.com/golang/mock v1.6.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect golang.org/x/mod v0.14.0 // indirect golang.org/x/sys v0.14.0 // indirect golang.org/x/tools v0.15.0 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 29305c2..b83f748 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,20 @@ +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/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU= +github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4= github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= +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= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -31,5 +46,10 @@ golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8= golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +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= diff --git a/main.go b/main.go index da29a2c..09c243e 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,6 @@ package main +//go:generate mockery --dir=pkg --name=ServerInterface --filename=server_interface.go --output pkg/mocks --outpkg=mocks func main() { + } diff --git a/pkg/server.go b/pkg/server.go index 3c10cb9..507972e 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -2,16 +2,16 @@ package pkg import ( "bytes" - "errors" + "fmt" "net/http" ) type ServerService struct { - client *http.Client - token string + client *http.Client + Interface ServerInterface + token string } -//go:generate mockgen -destination=mocks/mock_server.go -package=mocks github.com/cloud-club/Aviator-service/pkg ServerInterface type ServerInterface interface { Get(url string) (*http.Response, error) List(url string) (*http.Response, error) @@ -20,10 +20,6 @@ type ServerInterface interface { Delete(url string) (*http.Response, error) } -func NewServerService(token string) ServerInterface { - return &ServerService{token: token} -} - func (server *ServerService) GetToken() string { return server.token } @@ -38,7 +34,7 @@ func (server *ServerService) Get(url string) (*http.Response, error) { func (server *ServerService) List(url string) (*http.Response, error) { if len(url) == 0 { - return nil, errors.New("please input url") + return nil, fmt.Errorf("please input url") } return nil, nil //return server.do(http.MethodGet, url, nil) diff --git a/pkg/server_interface.go b/pkg/server_interface.go new file mode 100644 index 0000000..aee89a1 --- /dev/null +++ b/pkg/server_interface.go @@ -0,0 +1,328 @@ +// Code generated by mockery v2.38.0. DO NOT EDIT. + +package pkg + +import ( + http "net/http" + + mock "github.com/stretchr/testify/mock" +) + +// MockServerInterface is an autogenerated mock type for the ServerInterface type +type MockServerInterface struct { + mock.Mock +} + +type MockServerInterface_Expecter struct { + mock *mock.Mock +} + +func (_m *MockServerInterface) EXPECT() *MockServerInterface_Expecter { + return &MockServerInterface_Expecter{mock: &_m.Mock} +} + +// Create provides a mock function with given fields: url, payload +func (_m *MockServerInterface) Create(url string, payload []byte) (*http.Response, error) { + ret := _m.Called(url, payload) + + if len(ret) == 0 { + panic("no return value specified for Create") + } + + var r0 *http.Response + var r1 error + if rf, ok := ret.Get(0).(func(string, []byte) (*http.Response, error)); ok { + return rf(url, payload) + } + if rf, ok := ret.Get(0).(func(string, []byte) *http.Response); ok { + r0 = rf(url, payload) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*http.Response) + } + } + + if rf, ok := ret.Get(1).(func(string, []byte) error); ok { + r1 = rf(url, payload) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockServerInterface_Create_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Create' +type MockServerInterface_Create_Call struct { + *mock.Call +} + +// Create is a helper method to define mock.On call +// - url string +// - payload []byte +func (_e *MockServerInterface_Expecter) Create(url interface{}, payload interface{}) *MockServerInterface_Create_Call { + return &MockServerInterface_Create_Call{Call: _e.mock.On("Create", url, payload)} +} + +func (_c *MockServerInterface_Create_Call) Run(run func(url string, payload []byte)) *MockServerInterface_Create_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string), args[1].([]byte)) + }) + return _c +} + +func (_c *MockServerInterface_Create_Call) Return(_a0 *http.Response, _a1 error) *MockServerInterface_Create_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockServerInterface_Create_Call) RunAndReturn(run func(string, []byte) (*http.Response, error)) *MockServerInterface_Create_Call { + _c.Call.Return(run) + return _c +} + +// Delete provides a mock function with given fields: url +func (_m *MockServerInterface) Delete(url string) (*http.Response, error) { + ret := _m.Called(url) + + if len(ret) == 0 { + panic("no return value specified for Delete") + } + + var r0 *http.Response + var r1 error + if rf, ok := ret.Get(0).(func(string) (*http.Response, error)); ok { + return rf(url) + } + if rf, ok := ret.Get(0).(func(string) *http.Response); ok { + r0 = rf(url) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*http.Response) + } + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(url) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockServerInterface_Delete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Delete' +type MockServerInterface_Delete_Call struct { + *mock.Call +} + +// Delete is a helper method to define mock.On call +// - url string +func (_e *MockServerInterface_Expecter) Delete(url interface{}) *MockServerInterface_Delete_Call { + return &MockServerInterface_Delete_Call{Call: _e.mock.On("Delete", url)} +} + +func (_c *MockServerInterface_Delete_Call) Run(run func(url string)) *MockServerInterface_Delete_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *MockServerInterface_Delete_Call) Return(_a0 *http.Response, _a1 error) *MockServerInterface_Delete_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockServerInterface_Delete_Call) RunAndReturn(run func(string) (*http.Response, error)) *MockServerInterface_Delete_Call { + _c.Call.Return(run) + return _c +} + +// Get provides a mock function with given fields: url +func (_m *MockServerInterface) Get(url string) (*http.Response, error) { + ret := _m.Called(url) + + if len(ret) == 0 { + panic("no return value specified for Get") + } + + var r0 *http.Response + var r1 error + if rf, ok := ret.Get(0).(func(string) (*http.Response, error)); ok { + return rf(url) + } + if rf, ok := ret.Get(0).(func(string) *http.Response); ok { + r0 = rf(url) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*http.Response) + } + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(url) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockServerInterface_Get_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Get' +type MockServerInterface_Get_Call struct { + *mock.Call +} + +// Get is a helper method to define mock.On call +// - url string +func (_e *MockServerInterface_Expecter) Get(url interface{}) *MockServerInterface_Get_Call { + return &MockServerInterface_Get_Call{Call: _e.mock.On("Get", url)} +} + +func (_c *MockServerInterface_Get_Call) Run(run func(url string)) *MockServerInterface_Get_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *MockServerInterface_Get_Call) Return(_a0 *http.Response, _a1 error) *MockServerInterface_Get_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockServerInterface_Get_Call) RunAndReturn(run func(string) (*http.Response, error)) *MockServerInterface_Get_Call { + _c.Call.Return(run) + return _c +} + +// List provides a mock function with given fields: url +func (_m *MockServerInterface) List(url string) (*http.Response, error) { + ret := _m.Called(url) + + if len(ret) == 0 { + panic("no return value specified for List") + } + + var r0 *http.Response + var r1 error + if rf, ok := ret.Get(0).(func(string) (*http.Response, error)); ok { + return rf(url) + } + if rf, ok := ret.Get(0).(func(string) *http.Response); ok { + r0 = rf(url) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*http.Response) + } + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(url) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockServerInterface_List_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'List' +type MockServerInterface_List_Call struct { + *mock.Call +} + +// List is a helper method to define mock.On call +// - url string +func (_e *MockServerInterface_Expecter) List(url interface{}) *MockServerInterface_List_Call { + return &MockServerInterface_List_Call{Call: _e.mock.On("List", url)} +} + +func (_c *MockServerInterface_List_Call) Run(run func(url string)) *MockServerInterface_List_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *MockServerInterface_List_Call) Return(_a0 *http.Response, _a1 error) *MockServerInterface_List_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockServerInterface_List_Call) RunAndReturn(run func(string) (*http.Response, error)) *MockServerInterface_List_Call { + _c.Call.Return(run) + return _c +} + +// Update provides a mock function with given fields: url, payload +func (_m *MockServerInterface) Update(url string, payload []byte) (*http.Response, error) { + ret := _m.Called(url, payload) + + if len(ret) == 0 { + panic("no return value specified for Update") + } + + var r0 *http.Response + var r1 error + if rf, ok := ret.Get(0).(func(string, []byte) (*http.Response, error)); ok { + return rf(url, payload) + } + if rf, ok := ret.Get(0).(func(string, []byte) *http.Response); ok { + r0 = rf(url, payload) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*http.Response) + } + } + + if rf, ok := ret.Get(1).(func(string, []byte) error); ok { + r1 = rf(url, payload) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MockServerInterface_Update_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Update' +type MockServerInterface_Update_Call struct { + *mock.Call +} + +// Update is a helper method to define mock.On call +// - url string +// - payload []byte +func (_e *MockServerInterface_Expecter) Update(url interface{}, payload interface{}) *MockServerInterface_Update_Call { + return &MockServerInterface_Update_Call{Call: _e.mock.On("Update", url, payload)} +} + +func (_c *MockServerInterface_Update_Call) Run(run func(url string, payload []byte)) *MockServerInterface_Update_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string), args[1].([]byte)) + }) + return _c +} + +func (_c *MockServerInterface_Update_Call) Return(_a0 *http.Response, _a1 error) *MockServerInterface_Update_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *MockServerInterface_Update_Call) RunAndReturn(run func(string, []byte) (*http.Response, error)) *MockServerInterface_Update_Call { + _c.Call.Return(run) + return _c +} + +// NewMockServerInterface creates a new instance of MockServerInterface. 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 NewMockServerInterface(t interface { + mock.TestingT + Cleanup(func()) +}) *MockServerInterface { + mock := &MockServerInterface{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/server_test.go b/pkg/server_test.go index 41b107f..fd6e995 100644 --- a/pkg/server_test.go +++ b/pkg/server_test.go @@ -1,13 +1,13 @@ package pkg_test import ( + "fmt" "github.com/cloud-club/Aviator-service/pkg" "testing" ) func (suite *NcpSuite) TestList() { - ncp := pkg.NewNcpService("ncp service token") - ncp.Server = pkg.NewServerService("ncp server token") + mockInterface := &pkg.MockServerInterface{} // given tests := []struct { @@ -15,12 +15,14 @@ func (suite *NcpSuite) TestList() { url string payload interface{} expectedError string + actualError string expectedData interface{} }{ { name: "성공", url: "http://localhost:8080", payload: nil, + actualError: "", expectedError: "", expectedData: nil, }, @@ -28,29 +30,29 @@ func (suite *NcpSuite) TestList() { name: "(실패) url 입력 안함", url: "", payload: nil, + actualError: "please input url", expectedError: "please input url", expectedData: nil, }, } for i := range tests { + suite.T().Helper() - suite.T().Logf("%s : running scenario %d", tests[i].name, i) suite.T().Run(tests[i].name, func(t *testing.T) { - _, err := suite.ncp.Server.List(tests[i].url) - if err != nil { - suite.Assert().Error(err, tests[i].expectedError) + suite.T().Logf("%s : running scenario %d", tests[i].name, i) + + mockInterface.On("List", tests[i].url). + Return(nil, fmt.Errorf("%v", tests[i].actualError)). + Once() + + suite.ncp.Server.Interface = mockInterface + + _, err := suite.ncp.Server.Interface.List(tests[i].url) + if err.Error() != "" { + suite.Assert().EqualError(err, tests[i].expectedError) } - // - //if err != nil { - // if err.Error() != tests[i].expectedError { - // t.Fatalf("expected error : %v, got : %v", tests[i].expectedError, err) - // } - //} else { - // if tests[i].expectedError != "" { - // t.Fatalf("expected error : %v, got : %v", tests[i].expectedError, err) - // } - //} + }) } diff --git a/pkg/suite_test.go b/pkg/suite_test.go index 39b2554..9ae486f 100644 --- a/pkg/suite_test.go +++ b/pkg/suite_test.go @@ -21,7 +21,6 @@ func TestNcpSuite(t *testing.T) { // This method gets run only first time before starting test func (suite *NcpSuite) SetupSuite() { suite.ncp = pkg.NewNcpService("ncp service token") - suite.ncp.Server = pkg.NewServerService("ncp server token") err := suite.ncp.CreateToken("admin", "CloudClubAdmin", []string{"admin"}) assert.NoError(suite.T(), err, nil)