-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredisBackend_docker_test.go
106 lines (84 loc) · 2.17 KB
/
redisBackend_docker_test.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//go:build integration
package httpcache_test
import (
"context"
"fmt"
"log"
"os"
"testing"
"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/httpcache"
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
var (
redisContainer testcontainers.Container
redisHost string
redisPort string
)
// TestMain set
func TestMain(m *testing.M) {
setup()
code := m.Run()
teardown() // comment out, if you want to keep the docker-instance running for debugging
os.Exit(code)
}
// setup the redis docker-container for integration tests
func setup() {
ctx := context.Background()
req := testcontainers.ContainerRequest{
Image: "redis:latest",
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForLog("Ready to accept connections"),
}
var err error
redisContainer, err = testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
log.Fatal(err)
}
port, err := redisContainer.MappedPort(ctx, "6379")
if err != nil {
log.Fatal(err)
}
redisPort = port.Port()
redisHost, err = redisContainer.Host(ctx)
if err != nil {
log.Fatal(err)
}
address := fmt.Sprintf("%s:%s", redisHost, port.Port())
conn, err := redis.Dial("tcp", address)
if err != nil {
log.Fatal(err)
}
_, err = conn.Do("PING")
if err != nil {
log.Fatal(err)
}
_ = conn.Close()
}
// teardown the redis docker-container
func teardown() {
err := redisContainer.Terminate(context.Background())
if err != nil {
log.Fatalf("Error purging docker resources: %s", err)
}
}
func Test_RunDefaultBackendTestCase_RedisBackend(t *testing.T) {
t.Parallel()
config := httpcache.RedisBackendConfig{
MaxIdle: 8,
IdleTimeOutSeconds: 30,
Host: redisHost,
Port: redisPort,
}
factory := httpcache.RedisBackendFactory{}
backend, err := factory.Inject(flamingo.NullLogger{}).SetConfig(config).SetFrontendName("testfrontend").Build()
assert.NoError(t, err)
testcase := NewBackendTestCase(t, backend, false)
testcase.RunTests()
}