test 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/bash
  2. shellfu import jat
  3. shellfu import jat_dump
  4. shellfu import preupg
  5. shellfu import preupg_fupath
  6. shellfu import xcase
  7. T_MESSAGE=
  8. T_RISKTXT='Please, install all required packages (and binaries) and run preupg again to process check properly.'
  9. #shellcheck disable=SC2034
  10. PREUPG__RULE=xccdf_preupg_rule_api_binary_req_check
  11. assert_irisknum() {
  12. jat__cmp -h "number of per-item risks is $1" \
  13. "$(wc -l <irisks)" eq "$1"
  14. }
  15. mkbrq() {
  16. #
  17. # Produce the binary_req INI key/value pair
  18. #
  19. local head=true
  20. test -n "$bins" || return 0
  21. echo -n "binary_req = "
  22. for bin in "${bins[@]}";
  23. do
  24. $head || echo -n ', '
  25. case $bin in
  26. mandatory) echo -n "rpm" ;;
  27. impossible) echo -n "nonexistentbin$(rtoken)" ;;
  28. esac
  29. head=false
  30. done
  31. echo
  32. }
  33. rtoken() {
  34. head -c 1000 /dev/urandom \
  35. | md5sum \
  36. | head -c 7
  37. }
  38. XCASE__ARRAYS=bins
  39. xcase__enum() {
  40. #
  41. # Enumerate subtest ids
  42. #
  43. # Variables:
  44. #
  45. # * lang - language used: `py` for Python, `sh` for Bash
  46. #
  47. # * bins - binary "availability classes" used for testing. This defines
  48. # what kind of binary name will be added to binary_req. Multiple
  49. # classes mean there will be multiple binary names.
  50. #
  51. # `mandatory` means a binary such as "rpm" that is impossible to
  52. # *not* be on the system. `impossible` means a randomly generated
  53. # binary name.
  54. #
  55. #
  56. for lang in py sh;
  57. do
  58. echo lang=$lang,bins=mandatory
  59. echo lang=$lang,bins=impossible
  60. echo lang=$lang,bins=
  61. echo lang=$lang,bins=mandatory+mandatory
  62. echo lang=$lang,bins=mandatory+impossible
  63. echo lang=$lang,bins=impossible+mandatory
  64. echo lang=$lang,bins=impossible+impossible
  65. done
  66. }
  67. #shellcheck disable=SC2154
  68. xcase__setup() {
  69. #
  70. # Prepare for subtest
  71. #
  72. T_MESSAGE=message$(rtoken)
  73. {
  74. echo '[preupgrade]'
  75. mkbrq
  76. echo "[MODULE]"
  77. echo "GROUP = api"
  78. echo "NAME = binary_req"
  79. echo "LANG = $lang"
  80. case $lang in
  81. py)
  82. echo "CODE = log_info('$T_MESSAGE')"
  83. echo "CODE = exit_informational()"
  84. ;;
  85. sh)
  86. echo "CODE = log_info $T_MESSAGE"
  87. echo "CODE = exit_informational"
  88. ;;
  89. esac
  90. } > module.ini
  91. preupg_fupath RHEL6_7 module.ini
  92. jat__cmd -h "work around BZ#1368823" \
  93. cp -ar RHEL6_7 RHEL6_7-backup
  94. PREUPG__UPATH="RHEL6_7/all-xccdf.xml" \
  95. preupg__run1
  96. preupg__get_messages > messages
  97. preupg__get_risks > risks
  98. grep 'HIGH: Binary .* is not installed' risks > irisks
  99. }
  100. xcase__test() {
  101. #
  102. # Do the work
  103. #
  104. local o_behavior=ok # behavior class
  105. local o_irisknum=0 # per-item risks
  106. for bin in "${bins[@]}"
  107. do
  108. case $bin in
  109. impossible)
  110. o_behavior=fail
  111. ((o_irisknum++))
  112. ;;
  113. esac
  114. done
  115. case $o_behavior in
  116. ok)
  117. preupg__assert \
  118. result "informational" \
  119. -N risk.high "$T_RISKTXT" \
  120. msg.info "$T_MESSAGE"
  121. assert_irisknum 0
  122. ;;
  123. fail)
  124. preupg__assert \
  125. result "needs_action" \
  126. risk.high "$T_RISKTXT" \
  127. -N msg.info "$T_MESSAGE"
  128. assert_irisknum $o_irisknum
  129. ;;
  130. esac
  131. }
  132. xcase__diag() {
  133. #
  134. # Burp up some diag
  135. #
  136. jat_dump__file module.ini risks messages irisks
  137. }
  138. xcase__cleanup() {
  139. #
  140. # Clean up after subtest
  141. #
  142. preupg__rmresult
  143. }
  144. xcase__run -v
  145. preupg__Cleanup