forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testVerifyUtils.go
99 lines (84 loc) · 3.5 KB
/
testVerifyUtils.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package modlog
import (
"bytes"
"fmt"
"regexp"
"testing"
"github.com/hyperledger/fabric-sdk-go/pkg/core/logging/api"
"github.com/hyperledger/fabric-sdk-go/pkg/core/logging/metadata"
"github.com/stretchr/testify/assert"
)
const (
basicLevelOutputWithCallerInfoExpectedRegex = "\\[%s\\] .* UTC - modlog.* -> %4.4s brown fox jumps over the lazy dog"
basicLevelOutputExpectedRegex = "\\[%s\\] .* UTC .*-> %4.4s brown fox jumps over the lazy dog"
printLevelOutputExpectedRegex = "\\[%s\\] .* brown fox jumps over the lazy dog"
customLevelOutputExpectedRegex = "\\[%s\\] .* CUSTOM LOG OUTPUT"
moduleName = "module-xyz"
moduleName2 = "module-xyz-deftest"
)
type fn func(...interface{})
type fnf func(string, ...interface{})
//VerifyCriticalLoggings utility func which does job calling and verifying CRITICAL log level functions - PANIC
func VerifyCriticalLoggings(t *testing.T, level api.Level, loggerFunc fn, loggerFuncf fnf, buf *bytes.Buffer) {
//Handling panic as well as checking log output
defer func() {
if r := recover(); r == nil {
t.Errorf("%v was supposed to panic", loggerFunc)
}
var regex string
opts := getLoggerOpts(moduleName, level)
if opts.callerInfoEnabled {
//with caller info
regex = fmt.Sprintf(basicLevelOutputWithCallerInfoExpectedRegex, moduleName, metadata.ParseString(level))
} else {
//without caller info
regex = fmt.Sprintf(basicLevelOutputExpectedRegex, moduleName, metadata.ParseString(level))
}
match, err := regexp.MatchString(regex, buf.String())
assert.Empty(t, err, "error while matching regex with logoutput wasnt expected")
assert.True(t, match, "CRITICAL logger isn't producing output as expected, \n logoutput:%s\n regex: %s", buf.String(), regex)
}()
//Call logger func
if loggerFunc != nil {
loggerFunc("brown fox jumps over the lazy dog")
} else if loggerFuncf != nil {
loggerFuncf("brown %s jumps over the lazy %s", "fox", "dog")
}
}
//VerifyBasicLogging utility func which does job calling and verifying basic log level functions - DEBUG, INFO, ERROR, WARNING
func VerifyBasicLogging(t *testing.T, level api.Level, loggerFunc fn, loggerFuncf fnf, buf *bytes.Buffer, verifyCustom bool, moduleName string) {
//Call logger func
if loggerFunc != nil {
loggerFunc("brown fox jumps over the lazy dog")
} else if loggerFuncf != nil {
loggerFuncf("brown %s jumps over the lazy %s", "fox", "dog")
}
//check output
regex := ""
levelName := "print"
if verifyCustom {
levelName = metadata.ParseString(level)
regex = fmt.Sprintf(customLevelOutputExpectedRegex, moduleName)
} else if level > 0 && !verifyCustom {
levelName = metadata.ParseString(level)
opts := getLoggerOpts(moduleName, level)
if opts.callerInfoEnabled {
//with caller info
regex = fmt.Sprintf(basicLevelOutputWithCallerInfoExpectedRegex, moduleName, levelName)
} else {
//without caller info
regex = fmt.Sprintf(basicLevelOutputExpectedRegex, moduleName, levelName)
}
} else {
regex = fmt.Sprintf(printLevelOutputExpectedRegex, moduleName)
}
match, err := regexp.MatchString(regex, buf.String())
assert.Empty(t, err, "error while matching regex with logoutput wasnt expected")
assert.True(t, match, "%s logger isn't producing output as expected, \n logoutput:%s\n regex: %s", levelName, buf.String(), regex)
//Reset output buffer, for next use
buf.Reset()
}