-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathpgtest.go
More file actions
188 lines (172 loc) · 5.44 KB
/
pgtest.go
File metadata and controls
188 lines (172 loc) · 5.44 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright 2018- The Pixie Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package pgtest
import (
"bufio"
"fmt"
"os"
"regexp"
"time"
"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/database/postgres"
bindata "github.com/golang-migrate/migrate/source/go_bindata"
"github.com/jmoiron/sqlx"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"px.dev/pixie/src/shared/services/pg"
)
// selfContainerID returns the current Docker container ID by parsing
// /proc/self/mountinfo. Docker bind-mounts /etc/hostname from
// /var/lib/docker/containers/<id>/hostname, exposing the container ID.
// Returns empty string if not running inside a Docker container.
func selfContainerID() string {
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return ""
}
defer f.Close()
re := regexp.MustCompile(`/containers/([a-f0-9]{64})/hostname`)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if m := re.FindStringSubmatch(scanner.Text()); m != nil {
return m[1]
}
}
return ""
}
// SetupTestDB sets up a test database instance and applies migrations.
func SetupTestDB(schemaSource *bindata.AssetSource) (*sqlx.DB, func(), error) {
var db *sqlx.DB
pool, err := dockertest.NewPool("")
if err != nil {
return nil, nil, fmt.Errorf("connect to docker failed: %w", err)
}
const dbName = "testdb"
resource, err := pool.RunWithOptions(
&dockertest.RunOptions{
Repository: "postgres",
Tag: "13.3",
Env: []string{"POSTGRES_PASSWORD=secret", "POSTGRES_DB=" + dbName},
}, func(config *docker.HostConfig) {
config.AutoRemove = true
config.RestartPolicy = docker.RestartPolicy{Name: "no"}
config.Mounts = []docker.HostMount{
{
Target: "/var/lib/postgresql/data",
Type: "tmpfs",
TempfsOptions: &docker.TempfsOptions{
SizeBytes: 100 * 1024 * 1024,
},
},
}
config.CPUCount = 1
config.Memory = 512 * 1024 * 1024
config.MemorySwap = 0
config.MemorySwappiness = 0
},
)
if err != nil {
return nil, nil, fmt.Errorf("Failed to run docker pool: %w", err)
}
// Set a 15 minute expiration on resources (extended for debugging).
err = resource.Expire(900)
if err != nil {
return nil, nil, err
}
// When running inside a container (e.g. CI), the postgres container is on
// a different Docker network and we can't reach it via host port mapping.
// Detect this and connect postgres to our network instead.
pgHost := resource.Container.NetworkSettings.Gateway
pgPort := resource.GetPort("5432/tcp")
selfID := selfContainerID()
log.Infof("selfContainerID: %q", selfID)
if selfID != "" {
selfContainer, err := pool.Client.InspectContainer(selfID)
if err != nil {
return nil, nil, fmt.Errorf("failed to inspect self container %s: %w", selfID, err)
}
for netName, net := range selfContainer.NetworkSettings.Networks {
if netName == "host" {
continue
}
err := pool.Client.ConnectNetwork(net.NetworkID, docker.NetworkConnectionOptions{
Container: resource.Container.ID,
})
if err != nil {
return nil, nil, fmt.Errorf("failed to connect postgres to network %s: %w", netName, err)
}
// Re-inspect to get the postgres container's IP on our network.
updated, err := pool.Client.InspectContainer(resource.Container.ID)
if err != nil {
return nil, nil, fmt.Errorf("failed to re-inspect postgres container: %w", err)
}
resource.Container = updated
if pgNet, ok := updated.NetworkSettings.Networks[netName]; ok {
pgHost = pgNet.IPAddress
pgPort = "5432"
log.Infof("pgHost set to %s:%s via network %s", pgHost, pgPort, netName)
}
break
}
}
if pgHost == "" {
pgHost = "localhost"
}
viper.Set("postgres_port", pgPort)
viper.Set("postgres_hostname", pgHost)
viper.Set("postgres_db", dbName)
viper.Set("postgres_username", "postgres")
viper.Set("postgres_password", "secret")
pool.MaxWait = 10 * time.Minute
if err = pool.Retry(func() error {
log.Info("trying to connect")
db = pg.MustCreateDefaultPostgresDB()
return db.Ping()
}); err != nil {
return nil, nil, fmt.Errorf("failed to create postgres on docker: %w", err)
}
driver, err := postgres.WithInstance(db.DB, &postgres.Config{})
if err != nil {
return nil, nil, fmt.Errorf("failed to get postgres driver: %w", err)
}
if schemaSource != nil {
d, err := bindata.WithInstance(schemaSource)
if err != nil {
return nil, nil, fmt.Errorf("failed to load schema: %w", err)
}
mg, err := migrate.NewWithInstance(
"go-bindata",
d, "postgres", driver)
if err != nil {
return nil, nil, fmt.Errorf("failed to load migrations: %w", err)
}
if err = mg.Up(); err != nil {
return nil, nil, fmt.Errorf("migrations failed: %w", err)
}
}
return db, func() {
if db != nil {
db.Close()
}
if err := pool.Purge(resource); err != nil {
log.WithError(err).Error("could not purge docker resource")
}
}, nil
}