From 2220c6dfbf33cf34dff999d68ac7c894d6689f18 Mon Sep 17 00:00:00 2001 From: Maciej Kwiek Date: Thu, 19 Jan 2017 14:25:40 +0100 Subject: [PATCH] pkg/util/iptables missing unit tests Added tests for Save, SaveAll, Restore and RestoreAll --- pkg/util/iptables/iptables.go | 1 - pkg/util/iptables/iptables_test.go | 221 +++++++++++++++++++++++++++++ 2 files changed, 221 insertions(+), 1 deletion(-) diff --git a/pkg/util/iptables/iptables.go b/pkg/util/iptables/iptables.go index 02ad18637d..1b09ff7720 100644 --- a/pkg/util/iptables/iptables.go +++ b/pkg/util/iptables/iptables.go @@ -54,7 +54,6 @@ type Interface interface { DeleteRule(table Table, chain Chain, args ...string) error // IsIpv6 returns true if this is managing ipv6 tables IsIpv6() bool - // TODO: (BenTheElder) Unit-Test Save/SaveAll, Restore/RestoreAll // Save calls `iptables-save` for table. Save(table Table) ([]byte, error) // SaveAll calls `iptables-save`. diff --git a/pkg/util/iptables/iptables_test.go b/pkg/util/iptables/iptables_test.go index 76c4b48825..14ed4dedcb 100644 --- a/pkg/util/iptables/iptables_test.go +++ b/pkg/util/iptables/iptables_test.go @@ -771,3 +771,224 @@ func TestReload(t *testing.T) { t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[6]) } } + +func TestSave(t *testing.T) { + output := `# Generated by iptables-save v1.6.0 on Thu Jan 19 11:38:09 2017 +*filter +:INPUT ACCEPT [15079:38410730] +:FORWARD ACCEPT [0:0] +:OUTPUT ACCEPT [11045:521562] +COMMIT +# Completed on Thu Jan 19 11:38:09 2017` + + fcmd := exec.FakeCmd{ + CombinedOutputScript: []exec.FakeCombinedOutputAction{ + // iptables version check + func() ([]byte, error) { return []byte("iptables v1.9.22"), nil }, + func() ([]byte, error) { return []byte(output), nil }, + func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 1} }, + }, + } + fexec := exec.FakeExec{ + CommandScript: []exec.FakeCommandAction{ + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + }, + } + runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4) + defer runner.Destroy() + // Success. + o, err := runner.Save(TableNAT) + if err != nil { + t.Fatalf("expected success, got %v", err) + } + + if string(o[:len(output)]) != output { + t.Errorf("expected output to be equal to mocked one, got %v", o) + } + + if fcmd.CombinedOutputCalls != 2 { + t.Errorf("expected 2 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls) + } + if !sets.NewString(fcmd.CombinedOutputLog[1]...).HasAll("iptables-save", "-t", "nat") { + t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[1]) + } + + // Failure. + _, err = runner.Save(TableNAT) + if err == nil { + t.Errorf("expected failure") + } +} + +func TestSaveAll(t *testing.T) { + output := `# Generated by iptables-save v1.6.0 on Thu Jan 19 11:38:09 2017 +*filter +:INPUT ACCEPT [15079:38410730] +:FORWARD ACCEPT [0:0] +:OUTPUT ACCEPT [11045:521562] +COMMIT +# Completed on Thu Jan 19 11:38:09 2017` + + fcmd := exec.FakeCmd{ + CombinedOutputScript: []exec.FakeCombinedOutputAction{ + // iptables version check + func() ([]byte, error) { return []byte("iptables v1.9.22"), nil }, + func() ([]byte, error) { return []byte(output), nil }, + func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 1} }, + }, + } + fexec := exec.FakeExec{ + CommandScript: []exec.FakeCommandAction{ + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + }, + } + runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4) + defer runner.Destroy() + // Success. + o, err := runner.SaveAll() + if err != nil { + t.Fatalf("expected success, got %v", err) + } + + if string(o[:len(output)]) != output { + t.Errorf("expected output to be equal to mocked one, got %v", o) + } + + if fcmd.CombinedOutputCalls != 2 { + t.Errorf("expected 2 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls) + } + if !sets.NewString(fcmd.CombinedOutputLog[1]...).HasAll("iptables-save") { + t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[1]) + } + + // Failure. + _, err = runner.SaveAll() + if err == nil { + t.Errorf("expected failure") + } +} + +func TestRestore(t *testing.T) { + fcmd := exec.FakeCmd{ + CombinedOutputScript: []exec.FakeCombinedOutputAction{ + // iptables version check + func() ([]byte, error) { return []byte("iptables v1.9.22"), nil }, + func() ([]byte, error) { return []byte{}, nil }, + func() ([]byte, error) { return []byte{}, nil }, + func() ([]byte, error) { return []byte{}, nil }, + func() ([]byte, error) { return []byte{}, nil }, + func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 1} }, + }, + } + fexec := exec.FakeExec{ + CommandScript: []exec.FakeCommandAction{ + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + }, + } + runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4) + defer runner.Destroy() + + // both flags true + err := runner.Restore(TableNAT, []byte{}, FlushTables, RestoreCounters) + if err != nil { + t.Errorf("expected success, got %v", err) + } + + commandSet := sets.NewString(fcmd.CombinedOutputLog[1]...) + if !commandSet.HasAll("iptables-restore", "-T", string(TableNAT), "--counters") || commandSet.HasAny("--noflush") { + t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[1]) + } + + // FlushTables, NoRestoreCounters + err = runner.Restore(TableNAT, []byte{}, FlushTables, NoRestoreCounters) + if err != nil { + t.Errorf("expected success, got %v", err) + } + + commandSet = sets.NewString(fcmd.CombinedOutputLog[2]...) + if !commandSet.HasAll("iptables-restore", "-T", string(TableNAT)) || commandSet.HasAny("--noflush", "--counters") { + t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2]) + } + + // NoFlushTables, RestoreCounters + err = runner.Restore(TableNAT, []byte{}, NoFlushTables, RestoreCounters) + if err != nil { + t.Errorf("expected success, got %v", err) + } + + commandSet = sets.NewString(fcmd.CombinedOutputLog[3]...) + if !commandSet.HasAll("iptables-restore", "-T", string(TableNAT), "--noflush", "--counters") { + t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[3]) + } + + // NoFlushTables, NoRestoreCounters + err = runner.Restore(TableNAT, []byte{}, NoFlushTables, NoRestoreCounters) + if err != nil { + t.Errorf("expected success, got %v", err) + } + + commandSet = sets.NewString(fcmd.CombinedOutputLog[4]...) + if !commandSet.HasAll("iptables-restore", "-T", string(TableNAT), "--noflush") || commandSet.HasAny("--counters") { + t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[4]) + } + + if fcmd.CombinedOutputCalls != 5 { + t.Errorf("expected 5 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls) + } + + // Failure. + err = runner.Restore(TableNAT, []byte{}, FlushTables, RestoreCounters) + if err == nil { + t.Errorf("expected failure") + } +} + +// TestRestoreAll tests only the simplest use case, as flag handling code is already tested in TestRestore +func TestRestoreAll(t *testing.T) { + fcmd := exec.FakeCmd{ + CombinedOutputScript: []exec.FakeCombinedOutputAction{ + // iptables version check + func() ([]byte, error) { return []byte("iptables v1.9.22"), nil }, + func() ([]byte, error) { return []byte{}, nil }, + func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 1} }, + }, + } + fexec := exec.FakeExec{ + CommandScript: []exec.FakeCommandAction{ + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) }, + }, + } + runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4) + defer runner.Destroy() + + err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters) + if err != nil { + t.Errorf("expected success, got %v", err) + } + + commandSet := sets.NewString(fcmd.CombinedOutputLog[1]...) + if !commandSet.HasAll("iptables-restore", "--counters", "--noflush") { + t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[1]) + } + + if fcmd.CombinedOutputCalls != 2 { + t.Errorf("expected 2 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls) + } + + // Failure. + err = runner.Restore(TableNAT, []byte{}, FlushTables, RestoreCounters) + if err == nil { + t.Errorf("expected failure") + } +}