test 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #!/bin/bash
  2. # vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
  3. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. #
  5. # runtest.sh of /CoreOS/preupgrade-assistant/api/nativity
  6. # Description: package "nativity" functions
  7. # Author: Alois Mahdal <amahdal@redhat.com>
  8. #
  9. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. #
  11. # Copyright (c) 2016 Red Hat, Inc.
  12. #
  13. # This program is free software: you can redistribute it and/or
  14. # modify it under the terms of the GNU General Public License as
  15. # published by the Free Software Foundation, either version 2 of
  16. # the License, or (at your option) any later version.
  17. #
  18. # This program is distributed in the hope that it will be
  19. # useful, but WITHOUT ANY WARRANTY; without even the implied
  20. # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  21. # PURPOSE. See the GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with this program. If not, see http://www.gnu.org/licenses/.
  25. #
  26. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. shellfu import jat_dump
  28. shellfu import preupg
  29. shellfu import preupg_fupath
  30. shellfu import xcase
  31. #shellcheck disable=SC2034
  32. {
  33. PREUPG__RULE=xccdf_preupg_rule_api_nativity_check
  34. JAT_DUMP__LIMIT_L=2000
  35. JAT_DUMP__LIMIT_B=20000
  36. Fn=
  37. Dno=
  38. Check=
  39. Lang=
  40. }
  41. EXCLUDED=git
  42. FOREIGN=preupgrade-assistant-CoreOS-preupgrade-assistant-Library-dummyreq
  43. NATIVE=rpm
  44. NINST=habakukova_asymetricka-skladanka
  45. assert_foreign() {
  46. #
  47. # Assert that package $1 was not deemed native
  48. #
  49. local pkg=$1
  50. case $Fn in
  51. get_dist_native_list)
  52. rlRun "grep '$pkg' r_native" \
  53. 1 "package $pkg not considered native"
  54. ;;
  55. is_dist_native)
  56. rlAssertNotEquals \
  57. "package $pkg not considered native" \
  58. "$(cat r_es)" 0
  59. ;;
  60. esac
  61. }
  62. assert_native() {
  63. #
  64. # Assert that package $1 was deemed native
  65. #
  66. local pkg=$1
  67. case $Fn in
  68. get_dist_native_list)
  69. rlRun "grep '$pkg' r_native" \
  70. 0 "package $pkg considered native"
  71. ;;
  72. is_dist_native)
  73. rlAssertEquals \
  74. "package $pkg considered native" \
  75. "$(cat r_es)" 0
  76. ;;
  77. esac
  78. }
  79. xcase__enum() {
  80. #
  81. # Enumerate subtest ids
  82. #
  83. # Format:
  84. #
  85. # Fn=FN,Dno=DNO,Check=CHECK
  86. #
  87. # FN is name of relevant function that is tested.
  88. #
  89. # DNO stands for 'dist_native override', and governs value
  90. # passed to preupg__run1() to override
  91. # 'native' mechanism, In this case, it can be:
  92. #
  93. # * 'all', 'sign' or empty - pass directly
  94. #
  95. # * 'excluded' pass special value to exclude one single package
  96. # from nativity
  97. #
  98. # CHECK can be (some make sense only for certain functions):
  99. #
  100. # * 'foreign' - query for surely non-native package
  101. #
  102. # * 'ninst' - query for package that's not installed
  103. #
  104. # * 'native' - query for "surely" native package (this can be hard to
  105. # maintain when distribution this test is runnning is under
  106. # development)
  107. #
  108. # * 'excluded' - query for package excluded from native by DNO
  109. #
  110. # * 'diff' - use standard diff tool to compare expected result to
  111. # actual result
  112. #
  113. {
  114. echo Fn=is_dist_native,Dno=,Check=foreign
  115. echo Fn=is_dist_native,Dno=,Check=native
  116. echo Fn=is_dist_native,Dno=,Check=ninst
  117. echo Fn=get_dist_native_list,Dno=,Check=foreign
  118. echo Fn=get_dist_native_list,Dno=,Check=native
  119. echo Fn=get_dist_native_list,Dno=all,Check=diff
  120. echo Fn=get_dist_native_list,Dno=excluded,Check=diff
  121. echo Fn=get_dist_native_list,Dno=excluded,Check=excluded
  122. # echo Fn=get_dist_native_list,Dno=sign,Check=diff #FIXME find out how to get oracle
  123. # echo Fn=get_dist_native_list,Dno=,Check=diff #FIXME find out how to get oracle
  124. } | xcase__per Lang py sh
  125. }
  126. xcase__setup() {
  127. #
  128. # Prepare for subtest
  129. #
  130. # Dropped files:
  131. #
  132. # * r_native -- result: native package name list from get_dist_native_list()
  133. # * r_es -- result: exit status after calling is_dist_native()
  134. #
  135. local t_dno # dist_native override
  136. # Create module
  137. #
  138. {
  139. echo "[MODULE]"
  140. echo "GROUP = api"
  141. echo "NAME = nativity"
  142. echo "LANG = $Lang"
  143. #shellcheck disable=SC2028
  144. case $Lang in
  145. sh)
  146. case $Fn:$Check in
  147. is_dist_native:foreign) echo "CODE = is_dist_native '$FOREIGN'; echo \$? > '$PWD/r_es'" ;;
  148. is_dist_native:native) echo "CODE = is_dist_native '$NATIVE'; echo \$? > '$PWD/r_es'" ;;
  149. is_dist_native:ninst) echo "CODE = is_dist_native '$NINST'; echo \$? > '$PWD/r_es'" ;;
  150. get_dist_native_list:*) echo "CODE = get_dist_native_list | LC_ALL=C sort > '$PWD/r_native'" ;;
  151. esac
  152. echo "CODE = exit_pass"
  153. ;;
  154. py)
  155. echo "CODE = def keep_native(lst):"
  156. echo "CODE = with open('$PWD/r_native', 'w') as fh:"
  157. echo "CODE = fh.write('\\n'.join(sorted(lst)) + '\\n')"
  158. echo "CODE = "
  159. echo "CODE = def keep_es(bit):"
  160. echo "CODE = with open('$PWD/r_es', 'w') as fh:"
  161. echo "CODE = fh.write('0\\n' if bit else '1\\n')"
  162. echo "CODE = "
  163. case $Fn:$Check in
  164. is_dist_native:foreign) echo "CODE = keep_es(is_dist_native('$FOREIGN'))" ;;
  165. is_dist_native:native) echo "CODE = keep_es(is_dist_native('$NATIVE'))" ;;
  166. is_dist_native:ninst) echo "CODE = keep_es(is_dist_native('$NINST'))" ;;
  167. get_dist_native_list:*) echo "CODE = keep_native(get_dist_native_list())" ;;
  168. esac
  169. echo "CODE = exit_pass()"
  170. ;;
  171. esac
  172. } > module.ini
  173. preupg_fupath RHEL6_7 module.ini
  174. # prepare dist_native override
  175. #
  176. case $Dno in
  177. all) t_dno="all" ;;
  178. excluded) t_dno="-$EXCLUDED" ;;
  179. sign) t_dno="sign" ;;
  180. "") t_dno="" ;;
  181. esac
  182. # ensure package (non-)presence)
  183. #
  184. case $Check in
  185. foreign) rlAssertRpm "$FOREIGN" ;;
  186. native) rlAssertRpm "$NATIVE" ;;
  187. ninst) rlAssertNotRpm "$NINST" ;;
  188. esac
  189. # collect data
  190. #
  191. PREUPG__DISTNATIVE="$t_dno" \
  192. PREUPG__UPATH="RHEL6_7/all-xccdf.xml" \
  193. preupg__run1
  194. }
  195. xcase__test() {
  196. #
  197. # Do the work
  198. #
  199. # Dropped files:
  200. #
  201. # * rpm_qa -- all installed package names
  202. # * o_native -- oracle: all native package names
  203. # * r_diff -- result: diff of above two (dumped as error)
  204. #
  205. # prepare oracles
  206. #
  207. rpm -qa --qf='%{NAME}\n' | LC_ALL=C sort > rpm_qa
  208. case $Dno:$Check in
  209. all:diff) cp rpm_qa o_native ;;
  210. excluded:diff) <rpm_qa grep -vxF "$EXCLUDED" > o_native ;;
  211. sign:diff) echo 42 >o_native ;; #FIXME: who knows?
  212. :diff) echo 42 >o_native ;; #FIXME: who knows?
  213. esac
  214. # do the test
  215. #
  216. case $Check in
  217. diff) rlRun "diff -U 1 o_native r_native > r_diff" \
  218. 0 "native list is same as expected" \
  219. || jat_dump__file -E r_diff ;;
  220. native) assert_native "$NATIVE" ;;
  221. excluded) assert_foreign "$EXCLUDED" ;;
  222. foreign) assert_foreign "$FOREIGN" ;;
  223. ninst) assert_foreign "$NINST" ;;
  224. esac
  225. }
  226. xcase__diag() {
  227. #
  228. # Burp up some diag
  229. #
  230. jat_dump__file module.ini
  231. preupg__get_messages | jat_dump__pipe MESSAGE
  232. }
  233. xcase__cleanup() {
  234. #
  235. # Clean up after subtest
  236. #
  237. preupg__rmresult
  238. }
  239. jat__log_info "EXCLUDED='$EXCLUDED'"
  240. jat__log_info "FOREIGN='$FOREIGN'"
  241. jat__log_info "NATIVE='$NATIVE'"
  242. jat__log_info "NINST='$NINST'"
  243. xcase__run -v
  244. preupg__Cleanup