fluentd_test.go 3.9 KB

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