This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
85 lines (73 loc) · 2.17 KB
/
config_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package log_test
import (
"encoding/json"
"testing"
"github.com/containerssh/structutils"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"github.com/containerssh/log"
)
func TestJSONDecode(t *testing.T) {
cfg := "{\"level\":\"debug\",\"format\":\"text\"}"
config := log.Config{}
err := json.Unmarshal([]byte(cfg), &config)
assert.NoError(t, err)
assert.Equal(t, log.LevelDebug, config.Level)
assert.Equal(t, log.FormatText, config.Format)
}
func TestJSONDecodeNumeric(t *testing.T) {
cfg := "{\"level\":7,\"format\":\"text\"}"
config := log.Config{}
err := json.Unmarshal([]byte(cfg), &config)
assert.NoError(t, err)
assert.Equal(t, log.LevelDebug, config.Level)
assert.Equal(t, log.FormatText, config.Format)
}
func TestYAMLDecode(t *testing.T) {
cfg := "---\nlevel: debug\nformat: text\n"
config := log.Config{}
err := yaml.Unmarshal([]byte(cfg), &config)
assert.NoError(t, err)
assert.Equal(t, log.LevelDebug, config.Level)
assert.Equal(t, log.FormatText, config.Format)
}
func TestYAMLDecodeNumeric(t *testing.T) {
cfg := "---\nlevel: 7\nformat: text\n"
config := log.Config{}
err := yaml.Unmarshal([]byte(cfg), &config)
assert.NoError(t, err)
assert.Equal(t, log.LevelDebug, config.Level)
assert.Equal(t, log.FormatText, config.Format)
}
func TestJSONEncode(t *testing.T) {
config := log.Config{
Level: log.LevelDebug,
Format: log.FormatText,
}
jsonData, err := json.Marshal(config)
assert.NoError(t, err)
rawData := map[string]interface{}{}
err = json.Unmarshal(jsonData, &rawData)
assert.NoError(t, err)
assert.Equal(t, "debug", rawData["level"])
assert.Equal(t, "text", rawData["format"])
}
func TestYAMLEncode(t *testing.T) {
config := log.Config{
Level: log.LevelDebug,
Format: log.FormatText,
}
jsonData, err := yaml.Marshal(config)
assert.NoError(t, err)
rawData := map[string]interface{}{}
err = yaml.Unmarshal(jsonData, &rawData)
assert.NoError(t, err)
assert.Equal(t, "debug", rawData["level"])
assert.Equal(t, "text", rawData["format"])
}
func TestDefault(t *testing.T) {
config := log.Config{}
structutils.Defaults(&config)
assert.Equal(t, log.LevelNotice, config.Level)
assert.Equal(t, log.FormatLJSON, config.Format)
}