Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Realmonia committed Sep 14, 2023
1 parent 6818a76 commit 6c7b434
Show file tree
Hide file tree
Showing 20 changed files with 608 additions and 516 deletions.
2 changes: 1 addition & 1 deletion ecs-agent/netlib/model/ecscni/bridge_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions ecs-agent/netlib/model/ecscni/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
109 changes: 109 additions & 0 deletions ecs-agent/netlib/model/ecscni/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//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"
"fmt"
"testing"

mock_libcni "github.com/aws/amazon-ecs-agent/ecs-agent/netlib/model/ecscni/mocks_libcni"

"github.com/containernetworking/cni/pkg/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestAddDel(t *testing.T) {
testCases := []struct {
name string
addError bool
delError bool
}{
{
name: "no error",
addError: false,
delError: false,
},
{
name: "add error",
addError: true,
delError: false,
},
{
name: "del error",
addError: false,
delError: true,
},
{
name: "both error",
addError: true,
delError: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(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,
}

var addErrRet, delErrRet error
var addRst types.Result
if tc.addError {
addErrRet = fmt.Errorf("add error")
} else {
addRst = &TestResult{
msg: CNIVersion,
}
}
if tc.delError {
delErrRet = fmt.Errorf("del error")
}
mockLibCNIClient.EXPECT().AddNetwork(gomock.Any(), gomock.Any(), gomock.Any()).Return(addRst, addErrRet)
mockLibCNIClient.EXPECT().DelNetwork(gomock.Any(), gomock.Any(), gomock.Any()).Return(delErrRet)

rst, addErr := client.Add(ctx, config)
delErr := client.Del(ctx, config)

if tc.addError {
assert.Error(t, addErr, "expecting error from add operation")
assert.Nil(t, rst)
} else {
assert.NoError(t, addErr, "expecting no error from add operation")
assert.NotNil(t, rst, "expecting result from add operation")
}
if tc.delError {
assert.Error(t, delErr, "expecting error from del operation")
} else {
assert.NoError(t, delErr, "expecting no error from del operation")
}
})
}
}
2 changes: 1 addition & 1 deletion ecs-agent/netlib/model/ecscni/eni_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (ec *ENIConfig) NSPath() string {

func (ec *ENIConfig) InterfaceName() string {
if ec.DeviceName == "" {
return defaultENIName
return DefaultENIName
}
return ec.DeviceName
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 0 additions & 19 deletions ecs-agent/netlib/model/ecscni/generate_mocks_windows.go

This file was deleted.

Loading

0 comments on commit 6c7b434

Please sign in to comment.