Skip to content

Commit

Permalink
fix action
Browse files Browse the repository at this point in the history
  • Loading branch information
m1yon committed Jul 1, 2024
1 parent 19f3f38 commit a5872f2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
2 changes: 2 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"log/slog"
"net/http"
Expand Down Expand Up @@ -41,5 +42,6 @@ func main() {
WriteTimeout: 10 * time.Second,
}

fmt.Println("server started")
log.Fatal(server.ListenAndServe())
}
10 changes: 8 additions & 2 deletions cmd/server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 2 additions & 3 deletions tests/adapters/web/driver.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package web

import (
"fmt"
"net/http"
"testing"
"time"
Expand All @@ -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()

Expand All @@ -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,
},
Expand Down
43 changes: 38 additions & 5 deletions tests/docker.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit a5872f2

Please sign in to comment.