Discard stderr output when calling iptables-save

This commit is contained in:
Marko Lukša 2019-05-28 14:43:28 +02:00
parent df23697ae7
commit 00e7505618
2 changed files with 7 additions and 4 deletions

View File

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil"
"regexp" "regexp"
"strings" "strings"
"sync" "sync"
@ -332,7 +333,7 @@ func (runner *runner) SaveInto(table Table, buffer *bytes.Buffer) error {
// creates a new buffer, redirects stdout and stderr to it and also // creates a new buffer, redirects stdout and stderr to it and also
// calls Run()]. // calls Run()].
cmd.SetStdout(buffer) cmd.SetStdout(buffer)
cmd.SetStderr(buffer) cmd.SetStderr(ioutil.Discard)
return cmd.Run() return cmd.Run()
} }

View File

@ -932,6 +932,8 @@ func testSaveInto(t *testing.T, protocol Protocol) {
COMMIT COMMIT
# Completed on Thu Jan 19 11:38:09 2017`, iptablesSaveCmd+version) # Completed on Thu Jan 19 11:38:09 2017`, iptablesSaveCmd+version)
stderrOutput := "#STDERR OUTPUT" // SaveInto() should should NOT capture stderr into the buffer
fcmd := fakeexec.FakeCmd{ fcmd := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{ CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
// iptables version check // iptables version check
@ -940,7 +942,7 @@ COMMIT
func() ([]byte, error) { return []byte(iptablesRestoreCmd + version), nil }, func() ([]byte, error) { return []byte(iptablesRestoreCmd + version), nil },
}, },
RunScript: []fakeexec.FakeRunAction{ RunScript: []fakeexec.FakeRunAction{
func() ([]byte, []byte, error) { return []byte(output), nil, nil }, func() ([]byte, []byte, error) { return []byte(output), []byte(stderrOutput), nil },
func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} }, func() ([]byte, []byte, error) { return nil, nil, &fakeexec.FakeExitError{Status: 1} },
}, },
} }
@ -962,8 +964,8 @@ COMMIT
t.Fatalf("%s: Expected success, got %v", protoStr, err) t.Fatalf("%s: Expected success, got %v", protoStr, err)
} }
if string(buffer.Bytes()[:len(output)]) != output { if string(buffer.Bytes()) != output {
t.Errorf("%s: Expected output '%s', got '%v'", protoStr, output, buffer.Bytes()) t.Errorf("%s: Expected output '%s', got '%v'", protoStr, output, string(buffer.Bytes()))
} }
if fcmd.CombinedOutputCalls != 2 { if fcmd.CombinedOutputCalls != 2 {