From 393ed6fb3271225b203483afda7f6dcde25ac85c Mon Sep 17 00:00:00 2001 From: Heming Han Date: Wed, 13 Sep 2023 00:19:09 -0700 Subject: [PATCH] add test --- .../netlib/model/ecscni/bridge_config.go | 2 +- ecs-agent/netlib/model/ecscni/client.go | 10 - ecs-agent/netlib/model/ecscni/client_test.go | 50 ++++ ecs-agent/netlib/model/ecscni/eni_config.go | 2 +- ...erate_mocks_linux.go => generate_mocks.go} | 5 +- .../model/ecscni/mocks/ecscni_mocks_linux.go | 255 ------------------ .../ecscni/mocks/ecscni_mocks_windows.go | 211 --------------- .../model/ecscni/mocks_libcni/libcni_mocks.go | 229 ++++++++++++++++ ecs-agent/netlib/model/ecscni/netconfig.go | 6 +- .../netlib/model/ecscni/netconfig_test.go | 5 +- ecs-agent/netlib/model/ecscni/nsutil_linux.go | 6 +- .../netlib/model/ecscni/nsutil_linux_test.go | 1 + .../netlib/model/ecscni/nsutil_windows.go | 2 +- .../model/ecscni/nsutil_windows_test.go | 1 + ecs-agent/netlib/model/ecscni/plugin.go | 18 +- ...enerate_mocks_windows.go => test_types.go} | 31 ++- .../netlib/model/ecscni/vpcbranch_config.go | 2 +- .../netlib/model/ecscni/vpceni_config.go | 2 +- .../netlib/model/ecscni/vpctunnel_config.go | 2 +- 19 files changed, 341 insertions(+), 499 deletions(-) create mode 100644 ecs-agent/netlib/model/ecscni/client_test.go rename ecs-agent/netlib/model/ecscni/{generate_mocks_linux.go => generate_mocks.go} (69%) delete mode 100644 ecs-agent/netlib/model/ecscni/mocks/ecscni_mocks_linux.go delete mode 100644 ecs-agent/netlib/model/ecscni/mocks/ecscni_mocks_windows.go create mode 100644 ecs-agent/netlib/model/ecscni/mocks_libcni/libcni_mocks.go create mode 100644 ecs-agent/netlib/model/ecscni/nsutil_linux_test.go create mode 100644 ecs-agent/netlib/model/ecscni/nsutil_windows_test.go rename ecs-agent/netlib/model/ecscni/{generate_mocks_windows.go => test_types.go} (54%) diff --git a/ecs-agent/netlib/model/ecscni/bridge_config.go b/ecs-agent/netlib/model/ecscni/bridge_config.go index ba8b1693163..ed352691165 100644 --- a/ecs-agent/netlib/model/ecscni/bridge_config.go +++ b/ecs-agent/netlib/model/ecscni/bridge_config.go @@ -37,7 +37,7 @@ func (bc *BridgeConfig) String() string { // InterfaceName returns the veth pair name will be used inside the namespace func (bc *BridgeConfig) InterfaceName() string { if bc.DeviceName == "" { - return defaultInterfaceName + return DefaultInterfaceName } return bc.DeviceName diff --git a/ecs-agent/netlib/model/ecscni/client.go b/ecs-agent/netlib/model/ecscni/client.go index a99c7e8b7c7..6097ba0827b 100644 --- a/ecs-agent/netlib/model/ecscni/client.go +++ b/ecs-agent/netlib/model/ecscni/client.go @@ -30,16 +30,6 @@ const ( versionCommand = "--version" ) -// CNI defines the plugin invocation interface -type CNI interface { - // Add calls the plugin add command with given configuration - Add(context.Context, PluginConfig) (types.Result, error) - // Del calls the plugin del command with given configuration - Del(context.Context, PluginConfig) error - // Version calls the version command of plugin - Version(string) (string, error) -} - // CNIClient is the client to invoke the plugin type cniClient struct { pluginPath []string diff --git a/ecs-agent/netlib/model/ecscni/client_test.go b/ecs-agent/netlib/model/ecscni/client_test.go new file mode 100644 index 00000000000..f7394247610 --- /dev/null +++ b/ecs-agent/netlib/model/ecscni/client_test.go @@ -0,0 +1,50 @@ +//go:build unit +// +build unit + +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +package ecscni + +import ( + "context" + "testing" + + mock_libcni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/ecscni/mocks_libcni" + + "github.com/golang/mock/gomock" +) + +func TestAddDel(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + ctx, _ := context.WithCancel(context.TODO()) + + client := NewCNIClient([]string{}) + mockLibCNIClient := mock_libcni.NewMockCNI(ctrl) + client.(*cniClient).cni = mockLibCNIClient + + config := &TestCNIConfig{ + CNIConfig: CNIConfig{ + NetNSPath: NetNS, + CNISpecVersion: CNIVersion, + CNIPluginName: PluginName, + }, + NetworkInterfaceName: IfName, + } + + client.Add(ctx, config) + client.Del(ctx, config) + mockLibCNIClient.EXPECT().AddNetwork(gomock.Any(), gomock.Any(), gomock.Any()) + mockLibCNIClient.EXPECT().DelNetwork(gomock.Any(), gomock.Any(), gomock.Any()) +} diff --git a/ecs-agent/netlib/model/ecscni/eni_config.go b/ecs-agent/netlib/model/ecscni/eni_config.go index e42357953f3..ed854ecbe43 100644 --- a/ecs-agent/netlib/model/ecscni/eni_config.go +++ b/ecs-agent/netlib/model/ecscni/eni_config.go @@ -75,7 +75,7 @@ func (ec *ENIConfig) NSPath() string { func (ec *ENIConfig) InterfaceName() string { if ec.DeviceName == "" { - return defaultENIName + return DefaultENIName } return ec.DeviceName } diff --git a/ecs-agent/netlib/model/ecscni/generate_mocks_linux.go b/ecs-agent/netlib/model/ecscni/generate_mocks.go similarity index 69% rename from ecs-agent/netlib/model/ecscni/generate_mocks_linux.go rename to ecs-agent/netlib/model/ecscni/generate_mocks.go index bd81607b3ac..500f90b9cc1 100644 --- a/ecs-agent/netlib/model/ecscni/generate_mocks_linux.go +++ b/ecs-agent/netlib/model/ecscni/generate_mocks.go @@ -11,9 +11,6 @@ // express or implied. See the License for the specific language governing // permissions and limitations under the License. -//go:build !windows -// +build !windows - package ecscni -//go:generate mockgen -build_flags=--mod=mod -destination=mocks/ecscni_mocks_linux.go -copyright_file=../../../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/ecscni Config,NetNSUtil,CNI +//go:generate mockgen -destination=../mocks_libcni/libcni_mocks.go -copyright_file=../../../../scripts/copyright_file github.com/containernetworking/cni/libcni CNI diff --git a/ecs-agent/netlib/model/ecscni/mocks/ecscni_mocks_linux.go b/ecs-agent/netlib/model/ecscni/mocks/ecscni_mocks_linux.go deleted file mode 100644 index 9c005842d46..00000000000 --- a/ecs-agent/netlib/model/ecscni/mocks/ecscni_mocks_linux.go +++ /dev/null @@ -1,255 +0,0 @@ -// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"). You may -// not use this file except in compliance with the License. A copy of the -// License is located at -// -// http://aws.amazon.com/apache2.0/ -// -// or in the "license" file accompanying this file. This file is distributed -// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either -// express or implied. See the License for the specific language governing -// permissions and limitations under the License. -// - -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/ecscni (interfaces: Config,NetNSUtil,CNI) - -// Package mock_ecscni is a generated GoMock package. -package mock_ecscni - -import ( - context "context" - reflect "reflect" - - ecscni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/ecscni" - types "github.com/containernetworking/cni/pkg/types" - ns "github.com/containernetworking/plugins/pkg/ns" - gomock "github.com/golang/mock/gomock" -) - -// MockConfig is a mock of Config interface. -type MockConfig struct { - ctrl *gomock.Controller - recorder *MockConfigMockRecorder -} - -// MockConfigMockRecorder is the mock recorder for MockConfig. -type MockConfigMockRecorder struct { - mock *MockConfig -} - -// NewMockConfig creates a new mock instance. -func NewMockConfig(ctrl *gomock.Controller) *MockConfig { - mock := &MockConfig{ctrl: ctrl} - mock.recorder = &MockConfigMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockConfig) EXPECT() *MockConfigMockRecorder { - return m.recorder -} - -// String mocks base method. -func (m *MockConfig) String() string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "String") - ret0, _ := ret[0].(string) - return ret0 -} - -// String indicates an expected call of String. -func (mr *MockConfigMockRecorder) String() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "String", reflect.TypeOf((*MockConfig)(nil).String)) -} - -// MockNetNSUtil is a mock of NetNSUtil interface. -type MockNetNSUtil struct { - ctrl *gomock.Controller - recorder *MockNetNSUtilMockRecorder -} - -// MockNetNSUtilMockRecorder is the mock recorder for MockNetNSUtil. -type MockNetNSUtilMockRecorder struct { - mock *MockNetNSUtil -} - -// NewMockNetNSUtil creates a new mock instance. -func NewMockNetNSUtil(ctrl *gomock.Controller) *MockNetNSUtil { - mock := &MockNetNSUtil{ctrl: ctrl} - mock.recorder = &MockNetNSUtilMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockNetNSUtil) EXPECT() *MockNetNSUtilMockRecorder { - return m.recorder -} - -// BuildResolvConfig mocks base method. -func (m *MockNetNSUtil) BuildResolvConfig(arg0, arg1 []string) string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "BuildResolvConfig", arg0, arg1) - ret0, _ := ret[0].(string) - return ret0 -} - -// BuildResolvConfig indicates an expected call of BuildResolvConfig. -func (mr *MockNetNSUtilMockRecorder) BuildResolvConfig(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BuildResolvConfig", reflect.TypeOf((*MockNetNSUtil)(nil).BuildResolvConfig), arg0, arg1) -} - -// DelNetNS mocks base method. -func (m *MockNetNSUtil) DelNetNS(arg0 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DelNetNS", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// DelNetNS indicates an expected call of DelNetNS. -func (mr *MockNetNSUtilMockRecorder) DelNetNS(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelNetNS", reflect.TypeOf((*MockNetNSUtil)(nil).DelNetNS), arg0) -} - -// ExecInNSPath mocks base method. -func (m *MockNetNSUtil) ExecInNSPath(arg0 string, arg1 func(ns.NetNS) error) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ExecInNSPath", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// ExecInNSPath indicates an expected call of ExecInNSPath. -func (mr *MockNetNSUtilMockRecorder) ExecInNSPath(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExecInNSPath", reflect.TypeOf((*MockNetNSUtil)(nil).ExecInNSPath), arg0, arg1) -} - -// GetNetNSName mocks base method. -func (m *MockNetNSUtil) GetNetNSName(arg0 string) string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetNetNSName", arg0) - ret0, _ := ret[0].(string) - return ret0 -} - -// GetNetNSName indicates an expected call of GetNetNSName. -func (mr *MockNetNSUtilMockRecorder) GetNetNSName(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNetNSName", reflect.TypeOf((*MockNetNSUtil)(nil).GetNetNSName), arg0) -} - -// GetNetNSPath mocks base method. -func (m *MockNetNSUtil) GetNetNSPath(arg0 string) string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetNetNSPath", arg0) - ret0, _ := ret[0].(string) - return ret0 -} - -// GetNetNSPath indicates an expected call of GetNetNSPath. -func (mr *MockNetNSUtilMockRecorder) GetNetNSPath(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNetNSPath", reflect.TypeOf((*MockNetNSUtil)(nil).GetNetNSPath), arg0) -} - -// NSExists mocks base method. -func (m *MockNetNSUtil) NSExists(arg0 string) (bool, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NSExists", arg0) - ret0, _ := ret[0].(bool) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// NSExists indicates an expected call of NSExists. -func (mr *MockNetNSUtilMockRecorder) NSExists(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NSExists", reflect.TypeOf((*MockNetNSUtil)(nil).NSExists), arg0) -} - -// NewNetNS mocks base method. -func (m *MockNetNSUtil) NewNetNS(arg0 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NewNetNS", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// NewNetNS indicates an expected call of NewNetNS. -func (mr *MockNetNSUtilMockRecorder) NewNetNS(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewNetNS", reflect.TypeOf((*MockNetNSUtil)(nil).NewNetNS), arg0) -} - -// MockCNI is a mock of CNI interface. -type MockCNI struct { - ctrl *gomock.Controller - recorder *MockCNIMockRecorder -} - -// MockCNIMockRecorder is the mock recorder for MockCNI. -type MockCNIMockRecorder struct { - mock *MockCNI -} - -// NewMockCNI creates a new mock instance. -func NewMockCNI(ctrl *gomock.Controller) *MockCNI { - mock := &MockCNI{ctrl: ctrl} - mock.recorder = &MockCNIMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockCNI) EXPECT() *MockCNIMockRecorder { - return m.recorder -} - -// Add mocks base method. -func (m *MockCNI) Add(arg0 context.Context, arg1 ecscni.PluginConfig) (types.Result, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Add", arg0, arg1) - ret0, _ := ret[0].(types.Result) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Add indicates an expected call of Add. -func (mr *MockCNIMockRecorder) Add(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Add", reflect.TypeOf((*MockCNI)(nil).Add), arg0, arg1) -} - -// Del mocks base method. -func (m *MockCNI) Del(arg0 context.Context, arg1 ecscni.PluginConfig) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Del", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// Del indicates an expected call of Del. -func (mr *MockCNIMockRecorder) Del(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Del", reflect.TypeOf((*MockCNI)(nil).Del), arg0, arg1) -} - -// Version mocks base method. -func (m *MockCNI) Version(arg0 string) (string, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Version", arg0) - ret0, _ := ret[0].(string) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Version indicates an expected call of Version. -func (mr *MockCNIMockRecorder) Version(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Version", reflect.TypeOf((*MockCNI)(nil).Version), arg0) -} diff --git a/ecs-agent/netlib/model/ecscni/mocks/ecscni_mocks_windows.go b/ecs-agent/netlib/model/ecscni/mocks/ecscni_mocks_windows.go deleted file mode 100644 index 10ed1d7c631..00000000000 --- a/ecs-agent/netlib/model/ecscni/mocks/ecscni_mocks_windows.go +++ /dev/null @@ -1,211 +0,0 @@ -// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"). You may -// not use this file except in compliance with the License. A copy of the -// License is located at -// -// http://aws.amazon.com/apache2.0/ -// or in the "license" file accompanying this file. This file is distributed -// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either -// express or implied. See the License for the specific language governing -// permissions and limitations under the License. -// - -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/ecscni (interfaces: Config,NetNSUtil,CNI) - -// Package mock_ecscni is a generated GoMock package. -package mock_ecscni - -import ( - context "context" - reflect "reflect" - - ecscni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/ecscni" - types "github.com/containernetworking/cni/pkg/types" - gomock "github.com/golang/mock/gomock" -) - -// MockConfig is a mock of Config interface -type MockConfig struct { - ctrl *gomock.Controller - recorder *MockConfigMockRecorder -} - -// MockConfigMockRecorder is the mock recorder for MockConfig -type MockConfigMockRecorder struct { - mock *MockConfig -} - -// NewMockConfig creates a new mock instance -func NewMockConfig(ctrl *gomock.Controller) *MockConfig { - mock := &MockConfig{ctrl: ctrl} - mock.recorder = &MockConfigMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use -func (m *MockConfig) EXPECT() *MockConfigMockRecorder { - return m.recorder -} - -// String mocks base method -func (m *MockConfig) String() string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "String") - ret0, _ := ret[0].(string) - return ret0 -} - -// String indicates an expected call of String -func (mr *MockConfigMockRecorder) String() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "String", reflect.TypeOf((*MockConfig)(nil).String)) -} - -// MockNetNSUtil is a mock of NetNSUtil interface -type MockNetNSUtil struct { - ctrl *gomock.Controller - recorder *MockNetNSUtilMockRecorder -} - -// MockNetNSUtilMockRecorder is the mock recorder for MockNetNSUtil -type MockNetNSUtilMockRecorder struct { - mock *MockNetNSUtil -} - -// NewMockNetNSUtil creates a new mock instance -func NewMockNetNSUtil(ctrl *gomock.Controller) *MockNetNSUtil { - mock := &MockNetNSUtil{ctrl: ctrl} - mock.recorder = &MockNetNSUtilMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use -func (m *MockNetNSUtil) EXPECT() *MockNetNSUtilMockRecorder { - return m.recorder -} - -// DelNetNS mocks base method -func (m *MockNetNSUtil) DelNetNS(arg0 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DelNetNS", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// DelNetNS indicates an expected call of DelNetNS -func (mr *MockNetNSUtilMockRecorder) DelNetNS(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelNetNS", reflect.TypeOf((*MockNetNSUtil)(nil).DelNetNS), arg0) -} - -// NSExists mocks base method -func (m *MockNetNSUtil) NSExists(arg0 string) (bool, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NSExists", arg0) - ret0, _ := ret[0].(bool) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// NSExists indicates an expected call of NSExists -func (mr *MockNetNSUtilMockRecorder) NSExists(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NSExists", reflect.TypeOf((*MockNetNSUtil)(nil).NSExists), arg0) -} - -// NewNetNS mocks base method -func (m *MockNetNSUtil) NewNetNS(arg0 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NewNetNS", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// NewNetNS indicates an expected call of NewNetNS -func (mr *MockNetNSUtilMockRecorder) NewNetNS(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewNetNS", reflect.TypeOf((*MockNetNSUtil)(nil).NewNetNS), arg0) -} - -// NewNetNSID mocks base method -func (m *MockNetNSUtil) NewNetNSID() string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NewNetNSID") - ret0, _ := ret[0].(string) - return ret0 -} - -// NewNetNSID indicates an expected call of NewNetNSID -func (mr *MockNetNSUtilMockRecorder) NewNetNSID() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewNetNSID", reflect.TypeOf((*MockNetNSUtil)(nil).NewNetNSID)) -} - -// MockCNI is a mock of CNI interface -type MockCNI struct { - ctrl *gomock.Controller - recorder *MockCNIMockRecorder -} - -// MockCNIMockRecorder is the mock recorder for MockCNI -type MockCNIMockRecorder struct { - mock *MockCNI -} - -// NewMockCNI creates a new mock instance -func NewMockCNI(ctrl *gomock.Controller) *MockCNI { - mock := &MockCNI{ctrl: ctrl} - mock.recorder = &MockCNIMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use -func (m *MockCNI) EXPECT() *MockCNIMockRecorder { - return m.recorder -} - -// Add mocks base method -func (m *MockCNI) Add(arg0 context.Context, arg1 ecscni.PluginConfig) (types.Result, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Add", arg0, arg1) - ret0, _ := ret[0].(types.Result) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Add indicates an expected call of Add -func (mr *MockCNIMockRecorder) Add(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Add", reflect.TypeOf((*MockCNI)(nil).Add), arg0, arg1) -} - -// Del mocks base method -func (m *MockCNI) Del(arg0 context.Context, arg1 ecscni.PluginConfig) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Del", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// Del indicates an expected call of Del -func (mr *MockCNIMockRecorder) Del(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Del", reflect.TypeOf((*MockCNI)(nil).Del), arg0, arg1) -} - -// Version mocks base method -func (m *MockCNI) Version(arg0 string) (string, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Version", arg0) - ret0, _ := ret[0].(string) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Version indicates an expected call of Version -func (mr *MockCNIMockRecorder) Version(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Version", reflect.TypeOf((*MockCNI)(nil).Version), arg0) -} diff --git a/ecs-agent/netlib/model/ecscni/mocks_libcni/libcni_mocks.go b/ecs-agent/netlib/model/ecscni/mocks_libcni/libcni_mocks.go new file mode 100644 index 00000000000..23c4638c751 --- /dev/null +++ b/ecs-agent/netlib/model/ecscni/mocks_libcni/libcni_mocks.go @@ -0,0 +1,229 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. +// + +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/containernetworking/cni/libcni (interfaces: CNI) + +// Package mock_libcni is a generated GoMock package. +package mock_libcni + +import ( + context "context" + reflect "reflect" + + libcni "github.com/containernetworking/cni/libcni" + types "github.com/containernetworking/cni/pkg/types" + gomock "github.com/golang/mock/gomock" +) + +// MockCNI is a mock of CNI interface. +type MockCNI struct { + ctrl *gomock.Controller + recorder *MockCNIMockRecorder +} + +// MockCNIMockRecorder is the mock recorder for MockCNI. +type MockCNIMockRecorder struct { + mock *MockCNI +} + +// NewMockCNI creates a new mock instance. +func NewMockCNI(ctrl *gomock.Controller) *MockCNI { + mock := &MockCNI{ctrl: ctrl} + mock.recorder = &MockCNIMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockCNI) EXPECT() *MockCNIMockRecorder { + return m.recorder +} + +// AddNetwork mocks base method. +func (m *MockCNI) AddNetwork(arg0 context.Context, arg1 *libcni.NetworkConfig, arg2 *libcni.RuntimeConf) (types.Result, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddNetwork", arg0, arg1, arg2) + ret0, _ := ret[0].(types.Result) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// AddNetwork indicates an expected call of AddNetwork. +func (mr *MockCNIMockRecorder) AddNetwork(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddNetwork", reflect.TypeOf((*MockCNI)(nil).AddNetwork), arg0, arg1, arg2) +} + +// AddNetworkList mocks base method. +func (m *MockCNI) AddNetworkList(arg0 context.Context, arg1 *libcni.NetworkConfigList, arg2 *libcni.RuntimeConf) (types.Result, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddNetworkList", arg0, arg1, arg2) + ret0, _ := ret[0].(types.Result) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// AddNetworkList indicates an expected call of AddNetworkList. +func (mr *MockCNIMockRecorder) AddNetworkList(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddNetworkList", reflect.TypeOf((*MockCNI)(nil).AddNetworkList), arg0, arg1, arg2) +} + +// CheckNetwork mocks base method. +func (m *MockCNI) CheckNetwork(arg0 context.Context, arg1 *libcni.NetworkConfig, arg2 *libcni.RuntimeConf) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CheckNetwork", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// CheckNetwork indicates an expected call of CheckNetwork. +func (mr *MockCNIMockRecorder) CheckNetwork(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CheckNetwork", reflect.TypeOf((*MockCNI)(nil).CheckNetwork), arg0, arg1, arg2) +} + +// CheckNetworkList mocks base method. +func (m *MockCNI) CheckNetworkList(arg0 context.Context, arg1 *libcni.NetworkConfigList, arg2 *libcni.RuntimeConf) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CheckNetworkList", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// CheckNetworkList indicates an expected call of CheckNetworkList. +func (mr *MockCNIMockRecorder) CheckNetworkList(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CheckNetworkList", reflect.TypeOf((*MockCNI)(nil).CheckNetworkList), arg0, arg1, arg2) +} + +// DelNetwork mocks base method. +func (m *MockCNI) DelNetwork(arg0 context.Context, arg1 *libcni.NetworkConfig, arg2 *libcni.RuntimeConf) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DelNetwork", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// DelNetwork indicates an expected call of DelNetwork. +func (mr *MockCNIMockRecorder) DelNetwork(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelNetwork", reflect.TypeOf((*MockCNI)(nil).DelNetwork), arg0, arg1, arg2) +} + +// DelNetworkList mocks base method. +func (m *MockCNI) DelNetworkList(arg0 context.Context, arg1 *libcni.NetworkConfigList, arg2 *libcni.RuntimeConf) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DelNetworkList", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// DelNetworkList indicates an expected call of DelNetworkList. +func (mr *MockCNIMockRecorder) DelNetworkList(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelNetworkList", reflect.TypeOf((*MockCNI)(nil).DelNetworkList), arg0, arg1, arg2) +} + +// GetNetworkCachedConfig mocks base method. +func (m *MockCNI) GetNetworkCachedConfig(arg0 *libcni.NetworkConfig, arg1 *libcni.RuntimeConf) ([]byte, *libcni.RuntimeConf, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetNetworkCachedConfig", arg0, arg1) + ret0, _ := ret[0].([]byte) + ret1, _ := ret[1].(*libcni.RuntimeConf) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// GetNetworkCachedConfig indicates an expected call of GetNetworkCachedConfig. +func (mr *MockCNIMockRecorder) GetNetworkCachedConfig(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNetworkCachedConfig", reflect.TypeOf((*MockCNI)(nil).GetNetworkCachedConfig), arg0, arg1) +} + +// GetNetworkCachedResult mocks base method. +func (m *MockCNI) GetNetworkCachedResult(arg0 *libcni.NetworkConfig, arg1 *libcni.RuntimeConf) (types.Result, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetNetworkCachedResult", arg0, arg1) + ret0, _ := ret[0].(types.Result) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetNetworkCachedResult indicates an expected call of GetNetworkCachedResult. +func (mr *MockCNIMockRecorder) GetNetworkCachedResult(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNetworkCachedResult", reflect.TypeOf((*MockCNI)(nil).GetNetworkCachedResult), arg0, arg1) +} + +// GetNetworkListCachedConfig mocks base method. +func (m *MockCNI) GetNetworkListCachedConfig(arg0 *libcni.NetworkConfigList, arg1 *libcni.RuntimeConf) ([]byte, *libcni.RuntimeConf, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetNetworkListCachedConfig", arg0, arg1) + ret0, _ := ret[0].([]byte) + ret1, _ := ret[1].(*libcni.RuntimeConf) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// GetNetworkListCachedConfig indicates an expected call of GetNetworkListCachedConfig. +func (mr *MockCNIMockRecorder) GetNetworkListCachedConfig(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNetworkListCachedConfig", reflect.TypeOf((*MockCNI)(nil).GetNetworkListCachedConfig), arg0, arg1) +} + +// GetNetworkListCachedResult mocks base method. +func (m *MockCNI) GetNetworkListCachedResult(arg0 *libcni.NetworkConfigList, arg1 *libcni.RuntimeConf) (types.Result, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetNetworkListCachedResult", arg0, arg1) + ret0, _ := ret[0].(types.Result) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetNetworkListCachedResult indicates an expected call of GetNetworkListCachedResult. +func (mr *MockCNIMockRecorder) GetNetworkListCachedResult(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNetworkListCachedResult", reflect.TypeOf((*MockCNI)(nil).GetNetworkListCachedResult), arg0, arg1) +} + +// ValidateNetwork mocks base method. +func (m *MockCNI) ValidateNetwork(arg0 context.Context, arg1 *libcni.NetworkConfig) ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ValidateNetwork", arg0, arg1) + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ValidateNetwork indicates an expected call of ValidateNetwork. +func (mr *MockCNIMockRecorder) ValidateNetwork(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateNetwork", reflect.TypeOf((*MockCNI)(nil).ValidateNetwork), arg0, arg1) +} + +// ValidateNetworkList mocks base method. +func (m *MockCNI) ValidateNetworkList(arg0 context.Context, arg1 *libcni.NetworkConfigList) ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ValidateNetworkList", arg0, arg1) + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ValidateNetworkList indicates an expected call of ValidateNetworkList. +func (mr *MockCNIMockRecorder) ValidateNetworkList(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateNetworkList", reflect.TypeOf((*MockCNI)(nil).ValidateNetworkList), arg0, arg1) +} diff --git a/ecs-agent/netlib/model/ecscni/netconfig.go b/ecs-agent/netlib/model/ecscni/netconfig.go index 30a307ee698..181a7e5c417 100644 --- a/ecs-agent/netlib/model/ecscni/netconfig.go +++ b/ecs-agent/netlib/model/ecscni/netconfig.go @@ -23,15 +23,15 @@ import ( ) const ( - defaultInterfaceName = "eth0" - defaultENIName = "eth1" + DefaultInterfaceName = "eth0" + DefaultENIName = "eth1" PluginLogPath = "/var/log/ecs/ecs-cni-warmpool.log" ) // PluginConfig is the general interface for a plugin's configuration type PluginConfig interface { - // String returns the human readable information of the configuration + // String returns the human-readable information of the configuration String() string // InterfaceName returns the name of the interface to be configured InterfaceName() string diff --git a/ecs-agent/netlib/model/ecscni/netconfig_test.go b/ecs-agent/netlib/model/ecscni/netconfig_test.go index ff877dd7c9d..c4e38d6ae6d 100644 --- a/ecs-agent/netlib/model/ecscni/netconfig_test.go +++ b/ecs-agent/netlib/model/ecscni/netconfig_test.go @@ -24,14 +24,15 @@ import ( func TestBuildNetworkConfig(t *testing.T) { nsPath := "nspath" - cfg := &VPCTunnelConfig{ + cfg := &TestCNIConfig{ CNIConfig: CNIConfig{ NetNSPath: nsPath, }, + NetworkInterfaceName: DefaultInterfaceName, } rt := BuildRuntimeConfig(cfg) require.Equal(t, nsPath, rt.NetNS) - require.Equal(t, defaultInterfaceName, rt.IfName) + require.Equal(t, DefaultInterfaceName, rt.IfName) require.Equal(t, "container-id", rt.ContainerID) } diff --git a/ecs-agent/netlib/model/ecscni/nsutil_linux.go b/ecs-agent/netlib/model/ecscni/nsutil_linux.go index 4e76979a6f8..33d1a320779 100644 --- a/ecs-agent/netlib/model/ecscni/nsutil_linux.go +++ b/ecs-agent/netlib/model/ecscni/nsutil_linux.go @@ -35,7 +35,7 @@ const ( dnsSearchDomainFormat = "search %s\n" ) -// NetNSUtils provides some basic methods for agent to deal with network namespace +// NetNSUtil provides some basic methods for agent to deal with network namespace type NetNSUtil interface { // NewNetNS creates a new network namespace in the system NewNetNS(nsPath string) error @@ -74,7 +74,7 @@ func (*netnsutil) NewNetNS(nspath string) error { if err != nil { // Create the default network namespace directory path if not exists if os.IsNotExist(err) { - err = os.Mkdir(NETNS_PATH_DEFAULT, nsFileMode) + err = os.Mkdir(NETNS_PATH_DEFAULT, NsFileMode) } if err != nil { return errors.Wrap(err, "unable to get status of default ns directory") @@ -88,7 +88,7 @@ func (*netnsutil) NewNetNS(nspath string) error { } // Make this network namespace persistent - f, err := os.OpenFile(nspath, os.O_CREATE|os.O_EXCL, nsFileMode) + f, err := os.OpenFile(nspath, os.O_CREATE|os.O_EXCL, NsFileMode) if err != nil { return errors.Wrap(err, "unable to create ns file") } diff --git a/ecs-agent/netlib/model/ecscni/nsutil_linux_test.go b/ecs-agent/netlib/model/ecscni/nsutil_linux_test.go new file mode 100644 index 00000000000..f2acb018bcb --- /dev/null +++ b/ecs-agent/netlib/model/ecscni/nsutil_linux_test.go @@ -0,0 +1 @@ +package ecscni diff --git a/ecs-agent/netlib/model/ecscni/nsutil_windows.go b/ecs-agent/netlib/model/ecscni/nsutil_windows.go index d4a18f4294d..535ed29303e 100644 --- a/ecs-agent/netlib/model/ecscni/nsutil_windows.go +++ b/ecs-agent/netlib/model/ecscni/nsutil_windows.go @@ -91,7 +91,7 @@ func (*nsUtil) NSExists(netNSID string) (bool, error) { _, err := hcn.GetNamespaceByID(netNSID) if err != nil { // If the HCN resource was not found then the namespace doesn't exist, so return false. - // Otherwise there was an error in HCN request, so return an error. + // Otherwise, there was an error in HCN request, so return an error. if hcn.IsNotFoundError(err) { return false, nil } else { diff --git a/ecs-agent/netlib/model/ecscni/nsutil_windows_test.go b/ecs-agent/netlib/model/ecscni/nsutil_windows_test.go new file mode 100644 index 00000000000..f2acb018bcb --- /dev/null +++ b/ecs-agent/netlib/model/ecscni/nsutil_windows_test.go @@ -0,0 +1 @@ +package ecscni diff --git a/ecs-agent/netlib/model/ecscni/plugin.go b/ecs-agent/netlib/model/ecscni/plugin.go index 49952fb8988..795e710e3fe 100644 --- a/ecs-agent/netlib/model/ecscni/plugin.go +++ b/ecs-agent/netlib/model/ecscni/plugin.go @@ -13,13 +13,29 @@ package ecscni +import ( + "context" + + "github.com/containernetworking/cni/pkg/types" +) + const ( NETNS_PATH_DEFAULT = "/var/run/netns" NETNS_PROC_FORMAT = "/proc/%d/task/%d/ns/net" - nsFileMode = 0444 + NsFileMode = 0444 ) +// CNI defines the plugin invocation interface +type CNI interface { + // Add calls the plugin add command with given configuration + Add(context.Context, PluginConfig) (types.Result, error) + // Del calls the plugin del command with given configuration + Del(context.Context, PluginConfig) error + // Version calls the version command of plugin + Version(string) (string, error) +} + // Config is a general interface represents all kinds of plugin configs type Config interface { String() string diff --git a/ecs-agent/netlib/model/ecscni/generate_mocks_windows.go b/ecs-agent/netlib/model/ecscni/test_types.go similarity index 54% rename from ecs-agent/netlib/model/ecscni/generate_mocks_windows.go rename to ecs-agent/netlib/model/ecscni/test_types.go index c685781ed1c..5917155bd72 100644 --- a/ecs-agent/netlib/model/ecscni/generate_mocks_windows.go +++ b/ecs-agent/netlib/model/ecscni/test_types.go @@ -11,9 +11,32 @@ // express or implied. See the License for the specific language governing // permissions and limitations under the License. -//go:build windows -// +build windows - package ecscni -//go:generate mockgen -build_flags=--mod=mod -destination=mocks/ecscni_mocks_windows.go -copyright_file=../../../../scripts/copyright_file github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/ecscni Config,NetNSUtil,CNI +const ( + PluginName = "testPlugin" + CNIVersion = "testVersion" + NetNS = "testNetNS" + IfName = "testIfName" +) + +type TestCNIConfig struct { + CNIConfig + NetworkInterfaceName string +} + +func (tc *TestCNIConfig) InterfaceName() string { + return tc.NetworkInterfaceName +} + +func (tc *TestCNIConfig) NSPath() string { + return tc.NetNSPath +} + +func (tc *TestCNIConfig) PluginName() string { + return tc.CNIPluginName +} + +func (tc *TestCNIConfig) CNIVersion() string { + return tc.CNISpecVersion +} diff --git a/ecs-agent/netlib/model/ecscni/vpcbranch_config.go b/ecs-agent/netlib/model/ecscni/vpcbranch_config.go index 0e54fbf67ee..1d468cca602 100644 --- a/ecs-agent/netlib/model/ecscni/vpcbranch_config.go +++ b/ecs-agent/netlib/model/ecscni/vpcbranch_config.go @@ -49,7 +49,7 @@ func (c *VPCBranchENIConfig) InterfaceName() string { return c.IfName } - return defaultInterfaceName + return DefaultInterfaceName } func (c *VPCBranchENIConfig) NSPath() string { diff --git a/ecs-agent/netlib/model/ecscni/vpceni_config.go b/ecs-agent/netlib/model/ecscni/vpceni_config.go index 2d305610b39..6c18807269c 100644 --- a/ecs-agent/netlib/model/ecscni/vpceni_config.go +++ b/ecs-agent/netlib/model/ecscni/vpceni_config.go @@ -52,7 +52,7 @@ func (ec *VPCENIConfig) String() string { // InterfaceName returns the veth pair name will be used inside the namespace. // For this plugin, interface name is redundant and would be generated in the plugin itself. func (ec *VPCENIConfig) InterfaceName() string { - return defaultInterfaceName + return DefaultInterfaceName } func (ec *VPCENIConfig) NSPath() string { diff --git a/ecs-agent/netlib/model/ecscni/vpctunnel_config.go b/ecs-agent/netlib/model/ecscni/vpctunnel_config.go index 2ef282d6316..607e64a1f0a 100644 --- a/ecs-agent/netlib/model/ecscni/vpctunnel_config.go +++ b/ecs-agent/netlib/model/ecscni/vpctunnel_config.go @@ -51,7 +51,7 @@ func (c *VPCTunnelConfig) InterfaceName() string { return c.IfName } - return defaultInterfaceName + return DefaultInterfaceName } func (c *VPCTunnelConfig) NSPath() string {