|
| 1 | +package loopdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "fmt" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + _ "github.com/lib/pq" |
| 13 | + "github.com/ory/dockertest/v3" |
| 14 | + "github.com/ory/dockertest/v3/docker" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + testPgUser = "test" |
| 20 | + testPgPass = "test" |
| 21 | + testPgDBName = "test" |
| 22 | + PostgresTag = "11" |
| 23 | +) |
| 24 | + |
| 25 | +// TestPgFixture is a test fixture that starts a Postgres 11 instance in a |
| 26 | +// docker container. |
| 27 | +type TestPgFixture struct { |
| 28 | + db *sql.DB |
| 29 | + pool *dockertest.Pool |
| 30 | + resource *dockertest.Resource |
| 31 | + host string |
| 32 | + port int |
| 33 | +} |
| 34 | + |
| 35 | +// NewTestPgFixture constructs a new TestPgFixture starting up a docker |
| 36 | +// container running Postgres 11. The started container will expire in after |
| 37 | +// the passed duration. |
| 38 | +func NewTestPgFixture(t *testing.T, expiry time.Duration) *TestPgFixture { |
| 39 | + // Use a sensible default on Windows (tcp/http) and linux/osx (socket) |
| 40 | + // by specifying an empty endpoint. |
| 41 | + pool, err := dockertest.NewPool("") |
| 42 | + require.NoError(t, err, "Could not connect to docker") |
| 43 | + |
| 44 | + // Pulls an image, creates a container based on it and runs it. |
| 45 | + resource, err := pool.RunWithOptions(&dockertest.RunOptions{ |
| 46 | + Repository: "postgres", |
| 47 | + Tag: PostgresTag, |
| 48 | + Env: []string{ |
| 49 | + fmt.Sprintf("POSTGRES_USER=%v", testPgUser), |
| 50 | + fmt.Sprintf("POSTGRES_PASSWORD=%v", testPgPass), |
| 51 | + fmt.Sprintf("POSTGRES_DB=%v", testPgDBName), |
| 52 | + "listen_addresses='*'", |
| 53 | + }, |
| 54 | + Cmd: []string{ |
| 55 | + "postgres", |
| 56 | + "-c", "log_statement=all", |
| 57 | + "-c", "log_destination=stderr", |
| 58 | + }, |
| 59 | + }, func(config *docker.HostConfig) { |
| 60 | + // Set AutoRemove to true so that stopped container goes away |
| 61 | + // by itself. |
| 62 | + config.AutoRemove = true |
| 63 | + config.RestartPolicy = docker.RestartPolicy{Name: "no"} |
| 64 | + }) |
| 65 | + require.NoError(t, err, "Could not start resource") |
| 66 | + |
| 67 | + hostAndPort := resource.GetHostPort("5432/tcp") |
| 68 | + parts := strings.Split(hostAndPort, ":") |
| 69 | + host := parts[0] |
| 70 | + port, err := strconv.ParseInt(parts[1], 10, 64) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + fixture := &TestPgFixture{ |
| 74 | + host: host, |
| 75 | + port: int(port), |
| 76 | + } |
| 77 | + databaseURL := fixture.GetDSN() |
| 78 | + log.Infof("Connecting to Postgres fixture: %v\n", databaseURL) |
| 79 | + |
| 80 | + // Tell docker to hard kill the container in "expiry" seconds. |
| 81 | + require.NoError(t, resource.Expire(uint(expiry.Seconds()))) |
| 82 | + |
| 83 | + // Exponential backoff-retry, because the application in the container |
| 84 | + // might not be ready to accept connections yet. |
| 85 | + pool.MaxWait = 120 * time.Second |
| 86 | + |
| 87 | + var testDB *sql.DB |
| 88 | + err = pool.Retry(func() error { |
| 89 | + testDB, err = sql.Open("postgres", databaseURL) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + return testDB.Ping() |
| 94 | + }) |
| 95 | + require.NoError(t, err, "Could not connect to docker") |
| 96 | + |
| 97 | + // Now fill in the rest of the fixture. |
| 98 | + fixture.db = testDB |
| 99 | + fixture.pool = pool |
| 100 | + fixture.resource = resource |
| 101 | + |
| 102 | + return fixture |
| 103 | +} |
| 104 | + |
| 105 | +// GetDSN returns the DSN (Data Source Name) for the started Postgres node. |
| 106 | +func (f *TestPgFixture) GetDSN() string { |
| 107 | + return f.GetConfig().DSN(false) |
| 108 | +} |
| 109 | + |
| 110 | +// GetConfig returns the full config of the Postgres node. |
| 111 | +func (f *TestPgFixture) GetConfig() *PostgresConfig { |
| 112 | + return &PostgresConfig{ |
| 113 | + Host: f.host, |
| 114 | + Port: f.port, |
| 115 | + User: testPgUser, |
| 116 | + Password: testPgPass, |
| 117 | + DBName: testPgDBName, |
| 118 | + RequireSSL: false, |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// TearDown stops the underlying docker container. |
| 123 | +func (f *TestPgFixture) TearDown(t *testing.T) { |
| 124 | + err := f.pool.Purge(f.resource) |
| 125 | + require.NoError(t, err, "Could not purge resource") |
| 126 | +} |
| 127 | + |
| 128 | +// ClearDB clears the database. |
| 129 | +func (f *TestPgFixture) ClearDB(t *testing.T) { |
| 130 | + dbConn, err := sql.Open("postgres", f.GetDSN()) |
| 131 | + require.NoError(t, err) |
| 132 | + |
| 133 | + _, err = dbConn.ExecContext( |
| 134 | + context.Background(), |
| 135 | + `DROP SCHEMA IF EXISTS public CASCADE; |
| 136 | + CREATE SCHEMA public;`, |
| 137 | + ) |
| 138 | + require.NoError(t, err) |
| 139 | +} |
0 commit comments