Last active
October 7, 2020 15:56
-
-
Save sgnn7/849744ff19c0e8660c731179c07b9681 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package log | |
import ( | |
"bytes" | |
"fmt" | |
"reflect" | |
"regexp" | |
"strings" | |
"testing" | |
"github.com/stretchr/testify/assert" | |
log_api "github.com/cyberark/secretless-broker/pkg/secretless/log" | |
) | |
// Datetime string should look something like: `2019/01/01 13:14:15` | |
var datetimeRegexString = `\d{4}\/\d{2}\/\d{2} \d{1,2}:\d{2}:\d{2}` | |
// Formatted string should include a string, int, and a float working | |
var formattedArgsResultsRegexString = `aaa stringval bbb 123 ccc 1\.2 ddd\s{1,8}eee` | |
// Unformatted string should include a string, int, and a float working | |
var unformattedArgsResultsRegexString = `stringval 123 1\.234 aaa` | |
type ExpectedEntry struct { | |
prefix string | |
expectedContent *regexp.Regexp | |
} | |
func NewExpectedEntry(prefix string, contentRegexString string) *ExpectedEntry { | |
return &ExpectedEntry{ | |
prefix: prefix, | |
expectedContent: regexp.MustCompile(contentRegexString), | |
} | |
} | |
type WrappedLogMethodF func(format string, args ...interface{}) (string, string) | |
var formattedPrefixMatchers = []*ExpectedEntry{ | |
NewExpectedEntry("", | |
"^"+datetimeRegexString+" "+formattedArgsResultsRegexString+"\n"), | |
NewExpectedEntry("prefix", | |
"^"+datetimeRegexString+" prefix: "+formattedArgsResultsRegexString+"\n"), | |
} | |
var unformattedPrefixMatchers = []*ExpectedEntry{ | |
NewExpectedEntry("", | |
"^"+datetimeRegexString+" "+unformattedArgsResultsRegexString+"\n"), | |
NewExpectedEntry("prefix", | |
"^"+datetimeRegexString+" prefix: "+unformattedArgsResultsRegexString+"\n"), | |
} | |
func AllLogMethodsF(logger log_api.Logger, backingBuffer *bytes.Buffer) []WrappedLogMethodF { | |
return []WrappedLogMethodF{ | |
func(format string, args ...interface{}) (string, string) { | |
logger.Debugf(format, args...) | |
return (*backingBuffer).String(), "Debug" | |
}, | |
func(format string, args ...interface{}) (string, string) { | |
logger.Infof(format, args...) | |
return (*backingBuffer).String(), "Info" | |
}, | |
func(format string, args ...interface{}) (string, string) { | |
logger.Warnf(format, args...) | |
return (*backingBuffer).String(), "Warn" | |
}, | |
func(format string, args ...interface{}) (string, string) { | |
logger.Errorf(format, args...) | |
return (*backingBuffer).String(), "Error" | |
}, | |
func(format string, args ...interface{}) (string, string) { | |
logger.Panicf(format, args...) | |
return (*backingBuffer).String(), "Panic" | |
}, | |
} | |
} | |
func TestFormattedLogging(t *testing.T) { | |
for _, prefixMatcher := range formattedPrefixMatchers { | |
for _, isDebug := range []bool{true, false} { | |
outputBuffer := &bytes.Buffer{} | |
logger := NewWithOptions(outputBuffer, prefixMatcher.prefix, isDebug) | |
for _, method := range AllLogMethodsF(logger, outputBuffer) { | |
t.Run(fmt.Sprintf("%s/prefix='%s'/isDebug=%t", "FIXME testCase.name", | |
prefixMatcher.prefix, isDebug), func(t *testing.T) { | |
actualOutput, methodName := method("aaa %s bbb %d ccc %2.1f ddd \t eee", "stringval", 123, 1.234) | |
if !isDebug && | |
(strings.HasPrefix(methodName, "Debug") || | |
strings.HasPrefix(methodName, "Info")) { | |
assert.Empty(t, actualOutput) | |
} else { | |
assert.Regexp(t, prefixMatcher.expectedContent, actualOutput) | |
} | |
}) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment