Skip to content

Commit d3bf19a

Browse files
committed
adds missing tests
1 parent 296fb38 commit d3bf19a

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

outbound_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,3 +1931,120 @@ func TestParseConfig_LogDrops(t *testing.T) {
19311931
})
19321932
}
19331933
}
1934+
1935+
// mockNoOpManager is a trivial Manager that does nothing, just to avoid iptables calls.
1936+
type mockNoOpManager struct{}
1937+
1938+
func (m *mockNoOpManager) EnsureMainChainExists() error { return nil }
1939+
func (m *mockNoOpManager) CreateContainerChain(chain string) error { return nil }
1940+
func (m *mockNoOpManager) AddRule(chain string, rule iptables.OutboundRule) error { return nil }
1941+
func (m *mockNoOpManager) AddJumpRule(sourceIP, targetChain string) error { return nil }
1942+
func (m *mockNoOpManager) RemoveJumpRule(sourceIP, targetChain string) error { return nil }
1943+
func (m *mockNoOpManager) RemoveJumpRuleByTargetChain(targetChain string) error { return nil }
1944+
func (m *mockNoOpManager) ClearAndDeleteChain(chainName string) error { return nil }
1945+
func (m *mockNoOpManager) ChainExists(chainName string) (bool, error) { return false, nil }
1946+
func (m *mockNoOpManager) VerifyRules(chainName string, rules []iptables.OutboundRule) error {
1947+
return nil
1948+
}
1949+
1950+
func TestCmdAdd_FailedToParsePrevResult(t *testing.T) {
1951+
// We craft a scenario where current.NewResultFromResult(...) fails
1952+
// or yields invalid structure.
1953+
1954+
// 1. Provide a fake config with a broken RawPrevResult
1955+
stdinConfig := `{
1956+
"cniVersion": "0.4.0",
1957+
"name": "test-net",
1958+
"type": "outbound",
1959+
"prevResult": {
1960+
"cniVersion": "0.4.0",
1961+
"interfaces": [
1962+
{ "name": "eth0" }
1963+
],
1964+
"ips": [
1965+
{
1966+
"address": "999.999.999.999/999"
1967+
}
1968+
]
1969+
}
1970+
}`
1971+
1972+
// 2. Create CmdArgs with that config
1973+
args := &skel.CmdArgs{
1974+
ContainerID: "test-container",
1975+
Netns: "/var/run/netns/test",
1976+
IfName: "eth0",
1977+
Args: "",
1978+
Path: "/opt/cni/bin",
1979+
StdinData: []byte(stdinConfig),
1980+
}
1981+
1982+
// 3. We mock newIPTablesManager if needed or just let the plugin create a no-op
1983+
originalNewIPTablesManager := newIPTablesManager
1984+
newIPTablesManager = func(conf *PluginConf) (iptables.Manager, error) {
1985+
// Return a mock or a no-op manager so we don't fail on iptables calls
1986+
return &mockNoOpManager{}, nil
1987+
}
1988+
defer func() { newIPTablesManager = originalNewIPTablesManager }()
1989+
1990+
// 4. Call cmdAdd, expecting "failed to parse prevResult"
1991+
err := cmdAdd(args)
1992+
if err == nil {
1993+
t.Fatal("Expected error but got nil")
1994+
}
1995+
1996+
// 5. Verify the error message
1997+
if !strings.Contains(err.Error(), "could not parse prevResult: could not parse prevResult: invalid CIDR address: 999.999.999.999/999") {
1998+
t.Errorf("Expected 'could not parse prevResult: could not parse prevResult: invalid CIDR address: 999.999.999.999/999' in error, got %q", err.Error())
1999+
}
2000+
}
2001+
2002+
func TestCmdAdd_NoIPv4Addresses(t *testing.T) {
2003+
// Provide a prevResult with only IPv6 or empty IP array
2004+
stdinConfig := `{
2005+
"cniVersion": "0.4.0",
2006+
"name": "test-net",
2007+
"type": "outbound",
2008+
"prevResult": {
2009+
"cniVersion": "0.4.0",
2010+
"interfaces": [
2011+
{ "name": "eth0" }
2012+
],
2013+
"ips": [
2014+
{
2015+
"version": "6",
2016+
"interface": 0,
2017+
"address": "fe80::1234/64",
2018+
"gateway": "fe80::1"
2019+
}
2020+
]
2021+
}
2022+
}`
2023+
2024+
args := &skel.CmdArgs{
2025+
ContainerID: "test-container",
2026+
Netns: "/var/run/netns/test",
2027+
IfName: "eth0",
2028+
Args: "",
2029+
Path: "/opt/cni/bin",
2030+
StdinData: []byte(stdinConfig),
2031+
}
2032+
2033+
// If we actually create an iptables manager, it won't matter because
2034+
// we'll fail before we call iptables methods. But we can still override:
2035+
originalNewIPTablesManager := newIPTablesManager
2036+
newIPTablesManager = func(conf *PluginConf) (iptables.Manager, error) {
2037+
return &mockNoOpManager{}, nil
2038+
}
2039+
defer func() { newIPTablesManager = originalNewIPTablesManager }()
2040+
2041+
// 4. Expect 'no IPv4 addresses found in prevResult'
2042+
err := cmdAdd(args)
2043+
if err == nil {
2044+
t.Fatal("Expected error but got nil")
2045+
}
2046+
2047+
if !strings.Contains(err.Error(), "no IPv4 addresses found in prevResult") {
2048+
t.Errorf("Expected 'no IPv4 addresses found in prevResult' in error, got %q", err.Error())
2049+
}
2050+
}

0 commit comments

Comments
 (0)