From d3b4e1ae039828956f86f116e11683b282d74fd6 Mon Sep 17 00:00:00 2001 From: Frankie Gallina-Jones Date: Thu, 11 Aug 2022 17:47:20 -0400 Subject: [PATCH] show container logs when expected port isn't available --- matchers/serve.go | 7 ++++++- matchers/serve_test.go | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/matchers/serve.go b/matchers/serve.go index 53593c5..9b7b4b4 100644 --- a/matchers/serve.go +++ b/matchers/serve.go @@ -1,6 +1,7 @@ package matchers import ( + "errors" "fmt" "io" "net/http" @@ -81,7 +82,11 @@ func (sm *ServeMatcher) Match(actual interface{}) (success bool, err error) { if _, ok := container.Ports[port]; !ok { // EITHER: you have multiple ports and didn't specify OR you specified a bad port - return false, fmt.Errorf("ServeMatcher looking for response from container port %s which is not in container port map", port) + message := fmt.Sprintf("ServeMatcher looking for response from container port %s which is not in container port map", port) + if logs, _ := sm.docker.Container.Logs.Execute(container.ID); logs != nil { + message = fmt.Sprintf("%s\n\nContainer logs:\n\n%s", message, logs) + } + return false, errors.New(message) } response, err := sm.client.Get(fmt.Sprintf("http://%s:%s%s", container.Host(), container.HostPort(port), sm.endpoint)) diff --git a/matchers/serve_test.go b/matchers/serve_test.go index 202afc9..89d2fc8 100644 --- a/matchers/serve_test.go +++ b/matchers/serve_test.go @@ -207,7 +207,15 @@ func testServe(t *testing.T, context spec.G, it spec.S) { context("failure cases", func() { context("the port is not in the container port mapping", func() { it.Before(func() { - matcher = matcher.OnPort(8080) + executable := &fakes.Executable{} + executable.ExecuteCall.Stub = func(execution pexec.Execution) error { + fmt.Fprintln(execution.Stdout, "some logs") + return nil + } + + docker := occam.NewDocker().WithExecutable(executable) + + matcher = matchers.Serve("some string").WithDocker(docker).OnPort(8080) }) it("returns an error", func() { @@ -216,6 +224,7 @@ func testServe(t *testing.T, context spec.G, it spec.S) { Env: map[string]string{"PORT": "8080"}, }) Expect(err).To(MatchError(ContainSubstring("ServeMatcher looking for response from container port 8080 which is not in container port map"))) + Expect(err).To(MatchError(ContainSubstring("Container logs:\n\nsome logs\n"))) Expect(result).To(BeFalse()) }) })