mirror of
https://github.com/k8snetworkplumbingwg/multus-cni.git
synced 2025-09-19 16:40:58 +00:00
multus: unit test multi-network interface names (#82)
This commit is contained in:
committed by
Tomofumi Hayashi
parent
e047460384
commit
589877b644
@@ -22,6 +22,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/containernetworking/cni/pkg/skel"
|
"github.com/containernetworking/cni/pkg/skel"
|
||||||
@@ -43,10 +44,11 @@ func TestMultus(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type fakePlugin struct {
|
type fakePlugin struct {
|
||||||
expectedEnv []string
|
expectedEnv []string
|
||||||
expectedConf string
|
expectedConf string
|
||||||
result cnitypes.Result
|
expectedIfname string
|
||||||
err error
|
result cnitypes.Result
|
||||||
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
type fakeExec struct {
|
type fakeExec struct {
|
||||||
@@ -57,12 +59,13 @@ type fakeExec struct {
|
|||||||
plugins []*fakePlugin
|
plugins []*fakePlugin
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeExec) addPlugin(expectedEnv []string, expectedConf string, result *types020.Result, err error) {
|
func (f *fakeExec) addPlugin(expectedEnv []string, expectedIfname, expectedConf string, result *types020.Result, err error) {
|
||||||
f.plugins = append(f.plugins, &fakePlugin{
|
f.plugins = append(f.plugins, &fakePlugin{
|
||||||
expectedEnv: expectedEnv,
|
expectedEnv: expectedEnv,
|
||||||
expectedConf: expectedConf,
|
expectedConf: expectedConf,
|
||||||
result: result,
|
expectedIfname: expectedIfname,
|
||||||
err: err,
|
result: result,
|
||||||
|
err: err,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,6 +84,19 @@ func matchArray(a1, a2 []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// When faking plugin execution the ExecPlugin() call environ is not populated
|
||||||
|
// (while it would be for real exec). Filter the environment variables for
|
||||||
|
// CNI-specific ones that testcases will care about.
|
||||||
|
func gatherCNIEnv() []string {
|
||||||
|
filtered := make([]string, 0)
|
||||||
|
for _, env := range os.Environ() {
|
||||||
|
if strings.HasPrefix(env, "CNI_") {
|
||||||
|
filtered = append(filtered, env)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filtered
|
||||||
|
}
|
||||||
|
|
||||||
func (f *fakeExec) ExecPlugin(pluginPath string, stdinData []byte, environ []string) ([]byte, error) {
|
func (f *fakeExec) ExecPlugin(pluginPath string, stdinData []byte, environ []string) ([]byte, error) {
|
||||||
cmd := os.Getenv("CNI_COMMAND")
|
cmd := os.Getenv("CNI_COMMAND")
|
||||||
var index int
|
var index int
|
||||||
@@ -91,8 +107,7 @@ func (f *fakeExec) ExecPlugin(pluginPath string, stdinData []byte, environ []str
|
|||||||
f.addIndex++
|
f.addIndex++
|
||||||
case "DEL":
|
case "DEL":
|
||||||
Expect(len(f.plugins)).To(BeNumerically(">", f.delIndex))
|
Expect(len(f.plugins)).To(BeNumerically(">", f.delIndex))
|
||||||
// +1 to skip loopback since it isn't run on DEL
|
index = len(f.plugins) - f.delIndex - 1
|
||||||
index = f.delIndex + 1
|
|
||||||
f.delIndex++
|
f.delIndex++
|
||||||
default:
|
default:
|
||||||
// Should never be reached
|
// Should never be reached
|
||||||
@@ -105,8 +120,11 @@ func (f *fakeExec) ExecPlugin(pluginPath string, stdinData []byte, environ []str
|
|||||||
if plugin.expectedConf != "" {
|
if plugin.expectedConf != "" {
|
||||||
Expect(string(stdinData)).To(MatchJSON(plugin.expectedConf))
|
Expect(string(stdinData)).To(MatchJSON(plugin.expectedConf))
|
||||||
}
|
}
|
||||||
|
if plugin.expectedIfname != "" {
|
||||||
|
Expect(os.Getenv("CNI_IFNAME")).To(Equal(plugin.expectedIfname))
|
||||||
|
}
|
||||||
if len(plugin.expectedEnv) > 0 {
|
if len(plugin.expectedEnv) > 0 {
|
||||||
matchArray(environ, plugin.expectedEnv)
|
matchArray(gatherCNIEnv(), plugin.expectedEnv)
|
||||||
}
|
}
|
||||||
|
|
||||||
if plugin.err != nil {
|
if plugin.err != nil {
|
||||||
@@ -179,7 +197,7 @@ var _ = Describe("multus operations", func() {
|
|||||||
"cniVersion": "0.2.0",
|
"cniVersion": "0.2.0",
|
||||||
"type": "weave-net"
|
"type": "weave-net"
|
||||||
}`
|
}`
|
||||||
fExec.addPlugin(nil, expectedConf1, expectedResult1, nil)
|
fExec.addPlugin(nil, "eth0", expectedConf1, expectedResult1, nil)
|
||||||
|
|
||||||
expectedResult2 := &types020.Result{
|
expectedResult2 := &types020.Result{
|
||||||
CNIVersion: "0.2.0",
|
CNIVersion: "0.2.0",
|
||||||
@@ -192,7 +210,7 @@ var _ = Describe("multus operations", func() {
|
|||||||
"cniVersion": "0.2.0",
|
"cniVersion": "0.2.0",
|
||||||
"type": "other-plugin"
|
"type": "other-plugin"
|
||||||
}`
|
}`
|
||||||
fExec.addPlugin(nil, expectedConf2, expectedResult2, nil)
|
fExec.addPlugin(nil, "net1", expectedConf2, expectedResult2, nil)
|
||||||
|
|
||||||
os.Setenv("CNI_COMMAND", "ADD")
|
os.Setenv("CNI_COMMAND", "ADD")
|
||||||
os.Setenv("CNI_IFNAME", "eth0")
|
os.Setenv("CNI_IFNAME", "eth0")
|
||||||
@@ -202,6 +220,12 @@ var _ = Describe("multus operations", func() {
|
|||||||
r := result.(*types020.Result)
|
r := result.(*types020.Result)
|
||||||
// plugin 1 is the masterplugin
|
// plugin 1 is the masterplugin
|
||||||
Expect(reflect.DeepEqual(r, expectedResult1)).To(BeTrue())
|
Expect(reflect.DeepEqual(r, expectedResult1)).To(BeTrue())
|
||||||
|
|
||||||
|
os.Setenv("CNI_COMMAND", "DEL")
|
||||||
|
os.Setenv("CNI_IFNAME", "eth0")
|
||||||
|
err = cmdDel(args, fExec, nil)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(fExec.delIndex).To(Equal(len(fExec.plugins)))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("executes delegates and kubernetes networks", func() {
|
It("executes delegates and kubernetes networks", func() {
|
||||||
@@ -250,14 +274,14 @@ var _ = Describe("multus operations", func() {
|
|||||||
"cniVersion": "0.2.0",
|
"cniVersion": "0.2.0",
|
||||||
"type": "weave-net"
|
"type": "weave-net"
|
||||||
}`
|
}`
|
||||||
fExec.addPlugin(nil, expectedConf1, expectedResult1, nil)
|
fExec.addPlugin(nil, "eth0", expectedConf1, expectedResult1, nil)
|
||||||
fExec.addPlugin(nil, net1, &types020.Result{
|
fExec.addPlugin(nil, "net1", net1, &types020.Result{
|
||||||
CNIVersion: "0.2.0",
|
CNIVersion: "0.2.0",
|
||||||
IP4: &types020.IPConfig{
|
IP4: &types020.IPConfig{
|
||||||
IP: *testhelpers.EnsureCIDR("1.1.1.3/24"),
|
IP: *testhelpers.EnsureCIDR("1.1.1.3/24"),
|
||||||
},
|
},
|
||||||
}, nil)
|
}, nil)
|
||||||
fExec.addPlugin(nil, net2, &types020.Result{
|
fExec.addPlugin(nil, "net2", net2, &types020.Result{
|
||||||
CNIVersion: "0.2.0",
|
CNIVersion: "0.2.0",
|
||||||
IP4: &types020.IPConfig{
|
IP4: &types020.IPConfig{
|
||||||
IP: *testhelpers.EnsureCIDR("1.1.1.4/24"),
|
IP: *testhelpers.EnsureCIDR("1.1.1.4/24"),
|
||||||
|
Reference in New Issue
Block a user