From 536a710c78a72df18d188f94df2120d2ac7bc29f Mon Sep 17 00:00:00 2001 From: Karl Hepworth Date: Wed, 28 Oct 2020 22:24:40 +1100 Subject: [PATCH 1/4] Implement Healthcheck for containers Signed-off-by: Karl Hepworth --- service/dnsmasq/dnsmasq.go | 7 +++++++ service/dnsmasq/dnsmasq_test.go | 4 ++++ service/haproxy/haproxy.go | 7 +++++++ service/haproxy/haproxy_test.go | 4 ++++ service/mailhog/mailhog.go | 7 +++++++ service/mailhog/mailhog_test.go | 4 ++++ service/ssh/agent/ssh_agent.go | 7 +++++++ service/ssh/agent/ssh_agent_test.go | 4 ++++ 8 files changed, 44 insertions(+) diff --git a/service/dnsmasq/dnsmasq.go b/service/dnsmasq/dnsmasq.go index c8a86f62..e3b42318 100644 --- a/service/dnsmasq/dnsmasq.go +++ b/service/dnsmasq/dnsmasq.go @@ -22,6 +22,13 @@ func New() model.Service { "pygmy.name": "amazeeio-dnsmasq", "pygmy.weight": "13", }, + Healthcheck: &container.HealthConfig{ + Test: []string{"CMD-SHELL", "stat /run/dnsmasq.pid || exit 1"}, + Interval: 30000000000, + Timeout: 5000000000, + StartPeriod: 5000000000, + Retries: 5, + }, }, HostConfig: container.HostConfig{ AutoRemove: false, diff --git a/service/dnsmasq/dnsmasq_test.go b/service/dnsmasq/dnsmasq_test.go index 2dc74a7e..4403e9d6 100644 --- a/service/dnsmasq/dnsmasq_test.go +++ b/service/dnsmasq/dnsmasq_test.go @@ -23,6 +23,10 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.enable"], ShouldEqual, "true") So(obj.Config.Labels["pygmy.name"], ShouldEqual, "amazeeio-dnsmasq") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "13") + So(obj.Config.Healthcheck.Test, ShouldEqual, []string{"CMD-SHELL", "stat /run/dnsmasq.pid || exit 1"}) + So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) + So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) + So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) So(obj.HostConfig.AutoRemove, ShouldBeFalse) So(fmt.Sprint(obj.HostConfig.CapAdd), ShouldEqual, fmt.Sprint([]string{"NET_ADMIN"})) So(obj.HostConfig.IpcMode, ShouldEqual, "private") diff --git a/service/haproxy/haproxy.go b/service/haproxy/haproxy.go index 2e4f3922..f665c926 100644 --- a/service/haproxy/haproxy.go +++ b/service/haproxy/haproxy.go @@ -20,6 +20,13 @@ func New() model.Service { "pygmy.url": "http://docker.amazee.io/stats", "pygmy.weight": "14", }, + Healthcheck: &container.HealthConfig{ + Test: []string{"CMD-SHELL", "wget http://docker.amazee.io/stats -O /dev/null || exit 1"}, + Interval: 30000000000, + Timeout: 5000000000, + StartPeriod: 5000000000, + Retries: 5, + }, }, HostConfig: container.HostConfig{ Binds: []string{"/var/run/docker.sock:/tmp/docker.sock"}, diff --git a/service/haproxy/haproxy_test.go b/service/haproxy/haproxy_test.go index 11157329..b13728ba 100644 --- a/service/haproxy/haproxy_test.go +++ b/service/haproxy/haproxy_test.go @@ -25,6 +25,10 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.network"], ShouldEqual, "amazeeio-network") So(obj.Config.Labels["pygmy.url"], ShouldEqual, "http://docker.amazee.io/stats") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "14") + So(obj.Config.Healthcheck.Test, ShouldEqual, []string{"CMD-SHELL", "wget http://docker.amazee.io/stats -O /dev/null || exit 1"}) + So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) + So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) + So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) So(obj.HostConfig.AutoRemove, ShouldBeFalse) So(fmt.Sprint(obj.HostConfig.Binds), ShouldEqual, fmt.Sprint([]string{"/var/run/docker.sock:/tmp/docker.sock"})) So(obj.HostConfig.PortBindings, ShouldEqual, nil) diff --git a/service/mailhog/mailhog.go b/service/mailhog/mailhog.go index 89996e9d..f5e0ac25 100644 --- a/service/mailhog/mailhog.go +++ b/service/mailhog/mailhog.go @@ -32,6 +32,13 @@ func New() model.Service { "pygmy.url": "http://mailhog.docker.amazee.io", "pygmy.weight": "15", }, + Healthcheck: &container.HealthConfig{ + Test: []string{"CMD-SHELL", "wget http://mailhog.docker.amazee.io -O /dev/null || exit 1"}, + Interval: 30000000000, + Timeout: 5000000000, + StartPeriod: 5000000000, + Retries: 5, + }, }, HostConfig: container.HostConfig{ AutoRemove: false, diff --git a/service/mailhog/mailhog_test.go b/service/mailhog/mailhog_test.go index 2a882d80..9dfd47b0 100644 --- a/service/mailhog/mailhog_test.go +++ b/service/mailhog/mailhog_test.go @@ -28,6 +28,10 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.network"], ShouldEqual, "amazeeio-network") So(obj.Config.Labels["pygmy.url"], ShouldEqual, "http://mailhog.docker.amazee.io") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "15") + So(obj.Config.Healthcheck.Test, ShouldEqual, []string{"CMD-SHELL", "wget http://mailhog.docker.amazee.io -O /dev/null || exit 1"}) + So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) + So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) + So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) So(obj.HostConfig.AutoRemove, ShouldBeFalse) So(obj.HostConfig.PortBindings, ShouldEqual, nil) So(obj.HostConfig.RestartPolicy.Name, ShouldEqual, "unless-stopped") diff --git a/service/ssh/agent/ssh_agent.go b/service/ssh/agent/ssh_agent.go index aca11678..5dbcef2c 100644 --- a/service/ssh/agent/ssh_agent.go +++ b/service/ssh/agent/ssh_agent.go @@ -23,6 +23,13 @@ func New() model.Service { "pygmy.purpose": "sshagent", "pygmy.weight": "30", }, + Healthcheck: &container.HealthConfig{ + Test: []string{"CMD-SHELL", "if [ ! \"$SSH_AUTH_SOCK\" = \"\" ]; then exit 0; else exit 1; fi;"}, + Interval: 30000000000, + Timeout: 5000000000, + StartPeriod: 5000000000, + Retries: 5, + }, }, HostConfig: container.HostConfig{ AutoRemove: false, diff --git a/service/ssh/agent/ssh_agent_test.go b/service/ssh/agent/ssh_agent_test.go index 0e029a81..61db5c03 100644 --- a/service/ssh/agent/ssh_agent_test.go +++ b/service/ssh/agent/ssh_agent_test.go @@ -31,6 +31,10 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.network"], ShouldEqual, "amazeeio-network") So(obj.Config.Labels["pygmy.purpose"], ShouldEqual, "sshagent") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "30") + So(obj.Config.Healthcheck.Test, ShouldEqual, []string{"CMD-SHELL", "if [ ! \"$SSH_AUTH_SOCK\" = \"\" ]; then exit 0; else exit 1; fi;"}) + So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) + So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) + So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) So(obj.HostConfig.AutoRemove, ShouldBeFalse) So(obj.HostConfig.IpcMode, ShouldEqual, "private") So(obj.HostConfig.PortBindings, ShouldEqual, nil) From c9d17923d7fbde9b1ea6eeeac4d824627c015207 Mon Sep 17 00:00:00 2001 From: Karl Hepworth Date: Wed, 28 Oct 2020 22:32:46 +1100 Subject: [PATCH 2/4] Changes ShouldEqual to ShouldResemble Signed-off-by: Karl Hepworth --- service/dnsmasq/dnsmasq_test.go | 2 +- service/haproxy/haproxy_test.go | 2 +- service/mailhog/mailhog_test.go | 2 +- service/ssh/agent/ssh_agent_test.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/service/dnsmasq/dnsmasq_test.go b/service/dnsmasq/dnsmasq_test.go index 4403e9d6..387ce191 100644 --- a/service/dnsmasq/dnsmasq_test.go +++ b/service/dnsmasq/dnsmasq_test.go @@ -23,7 +23,7 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.enable"], ShouldEqual, "true") So(obj.Config.Labels["pygmy.name"], ShouldEqual, "amazeeio-dnsmasq") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "13") - So(obj.Config.Healthcheck.Test, ShouldEqual, []string{"CMD-SHELL", "stat /run/dnsmasq.pid || exit 1"}) + So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "stat /run/dnsmasq.pid || exit 1"}) So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) diff --git a/service/haproxy/haproxy_test.go b/service/haproxy/haproxy_test.go index b13728ba..01bfa173 100644 --- a/service/haproxy/haproxy_test.go +++ b/service/haproxy/haproxy_test.go @@ -25,7 +25,7 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.network"], ShouldEqual, "amazeeio-network") So(obj.Config.Labels["pygmy.url"], ShouldEqual, "http://docker.amazee.io/stats") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "14") - So(obj.Config.Healthcheck.Test, ShouldEqual, []string{"CMD-SHELL", "wget http://docker.amazee.io/stats -O /dev/null || exit 1"}) + So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "wget http://docker.amazee.io/stats -O /dev/null || exit 1"}) So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) diff --git a/service/mailhog/mailhog_test.go b/service/mailhog/mailhog_test.go index 9dfd47b0..a61f0d76 100644 --- a/service/mailhog/mailhog_test.go +++ b/service/mailhog/mailhog_test.go @@ -28,7 +28,7 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.network"], ShouldEqual, "amazeeio-network") So(obj.Config.Labels["pygmy.url"], ShouldEqual, "http://mailhog.docker.amazee.io") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "15") - So(obj.Config.Healthcheck.Test, ShouldEqual, []string{"CMD-SHELL", "wget http://mailhog.docker.amazee.io -O /dev/null || exit 1"}) + So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "wget http://mailhog.docker.amazee.io -O /dev/null || exit 1"}) So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) diff --git a/service/ssh/agent/ssh_agent_test.go b/service/ssh/agent/ssh_agent_test.go index 61db5c03..dd7d6cdc 100644 --- a/service/ssh/agent/ssh_agent_test.go +++ b/service/ssh/agent/ssh_agent_test.go @@ -31,7 +31,7 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.network"], ShouldEqual, "amazeeio-network") So(obj.Config.Labels["pygmy.purpose"], ShouldEqual, "sshagent") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "30") - So(obj.Config.Healthcheck.Test, ShouldEqual, []string{"CMD-SHELL", "if [ ! \"$SSH_AUTH_SOCK\" = \"\" ]; then exit 0; else exit 1; fi;"}) + So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "if [ ! \"$SSH_AUTH_SOCK\" = \"\" ]; then exit 0; else exit 1; fi;"}) So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) From 305e995a2dcc951eaa05d59f876c0152abf53464 Mon Sep 17 00:00:00 2001 From: Karl Hepworth Date: Thu, 29 Oct 2020 21:01:21 +1100 Subject: [PATCH 3/4] Update tests to be more indicative of state Signed-off-by: Karl Hepworth --- service/dnsmasq/dnsmasq.go | 2 +- service/dnsmasq/dnsmasq_test.go | 2 +- service/haproxy/haproxy.go | 2 +- service/haproxy/haproxy_test.go | 2 +- service/mailhog/mailhog.go | 2 +- service/mailhog/mailhog_test.go | 2 +- service/ssh/agent/ssh_agent.go | 2 +- service/ssh/agent/ssh_agent_test.go | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/service/dnsmasq/dnsmasq.go b/service/dnsmasq/dnsmasq.go index e3b42318..87b4d832 100644 --- a/service/dnsmasq/dnsmasq.go +++ b/service/dnsmasq/dnsmasq.go @@ -23,7 +23,7 @@ func New() model.Service { "pygmy.weight": "13", }, Healthcheck: &container.HealthConfig{ - Test: []string{"CMD-SHELL", "stat /run/dnsmasq.pid || exit 1"}, + Test: []string{"CMD-SHELL", "stat /run/dnsmasq.pid"}, Interval: 30000000000, Timeout: 5000000000, StartPeriod: 5000000000, diff --git a/service/dnsmasq/dnsmasq_test.go b/service/dnsmasq/dnsmasq_test.go index 387ce191..70354ea2 100644 --- a/service/dnsmasq/dnsmasq_test.go +++ b/service/dnsmasq/dnsmasq_test.go @@ -23,7 +23,7 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.enable"], ShouldEqual, "true") So(obj.Config.Labels["pygmy.name"], ShouldEqual, "amazeeio-dnsmasq") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "13") - So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "stat /run/dnsmasq.pid || exit 1"}) + So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "stat /run/dnsmasq.pid"}) So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) diff --git a/service/haproxy/haproxy.go b/service/haproxy/haproxy.go index f665c926..795f7ee5 100644 --- a/service/haproxy/haproxy.go +++ b/service/haproxy/haproxy.go @@ -21,7 +21,7 @@ func New() model.Service { "pygmy.weight": "14", }, Healthcheck: &container.HealthConfig{ - Test: []string{"CMD-SHELL", "wget http://docker.amazee.io/stats -O /dev/null || exit 1"}, + Test: []string{"CMD-SHELL", " wget http://mailhog.docker.amazee.io -q -S -O - 2>&1 | grep email"}, Interval: 30000000000, Timeout: 5000000000, StartPeriod: 5000000000, diff --git a/service/haproxy/haproxy_test.go b/service/haproxy/haproxy_test.go index 01bfa173..f67696ed 100644 --- a/service/haproxy/haproxy_test.go +++ b/service/haproxy/haproxy_test.go @@ -25,7 +25,7 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.network"], ShouldEqual, "amazeeio-network") So(obj.Config.Labels["pygmy.url"], ShouldEqual, "http://docker.amazee.io/stats") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "14") - So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "wget http://docker.amazee.io/stats -O /dev/null || exit 1"}) + So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "wget http://docker.amazee.io/stats -q -S -O - 2>&1 | grep docker.amazee.io"}) So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) diff --git a/service/mailhog/mailhog.go b/service/mailhog/mailhog.go index f5e0ac25..bb5e6c85 100644 --- a/service/mailhog/mailhog.go +++ b/service/mailhog/mailhog.go @@ -33,7 +33,7 @@ func New() model.Service { "pygmy.weight": "15", }, Healthcheck: &container.HealthConfig{ - Test: []string{"CMD-SHELL", "wget http://mailhog.docker.amazee.io -O /dev/null || exit 1"}, + Test: []string{"CMD-SHELL", "wget http://mailhog.docker.amazee.io -q -S -O - 2>&1 | grep email"}, Interval: 30000000000, Timeout: 5000000000, StartPeriod: 5000000000, diff --git a/service/mailhog/mailhog_test.go b/service/mailhog/mailhog_test.go index a61f0d76..3b742e9a 100644 --- a/service/mailhog/mailhog_test.go +++ b/service/mailhog/mailhog_test.go @@ -28,7 +28,7 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.network"], ShouldEqual, "amazeeio-network") So(obj.Config.Labels["pygmy.url"], ShouldEqual, "http://mailhog.docker.amazee.io") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "15") - So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "wget http://mailhog.docker.amazee.io -O /dev/null || exit 1"}) + So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "wget http://mailhog.docker.amazee.io -q -S -O - 2>&1 | grep email"}) So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) diff --git a/service/ssh/agent/ssh_agent.go b/service/ssh/agent/ssh_agent.go index 5dbcef2c..0bf14394 100644 --- a/service/ssh/agent/ssh_agent.go +++ b/service/ssh/agent/ssh_agent.go @@ -24,7 +24,7 @@ func New() model.Service { "pygmy.weight": "30", }, Healthcheck: &container.HealthConfig{ - Test: []string{"CMD-SHELL", "if [ ! \"$SSH_AUTH_SOCK\" = \"\" ]; then exit 0; else exit 1; fi;"}, + Test: []string{"CMD-SHELL", "stat $SSH_AUTH_SOCK"}, Interval: 30000000000, Timeout: 5000000000, StartPeriod: 5000000000, diff --git a/service/ssh/agent/ssh_agent_test.go b/service/ssh/agent/ssh_agent_test.go index dd7d6cdc..789ac485 100644 --- a/service/ssh/agent/ssh_agent_test.go +++ b/service/ssh/agent/ssh_agent_test.go @@ -31,7 +31,7 @@ func Test(t *testing.T) { So(obj.Config.Labels["pygmy.network"], ShouldEqual, "amazeeio-network") So(obj.Config.Labels["pygmy.purpose"], ShouldEqual, "sshagent") So(obj.Config.Labels["pygmy.weight"], ShouldEqual, "30") - So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "if [ ! \"$SSH_AUTH_SOCK\" = \"\" ]; then exit 0; else exit 1; fi;"}) + So(obj.Config.Healthcheck.Test, ShouldResemble, []string{"CMD-SHELL", "stat $SSH_AUTH_SOCK"}) So(obj.Config.Healthcheck.Interval, ShouldEqual, 30000000000) So(obj.Config.Healthcheck.Timeout, ShouldEqual, 5000000000) So(obj.Config.Healthcheck.StartPeriod, ShouldEqual, 5000000000) From 2a24589f577aeb52b7145579c44d5e9dbacd5ace Mon Sep 17 00:00:00 2001 From: Karl Hepworth Date: Fri, 30 Oct 2020 22:03:36 +1100 Subject: [PATCH 4/4] Update haproxy test URL Signed-off-by: Karl Hepworth --- service/haproxy/haproxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/haproxy/haproxy.go b/service/haproxy/haproxy.go index 795f7ee5..1ef713eb 100644 --- a/service/haproxy/haproxy.go +++ b/service/haproxy/haproxy.go @@ -21,7 +21,7 @@ func New() model.Service { "pygmy.weight": "14", }, Healthcheck: &container.HealthConfig{ - Test: []string{"CMD-SHELL", " wget http://mailhog.docker.amazee.io -q -S -O - 2>&1 | grep email"}, + Test: []string{"CMD-SHELL", "wget http://docker.amazee.io/stats -q -S -O - 2>&1 | grep docker.amazee.io"}, Interval: 30000000000, Timeout: 5000000000, StartPeriod: 5000000000,