-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlogging_test.go
208 lines (173 loc) · 6.48 KB
/
logging_test.go
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright © 2021, 2022, 2023 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main_test
// Unit test definitions for functions and methods defined in source file
// logging.go
//
// Documentation in literate-programming-style is available at:
// https://redhatinsights.github.io/ccx-notification-writer/packages/logging_test.html
import (
"errors"
"os"
"testing"
"time"
"github.com/Shopify/sarama"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/tisnik/go-capture"
main "github.com/RedHatInsights/ccx-notification-writer"
types "github.com/RedHatInsights/insights-results-types"
)
// Constants used by various tests
const (
testOrganizationID = 6502
testClusterName = "thisIsClusterName"
testTopicName = "thisIsTopicName"
testError = "this is test error only"
testEventMessage = "this is event message"
)
// init function is called before tests
func init() {
// set default logging level regardles of config made in code
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
// checkCapture function perform checks if standard output has been captured by
// unit test
func checkCapture(t *testing.T, err error) {
if err != nil {
t.Fatal("Unable to capture standard output", err)
}
}
// Construct consumer used only for tests.
func constructTestConsumer() *main.KafkaConsumer {
// mocked broker configuration
var brokerConfiguration = main.BrokerConfiguration{
Addresses: "address",
Topic: testTopicName,
Group: "group",
Enabled: true,
}
// construct mocked consumer
return &main.KafkaConsumer{
Configuration: brokerConfiguration,
ConsumerGroup: nil,
}
}
// Construct parsed message used only for tests.
func constructParsedMessage() main.IncomingMessage {
// mocked message
var orgID types.OrgID = types.OrgID(testOrganizationID)
var clusterName types.ClusterName = types.ClusterName(testClusterName)
// construct mocked parsed message
return main.IncomingMessage{
Organization: &orgID,
ClusterName: &clusterName,
Version: 99,
}
}
// TestLogDuration check the logDuration function from the main module
func TestLogDuration(t *testing.T) {
startTime := time.Date(2000, time.November, 10, 23, 0, 0, 0, time.UTC)
endTime := time.Date(2000, time.November, 10, 23, 0, 1, 0, time.UTC)
// try to call the tested function and capture its output
output, err := capture.ErrorOutput(func() {
log.Logger = log.Output(zerolog.New(os.Stderr))
main.LogDuration(startTime, endTime, 9999, "test message")
})
// check the captured text
checkCapture(t, err)
// expected text(s) writen onto error output
assert.Contains(t, output, "test message") // key
assert.Contains(t, output, "9999") // offset
assert.Contains(t, output, "1000000") // duration
}
// TestLogMessageInfo check the logMessageInfo function from the main module
func TestLogMessageInfo(t *testing.T) {
consumer := constructTestConsumer()
originalMessage := sarama.ConsumerMessage{}
parsedMessage := constructParsedMessage()
// try to call the tested function and capture its output
output, err := capture.ErrorOutput(func() {
log.Logger = log.Output(zerolog.New(os.Stderr))
main.LogMessageInfo(consumer, &originalMessage, parsedMessage, testEventMessage)
})
// check the captured text
checkCapture(t, err)
// expected text(s) writen onto error output
assert.Contains(t, output, "organization")
assert.Contains(t, output, testClusterName)
assert.Contains(t, output, testTopicName)
assert.Contains(t, output, testEventMessage)
assert.Contains(t, output, "6502")
assert.Contains(t, output, "99") // version
}
// TestLogMessageError check the logMessageError function from the main module
func TestLogMessageError(t *testing.T) {
consumer := constructTestConsumer()
originalMessage := sarama.ConsumerMessage{}
parsedMessage := constructParsedMessage()
// try to call the tested function and capture its output
output, err := capture.ErrorOutput(func() {
log.Logger = log.Output(zerolog.New(os.Stderr))
main.LogMessageError(consumer, &originalMessage, parsedMessage, testEventMessage, errors.New(testError))
})
// check the captured text
checkCapture(t, err)
// expected text(s) writen onto error output
assert.Contains(t, output, "organization")
assert.Contains(t, output, testClusterName)
assert.Contains(t, output, testTopicName)
assert.Contains(t, output, testError)
assert.Contains(t, output, testEventMessage)
assert.Contains(t, output, "6502")
assert.Contains(t, output, "99") // version
}
// TestLogUnparsedMessageError check the logUnparsedMessageError function from
// the main module
func TestLogUnparsedMessageError(t *testing.T) {
consumer := constructTestConsumer()
originalMessage := sarama.ConsumerMessage{}
// try to call the tested function and capture its output
output, err := capture.ErrorOutput(func() {
log.Logger = log.Output(zerolog.New(os.Stderr))
main.LogUnparsedMessageError(consumer, &originalMessage, testEventMessage, errors.New(testError))
})
// check the captured text
checkCapture(t, err)
// expected text(s) writen onto error output
assert.Contains(t, output, testTopicName)
assert.Contains(t, output, testError)
assert.Contains(t, output, testEventMessage)
}
// TestLogMessageWarning check the logMessageWarning function from the main
// module
func TestLogMessageWarning(t *testing.T) {
consumer := constructTestConsumer()
originalMessage := sarama.ConsumerMessage{}
parsedMessage := constructParsedMessage()
// try to call the tested function and capture its output
output, err := capture.ErrorOutput(func() {
log.Logger = log.Output(zerolog.New(os.Stderr))
main.LogMessageWarning(consumer, &originalMessage, parsedMessage, testEventMessage)
})
// check the captured text
checkCapture(t, err)
// expected text(s) writen onto error output
assert.Contains(t, output, "organization")
assert.Contains(t, output, testClusterName)
assert.Contains(t, output, testTopicName)
assert.Contains(t, output, testEventMessage)
assert.Contains(t, output, "6502")
assert.Contains(t, output, "99") // version
}