From 52c6177ff7c91ece52b870373fccfb7bd6c50ad7 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 27 Apr 2020 09:42:44 +0200 Subject: [PATCH] API mock and a test case relying on it Signed-off-by: Nicolas De Loof --- ecs/go.mod | 1 + ecs/go.sum | 6 ++ ecs/pkg/amazon/down_test.go | 45 +++++++++ ecs/pkg/amazon/mock/api.go | 195 ++++++++++++++++++++++++++++++++++++ 4 files changed, 247 insertions(+) create mode 100644 ecs/pkg/amazon/down_test.go create mode 100644 ecs/pkg/amazon/mock/api.go diff --git a/ecs/go.mod b/ecs/go.mod index 0bc80e999..0ddf6c857 100644 --- a/ecs/go.mod +++ b/ecs/go.mod @@ -27,6 +27,7 @@ require ( github.com/go-sql-driver/mysql v1.5.0 // indirect github.com/gofrs/uuid v3.2.0+incompatible // indirect github.com/gogo/protobuf v1.3.1 // indirect + github.com/golang/mock v1.4.3 github.com/gorilla/mux v1.7.3 // indirect github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect github.com/jinzhu/gorm v1.9.12 // indirect diff --git a/ecs/go.sum b/ecs/go.sum index ee07c6080..5cd52e764 100644 --- a/ecs/go.sum +++ b/ecs/go.sum @@ -119,6 +119,8 @@ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2V github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.4.3 h1:GV+pQPG/EUUbkh47niozDcADz6go/dUwhVzdUQHIVRw= +github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= @@ -366,6 +368,7 @@ golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 h1:7TYNF4UdlohbFwpNH04CoPMp1 golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7 h1:HmbHVPwrPEKPGLAcHSrMe6+hqSUlvZU0rab6x5EXfGU= golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= @@ -378,6 +381,7 @@ golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= @@ -421,5 +425,7 @@ gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk= +rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= +rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= vbom.ml/util v0.0.0-20180919145318-efcd4e0f9787 h1:O69FD9pJA4WUZlEwYatBEEkRWKQ5cKodWpdKTrCS/iQ= vbom.ml/util v0.0.0-20180919145318-efcd4e0f9787/go.mod h1:so/NYdZXCz+E3ZpW0uAoCj6uzU2+8OWDFv/HxUSs7kI= diff --git a/ecs/pkg/amazon/down_test.go b/ecs/pkg/amazon/down_test.go new file mode 100644 index 000000000..3245ecef5 --- /dev/null +++ b/ecs/pkg/amazon/down_test.go @@ -0,0 +1,45 @@ +package amazon + +import ( + "github.com/docker/ecs-plugin/pkg/amazon/mock" + "github.com/docker/ecs-plugin/pkg/compose" + "github.com/golang/mock/gomock" + "testing" +) + +func Test_down_dont_delete_cluster(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + m := mock.NewMockAPI(ctrl) + c := &client{ + Cluster: "test_cluster", + Region: "region", + api: m, + } + + recorder := m.EXPECT() + recorder.DeleteStack("test_project").Return(nil).Times(1) + + c.ComposeDown(&compose.Project{ + Name: "test_project", + }, false, false) +} + +func Test_down_delete_cluster(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + m := mock.NewMockAPI(ctrl) + c := &client{ + Cluster: "test_cluster", + Region: "region", + api: m, + } + + recorder := m.EXPECT() + recorder.DeleteStack("test_project").Return(nil).Times(1) + recorder.DeleteCluster("test_cluster").Return(nil).Times(1) + + c.ComposeDown(&compose.Project{ + Name: "test_project", + }, false, true) +} \ No newline at end of file diff --git a/ecs/pkg/amazon/mock/api.go b/ecs/pkg/amazon/mock/api.go new file mode 100644 index 000000000..8adaca5f4 --- /dev/null +++ b/ecs/pkg/amazon/mock/api.go @@ -0,0 +1,195 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./pkg/amazon/api.go + +// Package mock is a generated GoMock package. +package mock + +import ( + cloudformation "github.com/awslabs/goformation/v4/cloudformation" + gomock "github.com/golang/mock/gomock" + reflect "reflect" +) + +// MockAPI is a mock of API interface +type MockAPI struct { + ctrl *gomock.Controller + recorder *MockAPIMockRecorder +} + +// MockAPIMockRecorder is the mock recorder for MockAPI +type MockAPIMockRecorder struct { + mock *MockAPI +} + +// NewMockAPI creates a new mock instance +func NewMockAPI(ctrl *gomock.Controller) *MockAPI { + mock := &MockAPI{ctrl: ctrl} + mock.recorder = &MockAPIMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockAPI) EXPECT() *MockAPIMockRecorder { + return m.recorder +} + +// ClusterExists mocks base method +func (m *MockAPI) ClusterExists(name string) (bool, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ClusterExists", name) + ret0, _ := ret[0].(bool) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ClusterExists indicates an expected call of ClusterExists +func (mr *MockAPIMockRecorder) ClusterExists(name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ClusterExists", reflect.TypeOf((*MockAPI)(nil).ClusterExists), name) +} + +// CreateCluster mocks base method +func (m *MockAPI) CreateCluster(name string) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateCluster", name) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateCluster indicates an expected call of CreateCluster +func (mr *MockAPIMockRecorder) CreateCluster(name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateCluster", reflect.TypeOf((*MockAPI)(nil).CreateCluster), name) +} + +// DeleteCluster mocks base method +func (m *MockAPI) DeleteCluster(name string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteCluster", name) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteCluster indicates an expected call of DeleteCluster +func (mr *MockAPIMockRecorder) DeleteCluster(name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteCluster", reflect.TypeOf((*MockAPI)(nil).DeleteCluster), name) +} + +// GetDefaultVPC mocks base method +func (m *MockAPI) GetDefaultVPC() (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetDefaultVPC") + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetDefaultVPC indicates an expected call of GetDefaultVPC +func (mr *MockAPIMockRecorder) GetDefaultVPC() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDefaultVPC", reflect.TypeOf((*MockAPI)(nil).GetDefaultVPC)) +} + +// GetSubNets mocks base method +func (m *MockAPI) GetSubNets(vpcId string) ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetSubNets", vpcId) + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetSubNets indicates an expected call of GetSubNets +func (mr *MockAPIMockRecorder) GetSubNets(vpcId interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSubNets", reflect.TypeOf((*MockAPI)(nil).GetSubNets), vpcId) +} + +// ListRolesForPolicy mocks base method +func (m *MockAPI) ListRolesForPolicy(policy string) ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListRolesForPolicy", policy) + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListRolesForPolicy indicates an expected call of ListRolesForPolicy +func (mr *MockAPIMockRecorder) ListRolesForPolicy(policy interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRolesForPolicy", reflect.TypeOf((*MockAPI)(nil).ListRolesForPolicy), policy) +} + +// GetRoleArn mocks base method +func (m *MockAPI) GetRoleArn(name string) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRoleArn", name) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRoleArn indicates an expected call of GetRoleArn +func (mr *MockAPIMockRecorder) GetRoleArn(name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRoleArn", reflect.TypeOf((*MockAPI)(nil).GetRoleArn), name) +} + +// StackExists mocks base method +func (m *MockAPI) StackExists(name string) (bool, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "StackExists", name) + ret0, _ := ret[0].(bool) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// StackExists indicates an expected call of StackExists +func (mr *MockAPIMockRecorder) StackExists(name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StackExists", reflect.TypeOf((*MockAPI)(nil).StackExists), name) +} + +// CreateStack mocks base method +func (m *MockAPI) CreateStack(name string, template *cloudformation.Template) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateStack", name, template) + ret0, _ := ret[0].(error) + return ret0 +} + +// CreateStack indicates an expected call of CreateStack +func (mr *MockAPIMockRecorder) CreateStack(name, template interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateStack", reflect.TypeOf((*MockAPI)(nil).CreateStack), name, template) +} + +// DescribeStackEvents mocks base method +func (m *MockAPI) DescribeStackEvents(stack string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DescribeStackEvents", stack) + ret0, _ := ret[0].(error) + return ret0 +} + +// DescribeStackEvents indicates an expected call of DescribeStackEvents +func (mr *MockAPIMockRecorder) DescribeStackEvents(stack interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DescribeStackEvents", reflect.TypeOf((*MockAPI)(nil).DescribeStackEvents), stack) +} + +// DeleteStack mocks base method +func (m *MockAPI) DeleteStack(name string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteStack", name) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteStack indicates an expected call of DeleteStack +func (mr *MockAPIMockRecorder) DeleteStack(name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteStack", reflect.TypeOf((*MockAPI)(nil).DeleteStack), name) +}