summaryrefslogtreecommitdiff
path: root/registry-scanner/pkg/log/log_test.go
blob: 2d28b753cac8101ca477f0b7472d4a38d1fcec58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package log

import (
	"fmt"
	"testing"

	"github.com/argoproj-labs/argocd-image-updater/registry-scanner/test/fixture"

	"github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func Test_LogToStdout(t *testing.T) {
	// We need tracing level
	Log().SetLevel(logrus.TraceLevel)

	t.Run("Test for Tracef() to log to stdout", func(t *testing.T) {
		out, err := fixture.CaptureStdout(func() {
			Tracef("this is a test")
		})
		require.NoError(t, err)
		assert.Contains(t, out, "this is a test")
		assert.Contains(t, out, "level=trace")
	})
	t.Run("Test for Debugf() to log to stdout", func(t *testing.T) {
		out, err := fixture.CaptureStdout(func() {
			Debugf("this is a test")
		})
		require.NoError(t, err)
		assert.Contains(t, out, "this is a test")
		assert.Contains(t, out, "level=debug")
	})
	t.Run("Test for Infof() to log to stdout", func(t *testing.T) {
		out, err := fixture.CaptureStdout(func() {
			Infof("this is a test")
		})
		require.NoError(t, err)
		assert.Contains(t, out, "this is a test")
		assert.Contains(t, out, "level=info")
	})
	t.Run("Test for Warnf() to log to stdout", func(t *testing.T) {
		out, err := fixture.CaptureStdout(func() {
			Warnf("this is a test")
		})
		require.NoError(t, err)
		assert.Contains(t, out, "this is a test")
		assert.Contains(t, out, "level=warn")
	})
	t.Run("Test for Errorf() to not log to stdout", func(t *testing.T) {
		out, err := fixture.CaptureStdout(func() {
			Errorf("this is a test")
		})
		require.NoError(t, err)
		assert.Empty(t, out)
	})
}

func Test_LogToStderr(t *testing.T) {
	// We need tracing level
	Log().SetLevel(logrus.TraceLevel)

	t.Run("Test for Tracef() to log to stdout", func(t *testing.T) {
		out, err := fixture.CaptureStderr(func() {
			Tracef("this is a test")
		})
		require.NoError(t, err)
		assert.Empty(t, out)
	})
	t.Run("Test for Debugf() to log to stdout", func(t *testing.T) {
		out, err := fixture.CaptureStderr(func() {
			Debugf("this is a test")
		})
		require.NoError(t, err)
		assert.Empty(t, out)
	})
	t.Run("Test for Infof() to log to stdout", func(t *testing.T) {
		out, err := fixture.CaptureStderr(func() {
			Infof("this is a test")
		})
		require.NoError(t, err)
		assert.Empty(t, out)
	})
	t.Run("Test for Warnf() to log to stdout", func(t *testing.T) {
		out, err := fixture.CaptureStderr(func() {
			Warnf("this is a test")
		})
		require.NoError(t, err)
		assert.Empty(t, out)
	})
	t.Run("Test for Errorf() to not log to stdout", func(t *testing.T) {
		out, err := fixture.CaptureStderr(func() {
			Errorf("this is a test")
		})
		require.NoError(t, err)
		assert.Contains(t, out, "this is a test")
		assert.Contains(t, out, "level=error")
	})
}

func Test_LoggerFields(t *testing.T) {
	Log().SetLevel(logrus.TraceLevel)
	t.Run("Test for Tracef() to log correctly with fields", func(t *testing.T) {
		out, err := fixture.CaptureStdout(func() {
			WithContext().AddField("foo", "bar").Tracef("this is a test")
		})
		require.NoError(t, err)
		assert.Contains(t, out, "foo=bar")
		assert.Contains(t, out, "msg=\"this is a test\"")
	})
	t.Run("Test for Debugf() to log correctly with fields", func(t *testing.T) {
		out, err := fixture.CaptureStdout(func() {
			WithContext().AddField("foo", "bar").Debugf("this is a test")
		})
		require.NoError(t, err)
		assert.Contains(t, out, "foo=bar")
		assert.Contains(t, out, "msg=\"this is a test\"")
	})
	t.Run("Test for Infof() to log correctly with fields", func(t *testing.T) {
		out, err := fixture.CaptureStdout(func() {
			WithContext().AddField("foo", "bar").Infof("this is a test")
		})
		require.NoError(t, err)
		assert.Contains(t, out, "foo=bar")
		assert.Contains(t, out, "msg=\"this is a test\"")
	})
	t.Run("Test for Warnf() to log correctly with fields", func(t *testing.T) {
		out, err := fixture.CaptureStdout(func() {
			WithContext().AddField("foo", "bar").Warnf("this is a test")
		})
		require.NoError(t, err)
		assert.Contains(t, out, "foo=bar")
		assert.Contains(t, out, "msg=\"this is a test\"")
	})
	t.Run("Test for Errorf() to log correctly with fields", func(t *testing.T) {
		out, err := fixture.CaptureStderr(func() {
			WithContext().AddField("foo", "bar").Errorf("this is a test")
		})
		require.NoError(t, err)
		assert.Contains(t, out, "foo=bar")
		assert.Contains(t, out, "msg=\"this is a test\"")
	})
}

func Test_LogLevel(t *testing.T) {
	for _, level := range []string{"trace", "debug", "info", "warn", "error"} {
		t.Run(fmt.Sprintf("Test set loglevel %s", level), func(t *testing.T) {
			err := SetLogLevel(level)
			assert.NoError(t, err)
		})
	}
	t.Run("Test set invalid loglevel", func(t *testing.T) {
		err := SetLogLevel("invalid")
		assert.Error(t, err)
	})
}