File: check.bats

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (1064 lines) | stat: -rw-r--r-- 35,386 bytes parent folder | download | duplicates (5)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
#!/usr/bin/env bats

load helpers

# Check that the storage driver doesn't have any layers that we don't know
# about, and would therefore never be able to clean up, i.e., that we can
# spot them.
@test "check-unmanaged-layers" {
	run storage --debug=false storage-layers
	echo storage-layers: "$output"
	if [ $status -eq 1 -a "$output" == "driver not supported" ] ; then
		skip "driver $STORAGE_DRIVER does not support ListLayers()"
	fi

	run storage --debug=false create-storage-layer
	echo create-storage-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	run storage create-storage-layer "$layer"
	echo create-storage-layer: "$output"
	[[ $status -eq 0 ]]

	run storage create-storage-layer
	echo create-storage-layer: "$output"
	[[ $status -eq 0 ]]

	run storage check -r
	echo "check -r:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "layer ${layer}: layer in lower level storage driver not accounted for" ]]

	run storage --debug=false storage-layers
	echo storage-layers: "$output"
	[[ $status -eq 0 ]]
	[[ $output == "" ]]
}

# Check that nothing happens when the storage driver had layers that we don't
# know about in a read-only store.  It's not as if we can do anything about
# them.
@test "check-unmanaged-ro-layers" {
	case "$STORAGE_DRIVER" in
	overlay*|vfs)
		;;
	*)
		skip "driver $STORAGE_DRIVER does not support additional image stores"
		;;
	esac

	# Put a couple of unmanaged layers in the read-only location.
	mkdir ${TESTDIR}/{ro-root,ro-runroot}

	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot --debug=false create-storage-layer
	echo create-storage-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot --debug=false create-storage-layer "$layer"
	echo create-storage-layer: "$output"
	[[ $status -eq 0 ]]
	otherlayer="$output"

        storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot shutdown

	# Put an image in the read-write location.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-layer
	echo create-storage-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-image "$layer"
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Check that we don't complain about unmanaged layers in the read-only location.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check
	echo "check:" "$output"
	[[ $status -eq 0 ]]
	[[ $output == "" ]]
}

# Check that the storage driver doesn't have any layers that we don't know
# about in a read-write store, and would therefore never be able to clean up,
# i.e., that we can spot them.
@test "check-unmanaged-rw-layers" {
	case "$STORAGE_DRIVER" in
	overlay*|vfs)
		;;
	*)
		skip "driver $STORAGE_DRIVER does not support additional image stores"
		;;
	esac

	# Put an image in the read-only location.
	mkdir ${TESTDIR}/{ro-root,ro-runroot}

	run storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	rolayer=$output

	run storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot --debug=false create-image "$rolayer"
	echo create-image: "$output"
	[[ $status -eq 0 ]]

        storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot shutdown

	# Put a couple of unmanaged layers in the read-write location.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-storage-layer
	echo create-storage-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-storage-layer "$layer"
	echo create-storage-layer: "$output"
	[[ $status -eq 0 ]]
	otherlayer=$output

	# Check that we find the unmanaged layers in the read-write location and remove them.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check -r
	echo "check -r:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "layer ${layer}: layer in lower level storage driver not accounted for" ]]
	[[ $output =~ "layer ${otherlayer}: layer in lower level storage driver not accounted for" ]]

	# So now there shouldn't be any layers at all if we're just looking at the read-write location.
	run storage --debug=false storage-layers
	echo storage-layers: "$output"
	[[ $status -eq 0 ]]
	[[ $output == "" ]]

	# But there should still be that managed layer we put in the read-only location.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root layers -q
	echo storage-layers: "$output"
	[[ $status -eq 0 ]]
	[[ $output == "$rolayer" ]]
}

# Check that we can detect layers that aren't part of an image or a container.
@test "check-unused-layers" {
	run storage --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	run storage --debug=false create-layer $output
	[[ $status -eq 0 ]]
	layer=$output

	# By default, an unreferenced layer must have reached some minimum age
	# in order for us to think it's been forgotten.
	run storage --debug=false check -r
	echo "check -r:" "$output"
	[[ $status -eq 0 ]]
	[[ $output == "" ]]

	# But if we set that minimum age to 0, we should clean it up.
	run storage check -r -m 0
	echo "check -r -m 0:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "layer ${layer}: layer not referenced by any images or containers" ]]

	# After the cleanup, there shouldn't be anything left.
	run storage --debug=false layers
	echo layers: "$output"
	[[ $status -eq 0 ]]
	[[ $output == "" ]]
}

# Check that we don't complain about layers in read-only storage that aren't
# part of an image or a container, since we can't do anything about them
# anyway.
@test "check-unused-ro-layers" {
	case "$STORAGE_DRIVER" in
	overlay*|vfs)
		;;
	*)
		skip "driver $STORAGE_DRIVER does not support additional image stores"
		;;
	esac

	# Put a couple of unreferenced layers in the read-only location.
	mkdir ${TESTDIR}/{ro-root,ro-runroot}
	run storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	rolayer="$output"
	run storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot --debug=false create-layer "$rolayer"
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	otherlayer="$output"

        storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot shutdown

	# Put an image in the read-write location.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-image "$layer"
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Check for errors.  We shouldn't be warning about the unreferenced read-only layers.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check -m 0
	echo "check -m 0:" "$output"
	[[ $status -eq 0 ]]
	[[ $output == "" ]]
}

# Check that we can detect layers in read-write storage that aren't part of an
# image or a container.
@test "check-unused-rw-layers" {
	case "$STORAGE_DRIVER" in
	overlay*|vfs)
		;;
	*)
		skip "driver $STORAGE_DRIVER does not support additional image stores"
		;;
	esac

	# Put an image in the read-only location.
	mkdir ${TESTDIR}/{ro-root,ro-runroot}
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	rolayer="$output"
	storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot --debug=false create-image "$rolayer"

        storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot shutdown

	# Put some unreferenced layers in the read-write location.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-layer "$layer"
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	otherlayer=$output

	# By default, an unreferenced layer must have reached some minimum age
	# in order for us to think it's been forgotten.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check -r
	echo "check -r:" "$output"
	[[ $status -eq 0 ]]
	[[ $output == "" ]]

	# Now check for errors and repair them.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check -r -m 0
	echo "check -r -m 0:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "layer $layer: layer not referenced by any images or containers" ]]
	[[ $output =~ "layer $otherlayer: layer not referenced by any images or containers" ]]

	# The read-only layer should be the only one present.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root layers -q
	echo layers: "$output"
	[[ $status -eq 0 ]]
	[[ $output == "$rolayer" ]]
}

# Check that we can detect when the contents of a layer's files, at least the
# ones that we'd need to read in order to reconstruct the diff, have been
# altered.
@test "check-layer-content-digest" {
	# This test needs "tar".
	if test -z "$(which tar 2> /dev/null)" ; then
		skip "need tar"
	fi

	# Create a layer.
	run storage --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Set contents of the layer.
	createrandom ${TESTDIR}/datafile1
	createrandom ${TESTDIR}/datafile2
	(cd ${TESTDIR}; tar cf - datafile1 datafile2) > ${TESTDIR}/diff
	storage apply-diff -f ${TESTDIR}/diff $layer

	# Mark that layer as part of an image.
	run storage --debug=false create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Put something in the layer that wasn't part of the diff.
	createrandom ${TESTDIR}/datafile3
	storage copy ${TESTDIR}/datafile3 ${layer}:/datafile1

	# Now check if the diff can be reproduced correctly.
	run storage check -r
	echo "check -r:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "layer ${layer}: layer content incorrect digest" ]] || [[ $output =~ "layer ${layer}: file integrity checksum failed" ]]

	# Having removed the layer, there should be no traces left.
	run storage --debug=false images
	echo images: "$output"
	[[ $status -eq 0 ]]
	[[ $output == "" ]]

	# Should look empty now
	run storage --debug=false layers
	echo layers: "$output"
	[[ $status -eq 0 ]]
	[[ $output == "" ]]
}

# Check that we can detect when the contents of a read-only layer's files, at
# least the ones that we'd need to read in order to reconstruct the diff, have
# been altered.
@test "check-ro-layer-content-digest" {
	# This test needs "tar".
	if test -z "$(which tar 2> /dev/null)" ; then
		skip "need tar"
	fi

	case "$STORAGE_DRIVER" in
	overlay*|vfs)
		;;
	*)
		skip "driver $STORAGE_DRIVER does not support additional image stores"
		;;
	esac

	# Put a layer record in the read-only location.
	mkdir ${TESTDIR}/{ro-root,ro-runroot}
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	rolayer="$output"

	# Set up that layer's contents.
	createrandom ${TESTDIR}/datafile1
	createrandom ${TESTDIR}/datafile2
	(cd ${TESTDIR}; tar cf - datafile1 datafile2) > ${TESTDIR}/diff
	storage apply-diff --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot -f ${TESTDIR}/diff $rolayer

	# Mess with that layer's contents.
	createrandom ${TESTDIR}/datafile3
	storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot copy ${TESTDIR}/datafile3 ${rolayer}:/datafile1

	# Create an image record that uses that layer.
	run storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot --debug=false create-image "$rolayer"
	echo create-image: "$output"
	[[ $status -eq 0 ]]

        storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot shutdown

	# Create a read-write layer.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	rwlayer=$output

	# Create a read-write image.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-image $rwlayer
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Check that we notice the added file, even if we can't fix it.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check -r
	echo "check -r:" "$output"
	[[ $status -ne 0 ]] # couldn't fix read-only layers
	[[ $output =~ "layer ${rolayer}: layer content incorrect digest" ]] || [[ $output =~ "layer ${rolayer}: file integrity checksum failed" ]]

	# A check of just the read-write storage shouldn't turn up anything.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect when a layer has had content added.  Due to the
# way diff reconstructs diffs from layers, items which weren't in the original
# diff won't be noticed if the check consists of only extracting the diff.
@test "check-layer-content-modified" {
	# This test needs "tar".
	if test -z "$(which tar 2> /dev/null)" ; then
		skip "need tar"
	fi

	# Create the layer record.
	run storage --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Set up the layer's contents.
	createrandom ${TESTDIR}/datafile1
	createrandom ${TESTDIR}/datafile2
	(cd ${TESTDIR}; tar cf - datafile1 datafile2) > ${TESTDIR}/diff
	storage apply-diff -f ${TESTDIR}/diff $layer

	# Add some contents to the layer.
	createrandom ${TESTDIR}/datafile3
	storage copy ${TESTDIR}/datafile3 ${layer}:/datafile3

	# Create the image record.
	run storage --debug=false create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Check if we can detect that file being added.
	run storage check -r
	echo "check -r:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "layer ${layer}: +/datafile3, layer content modified" ]]

	# Should be all clear now.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect when one of an image's layers is gone, or at least
# doesn't correspond to one that we know of.
@test "check-image-layer-missing" {
	# Create the layer record.
	run storage --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Create the image record.
	run storage --debug=false create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]
	image="$output"

	# Delete the layer with no safety checking.
	run storage --debug=false delete $layer
	echo delete layer: "$output"
	[[ $status -eq 0 ]]

	# Check that we know to flag the image as damaged, and fix it.
	run storage check -r
	echo "check -r:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "image ${image}: layer ${layer}: image layer is missing" ]]

	# Check that we no longer think there's damage.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect when one of an image's layers is gone, or at least
# doesn't correspond to one that we know of.
@test "check-ro-image-layer-missing" {
	case "$STORAGE_DRIVER" in
	overlay*|vfs)
		;;
	*)
		skip "driver $STORAGE_DRIVER does not support additional image stores"
		;;
	esac

	# Create the read-only layer record.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Create the read-only image record.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]
	image="$output"

	# Delete the layer with no safety checking.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot delete $layer
	echo delete layer: "$output"
	[[ $status -eq 0 ]]

        storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot shutdown

	# Create a read-write layer.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	rwlayer=$output

	# Create a read-write image.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-image $rwlayer
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Check that we know to flag the image as damaged, even if we can't fix it.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check -r
	echo "check -r:" "$output"
	[[ $status -ne 0 ]] # we can't fix it
	[[ $output =~ "image ${image}: layer ${layer}: image layer is missing" ]]

	# Check that we no longer think there's damage if we just look at read-write content.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect when a container's base image is gone, or at least
# doesn't correspond to one that we know of.
@test "check-container-image-missing" {
	# Create the layer.
	run storage --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Create the image that uses that layer.
	run storage --debug=false create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]
	image=$output

	# Create a container based on that image.
	run storage --debug=false create-container $image
	echo create-container: "$output"
	[[ $status -eq 0 ]]
	container=$output

	# Delete the image with no safety checks.
	run storage --debug=false delete $image
	echo delete image: "$output"
	[[ $status -eq 0 ]]

	# Check and repair.  Repair is okay with deleting images because they
	# can be rebuilt or re-pulled.
	run storage check -r
	echo "check -r:" "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "container ${container}:" ]]
	[[ $output =~ "image ${image}: image missing" ]]

	# didn't get rid of the container, though!

	run storage check
	echo check: "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "container ${container}:" ]]
	[[ $output =~ "image ${image}: image missing" ]]

	# Repair, but now we're okay with getting rid of containers.
	run storage check -r -f
	echo "check -r -f:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "container ${container}:" ]]
	[[ $output =~ "image ${image}: image missing" ]]

	# Should be all clear now.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect when a container's base image is deleted in a
# read-only store, or at least doesn't correspond to one that we know of.
@test "check-container-ro-image-missing" {
	case "$STORAGE_DRIVER" in
	overlay*|vfs)
		;;
	*)
		skip "driver $STORAGE_DRIVER does not support additional image stores"
		;;
	esac

	# Create the read-only layer.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Create the read-only image that uses that layer.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]
	image=$output

        storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot shutdown

	# Create a container based on that image.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-container $image
	echo create-container: "$output"
	[[ $status -eq 0 ]]
	container=$output
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root container ${container}
	echo container: "$output"
	[[ $status -eq 0 ]]
	clayer=$(grep ^Layer: <<< ${output})
	clayer=${clayer##* }
	echo clayer: "${clayer}"

	# Delete the layer with no safety checks.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot delete $layer
	echo delete layer: "$output"
	[[ $status -eq 0 ]]

	# Delete the image while we're at it.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot delete $image
	echo delete image: "$output"
	[[ $status -eq 0 ]]

        storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot shutdown

	# Check and repair.  Repair is okay with deleting images because they
	# can be rebuilt or re-pulled.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check -r
	echo "check -r:" "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "container ${container}:" ]]
	[[ $output =~ "image ${image}: image missing" ]]
	[[ $output =~ "layer ${clayer} used by container ${container}: layer is in use by a container" ]]

	# couldn't get rid of the container, even so

	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check
	echo check: "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "container ${container}:" ]]
	[[ $output =~ "image ${image}: image missing" ]]

	# Repair, but now we're okay with getting rid of damaged containers.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check -r -f
	echo "check -r -f:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "container ${container}:" ]]
	[[ $output =~ "image ${image}: image missing" ]]

	# Should be all clear now.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect layer data being lost.
@test "check-layer-data-missing" {
	# Create the layer.
	run storage --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Create the image.
	run storage --debug=false create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Set a data item associated with the layer.
	createrandom ${TESTDIR}/datafile
	storage set-layer-data -f ${TESTDIR}/datafile $layer datafile
	run storage --debug=false list-layer-data $layer
	echo list-layer-data: "$output"
	[[ $status -eq 0 ]]
	[[ $output != "" ]]

	# Everything should look okay.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]

	# Delete that content and see if we notice.
	rm -fv ${TESTDIR}/root/${STORAGE_DRIVER}-layers/$layer/datafile
	run storage check -r
	echo "check -r:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "layer ${layer}: data item \"datafile\": layer data item is missing" ]]

	# Should have repaired by deleting the image and layer, so we should be
	# in the clear.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect read-only layer data being lost.
@test "check-ro-layer-data-missing" {
	case "$STORAGE_DRIVER" in
	overlay*|vfs)
		;;
	*)
		skip "driver $STORAGE_DRIVER does not support additional image stores"
		;;
	esac

	# Create the read-only layer.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Create the image.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Set a data item associated with the layer.
	createrandom ${TESTDIR}/datafile
	storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot set-layer-data -f ${TESTDIR}/datafile $layer datafile
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot list-layer-data $layer
	echo list-layer-data: "$output"
	[[ $status -eq 0 ]]
	[[ $output != "" ]]

        storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot shutdown

	# Create a read-write layer.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	rwlayer=$output

	# Create a read-write image.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-image $rwlayer
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Everything should look okay.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check
	echo check: "$output"
	[[ $status -eq 0 ]]

	# Delete that content and see if we notice.
	rm -fv ${TESTDIR}/ro-root/${STORAGE_DRIVER}-layers/$layer/datafile
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check -r
	echo "check -r:" "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "layer ${layer}: data item \"datafile\": layer data item is missing" ]]

	# Can't repair it by deleting the image and layer, so we should be just
	# as broken as last time.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check
	echo check: "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "layer ${layer}: data item \"datafile\": layer data item is missing" ]]

	# A check of just the read-write storage shouldn't turn up anything.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect image data being lost.
@test "check-image-data-missing" {
	# Create the layer.
	run storage --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Create the image.
	run storage --debug=false create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]
	image=$output

	# Create the data associated with the image.
	createrandom ${TESTDIR}/datafile
	storage set-image-data -f ${TESTDIR}/datafile $image datafile
	run storage --debug=false list-image-data $image
	echo list-image-data: "$output"
	[[ $status -eq 0 ]]
	[[ $output != "" ]]

	# Everything should look good so far.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]

	# Now delete the data and check that we notice it's gone.
	rm -fv ${TESTDIR}/root/${STORAGE_DRIVER}-images/$image/datafile
	run storage check -r
	echo "check -r:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "image ${image}: data item \"datafile\": image data item is missing" ]]

	# Having repaired it by deleting the offending image, we should be okay again.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect image data in read-only stores being lost.
@test "check-ro-image-data-missing" {
	case "$STORAGE_DRIVER" in
	overlay*|vfs)
		;;
	*)
		skip "driver $STORAGE_DRIVER does not support additional image stores"
		;;
	esac

	# Create the read-only layer.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Create the image.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]
	image=$output

	# Create the data associated with the image.
	createrandom ${TESTDIR}/datafile
	storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot set-image-data -f ${TESTDIR}/datafile $image datafile
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot list-image-data $image
	echo list-image-data: "$output"
	[[ $status -eq 0 ]]
	[[ $output != "" ]]

	# Everything should look good so far.
	run storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot check
	echo check: "$output"
	[[ $status -eq 0 ]]

        storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot shutdown

	# Create a read-write layer.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	rwlayer=$output

	# Create a read-write image.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-image $rwlayer
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Now delete the data and check that we notice it's gone.
	rm -fv ${TESTDIR}/ro-root/${STORAGE_DRIVER}-images/$image/datafile
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check -r
	echo "check -r:" "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "image ${image}: data item \"datafile\": image data item is missing" ]]

	# Having been unable to repair it by deleting the offending image, we
	# should still flag the error.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check
	echo check: "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "image ${image}: data item \"datafile\": image data item is missing" ]]

	# A check of just the read-write storage shouldn't turn up anything.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect image data being modified.
@test "check-image-data-modified" {
	# Create the layer.
	run storage --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Create the image.
	run storage --debug=false create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]
	image=$output

	# Create some data to associate with the image.
	createrandom ${TESTDIR}/datafile
	storage set-image-data -f ${TESTDIR}/datafile $image datafile
	run storage --debug=false list-image-data $image
	echo list-image-data: "$output"
	[[ $status -eq 0 ]]
	[[ $output != "" ]]

	# Everything should look okay so far.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]

	# Corrupt that data and see if we notice.
	echo "" >> ${TESTDIR}/root/${STORAGE_DRIVER}-images/$image/datafile
	run storage check -r
	echo "check -r:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "image ${image}: data item \"datafile\": image data item has incorrect size" ]]

	# We fixed that by removing the image, so everything should be okay now.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect image data being modified in read-only locations.
@test "check-ro-image-data-modified" {
	case "$STORAGE_DRIVER" in
	overlay*|vfs)
		;;
	*)
		skip "driver $STORAGE_DRIVER does not support additional image stores"
		;;
	esac

	# Create the read-only layer.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	layer=$output

	# Create the read-only image.
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot create-image $layer
	echo create-image: "$output"
	[[ $status -eq 0 ]]
	image=$output

	# Create some data to associate with the read-only image.
	createrandom ${TESTDIR}/datafile
	storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot set-image-data -f ${TESTDIR}/datafile $image datafile
	run storage --debug=false --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot list-image-data $image
	echo list-image-data: "$output"
	[[ $status -eq 0 ]]
	[[ $output != "" ]]

	# Everything should look okay so far.
	run storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot check
	echo check: "$output"
	[[ $status -eq 0 ]]

        storage --graph ${TESTDIR}/ro-root --run ${TESTDIR}/ro-runroot shutdown

	# Create a read-write layer.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	rwlayer=$output

	# Create a read-write image.
	run storage --debug=false --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root create-image $rwlayer
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Corrupt that data and see if we notice.
	echo "" >> ${TESTDIR}/ro-root/${STORAGE_DRIVER}-images/$image/datafile
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check -r
	echo "check -r:" "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "image ${image}: data item \"datafile\": image data item has incorrect size" ]]

	# We couldn't fix that by removing the image, so we should still notice the problem.
	run storage --storage-opt ${STORAGE_DRIVER}.imagestore=${TESTDIR}/ro-root check
	echo check: "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "image ${image}: data item \"datafile\": image data item has incorrect size" ]]

	# A check of just the read-write storage shouldn't turn up anything.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect container data being lost.
@test "check-container-data-missing" {
	# Create a container that isn't using an image as its base.
	run storage --debug=false create-container ""
	echo create-container: "$output"
	[[ $status -eq 0 ]]
	container=$output

	# Store some data alongside the container.
	createrandom ${TESTDIR}/datafile
	storage set-container-data -f ${TESTDIR}/datafile $container datafile
	run storage --debug=false list-container-data $container
	echo list-container-data: "$output"
	[[ $status -eq 0 ]]
	[[ $output != "" ]]

	# Everything should look okay so far.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]

	# Now remove the associated data and see if we notice.
	rm -fv ${TESTDIR}/root/${STORAGE_DRIVER}-containers/$container/datafile
	run storage check -r
	echo "check -r:" "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "container ${container}: data item \"datafile\": container data item is missing" ]]

	# didn't get rid of the container, though

	# Should still look broken.
	run storage check
	echo check: "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "container ${container}: data item \"datafile\": container data item is missing" ]]

	# Now let repair remove containers.
	run storage check -r -f
	echo "check -r -f:" "$output"
	[[ $status -eq 0 ]]

	# Should look okay now.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}

# Check that we can detect container data being modified.
@test "check-container-data-modified" {
	# Create a container that isn't using an image as its base.
	run storage --debug=false create-container ""
	echo create-container: "$output"
	[[ $status -eq 0 ]]
	container=$output

	# Store some data alongside the container.
	createrandom ${TESTDIR}/datafile
	storage set-container-data -f ${TESTDIR}/datafile $container datafile
	run storage --debug=false list-container-data $container
	echo list-container-data: "$output"
	[[ $status -eq 0 ]]
	[[ $output != "" ]]

	# Everything should look okay so far.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]

	# Create a read-write layer.
	run storage --debug=false create-layer
	echo create-layer: "$output"
	[[ $status -eq 0 ]]
	rwlayer=$output

	# Create a read-write image.
	run storage --debug=false create-image $rwlayer
	echo create-image: "$output"
	[[ $status -eq 0 ]]

	# Now remove the associated data and see if we notice.
	echo "" >> ${TESTDIR}/root/${STORAGE_DRIVER}-containers/$container/datafile
	run storage check -r
	echo "check -r:" "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "container ${container}: data item \"datafile\": container data item has incorrect size" ]]

	# didn't get rid of the container, though

	# Should still look broken.
	run storage check
	echo check: "$output"
	[[ $status -ne 0 ]]
	[[ $output =~ "container ${container}: data item \"datafile\": container data item has incorrect size" ]]

	# Now let repair remove containers.
	run storage check -r -f
	echo "check -r -f:" "$output"
	[[ $status -eq 0 ]]
	[[ $output =~ "container ${container}: data item \"datafile\": container data item has incorrect size" ]]

	# Should look okay now.
	run storage check
	echo check: "$output"
	[[ $status -eq 0 ]]
}