Skip to content

Commit

Permalink
fix: use of log.Fatal in main (#2739)
Browse files Browse the repository at this point in the history
Remove use of log fatal which prevents defers.

Correct order of Write and WriteHeader a update test to validate the
correct behaviour, leveraging require.
  • Loading branch information
stevenh authored Aug 21, 2024
1 parent 0a5cd8b commit 6ab1134
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
21 changes: 5 additions & 16 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,14 +737,10 @@ func TestContainerCreationWaitsForLog(t *testing.T) {
}

func Test_BuildContainerFromDockerfileWithBuildArgs(t *testing.T) {
t.Log("getting ctx")
ctx := context.Background()

t.Log("got ctx, creating container request")

// fromDockerfileWithBuildArgs {
ba := "build args value"

req := ContainerRequest{
FromDockerfile: FromDockerfile{
Context: filepath.Join(".", "testdata"),
Expand All @@ -770,23 +766,16 @@ func Test_BuildContainerFromDockerfileWithBuildArgs(t *testing.T) {
terminateContainerOnEnd(t, ctx, c)

ep, err := c.Endpoint(ctx, "http")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

resp, err := http.Get(ep + "/env")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, ba, string(body))
require.NoError(t, err)
require.Equal(t, http.StatusAccepted, resp.StatusCode)
require.Equal(t, ba, string(body))
}

func Test_BuildContainerFromDockerfileWithBuildLog(t *testing.T) {
Expand Down
11 changes: 5 additions & 6 deletions testdata/echoserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (

func envHandler() http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
_, _ = rw.Write([]byte(os.Getenv("FOO")))

rw.WriteHeader(http.StatusAccepted)
rw.Write([]byte(os.Getenv("FOO"))) //nolint:errcheck // Nothing we can usefully do with the error here.
}
}

Expand All @@ -21,9 +20,7 @@ func echoHandler(destination *os.File) http.HandlerFunc {
echo := req.URL.Query()["echo"][0]

l := log.New(destination, "echo ", 0)

l.Println(echo)
_ = destination.Sync()

rw.WriteHeader(http.StatusAccepted)
}
Expand All @@ -39,10 +36,12 @@ func main() {

ln, err := net.Listen("tcp", ":8080")
if err != nil {
panic(err)
log.Fatal(err)
}

fmt.Println("ready")

_ = http.Serve(ln, mux)
if err := http.Serve(ln, mux); err != nil {
log.Fatal(err)
}
}
15 changes: 13 additions & 2 deletions wait/testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"log"
"net/http"
Expand All @@ -15,7 +16,7 @@ import (
"time"
)

func main() {
func run() error {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -93,5 +94,15 @@ func main() {
log.Println("stopping...")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
_ = server.Shutdown(ctx)
if err := server.Shutdown(ctx); err != nil {
return fmt.Errorf("shutdown: %w", err)
}

return nil
}

func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}

0 comments on commit 6ab1134

Please sign in to comment.