open-vault/sdk/helper/logging/logging_test.go
Hamid Ghaf 27bb03bbc0
adding copyright header (#19555)
* adding copyright header

* fix fmt and a test
2023-03-15 09:00:52 -07:00

68 lines
1.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package logging
import (
"errors"
"os"
"reflect"
"testing"
)
func Test_ParseLogFormat(t *testing.T) {
type testData struct {
format string
expected LogFormat
expectedErr error
}
tests := []testData{
{format: "", expected: UnspecifiedFormat, expectedErr: nil},
{format: " ", expected: UnspecifiedFormat, expectedErr: nil},
{format: "standard", expected: StandardFormat, expectedErr: nil},
{format: "STANDARD", expected: StandardFormat, expectedErr: nil},
{format: "json", expected: JSONFormat, expectedErr: nil},
{format: " json ", expected: JSONFormat, expectedErr: nil},
{format: "bogus", expected: UnspecifiedFormat, expectedErr: errors.New("unknown log format: bogus")},
}
for _, test := range tests {
result, err := ParseLogFormat(test.format)
if test.expected != result {
t.Errorf("expected %s, got %s", test.expected, result)
}
if !reflect.DeepEqual(test.expectedErr, err) {
t.Errorf("expected error %v, got %v", test.expectedErr, err)
}
}
}
func Test_ParseEnv_VAULT_LOG_FORMAT(t *testing.T) {
oldVLF := os.Getenv("VAULT_LOG_FORMAT")
defer os.Setenv("VAULT_LOG_FORMAT", oldVLF)
testParseEnvLogFormat(t, "VAULT_LOG_FORMAT")
}
func testParseEnvLogFormat(t *testing.T, name string) {
env := []string{
"json", "vauLT_Json", "VAULT-JSON", "vaulTJSon",
"standard", "STANDARD",
"bogus",
}
formats := []LogFormat{
JSONFormat, JSONFormat, JSONFormat, JSONFormat,
StandardFormat, StandardFormat,
UnspecifiedFormat,
}
for i, e := range env {
os.Setenv(name, e)
if lf := ParseEnvLogFormat(); formats[i] != lf {
t.Errorf("expected %s, got %s", formats[i], lf)
}
}
}