github.com/hashicorp/vault/sdk@v0.11.0/helper/logging/logging_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package logging
     5  
     6  import (
     7  	"errors"
     8  	"os"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func Test_ParseLogFormat(t *testing.T) {
    14  	type testData struct {
    15  		format      string
    16  		expected    LogFormat
    17  		expectedErr error
    18  	}
    19  
    20  	tests := []testData{
    21  		{format: "", expected: UnspecifiedFormat, expectedErr: nil},
    22  		{format: " ", expected: UnspecifiedFormat, expectedErr: nil},
    23  		{format: "standard", expected: StandardFormat, expectedErr: nil},
    24  		{format: "STANDARD", expected: StandardFormat, expectedErr: nil},
    25  		{format: "json", expected: JSONFormat, expectedErr: nil},
    26  		{format: " json ", expected: JSONFormat, expectedErr: nil},
    27  		{format: "bogus", expected: UnspecifiedFormat, expectedErr: errors.New("unknown log format: bogus")},
    28  	}
    29  
    30  	for _, test := range tests {
    31  		result, err := ParseLogFormat(test.format)
    32  		if test.expected != result {
    33  			t.Errorf("expected %s, got %s", test.expected, result)
    34  		}
    35  		if !reflect.DeepEqual(test.expectedErr, err) {
    36  			t.Errorf("expected error %v, got %v", test.expectedErr, err)
    37  		}
    38  	}
    39  }
    40  
    41  func Test_ParseEnv_VAULT_LOG_FORMAT(t *testing.T) {
    42  	oldVLF := os.Getenv("VAULT_LOG_FORMAT")
    43  	defer os.Setenv("VAULT_LOG_FORMAT", oldVLF)
    44  
    45  	testParseEnvLogFormat(t, "VAULT_LOG_FORMAT")
    46  }
    47  
    48  func testParseEnvLogFormat(t *testing.T, name string) {
    49  	env := []string{
    50  		"json", "vauLT_Json", "VAULT-JSON", "vaulTJSon",
    51  		"standard", "STANDARD",
    52  		"bogus",
    53  	}
    54  
    55  	formats := []LogFormat{
    56  		JSONFormat, JSONFormat, JSONFormat, JSONFormat,
    57  		StandardFormat, StandardFormat,
    58  		UnspecifiedFormat,
    59  	}
    60  
    61  	for i, e := range env {
    62  		os.Setenv(name, e)
    63  		if lf := ParseEnvLogFormat(); formats[i] != lf {
    64  			t.Errorf("expected %s, got %s", formats[i], lf)
    65  		}
    66  	}
    67  }