1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 func bufLogger() (*log.Logger, *bufio.Reader) {
18 logger := log.New(w, "", 0)
19 return logger, bufio.NewReader(r)
22 func TestReadAllOrWarnFail(t *testing.T) {
23 logger, rcv := bufLogger()
24 rep := Reporter{Logger: logger}
26 done := make(chan bool)
30 msg, err = rcv.ReadBytes('\n')
34 // The special file /proc/self/mem can be opened for
35 // reading, but reading from byte 0 returns an error.
36 f, err := os.Open("/proc/self/mem")
38 t.Fatalf("Opening /proc/self/mem: %s", err)
40 if x, err := rep.readAllOrWarn(f); err == nil {
41 t.Fatalf("Expected error, got %v", x)
47 } else if matched, err := regexp.MatchString("^warning: read /proc/self/mem: .*", string(msg)); err != nil || !matched {
48 t.Fatalf("Expected error message about unreadable file, got \"%s\"", msg)
52 func TestReadAllOrWarnSuccess(t *testing.T) {
53 rep := Reporter{Logger: log.New(os.Stderr, "", 0)}
55 f, err := os.Open("./crunchstat_test.go")
57 t.Fatalf("Opening ./crunchstat_test.go: %s", err)
59 data, err := rep.readAllOrWarn(f)
61 t.Fatalf("got error %s", err)
63 if matched, err := regexp.MatchString("\npackage crunchstat\n", string(data)); err != nil || !matched {
64 t.Fatalf("data failed regexp: err %v, matched %v", err, matched)