-
Notifications
You must be signed in to change notification settings - Fork 388
/
TestUtils.go
288 lines (268 loc) · 9.62 KB
/
TestUtils.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 testutils
import (
"bytes"
"context"
"fmt"
"github.com/rs/zerolog/pkgerrors"
"os"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/apache/plc4x/plc4go/spi/options"
"github.com/apache/plc4x/plc4go/spi/pool"
"github.com/apache/plc4x/plc4go/spi/transactions"
"github.com/apache/plc4x/plc4go/spi/transports/test"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/ajankovic/xdiff"
"github.com/ajankovic/xdiff/parser"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
)
func CompareResults(t *testing.T, actualString []byte, referenceString []byte) error {
localLog := ProduceTestingLogger(t)
// Now parse the xml strings of the actual and the reference in xdiff's dom
p := parser.New()
actual, err := p.ParseBytes(actualString)
if err != nil {
return errors.Wrap(err, "Error parsing actual input")
}
reference, err := p.ParseBytes(referenceString)
if err != nil {
return errors.Wrap(err, "Error parsing reference input")
}
// Use XDiff to actually do the comparison
diff, err := xdiff.Compare(actual, reference)
if err != nil {
return errors.Wrap(err, "Error comparing xml trees")
}
if diff == nil {
// All good
return nil
}
cleanDiff := make([]xdiff.Delta, 0)
for _, delta := range diff {
if delta.Operation == xdiff.Delete && delta.Subject.Value == nil || delta.Operation == xdiff.Insert && delta.Subject.Value == nil {
localLog.Info().Interface("delta", delta).Msg("We ignore empty elements which should be deleted")
continue
}
// Workaround for different precisions on float
if delta.Operation == xdiff.Update &&
string(delta.Subject.Parent.FirstChild.Name) == "dataType" &&
string(delta.Subject.Parent.FirstChild.Value) == "float" &&
string(delta.Object.Parent.FirstChild.Name) == "dataType" &&
string(delta.Object.Parent.FirstChild.Value) == "float" {
if strings.Contains(string(delta.Subject.Value), string(delta.Object.Value)) || strings.Contains(string(delta.Object.Value), string(delta.Subject.Value)) {
localLog.Info().Interface("delta", delta).Msg("We ignore precision diffs")
continue
}
}
if delta.Operation == xdiff.Update &&
string(delta.Subject.Parent.FirstChild.Name) == "dataType" &&
string(delta.Subject.Parent.FirstChild.Value) == "string" &&
string(delta.Object.Parent.FirstChild.Name) == "dataType" &&
string(delta.Object.Parent.FirstChild.Value) == "string" {
if diff, err := xdiff.Compare(delta.Subject, delta.Object); diff == nil && err == nil {
localLog.Info().Interface("delta", delta).Msg("We ignore newline diffs")
continue
}
}
cleanDiff = append(cleanDiff, delta)
}
enc := xdiff.NewTextEncoder(os.Stdout)
if err := enc.Encode(diff); err != nil {
return errors.Wrap(err, "Error outputting results")
}
if len(cleanDiff) <= 0 {
localLog.Warn().Msg("We only found non relevant changes")
return nil
}
assert.Equal(t, string(referenceString), string(actualString))
asciiBoxWriter := utils.NewAsciiBoxWriter()
expectedBox := asciiBoxWriter.BoxString("expected", string(referenceString), 0)
gotBox := asciiBoxWriter.BoxString("got", string(actualString), 0)
boxSideBySide := asciiBoxWriter.BoxSideBySide(expectedBox, gotBox)
_ = boxSideBySide // TODO: xml too distorted, we need a don't center option
return errors.New("there were differences: Expected: \n" + string(referenceString) + "\nBut Got: \n" + string(actualString))
}
// TestContext produces a context which is getting cleaned up by testing.T
func TestContext(t *testing.T) context.Context {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
ctx = ProduceTestingLogger(t).WithContext(ctx)
return ctx
}
var (
highLogPrecision bool
passLoggerToModel bool
receiveTimeout time.Duration
traceTransactionManagerWorkers bool
traceTransactionManagerTransactions bool
traceDefaultMessageCodecWorker bool
traceExecutorWorkers bool
traceTestTransportInstance bool
)
func init() {
getOrLeaveBool("PLC4X_TEST_HIGH_TEST_LOG_PRECISION", &highLogPrecision)
if highLogPrecision {
zerolog.TimeFieldFormat = time.RFC3339Nano
}
getOrLeaveBool("PLC4X_TEST_PASS_LOGGER_TO_MODEL", &passLoggerToModel)
receiveTimeout = 3 * time.Second
getOrLeaveDuration("PLC4X_TEST_RECEIVE_TIMEOUT_MS", &receiveTimeout)
getOrLeaveBool("PLC4X_TEST_TRACE_TRANSACTION_MANAGER_WORKERS", &traceTransactionManagerWorkers)
getOrLeaveBool("PLC4X_TEST_TRACE_TRANSACTION_MANAGER_TRANSACTIONS", &traceTransactionManagerTransactions)
getOrLeaveBool("PLC4X_TEST_TRACE_DEFAULT_MESSAGE_CODEC_WORKER", &traceDefaultMessageCodecWorker)
getOrLeaveBool("PLC4X_TEST_TRACE_EXECUTOR_WORKERS", &traceExecutorWorkers)
getOrLeaveBool("PLC4X_TEST_TEST_TRANSPORT_INSTANCE", &traceTestTransportInstance)
}
func getOrLeaveBool(key string, setting *bool) {
if env, ok := os.LookupEnv(key); ok {
*setting = strings.EqualFold(env, "true")
}
}
func getOrLeaveDuration(key string, setting *time.Duration) {
if env, ok := os.LookupEnv(key); ok && env != "" {
parsedDuration, err := strconv.ParseInt(env, 10, 64)
if err != nil {
panic(err)
}
*setting = time.Duration(parsedDuration) * time.Millisecond
}
}
func shouldNoColor() bool {
noColor := false
{
// TODO: this is really an issue with go-junit-report not sanitizing output before dumping into xml...
onJenkins := os.Getenv("JENKINS_URL") != ""
onGithubAction := os.Getenv("GITHUB_ACTIONS") != ""
onCI := os.Getenv("CI") != ""
if onJenkins || onGithubAction || onCI {
noColor = true
}
}
return noColor
}
type TestingLog interface {
Log(args ...interface{})
Logf(format string, args ...interface{})
Helper()
}
// ProduceTestingLogger produces a logger which redirects to testing.T
func ProduceTestingLogger(t TestingLog) zerolog.Logger {
noColor := shouldNoColor()
consoleWriter := zerolog.NewConsoleWriter(
zerolog.ConsoleTestWriter(t),
func(w *zerolog.ConsoleWriter) {
w.NoColor = noColor
},
func(w *zerolog.ConsoleWriter) {
if highLogPrecision {
w.TimeFormat = time.StampNano
}
},
func(w *zerolog.ConsoleWriter) {
w.FormatFieldValue = func(i interface{}) string {
if aString, ok := i.(string); ok && strings.Contains(aString, "\\n") {
if noColor {
return "see below"
} else {
return fmt.Sprintf("\x1b[%dm%v\x1b[0m", 31, "see below")
}
}
return fmt.Sprintf("%s", i)
}
w.FormatExtra = func(m map[string]interface{}, buffer *bytes.Buffer) error {
for key, i := range m {
if aString, ok := i.(string); ok && strings.Contains(aString, "\n") {
buffer.WriteString("\n")
if noColor {
buffer.WriteString("field " + key)
} else {
buffer.WriteString(fmt.Sprintf("\x1b[%dm%v\x1b[0m", 32, "field "+key))
}
buffer.WriteString(":\n" + aString)
}
}
return nil
}
},
)
logger := zerolog.New(
consoleWriter,
)
if highLogPrecision {
logger = logger.With().Timestamp().Logger()
}
stackSetter.Do(func() {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
})
logger = logger.With().Stack().Logger()
return logger
}
var stackSetter sync.Once
// EnrichOptionsWithOptionsForTesting appends options useful for testing to config.WithOption s
func EnrichOptionsWithOptionsForTesting(t *testing.T, _options ...options.WithOption) []options.WithOption {
if extractedTraceWorkers, found := options.ExtractTracerWorkers(_options...); found {
traceExecutorWorkers = extractedTraceWorkers
}
_options = append(_options,
options.WithCustomLogger(ProduceTestingLogger(t)),
options.WithPassLoggerToModel(passLoggerToModel),
options.WithReceiveTimeout(receiveTimeout),
options.WithTraceTransactionManagerWorkers(traceTransactionManagerWorkers),
options.WithTraceTransactionManagerTransactions(traceTransactionManagerTransactions),
options.WithTraceDefaultMessageCodecWorker(traceDefaultMessageCodecWorker),
options.WithExecutorOptionTracerWorkers(traceExecutorWorkers),
test.WithTraceTransportInstance(traceTestTransportInstance),
)
// We always create a custom executor to ensure shared executor for transaction manager is not used for tests
testSharedExecutorInstance := pool.NewFixedSizeExecutor(
runtime.NumCPU(),
100,
_options...,
)
testSharedExecutorInstance.Start()
t.Cleanup(testSharedExecutorInstance.Stop)
_options = append(_options,
transactions.WithCustomExecutor(testSharedExecutorInstance),
)
return _options
}
type _explodingGlobalLogger struct {
hardExplode bool
}
func (e _explodingGlobalLogger) Write(_ []byte) (_ int, err error) {
if e.hardExplode {
debug.PrintStack()
panic("found a global log usage")
}
return 0, errors.New("found a global log usage")
}
// ExplodingGlobalLogger Useful to find unredirected logs
func ExplodingGlobalLogger(hardExplode bool) {
log.Logger = zerolog.New(_explodingGlobalLogger{hardExplode: hardExplode})
}