test 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/bin/bash
  2. shellfu import jat_dump
  3. shellfu import preupg
  4. shellfu import preupg_fupath
  5. shellfu import xcase
  6. #shellcheck disable=SC2034
  7. {
  8. PREUPG__RULE=xccdf_preupg_rule_api_nativity_check
  9. JAT_DUMP__LIMIT_L=2000
  10. JAT_DUMP__LIMIT_B=20000
  11. Fn=
  12. Dno=
  13. Check=
  14. Lang=
  15. }
  16. EXCLUDED=git
  17. FOREIGN=preupgrade-assistant-CoreOS-preupgrade-assistant-Library-dummyreq
  18. NATIVE=rpm
  19. NINST=habakukova_asymetricka-skladanka
  20. assert_foreign() {
  21. #
  22. # Assert that package $1 was not deemed native
  23. #
  24. local pkg=$1
  25. case $Fn in
  26. get_dist_native_list)
  27. jat__cmd -h "package $pkg not considered native" \
  28. -S 1 \
  29. grep "$pkg" r_native
  30. ;;
  31. is_dist_native)
  32. jat__cmp -h "package $pkg not considered native" \
  33. "$(cat r_es)" eq 0
  34. ;;
  35. esac
  36. }
  37. assert_native() {
  38. #
  39. # Assert that package $1 was deemed native
  40. #
  41. local pkg=$1
  42. case $Fn in
  43. get_dist_native_list)
  44. jat__cmd -h "package $pkg considered native" \
  45. grep "$pkg" r_native
  46. ;;
  47. is_dist_native)
  48. jat__cmp -h "package $pkg considered native" \
  49. "$(cat r_es)" eq 0
  50. ;;
  51. esac
  52. }
  53. xcase__enum() {
  54. #
  55. # Enumerate subtest ids
  56. #
  57. # Format:
  58. #
  59. # Fn=FN,Dno=DNO,Check=CHECK
  60. #
  61. # FN is name of relevant function that is tested.
  62. #
  63. # DNO stands for 'dist_native override', and governs value
  64. # passed to preupg__run1() to override
  65. # 'native' mechanism, In this case, it can be:
  66. #
  67. # * 'all', 'sign' or empty - pass directly
  68. #
  69. # * 'excluded' pass special value to exclude one single package
  70. # from nativity
  71. #
  72. # CHECK can be (some make sense only for certain functions):
  73. #
  74. # * 'foreign' - query for surely non-native package
  75. #
  76. # * 'ninst' - query for package that's not installed
  77. #
  78. # * 'native' - query for "surely" native package (this can be hard to
  79. # maintain when distribution this test is runnning is under
  80. # development)
  81. #
  82. # * 'excluded' - query for package excluded from native by DNO
  83. #
  84. # * 'diff' - use standard diff tool to compare expected result to
  85. # actual result
  86. #
  87. {
  88. echo Fn=is_dist_native,Dno=,Check=foreign
  89. echo Fn=is_dist_native,Dno=,Check=native
  90. echo Fn=is_dist_native,Dno=,Check=ninst
  91. echo Fn=get_dist_native_list,Dno=,Check=foreign
  92. echo Fn=get_dist_native_list,Dno=,Check=native
  93. echo Fn=get_dist_native_list,Dno=all,Check=diff
  94. echo Fn=get_dist_native_list,Dno=excluded,Check=diff
  95. echo Fn=get_dist_native_list,Dno=excluded,Check=excluded
  96. # echo Fn=get_dist_native_list,Dno=sign,Check=diff #FIXME find out how to get oracle
  97. # echo Fn=get_dist_native_list,Dno=,Check=diff #FIXME find out how to get oracle
  98. } | xcase__per Lang py sh
  99. }
  100. xcase__setup() {
  101. #
  102. # Prepare for subtest
  103. #
  104. # Dropped files:
  105. #
  106. # * r_native -- result: native package name list from get_dist_native_list()
  107. # * r_es -- result: exit status after calling is_dist_native()
  108. #
  109. local t_dno # dist_native override
  110. # Create module
  111. #
  112. {
  113. echo "[MODULE]"
  114. echo "GROUP = api"
  115. echo "NAME = nativity"
  116. echo "LANG = $Lang"
  117. #shellcheck disable=SC2028
  118. case $Lang in
  119. sh)
  120. case $Fn:$Check in
  121. is_dist_native:foreign) echo "CODE = is_dist_native '$FOREIGN'; echo \$? > '$PWD/r_es'" ;;
  122. is_dist_native:native) echo "CODE = is_dist_native '$NATIVE'; echo \$? > '$PWD/r_es'" ;;
  123. is_dist_native:ninst) echo "CODE = is_dist_native '$NINST'; echo \$? > '$PWD/r_es'" ;;
  124. get_dist_native_list:*) echo "CODE = get_dist_native_list | LC_ALL=C sort > '$PWD/r_native'" ;;
  125. esac
  126. echo "CODE = exit_pass"
  127. ;;
  128. py)
  129. echo "CODE = def keep_native(lst):"
  130. echo "CODE = with open('$PWD/r_native', 'w') as fh:"
  131. echo "CODE = fh.write('\\n'.join(sorted(lst)) + '\\n')"
  132. echo "CODE = "
  133. echo "CODE = def keep_es(bit):"
  134. echo "CODE = with open('$PWD/r_es', 'w') as fh:"
  135. echo "CODE = fh.write('0\\n' if bit else '1\\n')"
  136. echo "CODE = "
  137. case $Fn:$Check in
  138. is_dist_native:foreign) echo "CODE = keep_es(is_dist_native('$FOREIGN'))" ;;
  139. is_dist_native:native) echo "CODE = keep_es(is_dist_native('$NATIVE'))" ;;
  140. is_dist_native:ninst) echo "CODE = keep_es(is_dist_native('$NINST'))" ;;
  141. get_dist_native_list:*) echo "CODE = keep_native(get_dist_native_list())" ;;
  142. esac
  143. echo "CODE = exit_pass()"
  144. ;;
  145. esac
  146. } > module.ini
  147. preupg_fupath RHEL6_7 module.ini
  148. # prepare dist_native override
  149. #
  150. case $Dno in
  151. all) t_dno="all" ;;
  152. excluded) t_dno="-$EXCLUDED" ;;
  153. sign) t_dno="sign" ;;
  154. "") t_dno="" ;;
  155. esac
  156. # ensure package (non-)presence)
  157. #
  158. case $Check in
  159. foreign) jat__cmd rpm -q "$FOREIGN" ;;
  160. native) jat__cmd rpm -q "$NATIVE" ;;
  161. ninst) jat__cmd -S 1 rpm -q "$NINST" ;;
  162. esac
  163. # collect data
  164. #
  165. PREUPG__DISTNATIVE="$t_dno" \
  166. PREUPG__UPATH="RHEL6_7/all-xccdf.xml" \
  167. preupg__run1
  168. }
  169. xcase__test() {
  170. #
  171. # Do the work
  172. #
  173. # Dropped files:
  174. #
  175. # * rpm_qa -- all installed package names
  176. # * o_native -- oracle: all native package names
  177. # * r_diff -- result: diff of above two (dumped as error)
  178. #
  179. # prepare oracles
  180. #
  181. rpm -qa --qf='%{NAME}\n' | LC_ALL=C sort > rpm_qa
  182. case $Dno:$Check in
  183. all:diff) cp rpm_qa o_native ;;
  184. excluded:diff) <rpm_qa grep -vxF "$EXCLUDED" > o_native ;;
  185. sign:diff) echo 42 >o_native ;; #FIXME: who knows?
  186. :diff) echo 42 >o_native ;; #FIXME: who knows?
  187. esac
  188. # do the test
  189. #
  190. case $Check in
  191. diff) jat__cmd -h "native list is same as expected" \
  192. -o r_diff \
  193. diff -U 1 o_native r_native \
  194. || jat_dump__file -E r_diff ;;
  195. native) assert_native "$NATIVE" ;;
  196. excluded) assert_foreign "$EXCLUDED" ;;
  197. foreign) assert_foreign "$FOREIGN" ;;
  198. ninst) assert_foreign "$NINST" ;;
  199. esac
  200. }
  201. xcase__diag() {
  202. #
  203. # Burp up some diag
  204. #
  205. jat_dump__file module.ini
  206. preupg__get_messages | jat_dump__pipe MESSAGE
  207. }
  208. xcase__cleanup() {
  209. #
  210. # Clean up after subtest
  211. #
  212. preupg__rmresult
  213. }
  214. jat__log_info "EXCLUDED='$EXCLUDED'"
  215. jat__log_info "FOREIGN='$FOREIGN'"
  216. jat__log_info "NATIVE='$NATIVE'"
  217. jat__log_info "NINST='$NINST'"
  218. xcase__run -v
  219. preupg__Cleanup