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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
package syslog // import "github.com/docker/docker/daemon/logger/syslog"
import (
"log"
"net"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
syslog "github.com/RackSec/srslog"
)
func functionMatches(expectedFun interface{}, actualFun interface{}) bool {
return reflect.ValueOf(expectedFun).Pointer() == reflect.ValueOf(actualFun).Pointer()
}
func TestParseLogFormat(t *testing.T) {
formatter, framer, err := parseLogFormat("rfc5424", "udp")
if err != nil || !functionMatches(rfc5424formatterWithAppNameAsTag, formatter) ||
!functionMatches(syslog.DefaultFramer, framer) {
t.Fatal("Failed to parse rfc5424 format", err, formatter, framer)
}
formatter, framer, err = parseLogFormat("rfc5424", "tcp+tls")
if err != nil || !functionMatches(rfc5424formatterWithAppNameAsTag, formatter) ||
!functionMatches(syslog.RFC5425MessageLengthFramer, framer) {
t.Fatal("Failed to parse rfc5424 format", err, formatter, framer)
}
formatter, framer, err = parseLogFormat("rfc5424micro", "udp")
if err != nil || !functionMatches(rfc5424microformatterWithAppNameAsTag, formatter) ||
!functionMatches(syslog.DefaultFramer, framer) {
t.Fatal("Failed to parse rfc5424 (microsecond) format", err, formatter, framer)
}
formatter, framer, err = parseLogFormat("rfc5424micro", "tcp+tls")
if err != nil || !functionMatches(rfc5424microformatterWithAppNameAsTag, formatter) ||
!functionMatches(syslog.RFC5425MessageLengthFramer, framer) {
t.Fatal("Failed to parse rfc5424 (microsecond) format", err, formatter, framer)
}
formatter, framer, err = parseLogFormat("rfc3164", "")
if err != nil || !functionMatches(syslog.RFC3164Formatter, formatter) ||
!functionMatches(syslog.DefaultFramer, framer) {
t.Fatal("Failed to parse rfc3164 format", err, formatter, framer)
}
formatter, framer, err = parseLogFormat("", "")
if err != nil || !functionMatches(syslog.UnixFormatter, formatter) ||
!functionMatches(syslog.DefaultFramer, framer) {
t.Fatal("Failed to parse empty format", err, formatter, framer)
}
formatter, framer, err = parseLogFormat("invalid", "")
if err == nil {
t.Fatal("Failed to parse invalid format", err, formatter, framer)
}
}
func TestValidateLogOptEmpty(t *testing.T) {
emptyConfig := make(map[string]string)
if err := ValidateLogOpt(emptyConfig); err != nil {
t.Fatal("Failed to parse empty config", err)
}
}
func TestValidateSyslogAddress(t *testing.T) {
const sockPlaceholder = "/TEMPDIR/socket.sock"
s, err := os.Create(filepath.Join(t.TempDir(), "socket.sock"))
if err != nil {
log.Fatal(err)
}
socketPath := s.Name()
_ = s.Close()
tests := []struct {
address string
expectedErr string
skipOn string
}{
{
address: "this is not an uri",
expectedErr: "unsupported scheme: ''",
},
{
address: "corrupted:42",
expectedErr: "unsupported scheme: 'corrupted'",
},
{
address: "unix://" + sockPlaceholder,
skipOn: "windows", // doesn't work with unix:// sockets
},
{
address: "unix:///does_not_exist",
expectedErr: "no such file or directory",
skipOn: "windows", // error message differs
},
{
address: "tcp://1.2.3.4",
},
{
address: "udp://1.2.3.4",
},
{
address: "http://1.2.3.4",
expectedErr: "unsupported scheme: 'http'",
},
}
for _, tc := range tests {
tc := tc
if tc.skipOn == runtime.GOOS {
continue
}
t.Run(tc.address, func(t *testing.T) {
address := strings.Replace(tc.address, sockPlaceholder, socketPath, 1)
err := ValidateLogOpt(map[string]string{"syslog-address": address})
if tc.expectedErr != "" {
if err == nil {
t.Fatal("expected an error, got nil")
}
if !strings.Contains(err.Error(), tc.expectedErr) {
t.Fatalf("expected error to contain '%s', got: '%s'", tc.expectedErr, err)
}
} else if err != nil {
t.Fatalf("unexpected error: '%s'", err)
}
})
}
}
func TestParseAddressDefaultPort(t *testing.T) {
_, address, err := parseAddress("tcp://1.2.3.4")
if err != nil {
t.Fatal(err)
}
_, port, _ := net.SplitHostPort(address)
if port != defaultPort {
t.Fatalf("Expected to default to port %s. It used port %s", defaultPort, port)
}
}
func TestValidateSyslogFacility(t *testing.T) {
err := ValidateLogOpt(map[string]string{
"syslog-facility": "Invalid facility",
})
if err == nil {
t.Fatal("Expected error if facility level is invalid")
}
}
func TestValidateLogOptSyslogFormat(t *testing.T) {
err := ValidateLogOpt(map[string]string{
"syslog-format": "Invalid format",
})
if err == nil {
t.Fatal("Expected error if format is invalid")
}
}
func TestValidateLogOpt(t *testing.T) {
err := ValidateLogOpt(map[string]string{
"env": "http://127.0.0.1",
"env-regex": "abc",
"labels": "labelA",
"labels-regex": "def",
"syslog-address": "udp://1.2.3.4:1111",
"syslog-facility": "daemon",
"syslog-tls-ca-cert": "/etc/ca-certificates/custom/ca.pem",
"syslog-tls-cert": "/etc/ca-certificates/custom/cert.pem",
"syslog-tls-key": "/etc/ca-certificates/custom/key.pem",
"syslog-tls-skip-verify": "true",
"tag": "true",
"syslog-format": "rfc3164",
})
if err != nil {
t.Fatal(err)
}
err = ValidateLogOpt(map[string]string{
"not-supported-option": "a",
})
if err == nil {
t.Fatal("Expecting error on unsupported options")
}
}
|