Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: avoid using of log.Fatal in main in tests #2739

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}