From df8bd16133755fd10092250a4321143ca725231f Mon Sep 17 00:00:00 2001 From: Cameron Davison Date: Wed, 6 Jul 2016 10:57:06 -0500 Subject: [PATCH 1/9] get into the hour minute second part of the time before looking for spaces, and then looking for the : seperator --- client/driver/logging/syslog_parser_unix.go | 15 +++++++++++++++ .../driver/logging/syslog_parser_unix_test.go | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/client/driver/logging/syslog_parser_unix.go b/client/driver/logging/syslog_parser_unix.go index 92fe70e02..e39ac7cf7 100644 --- a/client/driver/logging/syslog_parser_unix.go +++ b/client/driver/logging/syslog_parser_unix.go @@ -69,7 +69,21 @@ func (d *DockerLogParser) Parse(line []byte) *SyslogMessage { func (d *DockerLogParser) logContentIndex(line []byte) int { cursor := 0 numSpace := 0 + numColons := 0 + // first look for at least 2 colons. This matches into the date that has no more spaces in it + // DefaultFormatter log line look: '<30>2016-07-06T15:13:11Z00:00 hostname docker/9648c64f5037[16200]' + // UnixFormatter log line look: '<30>Jul 6 15:13:11 docker/9648c64f5037[16200]' for i := 0; i < len(line); i++ { + if line[i] == ':' { + numColons += 1 + if numColons == 2 { + cursor = i + break + } + } + } + // then look for the next space + for i := cursor; i < len(line); i++ { if line[i] == ' ' { numSpace += 1 if numSpace == 1 { @@ -78,6 +92,7 @@ func (d *DockerLogParser) logContentIndex(line []byte) int { } } } + // then the colon is what seperates it for i := cursor; i < len(line); i++ { if line[i] == ':' { cursor = i diff --git a/client/driver/logging/syslog_parser_unix_test.go b/client/driver/logging/syslog_parser_unix_test.go index aeb1959a8..80adc0007 100644 --- a/client/driver/logging/syslog_parser_unix_test.go +++ b/client/driver/logging/syslog_parser_unix_test.go @@ -26,3 +26,21 @@ func TestLogParser_Priority(t *testing.T) { t.Fatalf("expected idx: %v, got: %v", expected, idx) } } + +func TestLogParser_Priority_UnixFormatter(t *testing.T) { + line := []byte("<30>Feb 6, 10:16:43 docker/e2a1e3ebd3a3[22950]: 1:C 10 Feb 18:16:43.391 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf") + d := NewDockerLogParser(log.New(os.Stdout, "", log.LstdFlags)) + p, _, err := d.parsePriority(line) + if err != nil { + t.Fatalf("got an err: %v", err) + } + if p.Severity != syslog.LOG_INFO { + t.Fatalf("expected serverity: %v, got: %v", syslog.LOG_INFO, p.Severity) + } + + idx := d.logContentIndex(line) + expected := 48 + if idx != expected { + t.Fatalf("expected idx: %v, got: %v", expected, idx) + } +} From c7817127c04a21085359cb5a82847a32dbf99023 Mon Sep 17 00:00:00 2001 From: Cameron Davison Date: Wed, 6 Jul 2016 11:02:00 -0500 Subject: [PATCH 2/9] remove the expected leading space, after the colon in syslog --- client/driver/logging/syslog_parser_unix.go | 7 ++++--- client/driver/logging/syslog_parser_unix_test.go | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/client/driver/logging/syslog_parser_unix.go b/client/driver/logging/syslog_parser_unix.go index e39ac7cf7..4e0fec555 100644 --- a/client/driver/logging/syslog_parser_unix.go +++ b/client/driver/logging/syslog_parser_unix.go @@ -92,13 +92,14 @@ func (d *DockerLogParser) logContentIndex(line []byte) int { } } } - // then the colon is what seperates it + // then the colon is what seperates it, followed by a space for i := cursor; i < len(line); i++ { - if line[i] == ':' { - cursor = i + if line[i] == ':' && i+1 < len(line) && line[i+1] == ' ' { + cursor = i + 1 break } } + // return the cursor to the next character return cursor + 1 } diff --git a/client/driver/logging/syslog_parser_unix_test.go b/client/driver/logging/syslog_parser_unix_test.go index 80adc0007..5aa3345ec 100644 --- a/client/driver/logging/syslog_parser_unix_test.go +++ b/client/driver/logging/syslog_parser_unix_test.go @@ -3,6 +3,7 @@ package logging import ( + "bytes" "log" "log/syslog" "os" @@ -21,7 +22,7 @@ func TestLogParser_Priority(t *testing.T) { } idx := d.logContentIndex(line) - expected := 68 + expected := bytes.Index(line, []byte("1:C 10 Feb 18:16:43.391")) if idx != expected { t.Fatalf("expected idx: %v, got: %v", expected, idx) } @@ -39,7 +40,7 @@ func TestLogParser_Priority_UnixFormatter(t *testing.T) { } idx := d.logContentIndex(line) - expected := 48 + expected := bytes.Index(line, []byte("1:C 10 Feb 18:16:43.391")) if idx != expected { t.Fatalf("expected idx: %v, got: %v", expected, idx) } From 29f099cf9d7f0f7345bf010157a020263d0e0171 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 8 Jul 2016 15:37:44 -0700 Subject: [PATCH 3/9] Fixed the host port environment variable --- client/driver/env/env.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 460c8e22e..9fd40c4b3 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -133,15 +133,13 @@ func (t *TaskEnvironment) Build() *TaskEnvironment { // Build the ports for _, network := range t.Networks { - for label, value := range network.MapLabelToValues(t.PortMap) { - IPPort := fmt.Sprintf("%s:%d", network.IP, value) - t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort + for label, value := range network.MapLabelToValues(nil) { t.TaskEnv[fmt.Sprintf("%s%s", IpPrefix, label)] = network.IP - t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value) - - // Pass an explicit port mapping to the environment - if port, ok := t.PortMap[label]; ok { - t.TaskEnv[fmt.Sprintf("%s%s", HostPortPrefix, label)] = strconv.Itoa(port) + t.TaskEnv[fmt.Sprintf("%s%s", HostPortPrefix, label)] = strconv.Itoa(value) + if value, ok := t.PortMap[label]; ok { + t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value) + IPPort := fmt.Sprintf("%s:%d", network.IP, value) + t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort } } } From 0769e41982ee9c5fef1d71d8c14528ba447abe73 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 8 Jul 2016 17:42:34 -0700 Subject: [PATCH 4/9] Fixed the client tests --- client/driver/env/env.go | 10 ++++++---- client/driver/env/env_test.go | 9 ++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/client/driver/env/env.go b/client/driver/env/env.go index 9fd40c4b3..1ac9b7510 100644 --- a/client/driver/env/env.go +++ b/client/driver/env/env.go @@ -136,11 +136,13 @@ func (t *TaskEnvironment) Build() *TaskEnvironment { for label, value := range network.MapLabelToValues(nil) { t.TaskEnv[fmt.Sprintf("%s%s", IpPrefix, label)] = network.IP t.TaskEnv[fmt.Sprintf("%s%s", HostPortPrefix, label)] = strconv.Itoa(value) - if value, ok := t.PortMap[label]; ok { - t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value) - IPPort := fmt.Sprintf("%s:%d", network.IP, value) - t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort + if forwardedPort, ok := t.PortMap[label]; ok { + value = forwardedPort } + t.TaskEnv[fmt.Sprintf("%s%s", PortPrefix, label)] = fmt.Sprintf("%d", value) + IPPort := fmt.Sprintf("%s:%d", network.IP, value) + t.TaskEnv[fmt.Sprintf("%s%s", AddrPrefix, label)] = IPPort + } } diff --git a/client/driver/env/env_test.go b/client/driver/env/env_test.go index 2ea76f394..19c980ee1 100644 --- a/client/driver/env/env_test.go +++ b/client/driver/env/env_test.go @@ -151,7 +151,8 @@ func TestEnvironment_AsList(t *testing.T) { "NOMAD_ADDR_https=127.0.0.1:443", "NOMAD_PORT_https=443", "NOMAD_IP_https=127.0.0.1", - "NOMAD_HOST_PORT_https=443", + "NOMAD_HOST_PORT_http=80", + "NOMAD_HOST_PORT_https=8080", "NOMAD_META_FOO=baz", "NOMAD_META_BAZ=bam", } @@ -177,7 +178,8 @@ func TestEnvironment_ClearEnvvars(t *testing.T) { "NOMAD_ADDR_https=127.0.0.1:443", "NOMAD_PORT_https=443", "NOMAD_IP_https=127.0.0.1", - "NOMAD_HOST_PORT_https=443", + "NOMAD_HOST_PORT_http=80", + "NOMAD_HOST_PORT_https=8080", "bar=bang", "foo=baz", } @@ -198,7 +200,8 @@ func TestEnvironment_ClearEnvvars(t *testing.T) { "NOMAD_ADDR_https=127.0.0.1:443", "NOMAD_PORT_https=443", "NOMAD_IP_https=127.0.0.1", - "NOMAD_HOST_PORT_https=443", + "NOMAD_HOST_PORT_https=8080", + "NOMAD_HOST_PORT_http=80", } sort.Strings(act) sort.Strings(exp) From 3dbea05f57f371bf9ee6174555a1910722101a27 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 8 Jul 2016 18:27:51 -0700 Subject: [PATCH 5/9] Fixed tests --- client/driver/docker_test.go | 3 ++- client/driver/driver_test.go | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/client/driver/docker_test.go b/client/driver/docker_test.go index d5596fcb8..3229c0fd3 100644 --- a/client/driver/docker_test.go +++ b/client/driver/docker_test.go @@ -9,6 +9,7 @@ import ( "path/filepath" "reflect" "runtime/debug" + "strconv" "strings" "testing" "time" @@ -709,7 +710,7 @@ func TestDockerPortsMapping(t *testing.T) { expectedEnvironment := map[string]string{ "NOMAD_ADDR_main": "127.0.0.1:8080", "NOMAD_ADDR_REDIS": "127.0.0.1:6379", - "NOMAD_HOST_PORT_main": "8080", + "NOMAD_HOST_PORT_main": strconv.Itoa(docker_reserved), } for key, val := range expectedEnvironment { diff --git a/client/driver/driver_test.go b/client/driver/driver_test.go index 477c1832f..c8b37dda4 100644 --- a/client/driver/driver_test.go +++ b/client/driver/driver_test.go @@ -129,15 +129,19 @@ func TestDriver_GetTaskEnv(t *testing.T) { "NOMAD_ADDR_one": "1.2.3.4:80", "NOMAD_IP_one": "1.2.3.4", "NOMAD_PORT_one": "80", + "NOMAD_HOST_PORT_one": "80", "NOMAD_ADDR_two": "1.2.3.4:443", "NOMAD_IP_two": "1.2.3.4", "NOMAD_PORT_two": "443", + "NOMAD_HOST_PORT_two": "443", "NOMAD_ADDR_admin": "1.2.3.4:8081", "NOMAD_IP_admin": "1.2.3.4", "NOMAD_PORT_admin": "8081", + "NOMAD_HOST_PORT_admin": "8081", "NOMAD_ADDR_web": "1.2.3.4:8086", "NOMAD_IP_web": "1.2.3.4", "NOMAD_PORT_web": "8086", + "NOMAD_HOST_PORT_web": "8086", "NOMAD_META_CHOCOLATE": "cake", "NOMAD_META_STRAWBERRY": "icecream", "NOMAD_META_ELB_CHECK_INTERVAL": "30s", From 37fc6a4cdefc431d891c127f89609249d0f92d35 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 8 Jul 2016 22:25:04 -0700 Subject: [PATCH 6/9] Fixed the validation logic for check timeout --- nomad/structs/structs.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index b4cc1a237..a4998dcff 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -1527,7 +1527,7 @@ func (sc *ServiceCheck) Copy() *ServiceCheck { func (sc *ServiceCheck) validate() error { switch strings.ToLower(sc.Type) { case ServiceCheckTCP: - if sc.Timeout > 0 && sc.Timeout <= minCheckTimeout { + if sc.Timeout < minCheckTimeout { return fmt.Errorf("timeout %v is lower than required minimum timeout %v", sc.Timeout, minCheckInterval) } case ServiceCheckHTTP: @@ -1535,7 +1535,7 @@ func (sc *ServiceCheck) validate() error { return fmt.Errorf("http type must have a valid http path") } - if sc.Timeout > 0 && sc.Timeout <= minCheckTimeout { + if sc.Timeout < minCheckTimeout { return fmt.Errorf("timeout %v is lower than required minimum timeout %v", sc.Timeout, minCheckInterval) } case ServiceCheckScript: @@ -1549,7 +1549,7 @@ func (sc *ServiceCheck) validate() error { return fmt.Errorf(`invalid type (%+q), must be one of "http", "tcp", or "script" type`, sc.Type) } - if sc.Interval > 0 && sc.Interval <= minCheckInterval { + if sc.Interval < minCheckInterval { return fmt.Errorf("interval (%v) can not be lower than %v", sc.Interval, minCheckInterval) } From 29599b0152ddbb935cf52e9f7fd9961dc98fb9a6 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 8 Jul 2016 22:33:04 -0700 Subject: [PATCH 7/9] Added a test --- nomad/structs/structs.go | 4 ++-- nomad/structs/structs_test.go | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index a4998dcff..b8a94d423 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -1528,7 +1528,7 @@ func (sc *ServiceCheck) validate() error { switch strings.ToLower(sc.Type) { case ServiceCheckTCP: if sc.Timeout < minCheckTimeout { - return fmt.Errorf("timeout %v is lower than required minimum timeout %v", sc.Timeout, minCheckInterval) + return fmt.Errorf("timeout (%v) is lower than required minimum timeout %v", sc.Timeout, minCheckInterval) } case ServiceCheckHTTP: if sc.Path == "" { @@ -1536,7 +1536,7 @@ func (sc *ServiceCheck) validate() error { } if sc.Timeout < minCheckTimeout { - return fmt.Errorf("timeout %v is lower than required minimum timeout %v", sc.Timeout, minCheckInterval) + return fmt.Errorf("timeout (%v) is lower than required minimum timeout %v", sc.Timeout, minCheckInterval) } case ServiceCheckScript: if sc.Command == "" { diff --git a/nomad/structs/structs_test.go b/nomad/structs/structs_test.go index e60ceadcf..08547f44b 100644 --- a/nomad/structs/structs_test.go +++ b/nomad/structs/structs_test.go @@ -288,12 +288,14 @@ func TestTask_Validate_Services(t *testing.T) { PortLabel: "bar", Checks: []*ServiceCheck{ { - Name: "check-name", - Type: ServiceCheckTCP, + Name: "check-name", + Type: ServiceCheckTCP, + Interval: 0 * time.Second, }, { - Name: "check-name", - Type: ServiceCheckTCP, + Name: "check-name", + Type: ServiceCheckTCP, + Timeout: 2 * time.Second, }, }, } @@ -328,6 +330,10 @@ func TestTask_Validate_Services(t *testing.T) { if !strings.Contains(err.Error(), "check \"check-name\" is duplicate") { t.Fatalf("err: %v", err) } + + if !strings.Contains(err.Error(), "interval (0) can not be lower") { + t.Fatal("err: %v", err) + } } func TestTask_Validate_LogConfig(t *testing.T) { From 5e4d6e1c46f3d55f443e81585dcf6e5c695ad8a9 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Sat, 9 Jul 2016 00:12:53 -0700 Subject: [PATCH 8/9] Fixed a debug message --- client/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index 80c409637..dd1baa851 100644 --- a/client/client.go +++ b/client/client.go @@ -604,7 +604,7 @@ func (c *Client) reservePorts() { func (c *Client) fingerprint() error { whitelist := c.config.ReadStringListToMap("fingerprint.whitelist") whitelistEnabled := len(whitelist) > 0 - c.logger.Printf("[DEBUG] client: built-in fingerprints: %v", fingerprint.BuiltinFingerprints) + c.logger.Printf("[DEBUG] client: built-in fingerprints: %v", fingerprint.BuiltinFingerprints()) var applied []string var skipped []string From 3a98201f50fcf765cb0215f8e56a885a643445ee Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Mon, 11 Jul 2016 11:52:41 -0600 Subject: [PATCH 9/9] Get windows to build --- client/driver/exec_default.go | 2 +- client/fingerprint/fingerprint_default.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/driver/exec_default.go b/client/driver/exec_default.go index a17ef7dc5..975cbc595 100644 --- a/client/driver/exec_default.go +++ b/client/driver/exec_default.go @@ -1,4 +1,4 @@ -//+build darwin dragonfly freebsd netbsd openbsd solaris +//+build windows darwin dragonfly freebsd netbsd openbsd solaris package driver diff --git a/client/fingerprint/fingerprint_default.go b/client/fingerprint/fingerprint_default.go index 7efa77e10..2825408d9 100644 --- a/client/fingerprint/fingerprint_default.go +++ b/client/fingerprint/fingerprint_default.go @@ -1,4 +1,4 @@ -// +build darwin dragonfly freebsd netbsd openbsd solaris +// +build windows darwin dragonfly freebsd netbsd openbsd solaris package fingerprint