Browse code

daemon/logger/fluentd: add coverage for ValidateLogOpt(), parseAddress()

This exposed a bug where host is ignored on some valid cases (to be fixed).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2022/04/10 18:24:03
Showing 2 changed files
... ...
@@ -2,6 +2,7 @@ package fluentd // import "github.com/docker/docker/daemon/logger/fluentd"
2 2
 import (
3 3
 	"testing"
4 4
 
5
+	"github.com/google/go-cmp/cmp"
5 6
 	"gotest.tools/v3/assert"
6 7
 )
7 8
 
... ...
@@ -22,3 +23,128 @@ func TestValidateLogOptReconnectInterval(t *testing.T) {
22 22
 		})
23 23
 	}
24 24
 }
25
+
26
+func TestValidateLogOptAddress(t *testing.T) {
27
+
28
+	// paths to try
29
+	paths := []string{"/", "/some-path"}
30
+
31
+	tests := []struct {
32
+		addr        string
33
+		paths       []string // paths to append to addr, should be an error for tcp/udp
34
+		expected    location
35
+		expectedErr string
36
+	}{
37
+		{
38
+			addr: "",
39
+			expected: location{
40
+				protocol: defaultProtocol,
41
+				host:     defaultHost,
42
+				port:     defaultPort,
43
+			},
44
+		},
45
+		{
46
+			addr:  "192.168.1.1",
47
+			paths: paths,
48
+			expected: location{
49
+				port:     defaultPort,
50
+				protocol: defaultProtocol,
51
+			},
52
+		},
53
+		{
54
+			addr:  "[::1]",
55
+			paths: paths,
56
+			expected: location{
57
+				port:     defaultPort,
58
+				protocol: defaultProtocol,
59
+			},
60
+		},
61
+		{
62
+			addr:  "example.com",
63
+			paths: paths,
64
+			expected: location{
65
+				port:     defaultPort,
66
+				protocol: defaultProtocol,
67
+			},
68
+		},
69
+		{
70
+			addr:  "tcp://",
71
+			paths: paths,
72
+			expected: location{
73
+				protocol: "tcp",
74
+				port:     defaultPort,
75
+			},
76
+		},
77
+		{
78
+			addr:  "tcp://example.com",
79
+			paths: paths,
80
+			expected: location{
81
+				protocol: "tcp",
82
+				port:     defaultPort,
83
+			},
84
+		},
85
+		{
86
+			addr:  "tcp://example.com:65535",
87
+			paths: paths,
88
+			expected: location{
89
+				protocol: "tcp",
90
+				host:     "example.com",
91
+				port:     65535,
92
+			},
93
+		},
94
+		{
95
+			addr:        "://",
96
+			expectedErr: "invalid syntax",
97
+		},
98
+		{
99
+			addr:        "something://",
100
+			expectedErr: "invalid syntax",
101
+		},
102
+		{
103
+			addr:        "corrupted:c",
104
+			expectedErr: "invalid syntax",
105
+		},
106
+		{
107
+			addr:        "tcp://example.com:port",
108
+			expectedErr: "invalid port",
109
+		},
110
+		{
111
+			addr:        "tcp://example.com:-1",
112
+			expectedErr: "invalid port",
113
+		},
114
+		{
115
+			addr: "unix:///some/socket.sock",
116
+			expected: location{
117
+				protocol: "unix",
118
+				path:     "/some/socket.sock",
119
+			},
120
+		},
121
+		{
122
+			addr: "unix:///some/socket.sock:80", // unusual, but technically valid
123
+			expected: location{
124
+				protocol: "unix",
125
+				path:     "/some/socket.sock:80",
126
+			},
127
+		},
128
+	}
129
+	for _, tc := range tests {
130
+		tc := tc
131
+		if len(tc.paths) == 0 {
132
+			tc.paths = []string{""}
133
+		}
134
+		for _, path := range tc.paths {
135
+			address := tc.addr + path
136
+			t.Run(address, func(t *testing.T) {
137
+				err := ValidateLogOpt(map[string]string{addressKey: address})
138
+				if tc.expectedErr != "" {
139
+					assert.ErrorContains(t, err, tc.expectedErr)
140
+					return
141
+				}
142
+
143
+				assert.NilError(t, err)
144
+				addr, _ := parseAddress(address)
145
+				assert.DeepEqual(t, tc.expected, *addr, cmp.AllowUnexported(location{}))
146
+			})
147
+		}
148
+	}
149
+}
... ...
@@ -1671,14 +1671,6 @@ func (s *DockerDaemonSuite) TestDaemonRestartLocalVolumes(c *testing.T) {
1671 1671
 	assert.NilError(c, err, out)
1672 1672
 }
1673 1673
 
1674
-// FIXME(vdemeester) should be a unit test
1675
-func (s *DockerDaemonSuite) TestDaemonCorruptedFluentdAddress(c *testing.T) {
1676
-	d := daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
1677
-	assert.Assert(c, d.StartWithError("--log-driver=fluentd", "--log-opt", "fluentd-address=corrupted:c") != nil)
1678
-	expected := "invalid fluentd-address corrupted:c: "
1679
-	icmd.RunCommand("grep", expected, d.LogFileName()).Assert(c, icmd.Success)
1680
-}
1681
-
1682 1674
 // FIXME(vdemeester) Use a new daemon instance instead of the Suite one
1683 1675
 func (s *DockerDaemonSuite) TestDaemonStartWithoutHost(c *testing.T) {
1684 1676
 	s.d.UseDefaultHost = true