diff --git a/cmd/server/main.go b/cmd/server/main.go index 3425430..e5bf6f3 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "log/slog" "net/http" @@ -41,5 +42,6 @@ func main() { WriteTimeout: 10 * time.Second, } + fmt.Println("server started") log.Fatal(server.ListenAndServe()) } diff --git a/cmd/server/main_test.go b/cmd/server/main_test.go index 3081da1..fb32110 100644 --- a/cmd/server/main_test.go +++ b/cmd/server/main_test.go @@ -14,12 +14,18 @@ func TestServer(t *testing.T) { } port := "3000" - driver, err := web.NewWebDriver(port) + + baseURL, err := tests.StartDockerServer(t, port) + + if err != nil { + t.Fatal(err) + } + + driver, err := web.NewWebDriver(baseURL) if err != nil { t.Fatal("failed creating web driver:", err.Error()) } - tests.StartDockerServer(t, port) specifications.AuthSpecification(t, driver) } diff --git a/tests/adapters/web/driver.go b/tests/adapters/web/driver.go index df98967..905556f 100644 --- a/tests/adapters/web/driver.go +++ b/tests/adapters/web/driver.go @@ -1,7 +1,6 @@ package web import ( - "fmt" "net/http" "testing" "time" @@ -15,7 +14,7 @@ type Driver struct { browser *rod.Browser } -func NewWebDriver(port string) (*Driver, error) { +func NewWebDriver(baseURL string) (*Driver, error) { browser := rod.New() err := browser.Connect() @@ -24,7 +23,7 @@ func NewWebDriver(port string) (*Driver, error) { } return &Driver{ - BaseURL: fmt.Sprintf("http://localhost:%s", port), + BaseURL: "http://" + baseURL, Client: &http.Client{ Timeout: 1 * time.Second, }, diff --git a/tests/docker.go b/tests/docker.go index 8e9ecbe..691d3a3 100644 --- a/tests/docker.go +++ b/tests/docker.go @@ -1,46 +1,79 @@ package tests import ( + "bytes" "context" "fmt" + "io" + "net/http" "testing" "time" - "github.com/docker/go-connections/nat" "github.com/stretchr/testify/assert" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" ) const ( - startupTimeout = 5 * time.Second + startupTimeout = 1 * time.Minute dockerFilePath = "docker/server/Dockerfile" ) +type StdoutLogConsumer struct{} + +func (lc *StdoutLogConsumer) Accept(l testcontainers.Log) { + fmt.Print(string(l.Content)) +} + func StartDockerServer( t testing.TB, port string, -) { +) (string, error) { t.Helper() + g := StdoutLogConsumer{} + ctx := context.Background() req := testcontainers.ContainerRequest{ FromDockerfile: newTCDockerfile(), - ExposedPorts: []string{fmt.Sprintf("%s:%s", port, port)}, - WaitingFor: wait.ForListeningPort(nat.Port(port)).WithStartupTimeout(startupTimeout), + ExposedPorts: []string{port}, + WaitingFor: wait.ForLog("server started").WithStartupTimeout(startupTimeout), Env: map[string]string{ "LOCAL_DB": "true", }, + LogConsumerCfg: &testcontainers.LogConsumerConfig{ + Opts: []testcontainers.LogProductionOption{testcontainers.WithLogProductionTimeout(10 * time.Second)}, + Consumers: []testcontainers.LogConsumer{&g}, + }, } container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ ContainerRequest: req, Started: true, }) + assert.NoError(t, err) + + baseURL, err := container.Endpoint(ctx, "") + assert.NoError(t, err) + + res, err := http.Get("http://" + baseURL) + assert.NoError(t, err) + + if res.StatusCode != http.StatusOK { + body, err := io.ReadAll(res.Body) + if err != nil { + t.Fatal(err) + } + body = bytes.TrimSpace(body) + + return "", fmt.Errorf("received failing status code: %v\n%v", res.StatusCode, body) + } assert.NoError(t, err) t.Cleanup(func() { assert.NoError(t, container.Terminate(ctx)) }) + + return baseURL, nil } func newTCDockerfile() testcontainers.FromDockerfile {