-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathresult_test.go
234 lines (201 loc) · 7.21 KB
/
result_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2020 The Operator-SDK Authors
//
// 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 validate
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
log "github.com/sirupsen/logrus"
)
var _ = Describe("Output Result", func() {
var result *Result
BeforeEach(func() {
result = NewResult()
})
Describe("Result Model Manipulation", func() {
It("should add the error with ErrorLevel and passed should be flagged with false", func() {
result.AddError(errors.New("example of an error"))
Expect(result).NotTo(BeNil())
Expect(result.Passed).To(BeFalse())
Expect(result.Outputs[0].Type).To(Equal(log.ErrorLevel.String()))
Expect(result.Outputs[0].Message).To(Equal("example of an error"))
})
It("should add the error with WarnLevel and passed should be flagged with true", func() {
result.AddWarn(errors.New("example of a warn"))
Expect(result).NotTo(BeNil())
Expect(result.Passed).To(BeTrue())
Expect(result.Outputs[0].Type).To(Equal(log.WarnLevel.String()))
Expect(result.Outputs[0].Message).To(Equal("example of a warn"))
})
It("should add msg with InfoLevel and passed should be flagged with true", func() {
result.AddInfo("Example of an info")
Expect(result).NotTo(BeNil())
Expect(result.Passed).To(BeTrue())
Expect(result.Outputs[0].Type).To(Equal(log.InfoLevel.String()))
Expect(result.Outputs[0].Message).To(Equal("Example of an info"))
})
It("should passed be flagged with false when has many outputs and an error", func() {
result.AddError(errors.New("example of an error"))
result.AddWarn(errors.New("example of a warn"))
result.AddInfo("Example of an info")
Expect(result).NotTo(BeNil())
Expect(result.Passed).To(BeFalse())
Expect(result.Outputs).To(HaveLen(3))
})
})
Describe("PrintText", func() {
It("should work successfully with valid log levels", func() {
logger := log.NewEntry(newLoggerTo(os.Stderr))
result.AddError(errors.New("example of an error"))
result.AddWarn(errors.New("example of a warn"))
result.AddInfo("Example of an info")
err := result.printText(logger)
Expect(err).NotTo(HaveOccurred())
})
It("should fail when an invalid log level is found", func() {
// This scenario can just occurs if the setters are not used
logger := log.NewEntry(newLoggerTo(os.Stderr))
result.Outputs = append(result.Outputs, Output{
Type: log.TraceLevel.String(),
Message: "invalid",
})
err := result.printText(logger)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("unknown log output level \"trace\""))
})
It("should fail when is not possible parse the log level", func() {
// This scenario can just occurs if the setters are not used
logger := log.NewEntry(newLoggerTo(os.Stderr))
result.Outputs = append(result.Outputs, Output{
Type: "invalid",
Message: "invalid",
})
err := result.printText(logger)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("not a valid logrus Level: \"invalid\""))
})
})
Describe("prepare", func() {
It("should finished with passed flagged with false when has an output with the ErrorLevel", func() {
// This scenario can just occurs if the setters are not used
result.Outputs = append(result.Outputs, Output{
Type: log.ErrorLevel.String(),
Message: "error",
})
result.Outputs = append(result.Outputs, Output{
Type: log.InfoLevel.String(),
Message: "info",
})
result.Outputs = append(result.Outputs, Output{
Type: log.WarnLevel.String(),
Message: "warn",
})
err := result.prepare()
Expect(err).NotTo(HaveOccurred())
Expect(result).NotTo(BeNil())
Expect(result.Passed).To(BeFalse())
Expect(result.Outputs).To(HaveLen(3))
})
It("should fail when an invalid log level is found", func() {
// This scenario can just occurs if the setters are not used
result.Outputs = append(result.Outputs, Output{
Type: "invalid",
Message: "invalid",
})
err := result.prepare()
Expect(err).To(HaveOccurred())
})
})
Describe("PrintWithFormat", func() {
var w *bytes.Buffer
var output []byte
var resJSON Result
const warnText = "example of a warning"
const errorText = "example of an error"
BeforeEach(func() {
w = &bytes.Buffer{}
resJSON = Result{}
})
Context("json-alpha1 formatting", func() {
It("prints a warning", func() {
result.AddWarn(errors.New(warnText))
failed, err := result.printWithFormat(w, JSONAlpha1Output)
Expect(failed).To(BeFalse())
Expect(err).To(Succeed())
output = w.Bytes()
Expect(json.Unmarshal(output, &resJSON)).To(Succeed(), string(output))
Expect(resJSON.Passed).To(BeTrue())
Expect(resJSON.Outputs).To(HaveLen(1))
Expect(resJSON.Outputs[0].Type).To(Equal("warning"))
Expect(resJSON.Outputs[0].Message).To(Equal(warnText))
})
It("prints an error", func() {
result.AddError(errors.New(errorText))
failed, err := result.printWithFormat(w, JSONAlpha1Output)
fmt.Println(failed)
fmt.Println(err)
Expect(failed).To(BeTrue())
Expect(err).To(Succeed())
output = w.Bytes()
Expect(json.Unmarshal(output, &resJSON)).To(Succeed(), string(output))
Expect(resJSON.Passed).To(BeFalse())
Expect(resJSON.Outputs).To(HaveLen(1))
Expect(resJSON.Outputs[0].Type).To(Equal("error"))
Expect(resJSON.Outputs[0].Message).To(Equal(errorText))
})
})
Context("text formatting", func() {
It("prints a warning", func() {
result.AddWarn(errors.New(warnText))
failed, err := result.printWithFormat(w, TextOutput)
Expect(failed).To(BeFalse())
Expect(err).To(Succeed())
output = w.Bytes()
lines := bytes.Split(bytes.TrimSpace(output), []byte("\n"))
Expect(lines).To(HaveLen(1))
line := string(lines[0])
Expect(line).To(ContainSubstring("level=warning"), line)
Expect(line).To(ContainSubstring(`msg="example of a warning"`), line)
})
It("prints an error", func() {
result.AddError(errors.New(errorText))
failed, err := result.printWithFormat(w, TextOutput)
Expect(failed).To(BeTrue())
Expect(err).To(Succeed())
output = w.Bytes()
lines := bytes.Split(bytes.TrimSpace(output), []byte("\n"))
Expect(lines).To(HaveLen(1))
line := string(lines[0])
fmt.Println(line)
Expect(line).To(ContainSubstring("level=error"), line)
Expect(line).To(ContainSubstring(`msg="example of an error"`), line)
})
})
Context("returns an error", func() {
It("gets an invalid log level", func() {
result.Outputs = append(result.Outputs, Output{
Type: "invalid",
Message: "invalid",
})
failed, err := result.printWithFormat(w, TextOutput)
Expect(failed).To(BeFalse())
Expect(err).NotTo(Succeed())
})
})
})
})