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 <lizhijian@cn.fujitsu.com>
Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
pull/8828/head
Li Zhijian 4 years ago
parent c215782436
commit 59c77716ad

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

Loading…
Cancel
Save