Browse code

Add options validation to syslog logger test

Adds the following validations to the syslog logger test:

1. Only supported options are valid
2. Log option syslog-address has to be a valid URI
3. Log option syslog-address if is file has to exist
4. Log option syslog-address if udp/tcp scheme, default to port 513
5. Log-option syslog-facility has to be a valid facility
6. Log-option syslog-format has to be a valid format

Signed-off-by: Joao Trindade <trindade.joao@gmail.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Joao Trindade authored on 2017/12/27 22:11:21
Showing 1 changed files
... ...
@@ -1,6 +1,7 @@
1 1
 package syslog // import "github.com/docker/docker/daemon/logger/syslog"
2 2
 
3 3
 import (
4
+	"net"
4 5
 	"reflect"
5 6
 	"testing"
6 7
 
... ...
@@ -60,3 +61,99 @@ func TestValidateLogOptEmpty(t *testing.T) {
60 60
 		t.Fatal("Failed to parse empty config", err)
61 61
 	}
62 62
 }
63
+
64
+func TestValidateSyslogAddress(t *testing.T) {
65
+	err := ValidateLogOpt(map[string]string{
66
+		"syslog-address": "this is not an uri",
67
+	})
68
+	if err == nil {
69
+		t.Fatal("Expected error with invalid uri")
70
+	}
71
+
72
+	// File exists
73
+	err = ValidateLogOpt(map[string]string{
74
+		"syslog-address": "unix:///",
75
+	})
76
+	if err != nil {
77
+		t.Fatal(err)
78
+	}
79
+
80
+	// File does not exist
81
+	err = ValidateLogOpt(map[string]string{
82
+		"syslog-address": "unix:///does_not_exist",
83
+	})
84
+	if err == nil {
85
+		t.Fatal("Expected error when address is non existing file")
86
+	}
87
+
88
+	// accepts udp and tcp URIs
89
+	err = ValidateLogOpt(map[string]string{
90
+		"syslog-address": "udp://1.2.3.4",
91
+	})
92
+	if err != nil {
93
+		t.Fatal(err)
94
+	}
95
+
96
+	err = ValidateLogOpt(map[string]string{
97
+		"syslog-address": "tcp://1.2.3.4",
98
+	})
99
+	if err != nil {
100
+		t.Fatal(err)
101
+	}
102
+}
103
+
104
+func TestParseAddressDefaultPort(t *testing.T) {
105
+	_, address, err := parseAddress("tcp://1.2.3.4")
106
+	if err != nil {
107
+		t.Fatal(err)
108
+	}
109
+
110
+	_, port, _ := net.SplitHostPort(address)
111
+	if port != "514" {
112
+		t.Fatalf("Expected to default to port 514. It used port %s", port)
113
+	}
114
+}
115
+
116
+func TestValidateSyslogFacility(t *testing.T) {
117
+	err := ValidateLogOpt(map[string]string{
118
+		"syslog-facility": "Invalid facility",
119
+	})
120
+	if err == nil {
121
+		t.Fatal("Expected error if facility level is invalid")
122
+	}
123
+}
124
+
125
+func TestValidateLogOptSyslogFormat(t *testing.T) {
126
+	err := ValidateLogOpt(map[string]string{
127
+		"syslog-format": "Invalid format",
128
+	})
129
+	if err == nil {
130
+		t.Fatal("Expected error if format is invalid")
131
+	}
132
+}
133
+
134
+func TestValidateLogOpt(t *testing.T) {
135
+	err := ValidateLogOpt(map[string]string{
136
+		"env":                    "http://127.0.0.1",
137
+		"env-regex":              "abc",
138
+		"labels":                 "labelA",
139
+		"syslog-address":         "udp://1.2.3.4:1111",
140
+		"syslog-facility":        "daemon",
141
+		"syslog-tls-ca-cert":     "/etc/ca-certificates/custom/ca.pem",
142
+		"syslog-tls-cert":        "/etc/ca-certificates/custom/cert.pem",
143
+		"syslog-tls-key":         "/etc/ca-certificates/custom/key.pem",
144
+		"syslog-tls-skip-verify": "true",
145
+		"tag":                    "true",
146
+		"syslog-format":          "rfc3164",
147
+	})
148
+	if err != nil {
149
+		t.Fatal(err)
150
+	}
151
+
152
+	err = ValidateLogOpt(map[string]string{
153
+		"not-supported-option": "a",
154
+	})
155
+	if err == nil {
156
+		t.Fatal("Expecting error on unsupported options")
157
+	}
158
+}