-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgres_test.go
152 lines (122 loc) · 3.74 KB
/
postgres_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
//go:build integration
package integration_test
import (
"context"
"database/sql"
"fmt"
"testing"
"github.com/metal-stack/backup-restore-sidecar/pkg/generate/examples/examples"
"github.com/avast/retry-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "github.com/lib/pq"
)
func Test_Postgres_Restore(t *testing.T) {
restoreFlow(t, &flowSpec{
databaseType: examples.Postgres,
sts: examples.PostgresSts,
backingResources: examples.PostgresBackingResources,
addTestData: addPostgresTestData,
verifyTestData: verifyPostgresTestData,
})
}
func Test_Postgres_RestoreLatestFromMultipleBackups(t *testing.T) {
restoreLatestFromMultipleBackupsFlow(t, &flowSpec{
databaseType: examples.Postgres,
sts: examples.PostgresSts,
backingResources: examples.PostgresBackingResources,
addTestDataWithIndex: addPostgresTestDataWithIndex,
verifyTestDataWithIndex: verifyPostgresTestDataWithIndex,
})
}
func Test_Postgres_Upgrade(t *testing.T) {
upgradeFlow(t, &upgradeFlowSpec{
flowSpec: flowSpec{
databaseType: examples.Postgres,
sts: examples.PostgresSts,
backingResources: examples.PostgresBackingResources,
addTestData: addPostgresTestData,
verifyTestData: verifyPostgresTestData,
},
databaseImages: []string{
"postgres:12-alpine",
// "postgres:13-alpine", commented to test if two versions upgrade also work
"postgres:14-alpine",
"postgres:15-alpine",
},
})
}
func newPostgresSession(t *testing.T, ctx context.Context) *sql.DB {
var db *sql.DB
err := retry.Do(func() error {
connString := fmt.Sprintf("host=127.0.0.1 port=5432 user=%s password=%s dbname=%s sslmode=disable", examples.PostgresUser, examples.PostgresPassword, examples.PostgresDB)
var err error
db, err = sql.Open("postgres", connString)
if err != nil {
return err
}
err = db.PingContext(ctx)
if err != nil {
return err
}
return nil
}, retry.Context(ctx))
require.NoError(t, err)
return db
}
func addPostgresTestData(t *testing.T, ctx context.Context) {
db := newPostgresSession(t, ctx)
defer db.Close()
var (
createStmt = `CREATE TABLE backuprestore (
data text NOT NULL
);`
insertStmt = `INSERT INTO backuprestore("data") VALUES ('I am precious');`
)
_, err := db.Exec(createStmt)
require.NoError(t, err)
_, err = db.Exec(insertStmt)
require.NoError(t, err)
}
func addPostgresTestDataWithIndex(t *testing.T, ctx context.Context, index int) {
db := newPostgresSession(t, ctx)
defer db.Close()
var (
createStmt = `CREATE TABLE IF NOT EXISTS backuprestore (
data text NOT NULL
);`
insertStmt = fmt.Sprintf("INSERT INTO backuprestore (data) VALUES ('idx-%d');", index)
)
_, err := db.Exec(createStmt)
require.NoError(t, err)
_, err = db.Exec(insertStmt)
require.NoError(t, err)
}
func verifyPostgresTestDataWithIndex(t *testing.T, ctx context.Context, index int) {
db := newPostgresSession(t, ctx)
defer db.Close()
rows, err := db.Query(fmt.Sprintf("SELECT \"data\" FROM backuprestore WHERE data='idx-%d';", index))
require.NoError(t, err)
require.NoError(t, rows.Err())
defer rows.Close()
require.True(t, rows.Next())
var data string
err = rows.Scan(&data)
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("idx-%d", index), data)
assert.False(t, rows.Next())
}
func verifyPostgresTestData(t *testing.T, ctx context.Context) {
db := newPostgresSession(t, ctx)
defer db.Close()
rows, err := db.Query(`SELECT "data" FROM backuprestore;`)
require.NoError(t, err)
require.NoError(t, rows.Err())
defer rows.Close()
require.True(t, rows.Next())
var data string
err = rows.Scan(&data)
require.NoError(t, err)
assert.Equal(t, "I am precious", data)
assert.False(t, rows.Next())
}