-
Notifications
You must be signed in to change notification settings - Fork 54
/
main_test.go
62 lines (52 loc) · 1.63 KB
/
main_test.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package main
import (
"io/ioutil"
"os"
"path"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault-csi-provider/internal/config"
"github.com/stretchr/testify/require"
)
func TestListen(t *testing.T) {
logger := hclog.NewNullLogger()
dir, err := ioutil.TempDir("/tmp", "TestListen")
require.NoError(t, err)
endpoint := path.Join(dir, "vault.sock")
defer func() {
require.NoError(t, os.Remove(endpoint))
}()
// Works when no file in the way.
l, err := listen(logger, endpoint)
require.NoError(t, err)
// Will replace existing file.
require.NoError(t, l.Close())
_, err = os.Create(endpoint)
require.NoError(t, err)
}
func TestSetupLogger(t *testing.T) {
tests := []struct {
flags config.FlagsConfig
expected hclog.Level
}{
{config.FlagsConfig{Debug: true}, hclog.Debug}, // deprecated flag test
{config.FlagsConfig{LogLevel: "trace"}, hclog.Trace},
{config.FlagsConfig{LogLevel: "debug"}, hclog.Debug},
{config.FlagsConfig{LogLevel: "info"}, hclog.Info},
{config.FlagsConfig{LogLevel: "warn"}, hclog.Warn},
{config.FlagsConfig{LogLevel: "error"}, hclog.Error},
{config.FlagsConfig{LogLevel: "off"}, hclog.Off},
{config.FlagsConfig{LogLevel: "no-level"}, hclog.Info},
{config.FlagsConfig{Debug: true, LogLevel: "warn"}, hclog.Warn}, // if both set, LogLevel should take precedence
}
for _, tt := range tests {
t.Run(string(tt.expected), func(t *testing.T) {
logger := setupLogger(tt.flags)
if logger.GetLevel() != tt.expected {
t.Errorf("expected log level %v, got %v", tt.expected, logger.GetLevel())
}
})
}
}