syslog_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package syslog // import "github.com/docker/docker/daemon/logger/syslog"
  2. import (
  3. "net"
  4. "reflect"
  5. "testing"
  6. syslog "github.com/RackSec/srslog"
  7. )
  8. func functionMatches(expectedFun interface{}, actualFun interface{}) bool {
  9. return reflect.ValueOf(expectedFun).Pointer() == reflect.ValueOf(actualFun).Pointer()
  10. }
  11. func TestParseLogFormat(t *testing.T) {
  12. formatter, framer, err := parseLogFormat("rfc5424", "udp")
  13. if err != nil || !functionMatches(rfc5424formatterWithAppNameAsTag, formatter) ||
  14. !functionMatches(syslog.DefaultFramer, framer) {
  15. t.Fatal("Failed to parse rfc5424 format", err, formatter, framer)
  16. }
  17. formatter, framer, err = parseLogFormat("rfc5424", "tcp+tls")
  18. if err != nil || !functionMatches(rfc5424formatterWithAppNameAsTag, formatter) ||
  19. !functionMatches(syslog.RFC5425MessageLengthFramer, framer) {
  20. t.Fatal("Failed to parse rfc5424 format", err, formatter, framer)
  21. }
  22. formatter, framer, err = parseLogFormat("rfc5424micro", "udp")
  23. if err != nil || !functionMatches(rfc5424microformatterWithAppNameAsTag, formatter) ||
  24. !functionMatches(syslog.DefaultFramer, framer) {
  25. t.Fatal("Failed to parse rfc5424 (microsecond) format", err, formatter, framer)
  26. }
  27. formatter, framer, err = parseLogFormat("rfc5424micro", "tcp+tls")
  28. if err != nil || !functionMatches(rfc5424microformatterWithAppNameAsTag, formatter) ||
  29. !functionMatches(syslog.RFC5425MessageLengthFramer, framer) {
  30. t.Fatal("Failed to parse rfc5424 (microsecond) format", err, formatter, framer)
  31. }
  32. formatter, framer, err = parseLogFormat("rfc3164", "")
  33. if err != nil || !functionMatches(syslog.RFC3164Formatter, formatter) ||
  34. !functionMatches(syslog.DefaultFramer, framer) {
  35. t.Fatal("Failed to parse rfc3164 format", err, formatter, framer)
  36. }
  37. formatter, framer, err = parseLogFormat("", "")
  38. if err != nil || !functionMatches(syslog.UnixFormatter, formatter) ||
  39. !functionMatches(syslog.DefaultFramer, framer) {
  40. t.Fatal("Failed to parse empty format", err, formatter, framer)
  41. }
  42. formatter, framer, err = parseLogFormat("invalid", "")
  43. if err == nil {
  44. t.Fatal("Failed to parse invalid format", err, formatter, framer)
  45. }
  46. }
  47. func TestValidateLogOptEmpty(t *testing.T) {
  48. emptyConfig := make(map[string]string)
  49. if err := ValidateLogOpt(emptyConfig); err != nil {
  50. t.Fatal("Failed to parse empty config", err)
  51. }
  52. }
  53. func TestValidateSyslogAddress(t *testing.T) {
  54. err := ValidateLogOpt(map[string]string{
  55. "syslog-address": "this is not an uri",
  56. })
  57. if err == nil {
  58. t.Fatal("Expected error with invalid uri")
  59. }
  60. // File exists
  61. err = ValidateLogOpt(map[string]string{
  62. "syslog-address": "unix:///",
  63. })
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. // File does not exist
  68. err = ValidateLogOpt(map[string]string{
  69. "syslog-address": "unix:///does_not_exist",
  70. })
  71. if err == nil {
  72. t.Fatal("Expected error when address is non existing file")
  73. }
  74. // accepts udp and tcp URIs
  75. err = ValidateLogOpt(map[string]string{
  76. "syslog-address": "udp://1.2.3.4",
  77. })
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. err = ValidateLogOpt(map[string]string{
  82. "syslog-address": "tcp://1.2.3.4",
  83. })
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. }
  88. func TestParseAddressDefaultPort(t *testing.T) {
  89. _, address, err := parseAddress("tcp://1.2.3.4")
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. _, port, _ := net.SplitHostPort(address)
  94. if port != "514" {
  95. t.Fatalf("Expected to default to port 514. It used port %s", port)
  96. }
  97. }
  98. func TestValidateSyslogFacility(t *testing.T) {
  99. err := ValidateLogOpt(map[string]string{
  100. "syslog-facility": "Invalid facility",
  101. })
  102. if err == nil {
  103. t.Fatal("Expected error if facility level is invalid")
  104. }
  105. }
  106. func TestValidateLogOptSyslogFormat(t *testing.T) {
  107. err := ValidateLogOpt(map[string]string{
  108. "syslog-format": "Invalid format",
  109. })
  110. if err == nil {
  111. t.Fatal("Expected error if format is invalid")
  112. }
  113. }
  114. func TestValidateLogOpt(t *testing.T) {
  115. err := ValidateLogOpt(map[string]string{
  116. "env": "http://127.0.0.1",
  117. "env-regex": "abc",
  118. "labels": "labelA",
  119. "labels-regex": "def",
  120. "syslog-address": "udp://1.2.3.4:1111",
  121. "syslog-facility": "daemon",
  122. "syslog-tls-ca-cert": "/etc/ca-certificates/custom/ca.pem",
  123. "syslog-tls-cert": "/etc/ca-certificates/custom/cert.pem",
  124. "syslog-tls-key": "/etc/ca-certificates/custom/key.pem",
  125. "syslog-tls-skip-verify": "true",
  126. "tag": "true",
  127. "syslog-format": "rfc3164",
  128. })
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. err = ValidateLogOpt(map[string]string{
  133. "not-supported-option": "a",
  134. })
  135. if err == nil {
  136. t.Fatal("Expecting error on unsupported options")
  137. }
  138. }