Skip to content

Commit 3b986cc

Browse files
committed
adds tests
1 parent efd9a34 commit 3b986cc

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

outbound_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,52 @@ func TestCmdCheck(t *testing.T) {
11011101
mockManager.AssertExpectations(t)
11021102
}
11031103

1104+
func TestCmdCheckWithMetadata(t *testing.T) {
1105+
// Stdin JSON includes some metadata
1106+
input := `{
1107+
"cniVersion": "0.4.0",
1108+
"name": "test-net",
1109+
"type": "outbound",
1110+
"mainChainName": "TEST-OUTBOUND",
1111+
"defaultAction": "ACCEPT",
1112+
"metadata": {
1113+
"test_key": "test_value"
1114+
},
1115+
"outboundRules": [
1116+
{"host": "8.8.8.8", "proto": "udp", "port": "53", "action": "ACCEPT"}
1117+
]
1118+
}`
1119+
1120+
args := &skel.CmdArgs{
1121+
ContainerID: "test-container",
1122+
Netns: "/var/run/netns/test",
1123+
IfName: "eth0",
1124+
Args: "K8S_POD_NAMESPACE=test;K8S_POD_NAME=test-pod", // More metadata
1125+
Path: "/opt/cni/bin",
1126+
StdinData: []byte(input),
1127+
}
1128+
1129+
mockManager := new(MockIPTablesManager)
1130+
// Ensure main chain call
1131+
mockManager.On("ChainExists", "TEST-OUTBOUND").Return(true, nil)
1132+
// Container chain call
1133+
mockManager.On("ChainExists", mock.AnythingOfType("string")).Return(true, nil)
1134+
// Verify rules call
1135+
mockManager.On("VerifyRules", mock.Anything, mock.Anything).Return(nil)
1136+
1137+
origNewIPTablesManager := newIPTablesManager
1138+
newIPTablesManager = func(conf *PluginConf) (iptables.Manager, error) {
1139+
return mockManager, nil
1140+
}
1141+
defer func() { newIPTablesManager = origNewIPTablesManager }()
1142+
1143+
err := cmdCheck(args)
1144+
assert.NoError(t, err)
1145+
1146+
// This ensures the manager calls were made as expected
1147+
mockManager.AssertExpectations(t)
1148+
}
1149+
11041150
func TestCmdCheckNewIPTablesManagerFailure(t *testing.T) {
11051151
input := `{
11061152
"cniVersion": "0.4.0",

0 commit comments

Comments
 (0)