From 1e1854f8d087922f245fc9a03ff160c243702f47 Mon Sep 17 00:00:00 2001 From: Li Zhijian Date: Wed, 30 Sep 2020 11:54:56 +0800 Subject: [PATCH] TestCheckPerms: utilize pipe to read stderr Refer to the stderr manpage: $ man 3 stderr *Note that mixing use of FILEs and raw file descriptors can produce unexpected results and should generally be avoided.* And actually, we noticed that the warning() will output the message to stdout instead of stderr sometimes. lizj@FNSTPC:~/workspace/k8s/helm$ while true; do timeout 1m go test -count=1 -run TestCheckPerms ./cmd/helm -v 2>/dev/null; done === RUN TestCheckPerms --- PASS: TestCheckPerms (0.00s) PASS ok helm.sh/helm/v3/cmd/helm 0.028s === RUN TestCheckPerms --- PASS: TestCheckPerms (0.00s) PASS ok helm.sh/helm/v3/cmd/helm 0.027s === RUN TestCheckPerms --- PASS: TestCheckPerms (0.00s) PASS ok helm.sh/helm/v3/cmd/helm 0.028s === RUN TestCheckPerms --- PASS: TestCheckPerms (0.00s) PASS ok helm.sh/helm/v3/cmd/helm 0.029s === RUN TestCheckPerms --- PASS: TestCheckPerms (0.00s) PASS ok helm.sh/helm/v3/cmd/helm 0.029s === RUN TestCheckPerms --- PASS: TestCheckPerms (0.00s) PASS ok helm.sh/helm/v3/cmd/helm 0.028s === RUN TestCheckPerms --- PASS: TestCheckPerms (0.00s) PASS ok helm.sh/helm/v3/cmd/helm 0.030s === RUN TestCheckPerms WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /tmp/helmtest093620773/testconfig === RUN TestCheckPerms WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /tmp/helmtest083469215/testconfig === RUN TestCheckPerms WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /tmp/helmtest101343249/testconfig === RUN TestCheckPerms --- PASS: TestCheckPerms (0.00s) PASS ok helm.sh/helm/v3/cmd/helm 0.032s === RUN TestCheckPerms --- PASS: TestCheckPerms (0.00s) PASS ok helm.sh/helm/v3/cmd/helm 0.040s === RUN TestCheckPerms --- PASS: TestCheckPerms (0.00s) PASS ok helm.sh/helm/v3/cmd/helm 0.031s === RUN TestCheckPerms WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /tmp/helmtest706352639/testconfig Signed-off-by: Li Zhijian Signed-off-by: Lu Fengqi --- cmd/helm/root_unix_test.go | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/cmd/helm/root_unix_test.go b/cmd/helm/root_unix_test.go index 89c3c1eea..c62776c2a 100644 --- a/cmd/helm/root_unix_test.go +++ b/cmd/helm/root_unix_test.go @@ -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) }