Merge pull request #8828 from zhijianli88/pipe-stderr

TestCheckPerms: utilize pipe to read stderr
pull/8860/head
Matt Farina 4 years ago committed by GitHub
commit fc9b46067f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,7 +19,8 @@ limitations under the License.
package main
import (
"bufio"
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
@ -27,15 +28,27 @@ import (
"testing"
)
func TestCheckPerms(t *testing.T) {
// NOTE(bacongobbler): have to open a new file handler here as the default os.Sterr cannot be read from
stderr, err := os.Open("/dev/stderr")
func checkPermsStderr() (string, error) {
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("could not open /dev/stderr for reading: %s", err)
return "", err
}
defer stderr.Close()
reader := bufio.NewReader(stderr)
stderr := os.Stderr
os.Stderr = w
defer func() {
os.Stderr = stderr
}()
checkPerms()
w.Close()
var text bytes.Buffer
io.Copy(&text, r)
return text.String(), nil
}
func TestCheckPerms(t *testing.T) {
tdir, err := ioutil.TempDir("", "helmtest")
if err != nil {
t.Fatal(err)
@ -51,8 +64,7 @@ func TestCheckPerms(t *testing.T) {
settings.KubeConfig = tfile
defer func() { settings.KubeConfig = tconfig }()
checkPerms()
text, err := reader.ReadString('\n')
text, err := checkPermsStderr()
if err != nil {
t.Fatalf("could not read from stderr: %s", err)
}
@ -64,8 +76,7 @@ func TestCheckPerms(t *testing.T) {
if err := fh.Chmod(0404); err != nil {
t.Errorf("Could not change mode on file: %s", err)
}
checkPerms()
text, err = reader.ReadString('\n')
text, err = checkPermsStderr()
if err != nil {
t.Fatalf("could not read from stderr: %s", err)
}

Loading…
Cancel
Save