github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/logger/fluentd/fluentd_test.go (about) 1 package fluentd // import "github.com/docker/docker/daemon/logger/fluentd" 2 import ( 3 "testing" 4 5 "github.com/google/go-cmp/cmp" 6 "gotest.tools/v3/assert" 7 ) 8 9 func TestValidateLogOptReconnectInterval(t *testing.T) { 10 invalidIntervals := []string{"-1", "1", "-1s", "99ms", "11s"} 11 for _, v := range invalidIntervals { 12 t.Run("invalid "+v, func(t *testing.T) { 13 err := ValidateLogOpt(map[string]string{asyncReconnectIntervalKey: v}) 14 assert.ErrorContains(t, err, "invalid value for fluentd-async-reconnect-interval:") 15 }) 16 } 17 18 validIntervals := []string{"100ms", "10s"} 19 for _, v := range validIntervals { 20 t.Run("valid "+v, func(t *testing.T) { 21 err := ValidateLogOpt(map[string]string{asyncReconnectIntervalKey: v}) 22 assert.NilError(t, err) 23 }) 24 } 25 } 26 27 func TestValidateLogOptAddress(t *testing.T) { 28 // ports to try, and their results 29 validPorts := map[string]int{ 30 "": defaultPort, 31 ":": defaultPort, 32 ":123": 123, 33 ":65535": 65535, 34 } 35 // paths to try, which should result in an error 36 paths := []string{"/", "/some-path"} 37 38 tests := []struct { 39 addr string 40 ports map[string]int // combinations of addr + port -> expected port 41 paths []string // paths to append to addr, should be an error for tcp/udp 42 expected location 43 expectedErr string 44 }{ 45 { 46 addr: "", 47 expected: location{ 48 protocol: defaultProtocol, 49 host: defaultHost, 50 port: defaultPort, 51 }, 52 }, 53 { 54 addr: "192.168.1.1", 55 ports: validPorts, 56 paths: paths, 57 expected: location{ 58 protocol: defaultProtocol, 59 host: "192.168.1.1", 60 }, 61 }, 62 { 63 addr: "[::1]", 64 ports: validPorts, 65 paths: paths, 66 expected: location{ 67 protocol: defaultProtocol, 68 host: "::1", 69 }, 70 }, 71 { 72 addr: "example.com", 73 ports: validPorts, 74 paths: paths, 75 expected: location{ 76 protocol: defaultProtocol, 77 host: "example.com", 78 }, 79 }, 80 { 81 addr: "tcp://", 82 paths: paths, 83 expected: location{ 84 protocol: "tcp", 85 host: defaultHost, 86 port: defaultPort, 87 }, 88 }, 89 { 90 addr: "tcp://example.com", 91 ports: validPorts, 92 paths: paths, 93 expected: location{ 94 protocol: "tcp", 95 host: "example.com", 96 }, 97 }, 98 { 99 addr: "tls://", 100 paths: paths, 101 expected: location{ 102 protocol: "tls", 103 host: defaultHost, 104 port: defaultPort, 105 }, 106 }, 107 { 108 addr: "tls://example.com", 109 ports: validPorts, 110 paths: paths, 111 expected: location{ 112 protocol: "tls", 113 host: "example.com", 114 }, 115 }, 116 { 117 addr: "://", 118 expectedErr: "missing protocol scheme", 119 }, 120 { 121 addr: "something://", 122 expectedErr: "unsupported scheme: 'something'", 123 }, 124 { 125 addr: "udp://", 126 expectedErr: "unsupported scheme: 'udp'", 127 }, 128 { 129 addr: "unixgram://", 130 expectedErr: "unsupported scheme: 'unixgram'", 131 }, 132 { 133 addr: "tcp+tls://", 134 expectedErr: "unsupported scheme: 'tcp+tls'", 135 }, 136 { 137 addr: "corrupted:c", 138 expectedErr: "invalid port", 139 }, 140 { 141 addr: "tcp://example.com:port", 142 expectedErr: "invalid port", 143 }, 144 { 145 addr: "tcp://example.com:-1", 146 expectedErr: "invalid port", 147 }, 148 { 149 addr: "tcp://example.com:65536", 150 expectedErr: "invalid port", 151 }, 152 { 153 addr: "unix://", 154 expectedErr: "path is empty", 155 }, 156 { 157 addr: "unix:///some/socket.sock", 158 expected: location{ 159 protocol: "unix", 160 path: "/some/socket.sock", 161 }, 162 }, 163 { 164 addr: "unix:///some/socket.sock:80", // unusual, but technically valid 165 expected: location{ 166 protocol: "unix", 167 path: "/some/socket.sock:80", 168 }, 169 }, 170 } 171 for _, tc := range tests { 172 tc := tc 173 if len(tc.ports) == 0 { 174 tc.ports = map[string]int{"": tc.expected.port} 175 } 176 177 // always try empty paths; add paths to try if the test specifies it 178 tc.paths = append([]string{""}, tc.paths...) 179 for port, expectedPort := range tc.ports { 180 for _, path := range tc.paths { 181 address := tc.addr + port + path 182 expected := tc.expected 183 expected.port = expectedPort 184 t.Run(address, func(t *testing.T) { 185 err := ValidateLogOpt(map[string]string{addressKey: address}) 186 if path != "" { 187 assert.ErrorContains(t, err, "should not contain a path element") 188 return 189 } 190 if tc.expectedErr != "" { 191 assert.ErrorContains(t, err, tc.expectedErr) 192 return 193 } 194 195 assert.NilError(t, err) 196 addr, _ := parseAddress(address) 197 assert.DeepEqual(t, expected, *addr, cmp.AllowUnexported(location{})) 198 }) 199 } 200 } 201 } 202 }