/
postgres_setup.go
128 lines (102 loc) · 2.87 KB
/
postgres_setup.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
package docker
import (
"context"
"database/sql"
"fmt"
"io/ioutil"
"os"
"github.com/syncromatics/go-kit/database"
client "docker.io/go-docker"
"docker.io/go-docker/api/types"
"docker.io/go-docker/api/types/container"
"docker.io/go-docker/api/types/network"
)
var (
postgresImage = "postgres:10.2"
)
// SetupPostgresDatabase sets up a timescale database
func SetupPostgresDatabase(testName string) (*sql.DB, *database.PostgresDatabaseSettings, error) {
os.Setenv("DOCKER_API_VERSION", "1.35")
cli, err := client.NewEnvClient()
if err != nil {
return nil, nil, err
}
r, err := cli.ImagePull(context.Background(), postgresImage, types.ImagePullOptions{})
if err != nil {
return nil, nil, err
}
_, err = ioutil.ReadAll(r)
if err != nil {
return nil, nil, err
}
err = r.Close()
if err != nil {
return nil, nil, err
}
config := container.Config{
Image: postgresImage,
Env: []string{
"POSTGRES_PASSWORD=postgres",
"POSTGRES_USER=postgres",
"POSTGRES_DB=test",
},
}
hostConfig := container.HostConfig{}
networkConfig := network.NetworkingConfig{}
removePostgresContainer(cli, testName)
containerName := fmt.Sprintf("%s_postgres_db", testName)
create, err := cli.ContainerCreate(context.Background(), &config, &hostConfig, &networkConfig, containerName)
if err != nil {
return nil, nil, err
}
conChan, errChan := cli.ContainerWait(context.Background(), create.ID, container.WaitConditionNotRunning)
select {
case err = <-errChan:
return nil, nil, err
case <-conChan:
}
err = cli.ContainerStart(context.Background(), create.ID, types.ContainerStartOptions{})
if err != nil {
return nil, nil, err
}
db, settings, err := waitForPostgresToBeReady(cli, create.ID)
if err != nil {
return nil, nil, err
}
return db, settings, nil
}
// TeardownPostgresDatabase tears down the postgres db
func TeardownPostgresDatabase(testName string) error {
os.Setenv("DOCKER_API_VERSION", "1.35")
cli, err := client.NewEnvClient()
if err != nil {
return err
}
removePostgresContainer(cli, testName)
return nil
}
func removePostgresContainer(client *client.Client, testName string) {
containerName := fmt.Sprintf("%s_postgres_db", testName)
client.ContainerRemove(context.Background(), containerName, types.ContainerRemoveOptions{Force: true})
}
func waitForPostgresToBeReady(client *client.Client, id string) (*sql.DB, *database.PostgresDatabaseSettings, error) {
inspect, err := client.ContainerInspect(context.Background(), id)
if err != nil {
return nil, nil, err
}
settings := database.PostgresDatabaseSettings{
Host: inspect.NetworkSettings.IPAddress,
User: "postgres",
Password: "postgres",
Name: "test",
}
err = settings.WaitForDatabaseToBeOnline(60)
if err != nil {
return nil, nil, err
}
db, err := settings.EnsureDatabaseExistsAndGetConnection()
if err != nil {
return nil, nil, err
}
return db, &settings, nil
}