Skip to content

Commit

Permalink
show container logs when expected port isn't available
Browse files Browse the repository at this point in the history
  • Loading branch information
Frankie Gallina-Jones authored and sophiewigmore committed Aug 15, 2022
1 parent 6807938 commit d3b4e1a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion matchers/serve.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package matchers

import (
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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))
Expand Down
11 changes: 10 additions & 1 deletion matchers/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
})
})
Expand Down

0 comments on commit d3b4e1a

Please sign in to comment.