|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "runtime" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/containerd/nerdctl/pkg/rootlessutil" |
| 28 | + "github.com/containerd/nerdctl/pkg/testutil" |
| 29 | + "github.com/containerd/nerdctl/pkg/testutil/testca" |
| 30 | + "github.com/containerd/nerdctl/pkg/testutil/testsyslog" |
| 31 | + syslog "github.com/yuchanns/srslog" |
| 32 | +) |
| 33 | + |
| 34 | +func runSyslogTest(t *testing.T, networks []string, syslogFacilities map[string]syslog.Priority, fmtValidFuncs map[string]func(string, string, string, string, syslog.Priority, bool) error) { |
| 35 | + base := testutil.NewBase(t) |
| 36 | + base.Cmd("pull", testutil.CommonImage).AssertOK() |
| 37 | + hostname, err := os.Hostname() |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("Error retrieving hostname") |
| 40 | + } |
| 41 | + ca := testca.New(base.T) |
| 42 | + cert := ca.NewCert("127.0.0.1") |
| 43 | + t.Cleanup(func() { |
| 44 | + cert.Close() |
| 45 | + ca.Close() |
| 46 | + }) |
| 47 | + rI := 0 |
| 48 | + for _, network := range networks { |
| 49 | + for rFK, rFV := range syslogFacilities { |
| 50 | + fPriV := rFV |
| 51 | + // test both string and number facility |
| 52 | + for _, fPriK := range []string{rFK, fmt.Sprintf("%d", int(fPriV)>>3)} { |
| 53 | + for fmtK, fmtValidFunc := range fmtValidFuncs { |
| 54 | + fmtKT := "empty" |
| 55 | + if fmtK != "" { |
| 56 | + fmtKT = fmtK |
| 57 | + } |
| 58 | + subTestName := fmt.Sprintf("%s_%s_%s", strings.ReplaceAll(network, "+", "_"), fPriK, fmtKT) |
| 59 | + i := rI |
| 60 | + rI++ |
| 61 | + t.Run(subTestName, func(t *testing.T) { |
| 62 | + tID := testutil.Identifier(t) |
| 63 | + tag := tID + "_syslog_driver" |
| 64 | + msg := "hello, " + tID + "_syslog_driver" |
| 65 | + if !testsyslog.TestableNetwork(network) { |
| 66 | + if rootlessutil.IsRootless() { |
| 67 | + t.Skipf("skipping on %s/%s; '%s' for rootless containers are not supported", runtime.GOOS, runtime.GOARCH, network) |
| 68 | + } |
| 69 | + t.Skipf("skipping on %s/%s; '%s' is not supported", runtime.GOOS, runtime.GOARCH, network) |
| 70 | + } |
| 71 | + testContainerName := fmt.Sprintf("%s-%d-%s", tID, i, fPriK) |
| 72 | + done := make(chan string) |
| 73 | + addr, closer := testsyslog.StartServer(network, "", done, cert) |
| 74 | + args := []string{ |
| 75 | + "run", |
| 76 | + "-d", |
| 77 | + "--name", |
| 78 | + testContainerName, |
| 79 | + "--restart=no", |
| 80 | + "--log-driver=syslog", |
| 81 | + "--log-opt=syslog-facility=" + fPriK, |
| 82 | + "--log-opt=tag=" + tag, |
| 83 | + "--log-opt=syslog-format=" + fmtK, |
| 84 | + "--log-opt=syslog-address=" + fmt.Sprintf("%s://%s", network, addr), |
| 85 | + } |
| 86 | + if network == "tcp+tls" { |
| 87 | + args = append(args, |
| 88 | + "--log-opt=syslog-tls-cert="+cert.CertPath, |
| 89 | + "--log-opt=syslog-tls-key="+cert.KeyPath, |
| 90 | + "--log-opt=syslog-tls-ca-cert="+ca.CertPath, |
| 91 | + ) |
| 92 | + } |
| 93 | + args = append(args, testutil.CommonImage, "echo", msg) |
| 94 | + base.Cmd(args...).AssertOK() |
| 95 | + t.Cleanup(func() { |
| 96 | + base.Cmd("rm", "-f", testContainerName).AssertOK() |
| 97 | + }) |
| 98 | + defer closer.Close() |
| 99 | + defer close(done) |
| 100 | + select { |
| 101 | + case rcvd := <-done: |
| 102 | + if err := fmtValidFunc(rcvd, msg, tag, hostname, fPriV, network == "tcp+tls"); err != nil { |
| 103 | + t.Error(err) |
| 104 | + } |
| 105 | + case <-time.Tick(time.Second * 3): |
| 106 | + t.Errorf("timeout with %s", subTestName) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestSyslogNetwork(t *testing.T) { |
| 116 | + var syslogFacilities = map[string]syslog.Priority{ |
| 117 | + "user": syslog.LOG_USER, |
| 118 | + } |
| 119 | + |
| 120 | + networks := []string{ |
| 121 | + "udp", |
| 122 | + "tcp", |
| 123 | + "tcp+tls", |
| 124 | + "unix", |
| 125 | + "unixgram", |
| 126 | + } |
| 127 | + fmtValidFuncs := map[string]func(string, string, string, string, syslog.Priority, bool) error{ |
| 128 | + "rfc5424": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isTLS bool) error { |
| 129 | + var parsedHostname, timestamp string |
| 130 | + var length, version, pid int |
| 131 | + if !isTLS { |
| 132 | + exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n" |
| 133 | + if n, err := fmt.Sscanf(rcvd, exp, &version, ×tamp, &parsedHostname, &pid); n != 4 || err != nil || hostname != parsedHostname { |
| 134 | + return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err) |
| 135 | + } |
| 136 | + } else { |
| 137 | + exp := "%d " + fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n" |
| 138 | + if n, err := fmt.Sscanf(rcvd, exp, &length, &version, ×tamp, &parsedHostname, &pid); n != 5 || err != nil || hostname != parsedHostname { |
| 139 | + return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err) |
| 140 | + } |
| 141 | + } |
| 142 | + return nil |
| 143 | + }, |
| 144 | + } |
| 145 | + runSyslogTest(t, networks, syslogFacilities, fmtValidFuncs) |
| 146 | +} |
| 147 | + |
| 148 | +func TestSyslogFacilities(t *testing.T) { |
| 149 | + var syslogFacilities = map[string]syslog.Priority{ |
| 150 | + "kern": syslog.LOG_KERN, |
| 151 | + "user": syslog.LOG_USER, |
| 152 | + "mail": syslog.LOG_MAIL, |
| 153 | + "daemon": syslog.LOG_DAEMON, |
| 154 | + "auth": syslog.LOG_AUTH, |
| 155 | + "syslog": syslog.LOG_SYSLOG, |
| 156 | + "lpr": syslog.LOG_LPR, |
| 157 | + "news": syslog.LOG_NEWS, |
| 158 | + "uucp": syslog.LOG_UUCP, |
| 159 | + "cron": syslog.LOG_CRON, |
| 160 | + "authpriv": syslog.LOG_AUTHPRIV, |
| 161 | + "ftp": syslog.LOG_FTP, |
| 162 | + "local0": syslog.LOG_LOCAL0, |
| 163 | + "local1": syslog.LOG_LOCAL1, |
| 164 | + "local2": syslog.LOG_LOCAL2, |
| 165 | + "local3": syslog.LOG_LOCAL3, |
| 166 | + "local4": syslog.LOG_LOCAL4, |
| 167 | + "local5": syslog.LOG_LOCAL5, |
| 168 | + "local6": syslog.LOG_LOCAL6, |
| 169 | + "local7": syslog.LOG_LOCAL7, |
| 170 | + } |
| 171 | + |
| 172 | + networks := []string{"unix"} |
| 173 | + fmtValidFuncs := map[string]func(string, string, string, string, syslog.Priority, bool) error{ |
| 174 | + "rfc5424": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isTLS bool) error { |
| 175 | + var parsedHostname, timestamp string |
| 176 | + var length, version, pid int |
| 177 | + if !isTLS { |
| 178 | + exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n" |
| 179 | + if n, err := fmt.Sscanf(rcvd, exp, &version, ×tamp, &parsedHostname, &pid); n != 4 || err != nil || hostname != parsedHostname { |
| 180 | + return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err) |
| 181 | + } |
| 182 | + } else { |
| 183 | + exp := "%d " + fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n" |
| 184 | + if n, err := fmt.Sscanf(rcvd, exp, &length, &version, ×tamp, &parsedHostname, &pid); n != 5 || err != nil || hostname != parsedHostname { |
| 185 | + return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err) |
| 186 | + } |
| 187 | + } |
| 188 | + return nil |
| 189 | + }, |
| 190 | + } |
| 191 | + runSyslogTest(t, networks, syslogFacilities, fmtValidFuncs) |
| 192 | +} |
| 193 | + |
| 194 | +func TestSyslogFormat(t *testing.T) { |
| 195 | + var syslogFacilities = map[string]syslog.Priority{ |
| 196 | + "user": syslog.LOG_USER, |
| 197 | + } |
| 198 | + |
| 199 | + networks := []string{"unix"} |
| 200 | + fmtValidFuncs := map[string]func(string, string, string, string, syslog.Priority, bool) error{ |
| 201 | + "": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isSTLS bool) error { |
| 202 | + var mon, day, hrs string |
| 203 | + var pid int |
| 204 | + exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%s %s %s " + tag + "[%d]: " + msg + "\n" |
| 205 | + if n, err := fmt.Sscanf(rcvd, exp, &mon, &day, &hrs, &pid); n != 4 || err != nil { |
| 206 | + return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err) |
| 207 | + } |
| 208 | + return nil |
| 209 | + }, |
| 210 | + "rfc3164": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isTLS bool) error { |
| 211 | + var parsedHostname, mon, day, hrs string |
| 212 | + var pid int |
| 213 | + exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%s %s %s %s " + tag + "[%d]: " + msg + "\n" |
| 214 | + if n, err := fmt.Sscanf(rcvd, exp, &mon, &day, &hrs, &parsedHostname, &pid); n != 5 || err != nil || hostname != parsedHostname { |
| 215 | + return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err) |
| 216 | + } |
| 217 | + return nil |
| 218 | + }, |
| 219 | + "rfc5424": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isTLS bool) error { |
| 220 | + var parsedHostname, timestamp string |
| 221 | + var length, version, pid int |
| 222 | + if !isTLS { |
| 223 | + exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n" |
| 224 | + if n, err := fmt.Sscanf(rcvd, exp, &version, ×tamp, &parsedHostname, &pid); n != 4 || err != nil || hostname != parsedHostname { |
| 225 | + return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err) |
| 226 | + } |
| 227 | + } else { |
| 228 | + exp := "%d " + fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n" |
| 229 | + if n, err := fmt.Sscanf(rcvd, exp, &length, &version, ×tamp, &parsedHostname, &pid); n != 5 || err != nil || hostname != parsedHostname { |
| 230 | + return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err) |
| 231 | + } |
| 232 | + } |
| 233 | + return nil |
| 234 | + }, |
| 235 | + "rfc5424micro": func(rcvd, msg, tag, hostname string, pri syslog.Priority, isTLS bool) error { |
| 236 | + var parsedHostname, timestamp string |
| 237 | + var length, version, pid int |
| 238 | + if !isTLS { |
| 239 | + exp := fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n" |
| 240 | + if n, err := fmt.Sscanf(rcvd, exp, &version, ×tamp, &parsedHostname, &pid); n != 4 || err != nil || hostname != parsedHostname { |
| 241 | + return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err) |
| 242 | + } |
| 243 | + } else { |
| 244 | + exp := "%d " + fmt.Sprintf("<%d>", pri|syslog.LOG_INFO) + "%d %s %s " + tag + " %d " + tag + " - " + msg + "\n" |
| 245 | + if n, err := fmt.Sscanf(rcvd, exp, &length, &version, ×tamp, &parsedHostname, &pid); n != 5 || err != nil || hostname != parsedHostname { |
| 246 | + return fmt.Errorf("s.Info() = '%q', didn't match '%q' (%d %s)", rcvd, exp, n, err) |
| 247 | + } |
| 248 | + } |
| 249 | + return nil |
| 250 | + }, |
| 251 | + } |
| 252 | + runSyslogTest(t, networks, syslogFacilities, fmtValidFuncs) |
| 253 | +} |
0 commit comments