forked from m4ksio/testingdock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_test.go
158 lines (138 loc) · 4.21 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package testingdock_test
import (
"context"
"database/sql"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
_ "github.com/lib/pq"
"github.com/dapperlabs/testingdock"
)
func TestContainer_Start(t *testing.T) {
// set up docker client
cli, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
t.Fatalf("error creating docker client: %s", err.Error())
}
// create suite
name := "TestContainer_Start"
s, ok := testingdock.GetOrCreateSuite(t, name, testingdock.SuiteOpts{
Client: cli,
})
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",
Env: []string{"POSTGRES_HOST_AUTH_METHOD=trust"},
},
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",
Env: []string{"POSTGRES_HOST_AUTH_METHOD=trust"},
},
})
// 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 func() {
s.Close()
_ = s.Remove()
}()
// test stuff within the database
testQueries(t, db)
s.Reset(context.TODO())
testQueries(t, db)
if err = s.Close(); err != nil {
t.Fatalf("could not close containers: %s", err.Error())
}
list0, err := cli.ContainerList(context.TODO(), types.ContainerListOptions{All: true})
if err != nil {
t.Fatalf("could not retreive container list: %s", err.Error())
}
if err = s.Remove(); err != nil {
t.Fatalf("could not remove containers: %s", err.Error())
}
list1, err := cli.ContainerList(context.TODO(), types.ContainerListOptions{All: true})
if err != nil {
t.Fatalf("could not retreive container list: %s", err.Error())
}
if len(list0) != len(list1)+3 {
t.Fatalf("expected Remove to remove 3 containers from container list (len before %v, len after %v)", len(list0),
len(list1))
}
}
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())
}
}