github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/logger/syslog/syslog_test.go (about)

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