Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
# Copyright 2011 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.


utils_test_case one_test_program__all_pass
one_test_program__all_pass_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="simple_all_pass"}
EOF

    cat >expout <<EOF
simple_all_pass:pass  ->  passed  [S.UUUs]
simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]

2/2 passed (0 failed)
Committed action 1
EOF

    utils_cp_helper simple_all_pass .
    atf_check -s exit:0 -o file:expout -e empty kyua test
}


utils_test_case one_test_program__some_fail
one_test_program__some_fail_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="simple_some_fail"}
EOF

    cat >expout <<EOF
simple_some_fail:fail  ->  failed: This fails on purpose  [S.UUUs]
simple_some_fail:pass  ->  passed  [S.UUUs]

1/2 passed (1 failed)
Committed action 1
EOF

    utils_cp_helper simple_some_fail .
    atf_check -s exit:1 -o file:expout -e empty kyua test
}


utils_test_case many_test_programs__all_pass
many_test_programs__all_pass_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
atf_test_program{name="second"}
atf_test_program{name="third"}
plain_test_program{name="fourth", required_files="/non-existent/foo"}
EOF

    cat >expout <<EOF
first:pass  ->  passed  [S.UUUs]
first:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
second:pass  ->  passed  [S.UUUs]
second:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
third:pass  ->  passed  [S.UUUs]
third:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
fourth:main  ->  skipped: Required file '/non-existent/foo' not found  [S.UUUs]

7/7 passed (0 failed)
Committed action 1
EOF

    utils_cp_helper simple_all_pass first
    utils_cp_helper simple_all_pass second
    utils_cp_helper simple_all_pass third
    echo "not executed" >fourth; chmod +x fourth
    atf_check -s exit:0 -o file:expout -e empty kyua test
}


utils_test_case many_test_programs__some_fail
many_test_programs__some_fail_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
atf_test_program{name="second"}
atf_test_program{name="third"}
plain_test_program{name="fourth"}
EOF

    cat >expout <<EOF
first:fail  ->  failed: This fails on purpose  [S.UUUs]
first:pass  ->  passed  [S.UUUs]
second:fail  ->  failed: This fails on purpose  [S.UUUs]
second:pass  ->  passed  [S.UUUs]
third:pass  ->  passed  [S.UUUs]
third:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
fourth:main  ->  failed: Returned non-success exit status 76  [S.UUUs]

4/7 passed (3 failed)
Committed action 1
EOF

    utils_cp_helper simple_some_fail first
    utils_cp_helper simple_some_fail second
    utils_cp_helper simple_all_pass third
    echo '#! /bin/sh' >fourth
    echo 'exit 76' >>fourth
    chmod +x fourth
    atf_check -s exit:1 -o file:expout -e empty kyua test
}


utils_test_case expect__all_pass
expect__all_pass_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="expect_all_pass"}
EOF

    cat >expout <<EOF
expect_all_pass:die  ->  expected_failure: This is the reason for death  [S.UUUs]
expect_all_pass:exit  ->  expected_failure: Exiting with correct code  [S.UUUs]
expect_all_pass:failure  ->  expected_failure: Oh no: Forced failure  [S.UUUs]
expect_all_pass:signal  ->  expected_failure: Exiting with correct signal  [S.UUUs]
expect_all_pass:timeout  ->  expected_failure: This times out  [S.UUUs]

5/5 passed (0 failed)
Committed action 1
EOF

    utils_cp_helper expect_all_pass .
    atf_check -s exit:0 -o file:expout -e empty kyua test
}


utils_test_case expect__some_fail
expect__some_fail_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="expect_some_fail"}
EOF

    cat >expout <<EOF
expect_some_fail:die  ->  failed: Test case was expected to terminate abruptly but it continued execution  [S.UUUs]
expect_some_fail:exit  ->  failed: Test case expected to exit with code 12 but got code 34  [S.UUUs]
expect_some_fail:failure  ->  failed: Test case was expecting a failure but none were raised  [S.UUUs]
expect_some_fail:pass  ->  passed  [S.UUUs]
expect_some_fail:signal  ->  failed: Test case expected to receive signal 15 but got 9  [S.UUUs]
expect_some_fail:timeout  ->  failed: Test case was expected to hang but it continued execution  [S.UUUs]

1/6 passed (5 failed)
Committed action 1
EOF

    utils_cp_helper expect_some_fail .
    atf_check -s exit:1 -o file:expout -e empty kyua test
}


utils_test_case premature_exit
premature_exit_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="bogus_test_cases"}
EOF

    cat >expout <<EOF
bogus_test_cases:die  ->  broken: Premature exit; test case received signal 9  [S.UUUs]
bogus_test_cases:exit  ->  broken: Premature exit; test case exited with code 0  [S.UUUs]
bogus_test_cases:pass  ->  passed  [S.UUUs]

1/3 passed (2 failed)
Committed action 1
EOF

    utils_cp_helper bogus_test_cases .
    atf_check -s exit:1 -o file:expout -e empty kyua test
}


utils_test_case no_args
no_args_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="simple_all_pass"}
include("subdir/Kyuafile")
EOF
    utils_cp_helper metadata .
    utils_cp_helper simple_all_pass .

    mkdir subdir
    cat >subdir/Kyuafile <<EOF
syntax(2)
test_suite("integration2")
atf_test_program{name="simple_some_fail"}
EOF
    utils_cp_helper simple_some_fail subdir

    cat >expout <<EOF
simple_all_pass:pass  ->  passed  [S.UUUs]
simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
subdir/simple_some_fail:fail  ->  failed: This fails on purpose  [S.UUUs]
subdir/simple_some_fail:pass  ->  passed  [S.UUUs]

3/4 passed (1 failed)
Committed action 1
EOF
    atf_check -s exit:1 -o file:expout -e empty kyua test
}


utils_test_case one_arg__subdir
one_arg__subdir_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("top-level")
include("subdir/Kyuafile")
EOF

    mkdir subdir
    cat >subdir/Kyuafile <<EOF
syntax(2)
test_suite("in-subdir")
atf_test_program{name="simple_all_pass"}
EOF
    utils_cp_helper simple_all_pass subdir

    cat >expout <<EOF
subdir/simple_all_pass:pass  ->  passed  [S.UUUs]
subdir/simple_all_pass:skip  ->  skipped: The reason for skipping is this  [S.UUUs]

2/2 passed (0 failed)
Committed action 1
EOF
    atf_check -s exit:0 -o file:expout -e empty kyua test subdir
}


utils_test_case one_arg__test_case
one_arg__test_case_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("top-level")
atf_test_program{name="first"}
atf_test_program{name="second"}
EOF
    utils_cp_helper simple_all_pass first
    utils_cp_helper simple_all_pass second

    cat >expout <<EOF
first:skip  ->  skipped: The reason for skipping is this  [S.UUUs]

1/1 passed (0 failed)
Committed action 1
EOF
    atf_check -s exit:0 -o file:expout -e empty kyua test first:skip
}


utils_test_case one_arg__test_program
one_arg__test_program_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("top-level")
atf_test_program{name="first"}
atf_test_program{name="second"}
EOF
    utils_cp_helper simple_all_pass first
    utils_cp_helper simple_some_fail second

    cat >expout <<EOF
second:fail  ->  failed: This fails on purpose  [S.UUUs]
second:pass  ->  passed  [S.UUUs]

1/2 passed (1 failed)
Committed action 1
EOF
    atf_check -s exit:1 -o file:expout -e empty kyua test second
}


utils_test_case one_arg__invalid
one_arg__invalid_body() {
cat >experr <<EOF
kyua: E: Test case component in 'foo:' is empty.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua test foo:

cat >experr <<EOF
kyua: E: Program name '/a/b' must be relative to the test suite, not absolute.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua test /a/b
}


utils_test_case many_args__ok
many_args__ok_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("top-level")
include("subdir/Kyuafile")
atf_test_program{name="first"}
EOF
    utils_cp_helper simple_all_pass first

    mkdir subdir
    cat >subdir/Kyuafile <<EOF
syntax(2)
test_suite("in-subdir")
atf_test_program{name="second"}
EOF
    utils_cp_helper simple_some_fail subdir/second

    cat >expout <<EOF
subdir/second:fail  ->  failed: This fails on purpose  [S.UUUs]
subdir/second:pass  ->  passed  [S.UUUs]
first:pass  ->  passed  [S.UUUs]

2/3 passed (1 failed)
Committed action 1
EOF
    atf_check -s exit:1 -o file:expout -e empty kyua test subdir first:pass
}


utils_test_case many_args__invalid
many_args__invalid_body() {
cat >experr <<EOF
kyua: E: Program name component in ':badbad' is empty.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua test this-is-ok :badbad

cat >experr <<EOF
kyua: E: Program name '/foo' must be relative to the test suite, not absolute.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua test this-is-ok /foo
}


utils_test_case many_args__no_match__all
many_args__no_match__all_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("top-level")
atf_test_program{name="first"}
atf_test_program{name="second"}
EOF
    utils_cp_helper simple_all_pass first
    utils_cp_helper simple_all_pass second

    cat >expout <<EOF
Committed action 1
EOF
    cat >experr <<EOF
kyua: W: No test cases matched by the filter 'first1'.
EOF
    atf_check -s exit:1 -o file:expout -e file:experr kyua test first1
}


utils_test_case many_args__no_match__some
many_args__no_match__some_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("top-level")
atf_test_program{name="first"}
atf_test_program{name="second"}
atf_test_program{name="third"}
EOF
    utils_cp_helper simple_all_pass first
    utils_cp_helper simple_all_pass second
    utils_cp_helper simple_some_fail third

    cat >expout <<EOF
first:pass  ->  passed  [S.UUUs]
first:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
third:fail  ->  failed: This fails on purpose  [S.UUUs]
third:pass  ->  passed  [S.UUUs]

3/4 passed (1 failed)
Committed action 1
EOF

    cat >experr <<EOF
kyua: W: No test cases matched by the filter 'fifth'.
kyua: W: No test cases matched by the filter 'fourth'.
EOF
    atf_check -s exit:1 -o file:expout -e file:experr kyua test first fourth \
        third fifth
}


utils_test_case args_are_relative
args_are_relative_body() {
    utils_install_timestamp_wrapper

    mkdir root
    cat >root/Kyuafile <<EOF
syntax(2)
test_suite("integration-1")
atf_test_program{name="first"}
atf_test_program{name="second"}
include("subdir/Kyuafile")
EOF
    utils_cp_helper simple_all_pass root/first
    utils_cp_helper simple_some_fail root/second

    mkdir root/subdir
    cat >root/subdir/Kyuafile <<EOF
syntax(2)
test_suite("integration-2")
atf_test_program{name="third"}
atf_test_program{name="fourth"}
EOF
    utils_cp_helper simple_all_pass root/subdir/third
    utils_cp_helper simple_some_fail root/subdir/fourth

    cat >expout <<EOF
first:pass  ->  passed  [S.UUUs]
first:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
subdir/fourth:fail  ->  failed: This fails on purpose  [S.UUUs]

2/3 passed (1 failed)
Committed action 1
EOF
    atf_check -s exit:1 -o file:expout -e empty kyua test \
        -k "$(pwd)/root/Kyuafile" first subdir/fourth:fail
}


utils_test_case only_load_used_test_programs
only_load_used_test_programs_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
atf_test_program{name="second"}
EOF
    utils_cp_helper simple_all_pass first
    utils_cp_helper bad_test_program second

    cat >expout <<EOF
first:pass  ->  passed  [S.UUUs]
first:skip  ->  skipped: The reason for skipping is this  [S.UUUs]

2/2 passed (0 failed)
Committed action 1
EOF
    CREATE_COOKIE="$(pwd)/cookie"; export CREATE_COOKIE
    atf_check -s exit:0 -o file:expout -e empty kyua test first
    if test -f "${CREATE_COOKIE}"; then
        atf_fail "An unmatched test case has been executed, which harms" \
            "performance"
    fi
}


utils_test_case config_behavior
config_behavior_body() {
    cat >"my-config" <<EOF
syntax(2)
test_suites.suite1["X-the-variable"] = "value1"
test_suites.suite2["X-the-variable"] = "override me"
EOF

    cat >Kyuafile <<EOF
syntax(2)
atf_test_program{name="config1", test_suite="suite1"}
atf_test_program{name="config2", test_suite="suite2"}
atf_test_program{name="config3", test_suite="suite3"}
EOF
    utils_cp_helper config config1
    utils_cp_helper config config2
    utils_cp_helper config config3

    atf_check -s exit:1 -o save:stdout -e empty \
        kyua -c my-config -v test_suites.suite2.X-the-variable=value2 test
    atf_check -s exit:0 -o ignore -e empty \
        grep 'config1:get_variable.*failed' stdout
    atf_check -s exit:0 -o ignore -e empty \
        grep 'config2:get_variable.*passed' stdout
    atf_check -s exit:0 -o ignore -e empty \
        grep 'config3:get_variable.*skipped' stdout
}


utils_test_case store_contents
store_contents_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
atf_test_program{name="some-program", test_suite="suite1"}
EOF
    utils_cp_helper simple_all_pass some-program
    cat >expout <<EOF
some-program:pass  ->  passed  [S.UUUs]
some-program:skip  ->  skipped: The reason for skipping is this  [S.UUUs]

2/2 passed (0 failed)
Committed action 1
EOF

    # TODO(jmmv): The tests below should not care about the specific contents of
    # the database, but we cannot do better yet.  Instead, we should bundle
    # these tests with the tests of the future "report" tests so that we can
    # actually validate the integration of testing plus reporting.

    atf_check -s exit:0 -o file:expout -e empty kyua test
    atf_check -s exit:0 -o inline:'1\n' -e empty \
        kyua db-exec --no-headers "SELECT COUNT(action_id) FROM ACTIONS"
    atf_check -s exit:0 -o inline:'1\n' -e empty \
        kyua db-exec --no-headers "SELECT COUNT(context_id) FROM CONTEXTS"

    rm -f some-program
    utils_cp_helper simple_some_fail some-program
    cat >expout <<EOF
some-program:fail  ->  failed: This fails on purpose  [S.UUUs]
some-program:pass  ->  passed  [S.UUUs]

1/2 passed (1 failed)
Committed action 2
EOF

    atf_check -s exit:1 -o file:expout -e empty kyua test
    atf_check -s exit:0 -o inline:'2\n' -e empty \
        kyua db-exec --no-headers "SELECT COUNT(action_id) FROM ACTIONS"
    atf_check -s exit:0 -o inline:'2\n' -e empty \
        kyua db-exec --no-headers "SELECT COUNT(context_id) FROM CONTEXTS"

cat >expout <<EOF
1,some-program,pass,passed,NULL
1,some-program,skip,skipped,The reason for skipping is this
2,some-program,fail,failed,This fails on purpose
2,some-program,pass,passed,NULL
EOF
    atf_check -s exit:0 -o file:expout -e empty \
        kyua db-exec --no-headers \
        "SELECT actions.action_id, " \
        "       test_programs.relative_path, test_cases.name, " \
        "       test_results.result_type, test_results.result_reason " \
        "FROM actions " \
        "     JOIN test_programs " \
        "     ON actions.action_id = test_programs.action_id " \
        "     JOIN test_cases " \
        "     ON test_programs.test_program_id = test_cases.test_program_id " \
        "     JOIN test_results " \
        "     ON test_cases.test_case_id = test_results.test_case_id " \
        "ORDER BY actions.action_id, test_programs.relative_path, " \
        "         test_cases.name"
}


utils_test_case store_flag__ok
store_flag__ok_body() {
    cat >Kyuafile <<EOF
syntax(2)
atf_test_program{name="config1", test_suite="suite1"}
EOF
    utils_cp_helper config config1

    atf_check -s exit:0 -o ignore -e empty kyua test -s foo1.db
    test -f foo1.db || atf_fail "-s did not work"
    atf_check -s exit:0 -o ignore -e empty kyua test --store=foo2.db
    test -f foo2.db || atf_fail "--store did not work"
    test ! -f .kyua/store.db || atf_fail "Default database created"
}


utils_test_case store_flag__fail
store_flag__fail_body() {
    cat >Kyuafile <<EOF
syntax(2)
atf_test_program{name="config1", test_suite="suite1"}
EOF
    utils_cp_helper config config1

    atf_check -s exit:3 -o empty -e match:"Invalid.*--store" \
        kyua test --store=""
}


utils_test_case build_root_flag
build_root_flag_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
include("subdir/Kyuafile")
EOF

    mkdir subdir
    cat >subdir/Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="second"}
atf_test_program{name="third"}
EOF

    cat >expout <<EOF
first:pass  ->  passed  [S.UUUs]
first:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
subdir/second:pass  ->  passed  [S.UUUs]
subdir/second:skip  ->  skipped: The reason for skipping is this  [S.UUUs]
subdir/third:pass  ->  passed  [S.UUUs]
subdir/third:skip  ->  skipped: The reason for skipping is this  [S.UUUs]

6/6 passed (0 failed)
Committed action 1
EOF

    mkdir build
    mkdir build/subdir
    utils_cp_helper simple_all_pass build/first
    utils_cp_helper simple_all_pass build/subdir/second
    utils_cp_helper simple_all_pass build/subdir/third

    atf_check -s exit:0 -o file:expout -e empty kyua test --build-root=build
}


utils_test_case kyuafile_flag__no_args
kyuafile_flag__no_args_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
This file is bogus but it is not loaded.
EOF

    cat >myfile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="sometest"}
EOF
    utils_cp_helper simple_all_pass sometest

    cat >expout <<EOF
sometest:pass  ->  passed  [S.UUUs]
sometest:skip  ->  skipped: The reason for skipping is this  [S.UUUs]

2/2 passed (0 failed)
Committed action 1
EOF
    atf_check -s exit:0 -o file:expout -e empty kyua test -k myfile
    cat >expout <<EOF
sometest:pass  ->  passed  [S.UUUs]
sometest:skip  ->  skipped: The reason for skipping is this  [S.UUUs]

2/2 passed (0 failed)
Committed action 2
EOF
    atf_check -s exit:0 -o file:expout -e empty kyua test --kyuafile=myfile
}


utils_test_case kyuafile_flag__some_args
kyuafile_flag__some_args_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
This file is bogus but it is not loaded.
EOF

    cat >myfile <<EOF
syntax(2)
test_suite("hello-world")
atf_test_program{name="sometest"}
EOF
    utils_cp_helper simple_all_pass sometest

    cat >expout <<EOF
sometest:pass  ->  passed  [S.UUUs]
sometest:skip  ->  skipped: The reason for skipping is this  [S.UUUs]

2/2 passed (0 failed)
Committed action 1
EOF
    atf_check -s exit:0 -o file:expout -e empty kyua test -k myfile sometest
    cat >expout <<EOF
sometest:pass  ->  passed  [S.UUUs]
sometest:skip  ->  skipped: The reason for skipping is this  [S.UUUs]

2/2 passed (0 failed)
Committed action 2
EOF
    atf_check -s exit:0 -o file:expout -e empty kyua test --kyuafile=myfile \
        sometest
}


utils_test_case interrupt
interrupt_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="interrupts"}
EOF
    utils_cp_helper interrupts .

    kyua \
        -v test_suites.integration.X-body-cookie="$(pwd)/body" \
        -v test_suites.integration.X-cleanup-cookie="$(pwd)/cleanup" \
        test >stdout 2>stderr &
    pid=${!}
    echo "Kyua subprocess is PID ${pid}"

    while [ ! -f body ]; do
        echo "Waiting for body to start"
        sleep 1
    done
    echo "Body started"
    sleep 1

    echo "Sending INT signal to ${pid}"
    kill -INT ${pid}
    echo "Waiting for process ${pid} to exit"
    wait ${pid}
    ret=${?}
    sed -e 's,^,kyua stdout:,' stdout
    sed -e 's,^,kyua stderr:,' stderr
    echo "Process ${pid} exited"
    [ ${ret} -ne 0 ] || atf_fail 'No error code reported'

    [ -f cleanup ] || atf_fail 'Cleanup part not executed after signal'

    atf_check -s exit:0 -o ignore -e empty grep 'Signal caught' stderr
    atf_check -s exit:0 -o ignore -e empty \
        grep 'kyua: E: Interrupted by signal' stderr
}


utils_test_case no_test_program_match
no_test_program_match_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
EOF
    utils_cp_helper simple_all_pass first
    utils_cp_helper simple_all_pass second

    cat >expout <<EOF
Committed action 1
EOF
    cat >experr <<EOF
kyua: W: No test cases matched by the filter 'second'.
EOF
    atf_check -s exit:1 -o file:expout -e file:experr kyua test second
}


utils_test_case no_test_case_match
no_test_case_match_body() {
    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="first"}
EOF
    utils_cp_helper simple_all_pass first

    cat >expout <<EOF
Committed action 1
EOF
    cat >experr <<EOF
kyua: W: No test cases matched by the filter 'first:foobar'.
EOF
    atf_check -s exit:1 -o file:expout -e file:experr kyua test first:foobar
}


utils_test_case missing_kyuafile__no_args
missing_kyuafile__no_args_body() {
    cat >experr <<EOF
kyua: E: Load of 'Kyuafile' failed: File 'Kyuafile' not found.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua test
}


utils_test_case missing_kyuafile__test_program
missing_kyuafile__test_program_body() {
    mkdir subdir
    cat >subdir/Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="unused"}
EOF
    utils_cp_helper simple_all_pass subdir/unused

    cat >experr <<EOF
kyua: E: Load of 'Kyuafile' failed: File 'Kyuafile' not found.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua test subdir/unused
}


utils_test_case missing_kyuafile__subdir
missing_kyuafile__subdir_body() {
    mkdir subdir
    cat >subdir/Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="unused"}
EOF
    utils_cp_helper simple_all_pass subdir/unused

    cat >experr <<EOF
kyua: E: Load of 'Kyuafile' failed: File 'Kyuafile' not found.
EOF
    atf_check -s exit:2 -o empty -e file:experr kyua test subdir
}


utils_test_case bogus_config
bogus_config_body() {
    mkdir .kyua
    cat >"${HOME}/.kyua/kyua.conf" <<EOF
Hello, world.
EOF

    file_re='.*\.kyua/kyua.conf'
    atf_check -s exit:2 -o empty \
        -e match:"^kyua: E: Load of '${file_re}' failed: Failed to load Lua" \
        kyua test
}


utils_test_case bogus_kyuafile
bogus_kyuafile_body() {
    cat >Kyuafile <<EOF
Hello, world.
EOF
    atf_check -s exit:2 -o empty \
        -e match:"Load of 'Kyuafile' failed: .* Kyuafile:2:" kyua list
}


utils_test_case bogus_test_program
bogus_test_program_body() {
    utils_install_timestamp_wrapper

    cat >Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="crash_on_list"}
atf_test_program{name="non_executable"}
EOF
    utils_cp_helper bad_test_program crash_on_list
    echo 'I am not executable' >non_executable

    cat >expout <<EOF
crash_on_list:__test_cases_list__  ->  broken: Tester did not exit cleanly: kyua-atf-tester: Invalid test cases list header 'This is not a valid test program!'  [S.UUUs]
non_executable:__test_cases_list__  ->  broken: Tester did not exit cleanly: kyua-atf-tester: execvp failed: Permission denied  [S.UUUs]

0/2 passed (2 failed)
Committed action 1
EOF
    atf_check -s exit:1 -o file:expout -e empty kyua test
}


utils_test_case missing_test_program
missing_test_program_body() {
    cat >Kyuafile <<EOF
syntax(2)
include("subdir/Kyuafile")
EOF
    mkdir subdir
    cat >subdir/Kyuafile <<EOF
syntax(2)
test_suite("integration")
atf_test_program{name="ok"}
atf_test_program{name="i-am-missing"}
EOF
    echo 'I should not be touched because the Kyuafile is bogus' >subdir/ok

    cat >experr <<EOF
kyua: E: Load of 'Kyuafile' failed: .*Non-existent test program 'subdir/i-am-missing'.
EOF
    atf_check -s exit:2 -o empty -e "match:$(cat experr)" kyua list
}


atf_init_test_cases() {
    atf_add_test_case one_test_program__all_pass
    atf_add_test_case one_test_program__some_fail
    atf_add_test_case many_test_programs__all_pass
    atf_add_test_case many_test_programs__some_fail
    atf_add_test_case expect__all_pass
    atf_add_test_case expect__some_fail
    atf_add_test_case premature_exit

    atf_add_test_case no_args
    atf_add_test_case one_arg__subdir
    atf_add_test_case one_arg__test_case
    atf_add_test_case one_arg__test_program
    atf_add_test_case one_arg__invalid
    atf_add_test_case many_args__ok
    atf_add_test_case many_args__invalid
    atf_add_test_case many_args__no_match__all
    atf_add_test_case many_args__no_match__some

    atf_add_test_case args_are_relative

    atf_add_test_case only_load_used_test_programs

    atf_add_test_case config_behavior

    atf_add_test_case store_contents
    atf_add_test_case store_flag__ok
    atf_add_test_case store_flag__fail

    atf_add_test_case build_root_flag

    atf_add_test_case kyuafile_flag__no_args
    atf_add_test_case kyuafile_flag__some_args

    atf_add_test_case interrupt

    atf_add_test_case no_test_program_match
    atf_add_test_case no_test_case_match

    atf_add_test_case missing_kyuafile__no_args
    atf_add_test_case missing_kyuafile__test_program
    atf_add_test_case missing_kyuafile__subdir

    atf_add_test_case bogus_config
    atf_add_test_case bogus_kyuafile
    atf_add_test_case bogus_test_program
    atf_add_test_case missing_test_program
}