Skip to content

Commit f63eab3

Browse files
committed
Add testing log hook to context
Allows log output to sent along with test output for cleaner output and clearer ordering. Signed-off-by: Derek McGowan <[email protected]>
1 parent f2b6c31 commit f63eab3

3 files changed

Lines changed: 92 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ GO_GCFLAGS=$(shell \
111111
BINARIES=$(addprefix bin/,$(COMMANDS))
112112

113113
# Flags passed to `go test`
114-
TESTFLAGS ?= -v $(TESTFLAGS_RACE)
114+
TESTFLAGS ?= $(TESTFLAGS_RACE)
115115
TESTFLAGS_PARALLEL ?= 8
116116

117117
.PHONY: clean all AUTHORS build binaries test integration generate protos checkprotos coverage ci check help install uninstall vendor release mandir install-man

log/logtest/context.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package logtest
18+
19+
import (
20+
"context"
21+
"io/ioutil"
22+
"testing"
23+
24+
"github.com/containerd/containerd/log"
25+
"github.com/sirupsen/logrus"
26+
)
27+
28+
// WithT adds a logging hook for the given test
29+
// Changes debug level to debug, clears output, and
30+
// outputs all log messages as test logs.
31+
func WithT(ctx context.Context, t testing.TB) context.Context {
32+
// Create a new logger to avoid adding hooks from multiple tests
33+
l := logrus.New()
34+
35+
// Increase debug level for tests
36+
l.SetLevel(logrus.DebugLevel)
37+
l.SetOutput(ioutil.Discard)
38+
39+
// Add testing hook
40+
l.AddHook(&testHook{
41+
t: t,
42+
fmt: &logrus.TextFormatter{
43+
DisableColors: true,
44+
TimestampFormat: log.RFC3339NanoFixed,
45+
},
46+
})
47+
48+
return log.WithLogger(ctx, logrus.NewEntry(l))
49+
}

log/logtest/log_hook.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package logtest
18+
19+
import (
20+
"bytes"
21+
"testing"
22+
23+
"github.com/sirupsen/logrus"
24+
)
25+
26+
type testHook struct {
27+
t testing.TB
28+
fmt logrus.Formatter
29+
}
30+
31+
func (*testHook) Levels() []logrus.Level {
32+
return logrus.AllLevels
33+
}
34+
35+
func (h *testHook) Fire(e *logrus.Entry) error {
36+
s, err := h.fmt.Format(e)
37+
if err != nil {
38+
return err
39+
}
40+
h.t.Log(string(bytes.TrimRight(s, "\n")))
41+
return nil
42+
}

0 commit comments

Comments
 (0)