@@ -2,6 +2,7 @@ package fluentd // import "github.com/docker/docker/daemon/logger/fluentd"
22import (
33 "testing"
44
5+ "github.com/google/go-cmp/cmp"
56 "gotest.tools/v3/assert"
67)
78
@@ -22,3 +23,128 @@ func TestValidateLogOptReconnectInterval(t *testing.T) {
2223 })
2324 }
2425}
26+
27+ func TestValidateLogOptAddress (t * testing.T ) {
28+
29+ // paths to try
30+ paths := []string {"/" , "/some-path" }
31+
32+ tests := []struct {
33+ addr string
34+ paths []string // paths to append to addr, should be an error for tcp/udp
35+ expected location
36+ expectedErr string
37+ }{
38+ {
39+ addr : "" ,
40+ expected : location {
41+ protocol : defaultProtocol ,
42+ host : defaultHost ,
43+ port : defaultPort ,
44+ },
45+ },
46+ {
47+ addr : "192.168.1.1" ,
48+ paths : paths ,
49+ expected : location {
50+ port : defaultPort ,
51+ protocol : defaultProtocol ,
52+ },
53+ },
54+ {
55+ addr : "[::1]" ,
56+ paths : paths ,
57+ expected : location {
58+ port : defaultPort ,
59+ protocol : defaultProtocol ,
60+ },
61+ },
62+ {
63+ addr : "example.com" ,
64+ paths : paths ,
65+ expected : location {
66+ port : defaultPort ,
67+ protocol : defaultProtocol ,
68+ },
69+ },
70+ {
71+ addr : "tcp://" ,
72+ paths : paths ,
73+ expected : location {
74+ protocol : "tcp" ,
75+ port : defaultPort ,
76+ },
77+ },
78+ {
79+ addr : "tcp://example.com" ,
80+ paths : paths ,
81+ expected : location {
82+ protocol : "tcp" ,
83+ port : defaultPort ,
84+ },
85+ },
86+ {
87+ addr : "tcp://example.com:65535" ,
88+ paths : paths ,
89+ expected : location {
90+ protocol : "tcp" ,
91+ host : "example.com" ,
92+ port : 65535 ,
93+ },
94+ },
95+ {
96+ addr : "://" ,
97+ expectedErr : "invalid syntax" ,
98+ },
99+ {
100+ addr : "something://" ,
101+ expectedErr : "invalid syntax" ,
102+ },
103+ {
104+ addr : "corrupted:c" ,
105+ expectedErr : "invalid syntax" ,
106+ },
107+ {
108+ addr : "tcp://example.com:port" ,
109+ expectedErr : "invalid port" ,
110+ },
111+ {
112+ addr : "tcp://example.com:-1" ,
113+ expectedErr : "invalid port" ,
114+ },
115+ {
116+ addr : "unix:///some/socket.sock" ,
117+ expected : location {
118+ protocol : "unix" ,
119+ path : "/some/socket.sock" ,
120+ },
121+ },
122+ {
123+ addr : "unix:///some/socket.sock:80" , // unusual, but technically valid
124+ expected : location {
125+ protocol : "unix" ,
126+ path : "/some/socket.sock:80" ,
127+ },
128+ },
129+ }
130+ for _ , tc := range tests {
131+ tc := tc
132+ if len (tc .paths ) == 0 {
133+ tc .paths = []string {"" }
134+ }
135+ for _ , path := range tc .paths {
136+ address := tc .addr + path
137+ t .Run (address , func (t * testing.T ) {
138+ err := ValidateLogOpt (map [string ]string {addressKey : address })
139+ if tc .expectedErr != "" {
140+ assert .ErrorContains (t , err , tc .expectedErr )
141+ return
142+ }
143+
144+ assert .NilError (t , err )
145+ addr , _ := parseAddress (address )
146+ assert .DeepEqual (t , tc .expected , * addr , cmp .AllowUnexported (location {}))
147+ })
148+ }
149+ }
150+ }
0 commit comments