summaryrefslogtreecommitdiff
path: root/test/integration/container_test.go
blob: ff2c9144c537d404f8943371644b125e60561a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//go:build integration
// +build integration

package test

import (
	"context"
	"net/http"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/wait"
	"github.com/xlgmokha/x/pkg/env"
)

func TestContainer(t *testing.T) {
	image := env.Fetch("IMAGE_TAG", "sparkled:invalid")
	require.NotEmpty(t, image)

	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
	defer cancel()

	container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
		ContainerRequest: testcontainers.ContainerRequest{
			Image:        image,
			Env:          map[string]string{"HMAC_SESSION_SECRET": "secret", "OAUTH_CLIENT_SECRET": "secret"},
			ExposedPorts: []string{"8080/tcp", "9901/tcp", "10000/tcp"},
			WaitingFor:   wait.ForLog("Listening on"),
		},
		Started: true,
	})
	require.NoError(t, err)

	defer func() {
		require.NoError(t, container.Terminate(context.Background()))

		testcontainers.CleanupContainer(t, container)
	}()

	endpoint, err := container.Endpoint(ctx, "")
	require.NoError(t, err)

	paths := []string{
		"/health",
		"/favicon.ico",
	}

	for _, path := range paths {
		t.Run(path, func(t *testing.T) {
			url := "http://" + endpoint + path
			client := &http.Client{Timeout: 5 * time.Second}
			request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
			response, err := client.Do(request)
			require.NoError(t, err)
			assert.Equal(t, http.StatusOK, response.StatusCode)
		})
	}
}