forked from piotrkowalczuk/testingdock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
container_test.go
117 lines (103 loc) · 3.09 KB
/
container_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
107
108
109
110
111
112
113
114
115
116
117
package testingdock_test
import (
"context"
"database/sql"
"testing"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
_ "github.com/lib/pq"
"github.com/m4ksio/testingdock"
)
func TestContainer_Start(t *testing.T) {
// create suite
name := "TestContainer_Start"
s, ok := testingdock.GetOrCreateSuite(t, name, testingdock.SuiteOpts{})
if ok {
t.Fatal("this suite should not exists yet")
}
// create network
n := s.Network(testingdock.NetworkOpts{
Name: name,
})
// create postgres and mnemosyne configurations
postgresPort := testingdock.RandomPort(t)
mnemosynePort := testingdock.RandomPort(t)
mnemosyneDebugPort := testingdock.RandomPort(t)
db, err := sql.Open("postgres", "postgres://postgres:@localhost:"+postgresPort+"?sslmode=disable")
if err != nil {
t.Fatalf("database connection error: %s", err.Error())
}
postgres := s.Container(testingdock.ContainerOpts{
Name: "postgres",
ForcePull: false,
Config: &container.Config{
Image: "postgres:9.6",
},
HostConfig: &container.HostConfig{
PortBindings: nat.PortMap{
nat.Port("5432/tcp"): []nat.PortBinding{
{
HostPort: postgresPort,
},
},
},
},
HealthCheck: testingdock.HealthCheckCustom(db.Ping),
Reset: testingdock.ResetCustom(func() error {
_, err := db.Exec(`
DROP SCHEMA public CASCADE;
DROP SCHEMA mnemosyne CASCADE;
CREATE SCHEMA public;
`)
return err
}),
})
mnemosyned := s.Container(testingdock.ContainerOpts{
Name: "mnemosyned",
ForcePull: true,
Config: &container.Config{
Image: "piotrkowalczuk/mnemosyne:v0.8.4",
},
HostConfig: &container.HostConfig{
PortBindings: nat.PortMap{
nat.Port("8080/tcp"): []nat.PortBinding{{HostPort: mnemosynePort}},
nat.Port("8081/tcp"): []nat.PortBinding{{HostPort: mnemosyneDebugPort}},
},
},
HealthCheck: testingdock.HealthCheckHTTP("http://localhost:" + mnemosyneDebugPort + "/health"),
})
randomPostgres := s.Container(testingdock.ContainerOpts{
Name: "randomPostgres",
ForcePull: true,
Config: &container.Config{
Image: "postgres:9.6",
},
})
// add postgres to the test network
n.After(postgres)
// add another postgres to the test network
n.After(randomPostgres)
// start mnemosyned after postgres, this also adds it to the test network
postgres.After(mnemosyned)
// start the network, this also starts the containers
s.Start(context.TODO())
defer s.Close()
// test stuff within the database
testQueries(t, db)
s.Reset(context.TODO())
testQueries(t, db)
}
func testQueries(t *testing.T, db *sql.DB) {
_, err := db.ExecContext(context.TODO(), "CREATE TABLE public.example (name TEXT);")
if err != nil {
t.Fatalf("table creation error: %s", err.Error())
}
_, err = db.ExecContext(context.TODO(), "INSERT INTO public.example (name) VALUES ('anything')")
if err != nil {
t.Fatalf("insert error: %s", err.Error())
}
_, err = db.ExecContext(context.TODO(), "INSERT INTO mnemosyne.session (access_token, refresh_token,subject_id, bag) VALUES ('123', '123', '1', '{}')")
if err != nil {
t.Fatalf("insert error: %s", err.Error())
}
}