diff --git a/compose/parser.go b/compose/parser.go index 7dcfd7e..5ae754a 100644 --- a/compose/parser.go +++ b/compose/parser.go @@ -60,6 +60,7 @@ func (p *Parser) Parse(appname string) { parseEnv(s) parseCommand(s) parseEnvFiles(s) + parseHealthCheck(s) } c := p.Data @@ -212,18 +213,27 @@ func parseEnvFiles(s *Service) { func parseHealthCheck(s *Service) { // HealthCheck command can be a string or slice of strings + if s.HealthCheck == nil { + return + } if s.HealthCheck.RawTest == nil { return } + switch v := s.HealthCheck.RawTest.(type) { case string: - var err error - s.HealthCheck.Test, err = shlex.Split(v) + c, err := shlex.Split(v) if err != nil { log.Fatal(err) } + s.HealthCheck = &HealthCheck{ + Test: c, + } + case []string: - s.HealthCheck.Test = v + s.HealthCheck = &HealthCheck{ + Test: v, + } case []interface{}: for _, v := range v { diff --git a/compose/parser_test.go b/compose/parser_test.go index b6f5b74..904b00a 100644 --- a/compose/parser_test.go +++ b/compose/parser_test.go @@ -40,6 +40,22 @@ services: image: foo command: echo "hello world" + hc1: + image: foo + healthcheck: + test: ["CMD-SHELL", "echo 'hello world1'"] + + hc2: + image: foo + healthcheck: + test: echo "hello world2" + + hc3: + image: foo + healthcheck: + test: ["CMD", "echo 'hello world3'"] + + ` func init() { @@ -136,3 +152,48 @@ func TestParseCommand(t *testing.T) { } } } + +func TestHealthChecks(t *testing.T) { + p := NewParser("", DOCKER_COMPOSE_YML1) + p.Parse("test") + + for name, s := range p.Data.Services { + if name != "hc1" && name != "hc2" && name != "hc3" { + continue + } + + if name == "hc1" { + if len(s.HealthCheck.Test) != 2 { + t.Errorf("Expected 2 healthcheck tests, got %d", len(s.HealthCheck.Test)) + } + if s.HealthCheck.Test[0] != "CMD-SHELL" { + t.Errorf("Expected CMD-SHELL, got %s", s.HealthCheck.Test[0]) + } + if s.HealthCheck.Test[1] != "echo 'hello world1'" { + t.Errorf("Expected echo 'hello world1', got %s", s.HealthCheck.Test[1]) + } + } + if name == "hc2" { + if len(s.HealthCheck.Test) != 2 { + t.Errorf("Expected 2 healthcheck tests, got %d", len(s.HealthCheck.Test)) + } + if s.HealthCheck.Test[0] != "echo" { + t.Errorf("Expected echo, got %s", s.HealthCheck.Test[1]) + } + if s.HealthCheck.Test[1] != "hello world2" { + t.Errorf("Expected echo 'hello world2', got %s", s.HealthCheck.Test[1]) + } + } + if name == "hc3" { + if len(s.HealthCheck.Test) != 2 { + t.Errorf("Expected 2 healthcheck tests, got %d", len(s.HealthCheck.Test)) + } + if s.HealthCheck.Test[0] != "CMD" { + t.Errorf("Expected CMD, got %s", s.HealthCheck.Test[0]) + } + if s.HealthCheck.Test[1] != "echo 'hello world3'" { + t.Errorf("Expected echo 'hello world3', got %s", s.HealthCheck.Test[1]) + } + } + } +} diff --git a/generator/main.go b/generator/main.go index 8c1f9aa..e23bd47 100644 --- a/generator/main.go +++ b/generator/main.go @@ -607,10 +607,14 @@ func prepareProbes(name string, s *compose.Service, container *helm.Container) { Command: c, } } - } else if s.HealthCheck.Test[0] == "CMD" { + } else if s.HealthCheck.Test[0] == "CMD" || s.HealthCheck.Test[0] == "CMD-SHELL" { probe.Exec = &helm.Exec{ Command: s.HealthCheck.Test[1:], } + } else { + probe.Exec = &helm.Exec{ + Command: s.HealthCheck.Test, + } } container.LivenessProbe = probe }