diff --git a/pkg/util/ipset/ipset_test.go b/pkg/util/ipset/ipset_test.go index 3ad5cb70341..ff7ccd64538 100644 --- a/pkg/util/ipset/ipset_test.go +++ b/pkg/util/ipset/ipset_test.go @@ -454,7 +454,6 @@ func TestDelEntry(t *testing.T) { } func TestTestEntry(t *testing.T) { - // TODO: IPv6? testEntry := &Entry{ IP: "10.120.7.100", Port: 8080, @@ -502,6 +501,54 @@ func TestTestEntry(t *testing.T) { } } +func TestTestEntryIPv6(t *testing.T) { + testEntry := &Entry{ + IP: "fd00:1234:5678:dead:beaf::1", + Port: 8080, + Protocol: ProtocolTCP, + SetType: HashIPPort, + } + setName := "NOT" + fcmd := fakeexec.FakeCmd{ + CombinedOutputScript: []fakeexec.FakeAction{ + // Success + func() ([]byte, []byte, error) { + return []byte("fd00:1234:5678:dead:beaf::1,tcp:8080 is in set " + setName + "."), nil, nil + }, + // Failure + func() ([]byte, []byte, error) { + return []byte("fd00::2,tcp:8080 is NOT in set FOOBAR."), nil, &fakeexec.FakeExitError{Status: 1} + }, + }, + } + fexec := fakeexec.FakeExec{ + CommandScript: []fakeexec.FakeCommandAction{ + func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, + }, + } + runner := New(&fexec) + // Success + ok, err := runner.TestEntry(testEntry.String(), setName) + if err != nil { + t.Errorf("expected success, got %v", err) + } + if fcmd.CombinedOutputCalls != 1 { + t.Errorf("expected 1 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls) + } + if !sets.NewString(fcmd.CombinedOutputLog[0]...).HasAll("ipset", "test", setName, "fd00:1234:5678:dead:beaf::1,tcp:8080") { + t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[0]) + } + if !ok { + t.Errorf("expect entry exists in test set, got not") + } + // Failure + ok, err = runner.TestEntry(testEntry.String(), "FOOBAR") + if err == nil || ok { + t.Errorf("expect entry doesn't exist in test set") + } +} + func TestListEntries(t *testing.T) { output := `Name: foobar