-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
104 lines (84 loc) · 2.79 KB
/
util.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
package test
import (
"database/sql"
"fmt"
"time"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/pkg/errors"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/code-payments/code-server/pkg/retry"
"github.com/code-payments/code-server/pkg/retry/backoff"
)
const (
containerName = "postgres"
containerVersion = "10.4"
containerAutoKill = 120 // seconds
port = 5432
user = "localtest"
password = "localpassword"
dbname = "testdb"
)
const (
postgresUserEnv = "POSTGRES_USER=" + user
postgresPasswordEnv = "POSTGRES_PASSWORD=" + password
postgresDbEnv = "POSTGRES_DB=" + dbname
)
// StartPostgresDB starts a Docker container using the postgres image and returns a postgres client for testing purposes.
func StartPostgresDB(pool *dockertest.Pool) (db *sql.DB, closeFunc func(), err error) {
closeFunc = func() {}
// Pulls the image, creates a container based on it and runs it
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: containerName,
Tag: containerVersion,
Env: []string{
"listen_addresses = '*'",
postgresUserEnv,
postgresPasswordEnv,
postgresDbEnv,
},
}, func(config *docker.HostConfig) {
// set AutoRemove to true so that stopped container goes away by itself
config.AutoRemove = true
config.RestartPolicy = docker.RestartPolicy{Name: "no"}
})
// Check if the container resource was generated as expected
if err != nil {
return nil, closeFunc, errors.Wrapf(err, "failed to start resource")
}
// Uncomment this to view docker container logs (note: this will fully consume os.Stdout)
/*
opts := docker.LogsOptions{
Context: context.Background(),
Stderr: true,
Stdout: true,
Follow: true,
Timestamps: true,
RawTerminal: true,
Container: resource.Container.ID,
OutputStream: os.Stdout,
}
pool.Client.Logs(opts)
*/
hostAndPort := resource.GetHostPort(fmt.Sprintf("%d/tcp", port))
databaseUrl := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", user, password, hostAndPort, dbname)
// logrus.StandardLogger().Println("Connecting to database on url: ", databaseUrl)
// logrus.StandardLogger().Println("Setting container auto-kill to: ", containerAutoKill, " seconds")
// You may need to adjust this number if it is too low for your test environment.
resource.Expire(containerAutoKill) // Tell docker to hard kill the container in 120 seconds
_, err = retry.Retry(
func() error {
db, err = sql.Open("pgx", databaseUrl)
if err != nil {
return err
}
return db.Ping()
},
retry.Limit(50),
retry.Backoff(backoff.Constant(500*time.Millisecond), 500*time.Second),
)
if err != nil {
return nil, closeFunc, errors.Wrap(err, "timed out waiting for postgres container to become available")
}
return db, closeFunc, nil
}