/
sysupgrade_test.sh
1167 lines (920 loc) · 32.9 KB
/
sysupgrade_test.sh
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2012 Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Google Inc. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
shtk_import unittest
# Creates a fake program that records its invocations for later processing.
#
# The fake program, when invoked, will append its arguments to a commands.log
# file in the test case's work directory.
#
# \param binary The path to the program to create.
# \param delegate If set to 'yes', execute the real program afterwards.
create_mock_binary() {
local binary="${1}"; shift
local delegate=no
[ ${#} -eq 0 ] || { delegate="${1}"; shift; }
cat >"${binary}" <<EOF
#! /bin/sh
logfile="${HOME}/commands.log"
echo "Command: \${0##*/}" >>"\${logfile}"
echo "Directory: \$(pwd)" >>"\${logfile}"
for arg in "\${@}"; do
echo "Arg: \${arg}" >>"\${logfile}"
done
echo >>"\${logfile}"
EOF
if [ "${delegate}" = yes ]; then
cat >>"${binary}" <<EOF
PATH="${PATH}"
exec "\${0##*/}" "\${@}"
EOF
fi
chmod +x "${binary}"
}
# Generates a NetBSD release dir with fake sets and kernels.
#
# Each generated set contains a single file named '<set>.cookie'. The kernels
# contain just an hardcoded string within them that can later be validated to
# ensure the right set was unpacked.
#
# \param releasedir Path to the release directory.
# \param ... Names of the sets to create under releasedir/binary/sets/ and the
# kernels to create under releasedir/binary/kernel/. No extensions should
# be given.
create_mock_release() {
local releasedir="${1}"; shift
mkdir -p "${releasedir}/binary/kernel"
mkdir -p "${releasedir}/binary/sets"
for set_name in "${@}"; do
case "${set_name}" in
netbsd-*)
echo "File from ${set_name}" \
>"${releasedir}/binary/kernel/${set_name}"
gzip "${releasedir}/binary/kernel/${set_name}"
;;
*)
echo "File from ${set_name}" >"${set_name}.cookie"
tar czf "${releasedir}/binary/sets/${set_name}.tgz" \
"${set_name}.cookie"
rm "${set_name}.cookie"
;;
esac
done
}
shtk_unittest_add_test config__builtins
config__builtins_test() {
cat >expout <<EOF
AUTOCLEAN = yes
CACHEDIR = __SYSUPGRADE_CACHEDIR__
DESTDIR is undefined
ETCUPDATE = yes
KERNEL = AUTO
POSTINSTALL_AUTOFIX is undefined
RELEASEDIR is undefined
SETS = AUTO
EOF
assert_command -o file:expout sysupgrade -c /dev/null config
}
shtk_unittest_add_test config__default_file
config__default_file_test() {
mkdir system
export SYSUPGRADE_ETCDIR="$(pwd)/system"
echo "KERNEL=abc" >"system/sysupgrade.conf"
assert_command -o match:"KERNEL = abc" sysupgrade config
}
shtk_unittest_add_test config__explicit_file
config__explicit_file_test() {
mkdir system
export SYSUPGRADE_ETCDIR="$(pwd)/system"
echo "KERNEL=abc" >"system/sysupgrade.conf"
echo "SETS='d e'" >"my-file.conf"
assert_command -o not-match:"KERNEL = abc" -o match:"SETS = d e" \
sysupgrade -c ./my-file.conf config
}
shtk_unittest_add_test config__auto__kernel__found
config__auto__kernel__found_test() {
cat >config <<EOF
#! /bin/sh
if [ "\${1}" != "-x" ]; then
echo "-x not passed to config"
exit 1
fi
if [ "\${2}" != "$(pwd)/root/netbsd" ]; then
echo "Invalid path passed to config: \${2}"
exit 1
fi
cat "\${2}"
EOF
chmod +x config
PATH="$(pwd):${PATH}"
mkdir root
cat >root/netbsd <<EOF
### START CONFIG FILE "foo/bar/ABCDE"
these are some contents
### END CONFIG FILE "foo/bar/ABCDE"
EOF
assert_command -o match:"KERNEL = ABCDE" \
sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o KERNEL="AUTO" config -a
}
shtk_unittest_add_test config__auto__kernel__not_found
config__auto__kernel__not_found_test() {
assert_command -o match:"KERNEL is undefined" \
sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o KERNEL="AUTO" config -a
}
shtk_unittest_add_test config__auto__kernel__fail
config__auto__kernel__fail_test() {
cat >config <<EOF
#! /bin/sh
exit 1
EOF
chmod +x config
PATH="$(pwd):${PATH}"
mkdir root
touch root/netbsd
cat >experr <<EOF
sysupgrade: E: Failed to determine kernel name; please set KERNEL explicitly
EOF
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o KERNEL="AUTO" config -a
}
shtk_unittest_add_test config__auto__sets__found
config__auto__sets__found_test() {
mkdir -p root/etc/mtree
touch root/etc/mtree/set.first
touch root/etc/mtree/set.second
touch root/etc/mtree/set.third
assert_command -o match:"SETS = first second third" \
sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o SETS="AUTO" config -a
}
shtk_unittest_add_test config__auto__sets__not_found
config__auto__sets__not_found_test() {
assert_command -o match:"SETS is undefined" \
sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o SETS="AUTO" config -a
}
shtk_unittest_add_test config__not_found
config__not_found_test() {
mkdir .sysupgrade
mkdir system
export SYSUPGRADE_ETCDIR="$(pwd)/system"
cat >experr <<EOF
sysupgrade: E: Configuration file foobar does not exist
EOF
assert_command -s exit:1 -o empty -e file:experr sysupgrade -c foobar config
}
shtk_unittest_add_test config__overrides
config__overrides_test() {
cat >custom.conf <<EOF
CACHEDIR=/tmp/cache
KERNEL=foo
EOF
cat >expout <<EOF
AUTOCLEAN = yes
CACHEDIR = /tmp/cache2
DESTDIR = /tmp/destdir
ETCUPDATE = yes
KERNEL is undefined
POSTINSTALL_AUTOFIX is undefined
RELEASEDIR is undefined
SETS = AUTO
EOF
assert_command -o file:expout sysupgrade -c custom.conf -o KERNEL= \
-o CACHEDIR=/tmp/cache2 -o DESTDIR=/tmp/destdir config
}
shtk_unittest_add_test config__too_many_args
config__too_many_args_test() {
cat >experr <<EOF
sysupgrade: E: config does not take any arguments
Type 'man sysupgrade' for help
EOF
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null config foo
}
shtk_unittest_add_test fetch__ftp
fetch__ftp_test() {
# TODO(jmmv): It would be nice if this test used an actual FTP server, just
# like the http test below. Unfortunately, the ftpd shipped by NetBSD does
# not provide an easy mechanism to run it as non-root (e.g. no easy way to
# select on which port to serve).
create_mock_binary ftp
cat >>ftp <<EOF
for arg in "\${@}"; do
case "\${arg}" in
-o*) touch \$(echo "\${arg}" | sed -e s,^-o,,) ;;
esac
done
EOF
PATH="$(pwd):${PATH}"
SYSUPGRADE_CACHEDIR="$(pwd)/a/b/c"; export SYSUPGRADE_CACHEDIR
mkdir -p a/b/c
touch a/b/c/foo.tgz.tmp
assert_command -o ignore -e ignore sysupgrade -c /dev/null \
-o RELEASEDIR="ftp://example.net/pub/NetBSD/X.Y/a-machine" \
-o KERNEL=GENERIC -o SETS="a foo" fetch
assert_file stdin commands.log <<EOF
Command: ftp
Directory: $(pwd)
Arg: -o$(pwd)/a/b/c/a.tgz.tmp
Arg: ftp://example.net/pub/NetBSD/X.Y/a-machine/binary/sets/a.tgz
Command: ftp
Directory: $(pwd)
Arg: -R
Arg: -o$(pwd)/a/b/c/foo.tgz.tmp
Arg: ftp://example.net/pub/NetBSD/X.Y/a-machine/binary/sets/foo.tgz
Command: ftp
Directory: $(pwd)
Arg: -o$(pwd)/a/b/c/netbsd-GENERIC.gz.tmp
Arg: ftp://example.net/pub/NetBSD/X.Y/a-machine/binary/kernel/netbsd-GENERIC.gz
EOF
}
shtk_unittest_add_test fetch__http cleanup
fetch__http_test() {
create_mock_release www/a-machine base comp etc netbsd-GENERIC tests text
[ -x "/usr/libexec/httpd" ] || skip "/usr/libexec/httpd missing"
if ! /usr/libexec/httpd -b -s -d -I 30401 -P "$(pwd)/httpd.pid" \
"$(pwd)/www" >httpd.out 2>httpd.err; then
if grep 'unknown option -- P' httpd.err >/dev/null; then
skip "httpd does not support -P"
else
sed 's,^,httpd.out:,' httpd.out
sed 's,^,httpd.err:,' httpd.err
fail "Failed to start test HTTP server"
fi
fi
SYSUPGRADE_CACHEDIR="$(pwd)/cache"; export SYSUPGRADE_CACHEDIR
assert_command -o ignore -e ignore sysupgrade -c /dev/null \
-o RELEASEDIR="http://localhost:30401/a-machine" \
-o KERNEL=GENERIC -o SETS="base etc text" fetch
for set_name in base.tgz etc.tgz netbsd-GENERIC.gz text.tgz; do
[ -e "cache/${set_name}" ] || fail "${set_name} not fetched"
done
for set_name in comp.tgz tests.tgz; do
[ ! -e "cache/${set_name}" ] || fail "${set_name} fetched"
done
kill -9 "$(cat httpd.pid)"
rm -f httpd.pid
}
fetch__http_cleanup() {
if [ -f httpd.pid ]; then
echo "Killing stale HTTP server"
kill -9 "$(cat httpd.pid)"
fi
}
shtk_unittest_add_test fetch__ssh__one_set
fetch__ssh__one_set_test() {
create_mock_binary scp
PATH="$(pwd):${PATH}"
SYSUPGRADE_CACHEDIR="$(pwd)/a/b/c"; export SYSUPGRADE_CACHEDIR
mkdir -p a/b/c
assert_command -o ignore -e ignore sysupgrade -c /dev/null \
-o RELEASEDIR="ssh://example.net/home/sysbuild/release/machine" \
-o KERNEL="" -o SETS="one" fetch
assert_file stdin commands.log <<EOF
Command: scp
Directory: $(pwd)
Arg: example.net:/home/sysbuild/release/machine/{binary/sets/one.tgz}
Arg: $(pwd)/a/b/c/
EOF
}
shtk_unittest_add_test fetch__ssh__many_sets
fetch__ssh__many_sets_test() {
create_mock_binary scp
PATH="$(pwd):${PATH}"
SYSUPGRADE_CACHEDIR="$(pwd)/a/b/c"; export SYSUPGRADE_CACHEDIR
mkdir -p a/b/c
assert_command -o ignore -e ignore sysupgrade -c /dev/null \
-o RELEASEDIR="ssh://example.net/home/sysbuild/release/machine" \
-o KERNEL=GENERIC -o SETS="one two" fetch
assert_file stdin commands.log <<EOF
Command: scp
Directory: $(pwd)
Arg: example.net:/home/sysbuild/release/machine/{binary/sets/one.tgz,binary/sets/two.tgz,binary/kernel/netbsd-GENERIC.gz}
Arg: $(pwd)/a/b/c/
EOF
}
shtk_unittest_add_test fetch__ssh__already_exist
fetch__ssh__already_exist_test() {
create_mock_binary scp
PATH="$(pwd):${PATH}"
SYSUPGRADE_CACHEDIR="$(pwd)/a/b/c"; export SYSUPGRADE_CACHEDIR
mkdir -p a/b/c
touch a/b/c/netbsd-GENERIC.gz
touch a/b/c/one.tgz
assert_command -o ignore -e ignore sysupgrade -c /dev/null \
-o RELEASEDIR="ssh://example.net/home/sysbuild/release/machine" \
-o KERNEL=GENERIC -o SETS="one" fetch
[ ! -f commands.log ] || fail "scp was invoked"
}
shtk_unittest_add_test fetch__local
fetch__local_test() {
create_mock_release release base comp etc netbsd-GENERIC tests text
SYSUPGRADE_CACHEDIR="$(pwd)/cache"; export SYSUPGRADE_CACHEDIR
assert_command -o ignore -e ignore sysupgrade -c /dev/null \
-o KERNEL=GENERIC -o RELEASEDIR="$(pwd)/release" \
-o SETS="base etc text" fetch
for set_name in base.tgz etc.tgz netbsd-GENERIC.gz text.tgz; do
[ -e "cache/${set_name}" ] || fail "${set_name} not fetched"
done
for set_name in comp.tgz tests.tgz; do
[ ! -e "cache/${set_name}" ] || fail "${set_name} fetched"
done
}
shtk_unittest_add_test fetch__no_kernel
fetch__no_kernel_test() {
create_mock_release release base comp netbsd-GENERIC
SYSUPGRADE_CACHEDIR="$(pwd)/cache"; export SYSUPGRADE_CACHEDIR
assert_command -o ignore -e ignore sysupgrade -c /dev/null \
-o KERNEL= -o RELEASEDIR="$(pwd)/release" -o SETS="base comp" fetch
for set_name in base comp; do
[ -e "cache/${set_name}.tgz" ] || fail "${set_name} not fetched"
done
[ ! -e "cache/netbsd-GENERIC.gz" ] || fail "netbsd-GENERIC fetched"
}
shtk_unittest_add_test fetch__unknown
fetch__unknown_test() {
cat >experr <<EOF
sysupgrade: E: Don't know how to fetch from ./release; must be an absolute path or an FTP/HTTP site
EOF
SYSUPGRADE_CACHEDIR="$(pwd)/cache"; export SYSUPGRADE_CACHEDIR
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null \
-o RELEASEDIR="./release" -o SETS="base etc text" fetch
[ ! -d cache ] || fail "cache directory created even during errors"
}
shtk_unittest_add_test fetch__explicit
fetch__explicit_test() {
create_mock_release release base
SYSUPGRADE_CACHEDIR="$(pwd)/cache"; export SYSUPGRADE_CACHEDIR
assert_command -o ignore -e ignore sysupgrade -c /dev/null \
-o RELEASEDIR="$(pwd)/foo" -o KERNEL= -o SETS=base \
fetch "$(pwd)/release"
[ -e "cache/base.tgz" ] || fail "base not fetched"
}
shtk_unittest_add_test fetch__too_many_args
fetch__too_many_args_test() {
cat >experr <<EOF
sysupgrade: E: fetch takes zero or one arguments
Type 'man sysupgrade' for help
EOF
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null fetch \
foo bar
}
shtk_unittest_add_test kernel__skip
kernel__skip_test() {
cat >experr <<EOF
sysupgrade: I: Skipping kernel installation (KERNEL not set)
EOF
assert_command -s exit:0 -e file:experr sysupgrade -c /dev/null \
-o KERNEL= kernel
}
shtk_unittest_add_test kernel__from_config
kernel__from_config_test() {
mkdir root
echo "my old kernel" >root/netbsd
create_mock_release release netbsd-FOOBAR
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/kernel"
export SYSUPGRADE_CACHEDIR
assert_command -s exit:0 \
-e match:"Upgrading kernel using FOOBAR in $(pwd)/root/" \
-e match:"Backing up" \
sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o KERNEL=FOOBAR kernel
assert_command -o match:"File from netbsd-FOOBAR" cat root/netbsd
assert_command -o match:"my old kernel" cat root/onetbsd
}
shtk_unittest_add_test kernel__from_arg
kernel__from_arg_test() {
mkdir root
echo "my old kernel" >root/netbsd
create_mock_release release netbsd-FOOBAR netbsd-OTHER
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/kernel"
export SYSUPGRADE_CACHEDIR
assert_command -s exit:0 \
-e match:"Upgrading kernel using OTHER in $(pwd)/root/" \
-e match:"Backing up" \
sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o KERNEL=FOOBAR kernel OTHER
assert_command -o match:"File from netbsd-OTHER" cat root/netbsd
assert_command -o match:"my old kernel" cat root/onetbsd
}
shtk_unittest_add_test kernel__override_backup
kernel__override_backup_test() {
mkdir root
echo "my old kernel" >root/netbsd
echo "my older kernel" >root/onetbsd
create_mock_release release netbsd-FOOBAR
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/kernel"
export SYSUPGRADE_CACHEDIR
assert_command -s exit:0 \
-e match:"Upgrading kernel using FOOBAR in $(pwd)/root/" \
-e match:"Backing up" \
sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o KERNEL=FOOBAR kernel
assert_command -o match:"File from netbsd-FOOBAR" cat root/netbsd
assert_command -o match:"my old kernel" cat root/onetbsd
}
shtk_unittest_add_test kernel__no_backup
kernel__no_backup_test() {
mkdir root
create_mock_release release netbsd-FOOBAR
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/kernel"
export SYSUPGRADE_CACHEDIR
assert_command -s exit:0 -e not-match:"Backing up" sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o KERNEL=FOOBAR kernel
assert_command -o match:"File from netbsd-FOOBAR" cat root/netbsd
[ ! -f root/onetbsd ] || fail "onetbsd backup created, but not expected"
}
shtk_unittest_add_test kernel__missing_set
kernel__missing_set_test() {
mkdir root
echo "my old kernel" >root/netbsd
SYSUPGRADE_CACHEDIR="$(pwd)"; export SYSUPGRADE_CACHEDIR
cat >experr <<EOF
sysupgrade: E: Cannot find netbsd-GENERIC.gz; did you run 'sysupgrade fetch' first?
EOF
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o KERNEL=GENERIC kernel
assert_command -o match:"my old kernel" cat root/netbsd
}
shtk_unittest_add_test kernel__bad_file
kernel__bad_file_test() {
mkdir root
echo "my old kernel" >root/netbsd
echo "invalid gzip file" >netbsd-FOOBAR.gz
SYSUPGRADE_CACHEDIR="$(pwd)"; export SYSUPGRADE_CACHEDIR
assert_command -s exit:1 \
-e match:"Upgrading kernel using FOOBAR in $(pwd)/root/" \
-e match:"Failed to uncompress new kernel" \
sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o KERNEL=FOOBAR kernel
assert_command -o match:"my old kernel" cat root/netbsd
[ ! -f root/onetbsd ] || fail "onetbsd backup created, but not expected"
}
shtk_unittest_add_test kernel__too_many_args
kernel__too_many_args_test() {
cat >experr <<EOF
sysupgrade: E: kernel takes zero or one arguments
Type 'man sysupgrade' for help
EOF
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null kernel A B
}
shtk_unittest_add_test modules__skip
modules__skip_test() {
cat >experr <<EOF
sysupgrade: I: Skipping modules installation (modules not in SETS)
EOF
assert_command -s exit:0 -e file:experr sysupgrade -c /dev/null \
-o SETS="base comp modules2" modules
}
shtk_unittest_add_test modules__install
modules__install_test() {
create_mock_release release base modules
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/sets"; export SYSUPGRADE_CACHEDIR
assert_command -s exit:0 -e match:"Upgrading kernel modules" \
sysupgrade -c /dev/null -o DESTDIR="$(pwd)/root" \
-o SETS="base modules tests" modules
[ -f root/modules.cookie ] || fail "modules not extracted"
[ ! -f root/base.cookie ] || fail "base should not have been extracted"
}
shtk_unittest_add_test modules__too_many_args
modules__too_many_args_test() {
cat >experr <<EOF
sysupgrade: E: modules does not take any arguments
Type 'man sysupgrade' for help
EOF
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null modules A
}
shtk_unittest_add_test sets__from_config
sets__from_config_test() {
create_mock_release release base etc comp
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/sets"; export SYSUPGRADE_CACHEDIR
expected_sets="base comp"
unexpected_sets="etc modules xetc"
assert_command -s exit:0 -e ignore sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" \
-o SETS="${expected_sets} ${unexpected_sets}" \
sets
for set_name in ${expected_sets}; do
[ -f "root/${set_name}.cookie" ] \
|| fail "Expected set ${set_name} not extracted"
done
for set_name in ${unexpected_sets}; do
[ ! -f "root/${set_name}.cookie" ] \
|| fail "Unexpected set ${set_name} extracted"
done
}
shtk_unittest_add_test sets__from_args
sets__from_args_test() {
create_mock_release release base etc comp
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/sets"; export SYSUPGRADE_CACHEDIR
expected_sets="base comp"
unexpected_sets="etc modules xetc"
assert_command -s exit:0 -e ignore sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o SETS="foo bar baz" \
sets ${expected_sets} ${unexpected_sets}
for set_name in ${expected_sets}; do
[ -f "root/${set_name}.cookie" ] \
|| fail "Expected set ${set_name} not extracted"
done
for set_name in ${unexpected_sets}; do
[ ! -f "root/${set_name}.cookie" ] \
|| fail "Unexpected set ${set_name} extracted"
done
}
shtk_unittest_add_test sets__extract_fail
sets__extract_fail_test() {
create_mock_release release ok1 fail1 ok2
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/sets"; export SYSUPGRADE_CACHEDIR
local real_progress="$(which progress)"
cat >progress <<EOF
#! /bin/sh
for arg in "\${@}"; do
case "\${arg}" in
*/fail[0-9].tgz) exit 1 ;;
esac
done
exec "${real_progress}" "\${@}"
EOF
chmod +x progress
PATH="$(pwd):${PATH}"
expected_sets="ok1"
unexpected_sets="fail1 ok2"
assert_command -s exit:1 -e ignore sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" \
-o SETS="${expected_sets} ${unexpected_sets}" \
sets
for set_name in ${expected_sets}; do
[ -f "root/${set_name}.cookie" ] \
|| fail "Expected set ${set_name} not extracted"
done
for set_name in ${unexpected_sets}; do
[ ! -f "root/${set_name}.cookie" ] \
|| fail "Unexpected set ${set_name} extracted"
done
}
shtk_unittest_add_test sets__invalid_kern
sets__invalid_kern_test() {
cat >experr <<EOF
sysupgrade: E: SETS should not contain any kernel sets; found kern-FOO
EOF
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" -o SETS="foo kern-FOO baz" sets
}
shtk_unittest_add_test etcupdate__skip__none
etcupdate__skip__none_test() {
cat >experr <<EOF
sysupgrade: I: Skipping etcupdate (no etc sets in SETS)
EOF
assert_command -s exit:0 -e file:experr sysupgrade -c /dev/null \
-o SETS="base comp etcfoo" etcupdate
}
shtk_unittest_add_test etcupdate__skip__no_etc
etcupdate__skip__no_etc_test() {
cat >experr <<EOF
sysupgrade: I: Skipping etcupdate (required etc not in SETS)
EOF
assert_command -s exit:0 -e file:experr sysupgrade -c /dev/null \
-o SETS="base xetc xfonts" etcupdate
}
shtk_unittest_add_test etcupdate__skip__destdir
etcupdate__skip__destdir_test() {
cat >experr <<EOF
sysupgrade: I: Skipping etcupdate (DESTDIR upgrades not supported)
EOF
assert_command -s exit:0 -e file:experr sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)" -o SETS="etc" etcupdate
}
shtk_unittest_add_test etcupdate__some_etcs
etcupdate__some_etcs_test() {
create_mock_binary etcupdate
PATH="$(pwd):${PATH}"
create_mock_release release base etc comp xbase xetc
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/sets"; export SYSUPGRADE_CACHEDIR
assert_command -s exit:0 -o ignore -e ignore sysupgrade -c /dev/null \
-o RELEASEDIR="$(pwd)/release" \
-o SETS="base etc comp xbase xetc" etcupdate
assert_file stdin commands.log <<EOF
Command: etcupdate
Directory: $(pwd)
Arg: -a
Arg: -l
Arg: -s$(pwd)/release/binary/sets/etc.tgz
Arg: -s$(pwd)/release/binary/sets/xetc.tgz
EOF
}
shtk_unittest_add_test etcupdate__missing_etc
etcupdate__missing_etc_test() {
SYSUPGRADE_CACHEDIR="$(pwd)"; export SYSUPGRADE_CACHEDIR
cat >experr <<EOF
sysupgrade: E: Cannot find etc.tgz; did you run 'sysupgrade fetch' first?
EOF
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null \
-o RELEASEDIR="$(pwd)/missing" -o SETS="etc" etcupdate
}
shtk_unittest_add_test etcupdate__too_many_args
etcupdate__too_many_args_test() {
cat >experr <<EOF
sysupgrade: E: etcupdate does not take any arguments
Type 'man sysupgrade' for help
EOF
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null etcupdate foo
}
shtk_unittest_add_test postinstall__skip__none
postinstall__skip__none_test() {
cat >experr <<EOF
sysupgrade: I: Skipping postinstall (no etc sets in SETS)
EOF
assert_command -s exit:0 -e file:experr sysupgrade -c /dev/null \
-o SETS="base comp etcfoo" postinstall
}
shtk_unittest_add_test postinstall__skip__no_etc
postinstall__skip__no_etc_test() {
cat >experr <<EOF
sysupgrade: I: Skipping postinstall (required etc not in SETS)
EOF
assert_command -s exit:0 -e file:experr sysupgrade -c /dev/null \
-o SETS="base xetc xfonts" postinstall
}
shtk_unittest_add_test postinstall__some_etcs
postinstall__some_etcs_test() {
create_mock_binary postinstall
PATH="$(pwd):${PATH}"
create_mock_release release base etc comp xbase xetc
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/sets"; export SYSUPGRADE_CACHEDIR
assert_command -s exit:0 -o ignore -e ignore sysupgrade -c /dev/null \
-o RELEASEDIR="$(pwd)/release" \
-o SETS="base etc comp xbase xetc" postinstall
assert_file stdin commands.log <<EOF
Command: postinstall
Directory: $(pwd)
Arg: -d/
Arg: -s$(pwd)/release/binary/sets/etc.tgz
Arg: -s$(pwd)/release/binary/sets/xetc.tgz
Arg: check
EOF
}
shtk_unittest_add_test postinstall__destdir
postinstall__destdir_test() {
create_mock_binary postinstall
PATH="$(pwd):${PATH}"
create_mock_release release base etc
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/sets"; export SYSUPGRADE_CACHEDIR
assert_command -s exit:0 -o ignore -e ignore sysupgrade -c /dev/null \
-o DESTDIR="$(pwd)/root" \
-o RELEASEDIR="$(pwd)/release" \
-o SETS="base etc" postinstall
assert_file stdin commands.log <<EOF
Command: postinstall
Directory: $(pwd)
Arg: -d$(pwd)/root/
Arg: -s$(pwd)/release/binary/sets/etc.tgz
Arg: check
EOF
}
shtk_unittest_add_test postinstall__autofix
postinstall__autofix_test() {
create_mock_binary postinstall
PATH="$(pwd):${PATH}"
create_mock_release release etc
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/sets"; export SYSUPGRADE_CACHEDIR
assert_command -s exit:0 -o ignore -e ignore sysupgrade -c /dev/null \
-o POSTINSTALL_AUTOFIX="first second" \
-o RELEASEDIR="$(pwd)/release" \
-o SETS="etc" postinstall
assert_file stdin commands.log <<EOF
Command: postinstall
Directory: $(pwd)
Arg: -d/
Arg: -s$(pwd)/release/binary/sets/etc.tgz
Arg: fix
Arg: first
Arg: second
Command: postinstall
Directory: $(pwd)
Arg: -d/
Arg: -s$(pwd)/release/binary/sets/etc.tgz
Arg: check
EOF
}
shtk_unittest_add_test postinstall__missing_etc
postinstall__missing_etc_test() {
SYSUPGRADE_CACHEDIR="$(pwd)"; export SYSUPGRADE_CACHEDIR
cat >experr <<EOF
sysupgrade: E: Cannot find etc.tgz; did you run 'sysupgrade fetch' first?
EOF
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null \
-o RELEASEDIR="$(pwd)/missing" -o SETS="etc" postinstall
}
shtk_unittest_add_test postinstall__explicit_args
postinstall__explicit_args_test() {
create_mock_binary postinstall
PATH="$(pwd):${PATH}"
create_mock_release release base etc
SYSUPGRADE_CACHEDIR="$(pwd)/release/binary/sets"; export SYSUPGRADE_CACHEDIR
assert_command -s exit:0 -o ignore -e ignore sysupgrade -c /dev/null \
-o RELEASEDIR="$(pwd)/release" -o SETS="base etc" postinstall fix a b c
assert_file stdin commands.log <<EOF
Command: postinstall
Directory: $(pwd)
Arg: -d/
Arg: -s$(pwd)/release/binary/sets/etc.tgz
Arg: fix
Arg: a
Arg: b
Arg: c
EOF
}
shtk_unittest_add_test clean__nothing
clean__nothing_test() {
SYSUPGRADE_CACHEDIR="$(pwd)/cache"; export SYSUPGRADE_CACHEDIR
mkdir "${SYSUPGRADE_CACHEDIR}"
assert_command -e match:"Cleaning downloaded files" sysupgrade \
-c /dev/null clean
[ -d cache ] || fail "Cache directory should not have been deleted"
}
shtk_unittest_add_test clean__only_zipped
clean__only_zipped_test() {
SYSUPGRADE_CACHEDIR="$(pwd)/cache"; export SYSUPGRADE_CACHEDIR
mkdir "${SYSUPGRADE_CACHEDIR}"
touch cache/foo.tgz
touch cache/foo.tgz.tmp
touch cache/bar.gz
touch cache/bar.gz.tmp
assert_command -e match:"Cleaning downloaded files" sysupgrade \
-c /dev/null clean
[ ! -f cache/foo.tgz ] || fail "tgz not deleted"
[ ! -f cache/foo.tgz.tmp ] || fail "Temporary tgz not deleted"
[ ! -f cache/bar.gz ] || fail "gz not deleted"
[ ! -f cache/bar.gz.tmp ] || fail "Temporary gz not deleted"
[ -d cache ] || fail "Cache directory should not have been deleted"
}
shtk_unittest_add_test clean__too_many_args
clean__too_many_args_test() {
cat >experr <<EOF
sysupgrade: E: clean does not take any arguments
Type 'man sysupgrade' for help
EOF
assert_command -s exit:1 -e file:experr sysupgrade -c /dev/null clean foo
}
shtk_unittest_add_test auto__simple
auto__simple_test() {
create_mock_binary postinstall
create_mock_binary etcupdate
PATH="$(pwd):${PATH}"
create_mock_release release base comp etc netbsd-CUSTOM modules