-
Notifications
You must be signed in to change notification settings - Fork 0
/
library
513 lines (453 loc) · 13.1 KB
/
library
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# PostgreSQL DTF library useful for test-cases.
# Copyright (C) 2015 Red Hat, Inc.
# Written by Pavel Raiskup.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
export pgscl={{ m.pgscl }}
export pgscl_namespace=${pgscl:+$pgscl-}
export pgscl_prefix=${pgscl:+/opt/rh/$pgscl/root}
export pgdtf_service=${pgscl_namespace}postgresql
export pgdtf_homedir=/var/lib/pgsql
export pgdtf_datadir=/var/lib/pgsql/data/data
export pgdtf_datadirorigin=/var/lib/pgsql/data/data
export pgdtf_olddata=/var/lib/pgsql/data/data-old
test -n "$pgscl" && {
pgdtf_datadir=/var/opt/rh/$pgscl/lib/pgsql/data
pgdtf_olddata=/var/lib/pgsql/data
}
export pgdtf_server=${pgscl_namespace}postgresql-server
pgdtf_scl_paths ()
{
case $1 in
postgresql92)
pgdtf_scl_paths_data=/opt/rh/postgresql92/root/var/lib/pgsql/data
;;
*)
pgdtf_scl_paths_data=/var/opt/rh/"$1"/lib/pgsql/data
;;
esac
}
pgdtf_scl_wrapper ()
{
if test -z "$pgscl"; then
eval "$1"
else
scl enable $pgscl "$1"
fi
}
pgdtf_empty_dir ()
(
cd "$1" || exit 2
for _d_item in .* *; do
case $_d_item in
..|.)
continue
;;
\*)
test -f '*' && exit 1
;;
*)
exit 1
esac
done
exit 0
)
dtf_cmd_wrapper=pgdtf_scl_wrapper
__pgdtf_basic_assert ()
{
### Check that we can't actually break user's system. ###
dtf_assert cmd "service $pgdtf_service status" 3
dtf_assert_right_distro
test -f "/var/lib/pgsql/data/PG_VERSION" 2>/dev/null \
&& dtf_fail "/var/lib/pgsql/data/PG_VERSION shouldn't exist"
test -f "$pgdtf_datadir"/PG_VERSION 2>/dev/null \
&& dtf_fail "PG_VERSION file should not exist in $pgdtf_datadir"
test -e "$pgdtf_homedir" || return 0
find "$pgdtf_homedir" -maxdepth 1 -name '*.log' -exec false {} +
test $? -eq 0 || dtf_fail "some .log files exist in $pgdtf_homedir"
}
pgdtf_init ()
{
# TODO: don't require findutils
dtf_prereq pkg installed findutils
pgdtf_fix_service_datadir
__pgdtf_basic_assert
}
pgdtf_init_server_test ()
{
dtf_prereq pkg installed $pgdtf_server
pgdtf_init
}
pgdtf_cleanup_after_run ()
{
dtf_assert cmd 'rm -r $pgdtf_datadir/*' 0
}
pgdtf_cleanup_after_initdb ()
{
pgdtf_cleanup_after_run
dtf_assert cmd 'rm $pgdtf_homedir/*.log' 0
}
pgdtf_cleanup_after_upgrade ()
{
pgdtf_cleanup_after_initdb
dtf_assert cmd 'rm -r $pgdtf_olddata' 0
}
__pgdtf_setup_wrap ()
{
_d_action=$1 ; shift
func_quote eval "$@"
test -n "$func_quote_result" && _d_action="$_d_action $func_quote_result"
set dummy "{{ config.os.id }}" "{{ config.os.version }}" ; shift
case ${pgscl+scl-}$1-$2 in
scl-*)
case $pgscl in
postgresql92)
dtf_assert cmd "postgresql-setup $_d_action" 0
;;
*)
dtf_assert cmd "postgresql-setup --$_d_action" 0
;;
esac
;;
fedora-18|fedora-19|fedora-20|fedora-21|rhel-5*|rhel-6*|rhel-7*)
dtf_assert cmd "postgresql-setup $_d_action" 0
;;
fedora*|rhel*)
dtf_assert cmd "postgresql-setup --$_d_action" 0
;;
*)
dtf_fail "not implemented for this architecture"
;;
esac
}
pgdtf_simple_upgrade ()
{
__pgdtf_setup_wrap upgrade "$@"
}
pgdtf_initdb ()
{
__pgdtf_setup_wrap initdb
}
pgdtf_check_started ()
{
dtf_assert cmd "service ${pgdtf_service} status" 0
}
pgdtf_start ()
{
dtf_assert cmd 'service ${pgdtf_service} start' 0
pgdtf_check_started
}
pgdtf_initdb_and_start ()
{
pgdtf_initdb
pgdtf_start
}
pgdtf_stop ()
{
dtf_assert cmd "service ${pgdtf_service} stop" 0
pgdtf_check_stopped
}
pgdtf_stop_and_clean ()
{
pgdtf_stop
pgdtf_cleanup_after_initdb
}
pgdtf_check_stopped ()
{
dtf_assert cmd 'service ${pgdtf_service} status' 3
}
pgdtf_unpack_remote_data_tarball ()
{
(
tarball="$1"
test -d "$2" || {
mkdir -p "$2"
}
cd "$2"
set -o pipefail
dtf_debug pgdtf_lib "downloading '$tarball' into '$2' directory"
curl "$tarball" | tar -xzf -
) || dtf_fail "can't download and unpack '$1'"
}
# Detect current distribution and print set of "distro version action" lines
# where DISTRO may be rhel/fedora, VERSION may be e.g. 6.6 for rhel or 20 for
# Fedora. ACTION is either run or upgrade and it means whether currently
# installed PostgreSQL server is able to RUN the older data from DISTRO-VERSION
# or it should be first upgraded.
#
# What versions are currently in supported Fedora versions:
#
# f20 & f21 (9.3.10)
# f22 & f23 & rawhide (9.4.5)
#
# Older Fedora versions:
#
# f17 (9.1.9)
# f18 (9.2.5)
# f19 (9.2.9)
pgdtf_upgrade_matrix ()
{
# {% if config.os.id == "fedora" and config.os.version == 19 %}
pgdtf_upgrade_matrix_result='
fedora_17_upgrade
fedora_18_run
fedora_19_run
'
# {% elif config.os.id == "fedora" and config.os.version == 20 %}
pgdtf_upgrade_matrix_result='
fedora_18_upgrade
fedora_19_upgrade
fedora_20_run
'
# {% elif config.os.id == "fedora" and config.os.version == 21 %}
pgdtf_upgrade_matrix_result='
fedora_18_upgrade
fedora_19_upgrade
fedora_20_run
fedora_21_run
'
# {% elif config.os.id == "fedora" and config.os.version == 22 %}
pgdtf_upgrade_matrix_result='
fedora_20_upgrade
fedora_21_upgrade
fedora_22_run
'
# {% elif config.os.id == "fedora" and config.os.version == 23 %}
pgdtf_upgrade_matrix_result='
fedora_20_upgrade
fedora_21_upgrade
fedora_22_run
fedora_23_run
'
# {% elif config.os.id == "fedora" and config.os.version == 24 %}
pgdtf_upgrade_matrix_result='
fedora_22_upgrade
fedora_23_upgrade
fedora_24_run
'
# {% elif config.os.id == "fedora" and config.os.version == 25 %}
pgdtf_upgrade_matrix_result='
fedora_22_upgrade
fedora_23_upgrade
fedora_24_run
fedora_25_run
'
# {% elif config.os.id == "fedora" and config.os.version == 26 %}
pgdtf_upgrade_matrix_result='
fedora_24_upgrade
fedora_25_upgrade
fedora_26_run
'
# {% elif config.os.id == "fedora" and config.os.version == 27 %}
pgdtf_upgrade_matrix_result='
fedora_24_upgrade
fedora_25_upgrade
fedora_26_run
fedora_27_run
'
# {% elif config.os.id == "fedora" and config.os.version == 28 %}
pgdtf_upgrade_matrix_result='
fedora_26_upgrade
fedora_27_upgrade
fedora_28_run
'
# {% elif config.os.id == "rhel" and m.pgscl != "" %}
# {% if m.pgscl == "rh-postgresql94" %}
pgdtf_upgrade_matrix_result='
rhel_7_upgrade
rhel_7:postgresql92_upgrade
'
# {% elif m.pgscl == "rh-postgresql95" %}
pgdtf_upgrade_matrix_result='
rhel_7_upgrade
rhel_7:postgresql92_upgrade
rhel_7:rh-postgresql94_upgrade
'
# {% endif %}
# {% else %}
pgdtf_upgrade_matrix_result=
# {% endif %}
}
__pgdtf_drastic_cleanup ()
{
service postgresql stop
/bin/rm -rf "$pgdtf_datadir"/*
cp "$pgdtf_homedir"/*.log ./
/bin/rm -rf "$pgdtf_homedir"/*.log
}
pgdtf_fix_service_datadir ()
{
# TODO: fix for RHEL6
hookdir=/etc/systemd/system/postgresql.service.d/
mkdir -p "$hookdir" >/dev/null \
|| dtf_fail "can't create systemd hook directory"
cat > "$hookdir"/dtf-tests.conf <<EOF
[Service]
Environment=PGDATA=$pgdtf_datadir
EOF
systemctl daemon-reload
}
pgdtf_test_upgrades ()
{
dtf_fail_hooks='__pgdtf_drastic_cleanup'
_d_archive=$1 ; shift || dtf_die "pgdtf_test_upgrades requires argument"
pgdtf_mirror
pgdtf_upgrade_matrix
for _d_item in $pgdtf_upgrade_matrix_result
do
eval set dummy `echo "$_d_item" | sed 's/_/ /g'` ; shift
echo >&2 "checking upgrade from: $*"
_d_from_scl=
_d_datadir=$pgdtf_datadirorigin
_d_upgrade_args=
_d_old_server_pkg=postgresql-upgrade
_d_from_distro=$1
case $2 in
*:*)
_d_from_distroversion=${2%%:*}
_d_from_scl=${2##*:}
pgdtf_scl_paths "$_d_from_scl"
pgdtf_olddata=$pgdtf_scl_paths_data
func_quote eval --upgrade-from $_d_from_scl-postgresql
_d_upgrade_args=$func_quote_result
_d_datadir=$pgdtf_scl_paths_data
_d_old_server_pkg="${_d_from_scl}-postgresql-server"
case $pgscl in
rh-postgresql94|postgresql92)
# we installed 'pg_upgrade' binary into *-upgrade subpackage
# previously. From rh-postgresql95 the pg_upgrade binary is in base
# package (e.g. in rh-postgresql95-postgresql).
dtf_prereq pkg installed "${pgscl_namespace}postgresql-upgrade"
;;
esac
;;
*)
_d_from_distroversion=$2
if test -z "$pgscl"; then
_d_old_server_pkg=postgresql-upgrade
else
_d_old_server_pkg=postgresql-server
fi
;;
esac
dtf_prereq pkg installed "$_d_old_server_pkg"
_d_how_upgrade=$3
_d_url="$pgdtf_mirror_result/$_d_from_distro/$_d_from_distroversion/"
test -n "$_d_from_scl" && {
_d_url="$_d_url/scl/$_d_from_scl/"
}
_d_url=$_d_url"{{ config.os.arch }}"/$_d_archive
case $_d_datadir in
*data-old)
test -e "$_d_datadir" >/dev/null \
&& dtf_fail "$_d_datadir directory should not exist"
;;
*)
if test -d "$_d_datadir"; then
pgdtf_empty_dir "$_d_datadir" \
|| dtf_fail "$_d_datadir should be empty"
fi
;;
esac
pgdtf_fix_service_datadir
pgdtf_unpack_remote_data_tarball "$_d_url" "$(dirname "$_d_datadir")"
clean_action=pgdtf_cleanup_after_run
case $_d_how_upgrade in
upgrade)
eval pgdtf_simple_upgrade $_d_upgrade_args
clean_action=pgdtf_cleanup_after_upgrade
;;
esac
pgdtf_start
pgdtf_stop
$clean_action
done
dtf_fail_hooks=
}
pgdtf_mirror ()
{
dtf_dataurls='
http://pensioner.lab.eng.brq.redhat.com/root-tests/data/postgresql/latest
https://praiskup.fedorapeople.org/dtf/data/postgresql/latest
'
test -n "$pgdtf_mirror_result" && return 0
_d_timeout=1
while test $_d_timeout -lt 32; do
for _d_url in $dtf_dataurls
do
echo >&2 "trying $_d_url for $_d_timeout seconds"
timeout $_d_timeout curl "$_d_url" >/dev/null \
&& pgdtf_mirror_result="$_d_url" \
&& return 0
done
_d_timeout=`expr $_d_timeout '*' 2`
done
dtf_fail "pgdtf_mirror: no mirror found"
}
# pgdtf_fix_locale LANGS
# ----------------------
# Reinstall locale for LANGS (space separted list of language codes).
pgdtf_fix_locale ()
{
for_languages=$1
reinstall=false
dtf_debug pgdtf_lib "making sure '$for_languages' langs are installed"
# Fedora (probably 23+) glibc-common does not install all locales by
# default.
if test -f /etc/rpm/macros.image-language-conf; then
for _d_lang in $for_languages
do
set dummy "$_d_lang".{UTF-8,utf-8,utf8} ; shift
for _d_locale
do
case `rpm --eval "%_install_langs"` in
*"$_d_locale"*|all)
;;
*)
sed -i 's/\(C:.*\)/\1:'"$_d_locale/" \
/etc/rpm/macros.image-language-conf \
|| dtf_fail "can't fix %_install_langs"
echo >&2 "reinstalling glibc-common to fix $_d_locale"
reinstall=:
;;
esac
done
done
fi
for _d_lang in $for_languages
do
test -f /usr/share/i18n/locales/"$_d_lang" || {
reinstall=:
echo >&2 "reinstalling glibc-common to fix $_d_lang"
}
done
$reinstall || return 0
# https://bugzilla.redhat.com/show_bug.cgi?id=1129697
# {{ "\n " + commands.pkginstaller.reinstall(['glibc-common'], {'docs': False}) }}
test $? -eq 0 || dtf_fail "can't reinstall glibc-common"
}
# pgdtf_run_rpmbuild ARGS..
# -------------------------
# Run rpmbuild against local directory.
pgdtf_run_rpmbuild ()
{
rpmbuild \
--define "_sourcedir $PWD" \
--define "_rpmdir $PWD" \
--define "_specdir $PWD" \
--define "_builddir $PWD" \
--define "_srcrpmdir $PWD" \
"$@"
}