File: build_test.sh

package info (click to toggle)
kworkflow 1%3A0.6.2-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,148 kB
  • sloc: sh: 22,233; perl: 2,172; ansic: 96; python: 72; sql: 28; makefile: 19
file content (953 lines) | stat: -rwxr-xr-x 28,350 bytes parent folder | download
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
#!/bin/bash

include './src/build.sh' > /dev/null
include './tests/utils.sh'

oneTimeSetUp()
{
  original_dir="$PWD"
  FAKE_KERNEL="${SHUNIT_TMPDIR}/fake_kernel"
  KW_DATA_DIR="$SHUNIT_TMPDIR"

  kw_etc_dir="$KW_ETC_DIR"
  xdg_config_dirs="$XDG_CONFIG_DIRS"
  KW_BUILD_CONFIG_TEMPLATE="$PWD/etc/init_templates/x86-64/build_template.config"
  parse_configuration "$KW_CONFIG_SAMPLE"
  parse_configuration "$KW_BUILD_CONFIG_SAMPLE" build_config

  BUILD_CONFIG_FILENAME='build.config'

  if [ -x "$(command -v nproc)" ]; then
    PARALLEL_CORES=$(nproc --all)
  else
    PARALLEL_CORES=$(grep -c ^processor /proc/cpuinfo)
  fi
  export PARALLEL_CORES
  export SCALING=$((PARALLEL_CORES / 2))

  shopt -s expand_aliases
  alias get_kernel_release='get_kernel_release_mock'
  alias get_kernel_version='get_kernel_version_mock'
}

oneTimeTearDown()
{
  [[ -d "$SHUNIT_TMPDIR" ]] && rm -rf "$SHUNIT_TMPDIR"
  rm -rf "$SHUNIT_TMPDIR"
}

setUp()
{
  [[ -d "$SHUNIT_TMPDIR" ]] || mkdir -p "$SHUNIT_TMPDIR"
  cp "$KW_BUILD_CONFIG_TEMPLATE" "$SHUNIT_TMPDIR/build.config"

  KW_ETC_DIR="$kw_etc_dir"
  XDG_CONFIG_DIRS="$kw_etc_dir"

  mkdir -p "$FAKE_KERNEL"
  mk_fake_kernel_root "$FAKE_KERNEL"

  # In this case we actually want to exit, since all tests below rely on
  # being in a kernel root
  cd "$FAKE_KERNEL" || {
    fail "($LINENO) It was not possible to move into fake kernel directory"
    return
  }
}

tearDown()
{
  [[ -d "$SHUNIT_TMPDIR" ]] && rm -rf "$SHUNIT_TMPDIR"

  cd "$original_dir" || {
    fail "($LINENO) It was not possible to move back to original directory"
    return
  }
}

function get_kernel_release_mock()
{
  printf '%s\n' '5.4.0-rc7-test'
}

function get_kernel_version_mock()
{
  printf '%s\n' '5.4.0-rc7'
}

function assertConfigurations()
{
  declare -n configurations_ref="$1"
  declare -n expected_configurations_ref="$2"

  # Check if configurations is contained in expected_configurations
  for k in "${!configurations_ref[@]}"; do
    assert_equals_helper "Did not expect setting '$k'" "($LINENO)" "${expected_configurations_ref[$k]+token}" token
    assert_equals_helper "Did not expect setting '$k'" "($LINENO)" "${expected_configurations_ref[$k]}" "${configurations_ref[$k]}"
  done

  # check if configurations has all expected_configurations keys
  for k in "${!expected_configurations_ref[@]}"; do
    assert_equals_helper "Expected setting '$k' to be present" "($LINENO)" "${configurations_ref[$k]+token}" token
  done
}

function test_parse_build_config_success_exit_code()
{
  local build_config=()

  parse_configuration "$KW_BUILD_CONFIG_SAMPLE" build_config
  assert_equals_helper 'kw failed to load a regular config file' "($LINENO)" 0 "$?"
}

# Test if etc/init_templates/build_template.config contains all the expected settings
function test_parse_build_config_standard_config()
{
  build_config=()

  declare -A expected_build_configurations=(
    [arch]='x86_64'
    [kernel_img_name]='bzImage'
    [menu_config]='nconfig'
    [doc_type]='htmldocs'
    [doc_type]='htmldocs'
    [cpu_scaling_factor]=100
  )

  parse_configuration "$SHUNIT_TMPDIR/build.config" build_config
  assertConfigurations build_config expected_build_configurations "$LINENO"
}

# To test the order of config file loading, we will put a file named
# kworkflow.config in each place, in order, and remove the previous one.
# The order is: PWD, XDG_CONFIG_HOME, XDG_CONFIG_DIRS, KW_ETC_DIR
function test_parse_configuration_files_loading_order()
{
  local -a expected
  local original_dir="$PWD"

  build_config=()

  KW_ETC_DIR='1'
  XDG_CONFIG_DIRS='2:3:4'
  XDG_CONFIG_HOME='5'

  local expected=(
    "1/${BUILD_CONFIG_FILENAME}"
    "4/${KWORKFLOW}/${BUILD_CONFIG_FILENAME}"
    "3/${KWORKFLOW}/${BUILD_CONFIG_FILENAME}"
    "2/${KWORKFLOW}/${BUILD_CONFIG_FILENAME}"
    "5/${KWORKFLOW}/${BUILD_CONFIG_FILENAME}"
    'Please use kw init to update your config files'
  )

  output="$(
    function parse_configuration()
    {
      printf '%s\n' "$1"
    }
    load_build_config
  )"

  compare_command_sequence 'Wrong config file reading order' "$LINENO" 'expected' "$output"

  # IF XDG global variables are not defined
  unset XDG_CONFIG_DIRS
  unset XDG_CONFIG_HOME
  HOME='5'

  expected=(
    "1/${BUILD_CONFIG_FILENAME}"
    "/etc/xdg/${KWORKFLOW}/${BUILD_CONFIG_FILENAME}"
    "5/.config/${KWORKFLOW}/${BUILD_CONFIG_FILENAME}"
    'Please use kw init to update your config files'
    "${PWD}/${BUILD_CONFIG_FILENAME}"
  )

  output="$(
    function parse_configuration()
    {
      printf '%s\n' "$1"
    }
    load_build_config
  )"

  compare_command_sequence 'Wrong config file reading order' "$LINENO" 'expected' "$output"
}

function test_load_build_config()
{
  local current_path="$PWD"
  local -a expected

  build_config=()

  KW_ETC_DIR='1'
  unset XDG_CONFIG_DIRS
  unset XDG_CONFIG_HOME
  HOME='5'

  # Test preparation
  mkdir -p "${FAKE_KERNEL}/${KW_DIR}"
  cp "${KW_BUILD_CONFIG_TEMPLATE}" "${FAKE_KERNEL}/${KW_DIR}/build.config"

  expected=(
    "1/${BUILD_CONFIG_FILENAME}"
    "/etc/xdg/${KWORKFLOW}/${BUILD_CONFIG_FILENAME}"
    "5/.config/${KWORKFLOW}/${BUILD_CONFIG_FILENAME}"
    "${PWD}/${KW_DIR}/${BUILD_CONFIG_FILENAME}"
  )

  output="$(
    function parse_configuration()
    {
      printf '%s\n' "$1"
    }
    load_build_config
  )"

  compare_command_sequence '' "$LINENO" 'expected' "$output"
}

function test_kernel_build_cross_compilation_flags()
{
  local expected_result
  local output
  build_config=()

  parse_configuration "$SAMPLES_DIR/build_no_llvm.config" build_config

  # For CI: The tail and head command remove statistics output
  output=$(kernel_build 'TEST_MODE' | tail -n +1 | head -2)

  declare -a expected_cmd=(
    'make -j ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- --silent olddefconfig'
    "make -j${PARALLEL_CORES} ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- W=2 2>&1 | tee ./log"
  )

  compare_command_sequence '' "$LINENO" 'expected_cmd' "$output"
}

function test_kernel_build_menu_cross_compilation_flags()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --menu)
  expected_result='make -j ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- nconfig'
  assertEquals "($LINENO)" "$expected_result" "$output"
}

function test_kernel_build_clean()
{
  local expected_result
  local output

  build_config=()
  parse_configuration "${SAMPLES_DIR}/build_no_log.config" build_config

  output=$(kernel_build 'TEST_MODE' --clean 2> /dev/null)
  expected_result='make clean'
  assertEquals "($LINENO)" "$expected_result" "$output"
}

function test_kernel_build_full_cleanup()
{
  local expected_result
  local output

  build_config=()
  parse_configuration "${SAMPLES_DIR}/build_no_log.config" build_config

  output=$(kernel_build 'TEST_MODE' --full-cleanup 2> /dev/null)
  expected_result='make distclean'
  assertEquals "($LINENO)" "$expected_result" "$output"
}

function test_kernel_build_html_doc()
{
  local expected_result
  local output

  build_config=()
  parse_configuration "$SAMPLES_DIR/build_no_log.config" build_config

  output=$(kernel_build 'TEST_MODE' --doc)
  expected_result="make -j${PARALLEL_CORES} htmldocs"
  assertEquals "($LINENO)" "$expected_result" "$output"
}

function test_kernel_build_html_doc_with_ccache()
{
  local expected_result
  local output

  build_config=()
  parse_configuration "$SAMPLES_DIR/build_no_log.config" build_config

  output=$(kernel_build 'TEST_MODE' --doc --ccache)
  expected_result="make CC=\"ccache gcc -fdiagnostics-color\" -j${PARALLEL_CORES} htmldocs"
  assertEquals "($LINENO)" "$expected_result" "$output"
}

function test_kernel_build_html_doc_with_save_log_option()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --doc --save-log-to docs.out)
  expected_result="make -j${PARALLEL_CORES} htmldocs 2>&1 | tee docs.out"
  assertEquals "($LINENO)" "$expected_result" "$output"
}

function test_kernel_build_invalid_flag()
{
  local output
  local ret

  output=$(kernel_build 'TEST_MODE' --notvalid 2> /dev/null)
  ret="$?"
  assertEquals "($LINENO)" "$ret" 22
}

function test_kernel_build_outside_kernel_repository()
{
  local ret
  local output

  cd "$original_dir" || {
    fail "($LINENO) It was not possible to move back to original directory"
    return
  }

  output=$(kernel_build 'TEST_MODE')
  ret="$?"
  assert_equals_helper 'We expected an error' "($LINENO)" "$ret" 125

  cd "$FAKE_KERNEL" || {
    fail "($LINENO) It was not possible to move into temporary directory"
    return
  }
}

function test_kernel_build_x86()
{
  local expected_result
  local output

  # Test preparation
  cd "$original_dir" || {
    fail "($LINENO) It was not possible to move back to original directory"
    return
  }

  configurations=()
  build_config=()
  cp "$KW_BUILD_CONFIG_SAMPLE_X86" "${FAKE_KERNEL}/build.config"
  parse_configuration "${FAKE_KERNEL}/build.config" build_config

  cd "$FAKE_KERNEL" || {
    fail "($LINENO) It was not possible to move into temporary directory"
    return
  }

  # For CI: The tail and head command remove statistics output
  output=$(kernel_build 'TEST_MODE' | tail -n +1 | head -2)
  declare -a expected_cmd=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make -j$PARALLEL_CORES ARCH=x86_64"
  )

  compare_command_sequence '' "$LINENO" 'expected_cmd' "$output"
}

function test_parse_build_options_check_options_values()
{
  unset options_values
  declare -gA options_values

  # test default options
  parse_build_options

  assert_equals_helper 'Default ARCH did not match expectation' \
    "($LINENO)" 'x86_64' "${options_values['ARCH']}"

  assert_equals_helper 'Default MENU_CONFIG did not match expectation' \
    "($LINENO)" '' "${options_values['MENU_CONFIG']}"

  assert_equals_helper 'Default CROSS_COMPILE did not match expectation' \
    "($LINENO)" '' "${options_values['CROSS_COMPILE']}"

  assert_equals_helper 'Default CPU_SCALING_FACTOR did not match expectation' \
    "($LINENO)" 100 "${options_values['CPU_SCALING_FACTOR']}"

  assert_equals_helper 'Default CCACHE did not match expectation' \
    "($LINENO)" '' "${options_values['CCACHE']}"

  assert_equals_helper 'Default INFO did not match expectation' \
    "($LINENO)" '' "${options_values['INFO']}"

  assert_equals_helper 'Default DOC_TYPE did not match expectation' \
    "($LINENO)" '' "${options_values['DOC_TYPE']}"

  assert_equals_helper 'Default LOG_PATH did not match expectation' \
    "($LINENO)" '' "${options_values['LOG_PATH']}"

  assert_equals_helper 'Default CLEAN did not match expectation' \
    "($LINENO)" '' "${options_values['CLEAN']}"
}

function test_parse_build_options()
{
  unset options_values
  declare -gA options_values
  local output
  local help_output

  # Warning options
  options_values=()
  parse_build_options --warnings
  assert_equals_helper 'Could not set build option WARNINGS' \
    "($LINENO)" '1' "${options_values['WARNINGS']}"

  options_values=()
  parse_build_options -w
  assert_equals_helper 'Could not set build option WARNINGS' \
    "($LINENO)" '1' "${options_values['WARNINGS']}"

  options_values=()
  parse_build_options --warnings 123
  assert_equals_helper 'Could not set build option WARNINGS' \
    "($LINENO)" '123' "${options_values['WARNINGS']}"

  options_values=()
  parse_build_options -w 123
  assert_equals_helper 'Could not set build option WARNINGS' \
    "($LINENO)" '123' "${options_values['WARNINGS']}"

  # Check help option
  help_output=$(build_help)
  options_values=()
  output=$(parse_build_options -h)
  assert_equals_helper 'Could not access build help' \
    "($LINENO)" "$help_output" "$output"

  # Test info
  options_values=()
  parse_build_options --info
  assert_equals_helper 'Could not set build option INFO' \
    "($LINENO)" 1 "${options_values['INFO']}"

  options_values=()
  parse_build_options -i
  assert_equals_helper 'Could not set build option INFO' \
    "($LINENO)" 1 "${options_values['INFO']}"

  # Menu
  options_values=()
  parse_build_options --menu
  assert_equals_helper 'Could not set build option MENU_CONFIG' \
    "($LINENO)" 'nconfig' "${options_values['MENU_CONFIG']}"

  # Doc
  options_values=()
  parse_build_options --doc
  assert_equals_helper 'Could not set build option DOC_TYPE' \
    "($LINENO)" 'htmldocs' "${options_values['DOC_TYPE']}"

  options_values=()
  parse_build_options -d
  assert_equals_helper 'Could not set build option DOC_TYPE' \
    "($LINENO)" 'htmldocs' "${options_values['DOC_TYPE']}"

  # CPU Scaling option
  options_values=()
  parse_build_options --cpu-scaling 150 > /dev/null
  assert_equals_helper 'Could not set build option CPU_SCALING_FACTOR' \
    "($LINENO)" 150 "${options_values['CPU_SCALING_FACTOR']}"

  options_values=()
  parse_build_options -S 150 > /dev/null
  assert_equals_helper 'Could not set build option CPU_SCALING_FACTOR' \
    "($LINENO)" 150 "${options_values['CPU_SCALING_FACTOR']}"

  # CCACHE
  options_values=()
  parse_build_options --ccache
  assert_equals_helper 'Could not set build option CCACHE' \
    "($LINENO)" 1 "${options_values['CCACHE']}"

  # Save log option
  options_values=()
  parse_build_options --save-log-to out.log
  assert_equals_helper 'Could not set build option LOG_PATH' \
    "($LINENO)" 'out.log' "${options_values['LOG_PATH']}"

  options_values=()
  parse_build_options -s out.log
  assert_equals_helper 'Could not set build option LOG_PATH' \
    "($LINENO)" 'out.log' "${options_values['LOG_PATH']}"

  # LLVM
  options_values=()
  parse_build_options --llvm
  assert_equals_helper 'Could not set build option USE_LLVM_TOOLCHAIN' \
    "($LINENO)" 1 "${options_values['USE_LLVM_TOOLCHAIN']}"

  # CLEAN
  options_values=()
  parse_build_options --clean
  assert_equals_helper 'Could not set build option CLEAN' \
    "($LINENO)" 1 "${options_values['CLEAN']}"

  # FULL CLEANUP
  options_values=()
  parse_build_options --full-cleanup
  assert_equals_helper 'Could not set build option FULL_CLEANUP' \
    "($LINENO)" 1 "${options_values['FULL_CLEANUP']}"

  # Unsopported option
  output="$(parse_build_options --mispelled 2>&1)"
  assertEquals "($LINENO)" 22 "$?"
}

function test_build_info()
{
  local release='5.4.0-rc7-test'
  local version='5.4.0-rc7'
  local release_output="Name: ${release}"
  local version_output="Version: ${version}"
  local modules
  local expected_cmd

  declare -a expected_cmd=(
    'Kernel source information'
    "$release_output"
    "$version_output"
  )

  output=$(kernel_build 'TEST_MODE' '--info')
  compare_command_sequence '' "$LINENO" 'expected_cmd' "$output"

  # Change modules
  modules='Total modules to be compiled: 5'
  cp "${original_dir}/tests/samples/.config" '.config'
  expected_cmd[3]="$modules"
  output=$(kernel_build 'TEST_MODE' '--info')
  compare_command_sequence '' "$LINENO" 'expected_cmd' "$output"
}

function test_kernel_build_only_cpu_scaling_option()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --cpu-scaling 50 | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make -j${SCALING} ARCH=x86_64"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_cpu_scaling_and_warning()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --cpu-scaling 50 --warnings 123 | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make -j${SCALING} ARCH=x86_64 W=123"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_cpu_scaling_with_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --cpu-scaling 50 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make -j${SCALING} ARCH=x86_64 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_cpu_scaling_llvm()
{
  local expected_result
  local output

  build_config['USE_LLVM_TOOLCHAIN']='yes'
  output=$(kernel_build 'TEST_MODE' --cpu-scaling 50 --llvm | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make -j$SCALING LLVM=1 ARCH=x86_64"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_warning_and_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --warnings 123 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make -j${PARALLEL_CORES} ARCH=x86_64 W=123 2>&1 | tee log.out"
  )

  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_kernel_ccache_cpu_scaling_warning()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --cpu-scaling 50 --warnings 123 | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache gcc -fdiagnostics-color\" -j${SCALING} ARCH=x86_64 W=123"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"

  output=$(kernel_build 'TEST_MODE' --ccache --cpu-scaling 50 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_cmd=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache gcc -fdiagnostics-color\" -j${SCALING} ARCH=x86_64 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_cmd' "$output"

  output=$(kernel_build 'TEST_MODE' --ccache --warnings 123 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_cmd=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache gcc -fdiagnostics-color\" -j${PARALLEL_CORES} ARCH=x86_64 W=123 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_cmd' "$output"
}

function test_kernel_build_kernel_ccache_cpu_scaling_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --cpu-scaling 50 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache gcc -fdiagnostics-color\" -j${SCALING} ARCH=x86_64 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_ccache_warning_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --warnings 123 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache gcc -fdiagnostics-color\" -j${PARALLEL_CORES} ARCH=x86_64 W=123 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_with_llvm_warning()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --llvm --warnings 123 | tail -n +1 | head -2)
  declare -a expected_cmd=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make -j${PARALLEL_CORES} LLVM=1 ARCH=x86_64 W=123"
  )
  compare_command_sequence '' "($LINENO)" 'expected_cmd' "$output"
}

function test_kernel_build_with_llvm_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --llvm --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make -j${PARALLEL_CORES} LLVM=1 ARCH=x86_64 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_ccache_cpu_scaling_llvm()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --cpu-scaling 50 --llvm | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache clang -fdiagnostics-color\" -j${SCALING} LLVM=1 ARCH=x86_64"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_ccache_cpu_scaling_warning()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --cpu-scaling 50 --warnings 123 | tail -n +1 | head -2)
  declare -a expected_cmd=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache gcc -fdiagnostics-color\" -j${SCALING} ARCH=x86_64 W=123"
  )
  compare_command_sequence '' "($LINENO)" 'expected_cmd' "$output"
}

function test_kernel_build_ccache_cpu_scaling_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --cpu-scaling 50 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache gcc -fdiagnostics-color\" -j${SCALING} ARCH=x86_64 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_ccache_llvm_warning()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --llvm --warnings 123 | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache clang -fdiagnostics-color\" -j${PARALLEL_CORES} LLVM=1 ARCH=x86_64 W=123"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_ccache_llvm_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --llvm --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache clang -fdiagnostics-color\" -j${PARALLEL_CORES} LLVM=1 ARCH=x86_64 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_cmd' "$output"
}

function test_kernel_build_ccache_llvm_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --warnings 123 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache gcc -fdiagnostics-color\" -j${PARALLEL_CORES} ARCH=x86_64 W=123 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_cpu_scaling_llvm_warning()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --cpu-scaling 50 --llvm --warnings 123 | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make -j${SCALING} LLVM=1 ARCH=x86_64 W=123"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_cpu_scaling_llvm_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --cpu-scaling 50 --llvm --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make -j${SCALING} LLVM=1 ARCH=x86_64 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_cpu_scaling_warning_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --cpu-scaling 50 --warnings 123 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_cmd=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make -j${SCALING} ARCH=x86_64 W=123 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_cmd' "$output"
}

function test_kernel_build_ccache_cpu_scaling_llvm_warning()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --cpu-scaling 50 --llvm --warnings 123 | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache clang -fdiagnostics-color\" -j${SCALING} LLVM=1 ARCH=x86_64 W=123"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_ccache_cpu_scaling_llvm_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --cpu-scaling 50 --llvm --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache clang -fdiagnostics-color\" -j${SCALING} LLVM=1 ARCH=x86_64 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_ccache_cpu_scaling_warning_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --cpu-scaling 50 --warnings 123 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_cmd=(
    'make -j ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache gcc -fdiagnostics-color\" -j${SCALING} ARCH=x86_64 W=123 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_cmd' "$output"
}

function test_kernel_build_ccache_llvm_warning_save_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --ccache --llvm --warnings 123 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make CC=\"ccache clang -fdiagnostics-color\" -j${PARALLEL_CORES} LLVM=1 ARCH=x86_64 W=123 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_cpu_scaling_llvm_warning_sava_log_to()
{
  local expected_result
  local output

  output=$(kernel_build 'TEST_MODE' --cpu-scaling 50 --llvm --warnings 123 --save-log-to log.out | tail -n +1 | head -2)
  declare -a expected_result=(
    'make -j LLVM=1 ARCH=x86_64 --silent olddefconfig'
    "make -j$SCALING LLVM=1 ARCH=x86_64 W=123 2>&1 | tee log.out"
  )
  compare_command_sequence '' "($LINENO)" 'expected_result' "$output"
}

function test_kernel_build_inside_an_env()
{
  local output
  local expected_result
  local env_output="${KW_CACHE_DIR}/envs/fake_env"
  build_config=()

  mk_fake_kw_env

  parse_configuration "$SAMPLES_DIR/build_no_llvm.config" build_config

  output=$(kernel_build 'TEST_MODE' | tail -n +1 | head -2)

  declare -a expected_cmd=(
    "make -j ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- --silent olddefconfig O=${env_output}"
    "make -j${PARALLEL_CORES} ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- W=2 2>&1 | tee ./log O=${env_output}"
  )

  compare_command_sequence '' "$LINENO" 'expected_cmd' "$output"
}

function test_kernel_build_html_doc_inside_env()
{
  local expected_result
  local output
  local env_output="${KW_CACHE_DIR}/envs/fake_env"
  build_config=()

  mk_fake_kw_env

  parse_configuration "$SAMPLES_DIR/build_no_log.config" build_config

  output=$(kernel_build 'TEST_MODE' --doc)
  expected_result="make -j${PARALLEL_CORES} htmldocs O=${env_output}"
  assertEquals "($LINENO)" "$expected_result" "$output"
}

function test_kernel_build_menu_inside_env()
{
  local expected_result
  local output
  local env_output="${KW_CACHE_DIR}/envs/fake_env"
  build_config=()

  mk_fake_kw_env

  parse_configuration "$SAMPLES_DIR/build_no_log.config" build_config

  output=$(kernel_build 'TEST_MODE' --menu)
  expected_result="make -j ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- nconfig O=${env_output}"
  assertEquals "($LINENO)" "$expected_result" "$output"
}

function test_kernel_build_clean_inside_env()
{
  local expected_result
  local output
  local env_output="${KW_CACHE_DIR}/envs/fake_env"
  build_config=()

  mk_fake_kw_env

  parse_configuration "${SAMPLES_DIR}/build_no_log.config" build_config

  output=$(kernel_build 'TEST_MODE' --clean 2> /dev/null)
  expected_result="make clean O=${env_output}"
  assertEquals "($LINENO)" "$expected_result" "$output"
}

function test_kernel_build_full_cleanup_inside_env()
{
  local expected_result
  local output
  local env_output="${KW_CACHE_DIR}/envs/fake_env"
  build_config=()

  mk_fake_kw_env

  parse_configuration "${SAMPLES_DIR}/build_no_log.config" build_config

  output=$(kernel_build 'TEST_MODE' --full-cleanup 2> /dev/null)
  expected_result="make distclean O=${env_output}"
  assertEquals "($LINENO)" "$expected_result" "$output"
}

invoke_shunit