Remove Save() from iptables interface

This commit is contained in:
Wojciech Tyczynski
2017-05-22 11:20:49 +02:00
parent 06c12e717a
commit 9e6de42745
8 changed files with 59 additions and 43 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package iptables
import (
"bytes"
"net"
"os"
"strings"
@@ -831,7 +832,7 @@ func TestReload(t *testing.T) {
}
}
func TestSave(t *testing.T) {
func TestSaveInto(t *testing.T) {
output := `# Generated by iptables-save v1.6.0 on Thu Jan 19 11:38:09 2017
*filter
:INPUT ACCEPT [15079:38410730]
@@ -846,8 +847,10 @@ COMMIT
func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
// iptables-restore version check
func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
func() ([]byte, error) { return []byte(output), nil },
func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 1} },
},
RunScript: []exec.FakeRunAction{
func() ([]byte, []byte, error) { return []byte(output), nil, nil },
func() ([]byte, []byte, error) { return nil, nil, &exec.FakeExitError{Status: 1} },
},
}
fexec := exec.FakeExec{
@@ -860,25 +863,31 @@ COMMIT
}
runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
defer runner.Destroy()
buffer := bytes.NewBuffer(nil)
// Success.
o, err := runner.Save(TableNAT)
err := runner.SaveInto(TableNAT, buffer)
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 string(buffer.Bytes()[:len(output)]) != output {
t.Errorf("expected output to be equal to mocked one, got %v", buffer.Bytes())
}
if fcmd.CombinedOutputCalls != 3 {
t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
if fcmd.CombinedOutputCalls != 2 {
t.Errorf("expected 2 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
}
if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll("iptables-save", "-t", "nat") {
t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
if fcmd.RunCalls != 1 {
t.Errorf("expected 1 Run() call, got %d", fcmd.RunCalls)
}
if !sets.NewString(fcmd.RunLog[0]...).HasAll("iptables-save", "-t", "nat") {
t.Errorf("wrong Run() log, got %s", fcmd.RunLog[0])
}
// Failure.
_, err = runner.Save(TableNAT)
buffer.Reset()
err = runner.SaveInto(TableNAT, buffer)
if err == nil {
t.Errorf("expected failure")
}