File: 9000-implements.yarn

package info (click to toggle)
obnam 1.21-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,032 kB
  • ctags: 2,505
  • sloc: python: 14,404; sh: 410; ansic: 252; makefile: 66; awk: 5
file content (782 lines) | stat: -rw-r--r-- 26,854 bytes parent folder | download | duplicates (2)
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
Test implementation
===================

This chapter documents the generic, shared IMPLEMENTS sections for
steps that are used in a variety of scenarios. It also discusses the
shell library that may be used by all IMPLEMENTS sections.

The shell library
-----------------

The shell library contains shell functions and sets some shell
variables that can be used by any IMPLEMENTS sections.

Variables:

* `REPO`: the pathname of the backup repository.

Functions:

* `run_obnam`: run Obnam from the source tree, ignoring any
  system-wide or user configuration and using only the configuration
  specified by the test suite itself (`--no-default-config`). Run in
  quiet mode (`--quiet`). The first argument to `run_obnam` is the
  client name.

* `manifest`: run summain in a way that produces a useable manifest,
  which can be compared with diff with a later manifest.

Live data generation
--------------------

The simplest way to generate test live data is to just generate the
necessary number of bytes, split over some number of files. We have
the user name the directory explicitly, to avoid hidden dependencies
between steps.

    IMPLEMENTS GIVEN (\S+) of new data in directory (\S+)
    genbackupdata --quiet --create "$MATCH_1" "$DATADIR/$MATCH_2"

Sometimes we need an amount of data in a specific file.

    IMPLEMENTS GIVEN (\S+) of data in file (\S+)
    "$SRCDIR/mkdata" --size "$MATCH_1" "$DATADIR/$MATCH_2"

We also need to generate a sparse file. A sparse file has at least one
hole in it, and it may matter where the hole is: at the beginning,
middle, or end of the file. Thus, we provide a way for scenarios to
specify that.

    IMPLEMENTS GIVEN a file (\S+) in (\S+), with (.+)
    mkdir -p "$DATADIR/$MATCH_2"
    "$SRCDIR/mksparse" "$DATADIR/$MATCH_2/$MATCH_1" "$MATCH_3"

Create some _interesting_ data, using the `mkfunnyfarm` utility.
See the utility for details, but this is about creating files and
other filesystem objects that are not the most common easy cases for
backup programs (regular files with data and no holes).

    IMPLEMENTS GIVEN directory (\S+) with interesting filesystem objects
    "$SRCDIR/mkfunnyfarm" "$DATADIR/$MATCH_1"

Some directories will be tagged as cache directories
(see [Cache directory tagging](http://www.bford.info/cachedir/)).

    IMPLEMENTS GIVEN directory (\S+) is tagged as a cache directory
    printf 'Signature: 8a477f597d28d172789f06886806bc55' \
        > "$DATADIR/$MATCH_1/CACHEDIR.TAG"

Sometimes it is necessary to set the modification filestamp of a file.
Actually, it's usually useful to set both `st_mtime` and `st_atime` to
the same value. The timestamp is given in the "seconds after epoch" in
UTC format, as is common in Unix.

    IMPLEMENTS GIVEN file (\S+) has Unix timestamp (-?\d+)
    parent=$(dirname "$MATCH_1")
    if [ ! -e "$DATADIR/$parent" ]
    then
        mkdir "$DATADIR/$parent"
    fi
    touch "$DATADIR/$MATCH_1"
    python -c '
    import os
    filename = os.path.join(
        os.environ["DATADIR"],
        os.environ["MATCH_1"])
    timestamp = int(os.environ["MATCH_2"])
    os.utime(filename, (timestamp, timestamp))
    '
    
Create a file with given permissions.

    IMPLEMENTS GIVEN file (\S+) with permissions (\S+)
    touch "$DATADIR/$MATCH_1"
    chmod "$MATCH_2" "$DATADIR/$MATCH_1"

Create a directory with given permissions.

    IMPLEMENTS GIVEN directory (\S+) with permissions (\S+)
    install -d -m "$MATCH_2" "$DATADIR/$MATCH_1"

We need to manipulate extended attributes.

    IMPLEMENTS GIVEN file (\S+) has extended attribute (\S+) set to (\S+)
    mkdir -p $(dirname "$DATADIR/$MATCH_1")
    setfattr --name="$MATCH_2" --value "$MATCH_3" "$DATADIR/$MATCH_1"

Create a symlink.

    IMPLEMENTS GIVEN a symlink (\S+) pointing at (\S+)
    ln -s "$MATCH_2" "$DATADIR/$MATCH_1"

Sometimes we need to remove a file.

    IMPLEMENTS WHEN user (\S+) removes file (\S+)
    rm -f "$DATADIR/$MATCH_2"

Copy a file.

    IMPLEMENTS GIVEN a copy of (.+) in (.+)
    mkdir -p "$DATADIR/$(dirname "$MATCH_2")"
    cp -a "$DATADIR/$MATCH_1" "$DATADIR/$MATCH_2"

Reset a repository's chunk files.

    IMPLEMENTS WHEN repository (.+) resets its chunks to those in (.+)
    r1="$DATADIR/$MATCH_1"
    r2="$DATADIR/$MATCH_2"
    if [ -e "$r1/chunks" ]
    then
        # format 6
        rm -rf "$r1/chunks"
        cp -a "$r2/chunks" "$r1/."
    else
        rm -rf "$r1/chunk-store"
        cp -a "$r2/chunk-store" "$r1/."
    fi


Manifest creation and checking
------------------------------

We make it explicit in the scenario when a manifest is generated, so
that naming of the manifest is explicit. This reduces the need to
debug weird test suite bugs, when an automatic or implicit manifest
naming goes wrong.

    IMPLEMENTS GIVEN a manifest of (\S+) in (\S+)
    manifest "$DATADIR/$MATCH_1" > "$DATADIR/$MATCH_2"

We need to check a directory tree against an existing manifest. We do
this by generating a temporary manifest and diffing against that. We
store the temporary manifest in a file so that if things fail, we can
see what the temporary manifest actually contains. Being able to look
at the actual file is easier than trying to interpret complicated
diffs.

We remove the restore directory prefix from the manifest (the `Name:`
field that Summain outputs). This is necessary so that comparisons
with **diff**(1) will work well.

    IMPLEMENTS THEN (\S+), restored to (\S+), matches manifest (\S+)
    manifest "$DATADIR/$MATCH_2/$DATADIR/$MATCH_1" |
        sed "s*$DATADIR/$MATCH_2/**" > "$DATADIR/temp-manifest"

    diff -u "$DATADIR/$MATCH_3" "$DATADIR/temp-manifest"
    rm -f "$DATADIR/temp-manifest"

We may also need to check two manifests against each other.

    IMPLEMENTS THEN manifests (\S+) and (\S+) match
    diff -u "$DATADIR/$MATCH_1" "$DATADIR/$MATCH_2"

In a special case, we may need to update the `Mtime` for the first
entry in a manifest. This is used, at least, when testing cache
directory exclusion: we create wanted data, then create a manifest,
then add the cache directory. This invalidates the Mtime for the first
entry.

    IMPLEMENTS GIVEN manifest (\S+) has Mtime for the first entry set from (\S+)
    x=$(date -r "$DATADIR/$MATCH_2" '+Mtime: %Y-%m-%d %H:%M:%S +0000')
    awk -v "x=$x" '
        !x && /^Mtime:/ { print x; next }
        /^$/ { x = 1 }
        { print }
        ' "$DATADIR/$MATCH_1" > "$DATADIR/new-manifest"
    mv "$DATADIR/new-manifest" "$DATADIR/$MATCH_1"

Sometimes we create manifests with extra stuff. This allows us to
remove them afterwards.

    IMPLEMENTS GIVEN (\S+) is removed from manifest (\S+)
    awk -v skip1="$MATCH_1" -v skip2="$MATCH_1/" '
        $1 == "Name:" &&
            ($2 == skip1 || substr($2, 1, length(skip2)) == skip2) {
                paragraph = ""; ignore = 1; next }
        NF > 0 && !ignore { paragraph = paragraph $0 "\n" }
        NF == 0 && paragraph { printf "%s\n", paragraph; paragraph = "" }
        NF == 0 { ignore = 0 }
        END { if (paragraph) printf "%s", paragraph }
    ' "$DATADIR/$MATCH_2" > "$DATADIR/$MATCH_2.new"
    mv "$DATADIR/$MATCH_2.new" "$DATADIR/$MATCH_2"


Obnam configuration management
------------------------------

In some scenarios, it is easier to maintain a configuration file than
to pass in all the options to `run_obnam` every time. This section
contains steps to do that.

Scenarios involving encryption need to specify the encryption key to
use. We store that.

    IMPLEMENTS GIVEN user (\S+) uses encryption key "(.*)" from (\S+)
    if [ ! -e "$DATADIR/$MATCH_1.gnupg" ]
    then
        mkdir "$DATADIR/$MATCH_1.gnupg"
        cp -a "$SRCDIR/$MATCH_3/." "$DATADIR/$MATCH_1.gnupg/."
        chmod -R 0700 "$DATADIR/$MATCH_1.gnupg"
        add_to_env "$MATCH_1" GNUPGHOME "$DATADIR/$MATCH_1.gnupg"
    else
        # Export public and secret keys from new keyring.
        export GNUPGHOME="$SRCDIR/$MATCH_3"
        gpg --export "$MATCH_2" > "$DATADIR/public.key"
        gpg --export-secret-keys "$MATCH_2" > "$DATADIR/secret.key"

        # Import into the keyring uses for tests.
        export GNUPGHOME="$DATADIR/$MATCH_1.gnupg"
        gpg --import "$DATADIR/public.key"
        gpg --import "$DATADIR/secret.key"
    fi

    add_to_config "$MATCH_1" encrypt-with "$MATCH_2"

Scenarios involving encryption may also use a private keyring directory.

    IMPLEMENTS GIVEN user (\S+) separately uses encryption key "(.*)" from (\S+)
    if [ ! -e "$DATADIR/$MATCH_1.gnupg" ]
    then
        mkdir "$DATADIR/$MATCH_1.gnupg"
        cp -a "$SRCDIR/$MATCH_3/." "$DATADIR/$MATCH_1.gnupg/."
        add_to_config "$MATCH_1" gnupghome "$DATADIR/$MATCH_1.gnupg"
    else
        # Export public and secret keys from new keyring.
        export GNUPGHOME="$SRCDIR/$MATCH_3"
        gpg --export "$MATCH_2" > "$DATADIR/public.key"
        gpg --export-secret-keys "$MATCH_2" > "$DATADIR/secret.key"

        # Import into the keyring uses for tests.
        export GNUPGHOME="$DATADIR/$MATCH_1.gnupg"
        gpg --import "$DATADIR/public.key"
        gpg --import "$DATADIR/secret.key"

        # Use the configuration rather than the environment.
        add_to_config "$MATCH_1" gnupghome "$GNUPGHOME"
        unset GNUPGHOME
    fi

    add_to_config "$MATCH_1" encrypt-with "$MATCH_2"

Encryption scenarios, at least, also need users that pretend to be
someone else.

    IMPLEMENTS GIVEN a user (\S+) calling themselves (\S+)
    add_to_config "$MATCH_1" client-name "$MATCH_2"

Add a setting to a client's configuration file.

    IMPLEMENTS GIVEN user (\S+) sets configuration (\S+) to (.*)
    add_to_config "$MATCH_1" "$MATCH_2" "$MATCH_3"

Backing up
----------

The simplest way to run a backup, for single-client scenarios. In
addition to backing up, this makes a manifest of the data.

    IMPLEMENTS WHEN user (\S+) backs up directory (\S+) to repository (\S+)
    run_obnam "$MATCH_1" backup -r "$DATADIR/$MATCH_3" "$DATADIR/$MATCH_2"

A test may make a backup fail. Run without failing the test so the
failure may be inspected.

    IMPLEMENTS WHEN user (\S+) attempts to back up directory (\S+) to repository (\S+)
    attempt run_obnam "$MATCH_1" \
        backup -r "$DATADIR/$MATCH_3" "$DATADIR/$MATCH_2"

We may also need to backup two directories at once.

    IMPLEMENTS WHEN user (\S+) backs up directories (\S+) and (\S+) to repository (\S+)
    run_obnam "$MATCH_1" backup -r "$DATADIR/$MATCH_4" \
         "$DATADIR/$MATCH_2" "$DATADIR/$MATCH_3"

We can also just pretend to make a backup.

    IMPLEMENTS WHEN user (\S+) pretends to back up directory (\S+) to repository (\S+)
    run_obnam "$MATCH_1" backup --pretend -r "$DATADIR/$MATCH_3" "$DATADIR/$MATCH_2"

fsck'ing a repository
---------------------

Verify that the repository itself is OK, by running `obnam fsck` on
it.

    IMPLEMENTS THEN user (\S+) can fsck the repository (\S+)
    run_obnam "$MATCH_1" fsck -r "$DATADIR/$MATCH_2"

Restoring data
--------------

We need a way to restore data from a test backup repository.

    IMPLEMENTS WHEN user (\S+) restores their latest generation in repository (\S+) into (\S+)
    run_obnam "$MATCH_1" restore -r "$DATADIR/$MATCH_2" \
        --to "$DATADIR/$MATCH_3"

Restore a specific generation. The generation number is an ordinal in
the list of generations, not the "generation id" Obnam assigns, as
that is unpredictable.

    IMPLEMENTS WHEN user (\S+) restores generation (\d+) to (\S+) from repository (\S+)
    client="$MATCH_1"
    gen="$MATCH_2"
    to="$DATADIR/$MATCH_3"
    repo="$DATADIR/$MATCH_4"
    id=$(run_obnam "$client" -r "$repo" genids |
        awk -v "n=$gen" 'NR == n')
    run_obnam "$client" restore -r "$repo" \
        --to "$to" --generation "$id"

We may also need to attempt a restore in a situation when we expect it
to fail.

    IMPLEMENTS WHEN user (\S+) attempts to restore their latest generation in repository (\S+) into (\S+)
    attempt run_obnam "$MATCH_1" restore -r "$DATADIR/$MATCH_2" \
        --to "$DATADIR/$MATCH_3"

We may need to restore only a single file.

    IMPLEMENTS WHEN user (\S+) restores file (\S+) to (\S+) from their latest generation in repository (\S+)
    run_obnam "$MATCH_1" ls -r "$DATADIR/$MATCH_4"
    run_obnam "$MATCH_1" restore -r "$DATADIR/$MATCH_4" \
        --to "$DATADIR/$MATCH_3" "$DATADIR/$MATCH_2"


Verifying live data
-------------------

Run `obnam verify` and remember the output.

    IMPLEMENTS WHEN user (\S+) attempts to verify (\S+) against repository (\S+)
    attempt run_obnam "$MATCH_1" \
        verify -r "$DATADIR/$MATCH_3" "$DATADIR/$MATCH_2"

Verify a random file in live data.

    IMPLEMENTS WHEN user (\S+) attempts to verify a random file in (\S+) against repository (\S+)
    attempt run_obnam "$MATCH_1" \
        verify -r "$DATADIR/$MATCH_3" --verify-randomly=1 "$DATADIR/$MATCH_2"

Removing (forgetting) generations
---------------------------------

Run `obnam forget` with neither a policy of what to keep, nor a
specific list of generations.

    IMPLEMENTS WHEN user (\S+) runs obnam forget without generations or keep policy on repository (\S+)
    run_obnam "$MATCH_1" forget -r "$DATADIR/$MATCH_2"

Remove the oldest generation.

    IMPLEMENTS WHEN user (\S+) forgets the oldest generation in repository (\S+)
    # The grep below at the end of pipeline is there to make sure
    # the pipeline fails if there were no generations.
    id=$(run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_2" genids | 
        head -n1 | grep .)
    run_obnam "$MATCH_1" forget -r "$DATADIR/$MATCH_2" "$id"

Remove the newest generation.

    IMPLEMENTS WHEN user (\S+) forgets their latest generation in repository (\S+)
    # The grep below at the end of pipeline is there to make sure
    # the pipeline fails if there were no generations.
    id=$(run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_2" genids |
        tail -n1 | grep .)
    run_obnam "$MATCH_1" forget -r "$DATADIR/$MATCH_2" "$id"

Remove according to a `--keep` schedule.

    IMPLEMENTS WHEN user (\S+) forgets according to schedule (\S+) in repository (\S+)
    run_obnam "$MATCH_1" forget -r "$DATADIR/$MATCH_3" --keep "$MATCH_2"

Pretend to forget.

    IMPLEMENTS WHEN user (\S+) pretends to forget according to schedule (\S+) in repository (\S+)
    run_obnam "$MATCH_1" \
        forget --pretend -r "$DATADIR/$MATCH_3" --keep "$MATCH_2"

List generations
----------------

List everything in a generation. Capture the listing in a named file.

    IMPLEMENTS WHEN user (\S+) lists latest generation in repository (\S+) into (\S+)
    run_obnam "$MATCH_1" ls -r "$DATADIR/$MATCH_2" > "$DATADIR/$MATCH_3"

List only parts of a generation. Again, capture in a named file.

    IMPLEMENTS WHEN user (\S+) lists (\S+) in latest generation in repository (\S+) into (\S+)
    run_obnam "$MATCH_1" ls -r "$DATADIR/$MATCH_3" "$DATADIR/$MATCH_2" > "$DATADIR/$MATCH_4"

Checks on generations
---------------------

Check that number of generations is correct.

    IMPLEMENTS THEN user (\S+) sees (\d+) generation(s?) in repository (\S+)
    run_obnam "$MATCH_1" generations -r "$DATADIR/$MATCH_4" \
        > "$DATADIR/generation.list"
    n=$(wc -l < "$DATADIR/generation.list")
    test "$MATCH_2" = "$n"

Ditto for generation ids.

    IMPLEMENTS THEN user (\S+) sees (\d+) generation ids in repository (\S+)
    run_obnam "$MATCH_1" generations -r "$DATADIR/$MATCH_3" \
        > "$DATADIR/generation-id.list"
    n=$(wc -l < "$DATADIR/generation-id.list")
    test "$MATCH_2" = "$n"

Check that there are no checkpoint generations.

    IMPLEMENTS THEN user (\S+) sees no checkpoint generations in repository (\S+)
    run_obnam "$MATCH_1" generations -r "$DATADIR/$MATCH_2" \
        > "$DATADIR/generation.list"
    if grep checkpoint "$DATADIR/generation.list"
    then
        echo "Can see checkpoint generations!" 1>&2
        exit 1
    fi

Check timestamps of specific generations.

    IMPLEMENTS THEN user (\S+) has (\d+)(st|nd|rd|th) generation timestamp (.*) in repository (\S+)
    run_obnam "$MATCH_1" generations -r "$DATADIR/$MATCH_5" |
    sed -n "${MATCH_2}p" |
    awk -v "T=$MATCH_4" '
        ($2 " " $3) != T {
            print "Fail to match: " $0
            exit 1 
        }
    '

Diffs between generations
-------------------------

Compute the difference between two generations. The generations are
identified by the ordinal, not generation id, since the ids are
unpredictable.

    IMPLEMENTS WHEN user (\S+) diffs generations (\d+) and (\d+) in repository (\S+) into (\S+)
    id1=$(run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_4" genids | awk -v "n=$MATCH_2" 'NR == n')
    id2=$(run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_4" genids | awk -v "n=$MATCH_3" 'NR == n')
    run_obnam "$MATCH_1" diff -r "$DATADIR/$MATCH_4" "$id1" "$id2" > "$DATADIR/$MATCH_5"

Show the diff between the latest generation and the generation before
that.

    IMPLEMENTS WHEN user (\S+) diffs latest generation in repository (\S+) into (\S+)
    run_obnam "$MATCH_1" diff -r "$DATADIR/$MATCH_2" latest > "$DATADIR/$MATCH_3"

Encryption key management
-------------------------

List clients and the encryption keys they use.

    IMPLEMENTS THEN user (\S+) uses key "(.+)" in repository (\S+)
    run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_3" client-keys |
        grep -x "$MATCH_1 $MATCH_2"

Import a key into one user's keyring from another keyring.

    IMPLEMENTS WHEN user (\S+) imports public key "(.+)" from (\S+)
    GNUPGHOME="$SRCDIR/$MATCH_3" gpg --export --armor "$MATCH_2" |
    GNUPGHOME="$DATADIR/$MATCH_1.gnupg" gpg --import

Add a public key to a repository, but not to the calling user, only to
the shared parts.

    IMPLEMENTS WHEN user (\S+) adds key "(.+)" to repository (\S+) only
    run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_3" \
        add-key --keyid "$MATCH_2"

Add a public key to a repository, and the calling user.

    IMPLEMENTS WHEN user (\S+) adds key "(.+)" to repository (\S+) and self
    run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_3" \
        add-key --keyid "$MATCH_2" "$MATCH_1"

Remove a public key from a repository.

    IMPLEMENTS WHEN user (\S+) removes key "(.+)" from repository (\S+)
    export GNUPGHOME="$DATADIR/$MATCH_1.gnupg"
    keyid="$(
        get_fingerprint "$MATCH_2" |
        awk '{ print substr($0, length-16) }')"
    run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_3" \
        remove-key --keyid "$keyid"

Forget a key from the user's keyring.

    IMPLEMENTS WHEN user (\S+) no longer has key "(.+)"
    export GNUPGHOME="$DATADIR/$MATCH_1.gnupg"
    ls -la "$GNUPGHOME"
    echo fingerprints
    gpg --fingerprint "$MATCH_2"
    echo with colons
    gpg --fingerprint --with-colons "$MATCH_2"
    echo just fingeprint
    gpg --fingerprint --with-colons "$MATCH_2" |
        awk -F: '{ print } /^fpr:/ { print $10; exit }'
    fingerprint="$(get_fingerprint "$MATCH_2")"
    gpg --batch --yes --delete-secret-key "$fingerprint"


Lock management
---------------

We need to lock parts of the repository, and force those locks open.

    IMPLEMENTS WHEN user (\S+) locks repository (\S+)
    run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_2" _lock

Force it open.

    IMPLEMENTS WHEN user (\S+) forces open the lock on repository (\S+)
    run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_2" force-lock


Client management
-----------------

Sometimes, even if rarely, one wants to remove a complete client from
a repository.

    IMPLEMENTS WHEN user (\S+) removes user (\S+) from repository (\S+)
    run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_3" remove-client "$MATCH_2"

After that, one wants to make sure the removed client isn't in the
repository anymore.

    IMPLEMENTS THEN user (\S+) can't see user (\S+) in repository (\S+)
    if run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_3" clients |
        grep -x "$MATCH_2"
    then
        echo "Client $MATCH_2 still in repository $MATCH_3" 1>&2
        exit 1
    fi

We may also want to make sure we do see a client.

    IMPLEMENTS THEN user (\S+) can see user (\S+) in repository (\S+)
    if ! run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_3" clients |
        grep -x "$MATCH_2"
    then
        echo "Client $MATCH_2 is not in repository $MATCH_3" 1>&2
        exit 1
    fi

Checks on results of an attempted operation
-------------------------------------------

The `attempt` shell function in `obnam.sh` runs a command, then
captures its stdout, stderr, and exit code. The scenarios can then
test the results in various ways as separate steps.

    IMPLEMENTS THEN the attempt succeeded
    attempt_exit_was 0

    IMPLEMENTS THEN the attempt failed with exit code (\d+)
    attempt_exit_was "$MATCH_1"

We want to match the stdout against a regular expression.
    
    IMPLEMENTS THEN the output matches "(.+)"
    echo -----------------------
    echo stdout being matched:
    cat "$DATADIR/attempt.stdout"
    echo -----------------------
    attempt_matches stdout "$MATCH_1"

We can also match the stderr against a regular expression.
    
    IMPLEMENTS THEN the error message matches "(.+)"
    echo -----------------------
    echo stderr being matched:
    cat "$DATADIR/attempt.stderr"
    echo -----------------------
    attempt_matches stderr "$MATCH_1"

Checks on files
---------------

Is a file empty?

    IMPLEMENTS THEN file (\S+) is empty
    diff -u /dev/null "$DATADIR/$MATCH_1"

Is a file not empty?

    IMPLEMENTS THEN file (\S+) is not empty
    ! diff -u /dev/null "$DATADIR/$MATCH_1"

Check that a restored file uses at most as much disk space as the
original one in live data.

    IMPLEMENTS THEN file (\S+) from (\S+), restored in (\S+) doesn't use more disk
    old=$(stat -c %b "$DATADIR/$MATCH_2/$MATCH_1")
    new=$(stat -c %b "$DATADIR/$MATCH_3/$DATADIR/$MATCH_2/$MATCH_1")
    test "$old" -lt "$new"

Check that a restored directory is empty.

    IMPLEMENTS THEN (\S+), restored to (\S+), is empty
    if find "$DATADIR/$MATCH_2/$DATADIR/$MATCH_1" -mindepth 1 | grep .
    then
        die "$DATADIR/$MATCH_2/$DATADIR/$MATCH_1 isn't empty"
    fi

Checks on contents of files
---------------------------

Regular expressions are very powerful, and sometimes that power is
warranted to use. It isn't always clear enough to the lay reader, so
be careful. `grep -E` regular expressions are used here.

Does any line match?

    IMPLEMENTS THEN (\S+) matches (.*)$
    grep -E -e "$MATCH_2" -- "$DATADIR/$MATCH_1"

Does first line match?

    IMPLEMENTS THEN first line of (\S+) matches (.*)$
    head -n1 "$DATADIR/$MATCH_1" | grep -E -e "$MATCH_2" --

Do all lines match?

    IMPLEMENTS THEN all lines in (\S+) match (\S+)
    ! grep -E -v -e "$MATCH_2" -- "$DATADIR/$MATCH_1"

Does no line match?

    IMPLEMENTS THEN nothing in (\S+) matches (\S+)
    if grep -E -e "$MATCH_2" -- "$DATADIR/$MATCH_1" | grep '.*'
    then
        echo "At least one line matches, when none may!" 1>&2
        exit 1
    fi

Merely read a file. This checks that the file exists and can be read
by the user.

    IMPLEMENTS WHEN user (\S+) reads file (\S+)
    cat "$DATADIR/$MATCH_2"

Does a restored file match what's in live data?

    IMPLEMENTS THEN file (.+), restored to (.+), matches live data
    cmp "$DATADIR/$MATCH_1" "$DATADIR/$MATCH_2/$DATADIR/$MATCH_1"


Check on user running test suite
--------------------------------

Some tests won't work correctly when `root` is running them.

    IMPLEMENTS ASSUMING not running as root
    test "$(id -u)" != 0

For testing FUSE stuff, we need to check that the relevant stuff is
available. Previously, we did this by checking that the user running
the test suite was in the group `fuse`, but that turns out to have
been a Debianism (removed in the Debian `jessie` release). A better
check is to check that the `fusermount` command is available.

However, if we're on Debian and on a version prior to 8.0, we need to
also be in the `fuse` group. (This can be dropped after support for
those versions of Debian is dropped from Obnam, probably around first
or second quarter of 2015.)

    IMPLEMENTS ASSUMING user can use FUSE

    # We _must_ have fusermount in any case.
    if ! command -v fusermount
    then
        echo "No fusermount found. User cannot use FUSE without it." 1>&2
        exit 1
    fi
    echo "fusermount found"

    # We also must be able to read from /dev/fuse. It might not exist,
    # and if it does, the kernel module providing it might not be
    # loaded. So we read 0 bytes from it, and if that works, it should
    # be OK.
    if ! dd if=/dev/fuse of=/dev/null bs=1 count=0
    then
        echo "Can't read from /dev/null. User can't use FUSE." 1>&2
        exit 1
    fi
    echo "Can read from /dev/fuse."

    # Are we on Debian? If so, /etc/debian_version exists.
    # If it doesn't, we're done.
    if [ ! -e /etc/debian_version ]
    then
        echo "We are not on Debian. User can use FUSE."
        exit 0
    fi

    # Read /etc/debian_version, and interpret it as a floating point
    # number, and compare it to 8.0. Prior to 8.0, we need to be in
    # the fuse group as well.
    if awk '($0 + 0.0) < 8.0 { exit 0 } END { exit 1 }' /etc/debian_version
    then
        echo "We're on Debian prior to 8.0."
        if groups | tr ' ' '\n' | grep -Fx fuse
        then
            echo "User is in group fuse."
        else
            echo "User is NOT in group fuse. User can't use FUSE."
            exit 1
        fi
    else
        echo "We're on Debian 8.0 or later."
    fi

    # We're good.
    echo "User can use FUSE."
    exit 0


Check on whether user extended attributes work
----------------------------------------------

Extended attributes are, at least on some filesystems, a mount-time
option which may be disabled. In fact, experience has shown that it
often is disabled on build servers.

    IMPLEMENTS ASSUMING extended attributes are allowed for users
    touch "$DATADIR/xattr.test"
    setfattr -n user.foo -v bar "$DATADIR/xattr.test"


Nagios
------

Run the Nagios monitoring subcommand.

    IMPLEMENTS WHEN user (\S+) attempts nagios-last-backup-age against repository (\S+)
    attempt run_obnam "$MATCH_1" nagios-last-backup-age \
        -r "$DATADIR/$MATCH_2"

kdirstat
--------

Create a kdirstat cache.

    IMPLEMENTS WHEN user (\S+) creates a kdirstat cache file (\S+) of repository (\S+)
    run_obnam "$MATCH_1" -r "$DATADIR/$MATCH_3" kdirstat > "$DATADIR/$MATCH_2"

Check that the cache mentions each file in the repository. Since grep
-E cannot specifically match a tab we check only for one character of
whitespace.

    IMPLEMENTS THEN for each entry in directory (\S+) a line in the kdircache (\S+) matches it
    find "$DATADIR/$MATCH_1" -type f | while read f ; do \
        grep -E -e "^F[[:space:]]$f" "$DATADIR/$MATCH_2" || exit 1; \
    done
    find "$DATADIR/$MATCH_1" -type p | while read p ; do \
        grep -E -e "^FIFO[[:space:]]$p" "$DATADIR/$MATCH_2" || exit 1; \
    done