Welcome to mirror list, hosted at ThFree Co, Russian Federation.

postgres_test.go « glsql « datastore « praefect « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c28c88062095e8dc0ce9245d697edcc5a8cc2ff8 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package glsql_test

import (
	"context"
	"fmt"
	"net"
	"testing"
	"time"

	"github.com/jackc/pgconn"
	migrate "github.com/rubenv/sql-migrate"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/internal/praefect/config"
	"gitlab.com/gitlab-org/gitaly/internal/praefect/datastore/glsql"
	"gitlab.com/gitlab-org/gitaly/internal/praefect/datastore/migrations"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper/testdb"
)

func TestOpenDB(t *testing.T) {
	dbCfg := testdb.GetConfig(t, "postgres")
	ctx := testhelper.Context(t)

	t.Run("failed to ping because of incorrect config", func(t *testing.T) {
		badCfg := dbCfg
		badCfg.Host = "not-existing.com"
		_, err := glsql.OpenDB(ctx, badCfg)
		require.Error(t, err)
		// The regexp is used because error message has a diff in local run an on CI.
		const errRegexp = "send ping: failed to connect to `host=not\\-existing.com user=.* database=.*`: hostname resolving error"
		require.Regexp(t, errRegexp, err.Error(), "opening of DB with incorrect configuration must fail")
	})

	t.Run("timeout on hanging connection attempt", func(t *testing.T) {
		lis, err := net.Listen("tcp", "localhost:0")
		require.NoError(t, err)
		badCfg := dbCfg
		badCfg.Host = "localhost"
		badCfg.Port = (lis.Addr().(*net.TCPAddr)).Port
		start := time.Now()

		ctx, cancel := context.WithCancel(testhelper.Context(t))
		cancel()

		_, err = glsql.OpenDB(ctx, badCfg)
		require.EqualError(t, err, "send ping: context canceled")
		duration := time.Since(start)
		require.Truef(t, duration < time.Second, "connection attempt took %s", duration.String())
	})

	t.Run("connected with proper config", func(t *testing.T) {
		db, err := glsql.OpenDB(ctx, dbCfg)
		require.NoError(t, err, "opening of DB with correct configuration must not fail")
		require.NoError(t, db.Close())
	})
}

func TestUint64Provider(t *testing.T) {
	var provider glsql.Uint64Provider

	dst1 := provider.To()
	require.Equal(t, []interface{}{new(uint64)}, dst1, "must be a single value holder")
	val1 := dst1[0].(*uint64)
	*val1 = uint64(100)

	dst2 := provider.To()
	require.Equal(t, []interface{}{new(uint64)}, dst2, "must be a single value holder")
	val2 := dst2[0].(*uint64)
	*val2 = uint64(200)

	require.Equal(t, []uint64{100, 200}, provider.Values())

	dst3 := provider.To()
	val3 := dst3[0].(*uint64)
	*val3 = uint64(300)

	require.Equal(t, []uint64{100, 200, 300}, provider.Values())
}

func TestScanAll(t *testing.T) {
	t.Parallel()
	db := testdb.New(t)

	var ids glsql.Uint64Provider
	notEmptyRows, err := db.Query("SELECT id FROM (VALUES (1), (200), (300500)) AS t(id)")
	require.NoError(t, err)
	defer func() { require.NoError(t, notEmptyRows.Close()) }()

	require.NoError(t, glsql.ScanAll(notEmptyRows, &ids))
	require.Equal(t, []uint64{1, 200, 300500}, ids.Values())
	require.NoError(t, notEmptyRows.Err())

	var nothing glsql.Uint64Provider
	emptyRows, err := db.Query("SELECT id FROM (VALUES (1), (200), (300500)) AS t(id) WHERE id < 0")
	require.NoError(t, err)
	defer func() { require.NoError(t, emptyRows.Close()) }()

	require.NoError(t, glsql.ScanAll(emptyRows, &nothing))
	require.Equal(t, ([]uint64)(nil), nothing.Values())
	require.NoError(t, emptyRows.Err())
}

func TestDSN(t *testing.T) {
	testCases := []struct {
		desc   string
		in     config.DB
		direct bool
		out    string
	}{
		{desc: "empty", in: config.DB{}, out: "prefer_simple_protocol=true"},
		{
			desc: "proxy connection",
			in: config.DB{
				Host:        "1.2.3.4",
				Port:        2345,
				User:        "praefect-user",
				Password:    "secret",
				DBName:      "praefect_production",
				SSLMode:     "require",
				SSLCert:     "/path/to/cert",
				SSLKey:      "/path/to/key",
				SSLRootCert: "/path/to/root-cert",
			},
			direct: false,
			out:    `port=2345 host=1.2.3.4 user=praefect-user password=secret dbname=praefect_production sslmode=require sslcert=/path/to/cert sslkey=/path/to/key sslrootcert=/path/to/root-cert prefer_simple_protocol=true`,
		},
		{
			desc: "direct connection with different host and port",
			in: config.DB{
				User:        "praefect-user",
				Password:    "secret",
				DBName:      "praefect_production",
				SSLMode:     "require",
				SSLCert:     "/path/to/cert",
				SSLKey:      "/path/to/key",
				SSLRootCert: "/path/to/root-cert",
				SessionPooled: config.DBConnection{
					Host: "1.2.3.4",
					Port: 2345,
				},
			},
			direct: true,
			out:    `port=2345 host=1.2.3.4 user=praefect-user password=secret dbname=praefect_production sslmode=require sslcert=/path/to/cert sslkey=/path/to/key sslrootcert=/path/to/root-cert prefer_simple_protocol=true`,
		},
		{
			desc: "direct connection with dbname",
			in: config.DB{
				Host:        "1.2.3.4",
				Port:        2345,
				User:        "praefect-user",
				Password:    "secret",
				DBName:      "praefect_production",
				SSLMode:     "require",
				SSLCert:     "/path/to/cert",
				SSLKey:      "/path/to/key",
				SSLRootCert: "/path/to/root-cert",
				SessionPooled: config.DBConnection{
					DBName: "praefect_production_sp",
				},
			},
			direct: true,
			out:    `port=2345 host=1.2.3.4 user=praefect-user password=secret dbname=praefect_production_sp sslmode=require sslcert=/path/to/cert sslkey=/path/to/key sslrootcert=/path/to/root-cert prefer_simple_protocol=true`,
		},
		{
			desc: "direct connection with exactly the same parameters",
			in: config.DB{
				Host:          "1.2.3.4",
				Port:          2345,
				User:          "praefect-user",
				Password:      "secret",
				DBName:        "praefect_production",
				SSLMode:       "require",
				SSLCert:       "/path/to/cert",
				SSLKey:        "/path/to/key",
				SSLRootCert:   "/path/to/root-cert",
				SessionPooled: config.DBConnection{},
			},
			direct: true,
			out:    `port=2345 host=1.2.3.4 user=praefect-user password=secret dbname=praefect_production sslmode=require sslcert=/path/to/cert sslkey=/path/to/key sslrootcert=/path/to/root-cert prefer_simple_protocol=true`,
		},
		{
			desc: "direct connection with completely different parameters",
			in: config.DB{
				Host:        "1.2.3.4",
				Port:        2345,
				User:        "praefect-user",
				Password:    "secret",
				DBName:      "praefect_production",
				SSLMode:     "require",
				SSLCert:     "/path/to/cert",
				SSLKey:      "/path/to/key",
				SSLRootCert: "/path/to/root-cert",
				SessionPooled: config.DBConnection{
					Host:        "2.3.4.5",
					Port:        6432,
					User:        "praefect_sp",
					Password:    "secret-sp",
					DBName:      "praefect_production_sp",
					SSLMode:     "prefer",
					SSLCert:     "/path/to/sp/cert",
					SSLKey:      "/path/to/sp/key",
					SSLRootCert: "/path/to/sp/root-cert",
				},
			},
			direct: true,
			out:    `port=6432 host=2.3.4.5 user=praefect_sp password=secret-sp dbname=praefect_production_sp sslmode=prefer sslcert=/path/to/sp/cert sslkey=/path/to/sp/key sslrootcert=/path/to/sp/root-cert prefer_simple_protocol=true`,
		},
		{
			desc: "with spaces and quotes",
			in: config.DB{
				Password: "secret foo'bar",
			},
			out: `password=secret\ foo\'bar prefer_simple_protocol=true`,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			require.Equal(t, tc.out, glsql.DSN(tc.in, tc.direct))
		})
	}
}

func TestStringArray(t *testing.T) {
	t.Parallel()
	db := testdb.New(t)

	t.Run("multiple elements", func(t *testing.T) {
		var res glsql.StringArray
		require.NoError(t, db.QueryRow("SELECT ARRAY['a', NULL, '42', 'c']").Scan(&res))
		require.Equal(t, []string{"a", "42", "c"}, res.Slice())
	})

	t.Run("NULL value", func(t *testing.T) {
		var res glsql.StringArray
		require.NoError(t, db.QueryRow("SELECT NULL").Scan(&res))
		require.Empty(t, res.Slice())
	})
}

func TestIsQueryCancelled(t *testing.T) {
	for _, tc := range []struct {
		desc string
		err  error
		exp  bool
	}{
		{
			desc: "nil input",
			err:  nil,
			exp:  false,
		},
		{
			desc: "wrong error type",
			err:  assert.AnError,
			exp:  false,
		},
		{
			desc: "wrong code",
			err:  &pgconn.PgError{Code: "stub"},
			exp:  false,
		},
		{
			desc: "cancellation error",
			err:  &pgconn.PgError{Code: "57014"},
			exp:  true,
		},
		{
			desc: "wrapped cancellation error",
			err:  fmt.Errorf("stub: %w", &pgconn.PgError{Code: "57014"}),
			exp:  true,
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			res := glsql.IsQueryCancelled(tc.err)
			require.Equal(t, tc.exp, res)
		})
	}
}

func TestIsUniqueViolation(t *testing.T) {
	for _, tc := range []struct {
		desc   string
		err    error
		constr string
		exp    bool
	}{
		{
			desc: "nil input",
			err:  nil,
			exp:  false,
		},
		{
			desc: "wrong error type",
			err:  assert.AnError,
			exp:  false,
		},
		{
			desc: "wrong code",
			err:  &pgconn.PgError{Code: "stub"},
			exp:  false,
		},
		{
			desc: "unique violation",
			err:  &pgconn.PgError{Code: "23505"},
			exp:  true,
		},
		{
			desc: "wrapped unique violation",
			err:  fmt.Errorf("stub: %w", &pgconn.PgError{Code: "23505"}),
			exp:  true,
		},
		{
			desc:   "unique violation with accepted conditions",
			err:    &pgconn.PgError{Code: "23505", ConstraintName: "cname"},
			constr: "cname",
			exp:    true,
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			res := glsql.IsUniqueViolation(tc.err, tc.constr)
			require.Equal(t, tc.exp, res)
		})
	}
}

func TestMigrateSome(t *testing.T) {
	db := testdb.New(t)
	dbCfg := testdb.GetConfig(t, db.Name)
	cfg := config.Config{DB: dbCfg}

	migs := migrations.All()
	migrationCt := len(migs)

	for _, tc := range []struct {
		desc      string
		up        int
		executed  int
		migration *migrate.Migration
	}{
		{
			desc:      "All migrations up",
			up:        migrationCt,
			executed:  0,
			migration: migs[migrationCt-1],
		},
		{
			desc:      "Apply only first migration",
			up:        0,
			executed:  1,
			migration: migs[0],
		},
		{
			desc:      "Apply only last migration",
			up:        migrationCt - 1,
			executed:  1,
			migration: migs[migrationCt-1],
		},
		{
			desc:      "Apply only 10th migration",
			up:        9,
			executed:  1,
			migration: migs[9],
		},
		{
			desc:      "Apply 5th to 10th migrations",
			up:        5,
			executed:  5,
			migration: migs[9],
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			testdb.SetMigrations(t, db, cfg, tc.up)

			n, err := glsql.MigrateSome(tc.migration, db.DB, true)
			assert.NoError(t, err)
			assert.Equal(t, tc.executed, n)
		})
	}
}