-
-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathcmd_test.go
More file actions
120 lines (105 loc) · 3.78 KB
/
Copy pathcmd_test.go
File metadata and controls
120 lines (105 loc) · 3.78 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package caddydockerproxy
import (
"testing"
"github.com/lucaslorentz/caddy-docker-proxy/v2/config"
"github.com/stretchr/testify/assert"
)
func TestNormalizeAdminListen(t *testing.T) {
testCases := []struct {
name string
input string
expected string
}{
{
name: "empty",
input: "",
expected: "",
},
{
name: "trim and add tcp prefix",
input: " 0.0.0.0:2019 ",
expected: "tcp/0.0.0.0:2019",
},
{
name: "keep prefixed listen value",
input: "tcp/0.0.0.0:2019",
expected: "tcp/0.0.0.0:2019",
},
{
name: "keep unix listen value",
input: "unix//run/caddy-admin.sock",
expected: "unix//run/caddy-admin.sock",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.expected, normalizeAdminListen(testCase.input))
})
}
}
func TestGetAdminListenPrefersConfiguredListen(t *testing.T) {
options := &config.Options{
AdminListen: "tcp/0.0.0.0:2019",
}
assert.Equal(t, "tcp/0.0.0.0:2019", getAdminListen(options))
}
func TestBuildCaddyLoggingConfig(t *testing.T) {
t.Run("empty default log when nothing customized (Caddy defaults apply)", func(t *testing.T) {
logging := buildCaddyLoggingConfig(&config.Options{Mode: config.Standalone})
assert.NotNil(t, logging)
dl := logging.Logs["default"]
assert.Empty(t, dl.Level)
assert.Nil(t, dl.EncoderRaw)
assert.Empty(t, dl.Exclude)
})
t.Run("sets default log level", func(t *testing.T) {
logging := buildCaddyLoggingConfig(&config.Options{Mode: config.Standalone, LogLevel: "error"})
assert.NotNil(t, logging)
assert.Equal(t, "ERROR", logging.Logs["default"].Level)
assert.Nil(t, logging.Logs["default"].EncoderRaw)
assert.Empty(t, logging.Logs["default"].Exclude)
})
t.Run("sets console encoder", func(t *testing.T) {
logging := buildCaddyLoggingConfig(&config.Options{Mode: config.Standalone, LogFormat: "console"})
assert.NotNil(t, logging)
assert.JSONEq(t, `{"format":"console"}`, string(logging.Logs["default"].EncoderRaw))
})
t.Run("sets json encoder and level together", func(t *testing.T) {
logging := buildCaddyLoggingConfig(&config.Options{Mode: config.Standalone, LogLevel: "WARN", LogFormat: "json"})
assert.NotNil(t, logging)
assert.Equal(t, "WARN", logging.Logs["default"].Level)
assert.JSONEq(t, `{"format":"json"}`, string(logging.Logs["default"].EncoderRaw))
})
t.Run("controller-only excludes the admin logger", func(t *testing.T) {
logging := buildCaddyLoggingConfig(&config.Options{Mode: config.Controller, LogLevel: "error"})
assert.NotNil(t, logging)
assert.Contains(t, logging.Logs["default"].Exclude, "admin")
})
}
func TestBuildCaddyRunConfig(t *testing.T) {
t.Run("server/standalone uses the admin endpoint", func(t *testing.T) {
cfg := buildCaddyRunConfig(&config.Options{Mode: config.Standalone})
assert.NotNil(t, cfg)
assert.NotNil(t, cfg.Admin)
assert.False(t, cfg.Admin.Disabled)
assert.Equal(t, "tcp/localhost:2019", cfg.Admin.Listen)
})
t.Run("controller-only disables admin and excludes admin logs", func(t *testing.T) {
cfg := buildCaddyRunConfig(&config.Options{Mode: config.Controller, LogLevel: "ERROR"})
assert.NotNil(t, cfg)
assert.NotNil(t, cfg.Admin)
assert.True(t, cfg.Admin.Disabled)
assert.Equal(t, "ERROR", cfg.Logging.Logs["default"].Level)
assert.Contains(t, cfg.Logging.Logs["default"].Exclude, "admin")
})
}
func TestBuildCaddyAdminConfig(t *testing.T) {
t.Run("server/standalone listens", func(t *testing.T) {
admin := buildCaddyAdminConfig(&config.Options{Mode: config.Standalone})
assert.False(t, admin.Disabled)
assert.Equal(t, "tcp/localhost:2019", admin.Listen)
})
t.Run("controller-only disables admin", func(t *testing.T) {
assert.True(t, buildCaddyAdminConfig(&config.Options{Mode: config.Controller}).Disabled)
})
}