mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-08 11:38:15 +00:00
Capture stderr output and write it to buffer on error
This commit is contained in:
parent
00e7505618
commit
93a549679f
@ -20,7 +20,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -327,14 +326,15 @@ func (runner *runner) SaveInto(table Table, buffer *bytes.Buffer) error {
|
|||||||
args := []string{"-t", string(table)}
|
args := []string{"-t", string(table)}
|
||||||
klog.V(4).Infof("running %s %v", iptablesSaveCmd, args)
|
klog.V(4).Infof("running %s %v", iptablesSaveCmd, args)
|
||||||
cmd := runner.exec.Command(iptablesSaveCmd, args...)
|
cmd := runner.exec.Command(iptablesSaveCmd, args...)
|
||||||
// Since CombinedOutput() doesn't support redirecting it to a buffer,
|
|
||||||
// we need to workaround it by redirecting stdout and stderr to buffer
|
|
||||||
// and explicitly calling Run() [CombinedOutput() underneath itself
|
|
||||||
// creates a new buffer, redirects stdout and stderr to it and also
|
|
||||||
// calls Run()].
|
|
||||||
cmd.SetStdout(buffer)
|
cmd.SetStdout(buffer)
|
||||||
cmd.SetStderr(ioutil.Discard)
|
stderrBuffer := bytes.NewBuffer(nil)
|
||||||
return cmd.Run()
|
cmd.SetStderr(stderrBuffer)
|
||||||
|
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
stderrBuffer.WriteTo(buffer) // ignore error, since we need to return the original error
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore is part of Interface.
|
// Restore is part of Interface.
|
||||||
|
@ -943,7 +943,7 @@ COMMIT
|
|||||||
},
|
},
|
||||||
RunScript: []fakeexec.FakeRunAction{
|
RunScript: []fakeexec.FakeRunAction{
|
||||||
func() ([]byte, []byte, error) { return []byte(output), []byte(stderrOutput), 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, []byte(stderrOutput), &fakeexec.FakeExitError{Status: 1} },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
fexec := fakeexec.FakeExec{
|
fexec := fakeexec.FakeExec{
|
||||||
@ -984,6 +984,9 @@ COMMIT
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("%s: Expected failure", protoStr)
|
t.Errorf("%s: Expected failure", protoStr)
|
||||||
}
|
}
|
||||||
|
if string(buffer.Bytes()) != stderrOutput {
|
||||||
|
t.Errorf("%s: Expected output '%s', got '%v'", protoStr, stderrOutput, string(buffer.Bytes()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSaveIntoIPv4(t *testing.T) {
|
func TestSaveIntoIPv4(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user