123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- #!/bin/bash
- # vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- #
- # runtest.sh of /CoreOS/preupgrade-assistant/api/nativity
- # Description: package "nativity" functions
- # Author: Alois Mahdal <amahdal@redhat.com>
- #
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- #
- # Copyright (c) 2016 Red Hat, Inc.
- #
- # This program is free software: you can redistribute it and/or
- # modify it under the terms of the GNU General Public License as
- # published by the Free Software Foundation, either version 2 of
- # the License, or (at your option) any later version.
- #
- # This program is distributed in the hope that it will be
- # useful, but WITHOUT ANY WARRANTY; without even the implied
- # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- # PURPOSE. See the GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see http://www.gnu.org/licenses/.
- #
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
- shellfu import jat_dump
- shellfu import preupg
- shellfu import preupg_fupath
- shellfu import xcase
-
- #shellcheck disable=SC2034
- {
- PREUPG__RULE=xccdf_preupg_rule_api_nativity_check
- JAT_DUMP__LIMIT_L=2000
- JAT_DUMP__LIMIT_B=20000
- Fn=
- Dno=
- Check=
- Lang=
- }
-
- EXCLUDED=git
- FOREIGN=preupgrade-assistant-CoreOS-preupgrade-assistant-Library-dummyreq
- NATIVE=rpm
- NINST=habakukova_asymetricka-skladanka
-
- assert_foreign() {
- #
- # Assert that package $1 was not deemed native
- #
- local pkg=$1
- case $Fn in
- get_dist_native_list)
- rlRun "grep '$pkg' r_native" \
- 1 "package $pkg not considered native"
- ;;
- is_dist_native)
- rlAssertNotEquals \
- "package $pkg not considered native" \
- "$(cat r_es)" 0
- ;;
- esac
- }
-
- assert_native() {
- #
- # Assert that package $1 was deemed native
- #
- local pkg=$1
- case $Fn in
- get_dist_native_list)
- rlRun "grep '$pkg' r_native" \
- 0 "package $pkg considered native"
- ;;
- is_dist_native)
- rlAssertEquals \
- "package $pkg considered native" \
- "$(cat r_es)" 0
- ;;
- esac
- }
-
- xcase__enum() {
- #
- # Enumerate subtest ids
- #
- # Format:
- #
- # Fn=FN,Dno=DNO,Check=CHECK
- #
- # FN is name of relevant function that is tested.
- #
- # DNO stands for 'dist_native override', and governs value
- # passed to preupg__run1() to override
- # 'native' mechanism, In this case, it can be:
- #
- # * 'all', 'sign' or empty - pass directly
- #
- # * 'excluded' pass special value to exclude one single package
- # from nativity
- #
- # CHECK can be (some make sense only for certain functions):
- #
- # * 'foreign' - query for surely non-native package
- #
- # * 'ninst' - query for package that's not installed
- #
- # * 'native' - query for "surely" native package (this can be hard to
- # maintain when distribution this test is runnning is under
- # development)
- #
- # * 'excluded' - query for package excluded from native by DNO
- #
- # * 'diff' - use standard diff tool to compare expected result to
- # actual result
- #
- {
- echo Fn=is_dist_native,Dno=,Check=foreign
- echo Fn=is_dist_native,Dno=,Check=native
- echo Fn=is_dist_native,Dno=,Check=ninst
- echo Fn=get_dist_native_list,Dno=,Check=foreign
- echo Fn=get_dist_native_list,Dno=,Check=native
- echo Fn=get_dist_native_list,Dno=all,Check=diff
- echo Fn=get_dist_native_list,Dno=excluded,Check=diff
- echo Fn=get_dist_native_list,Dno=excluded,Check=excluded
- # echo Fn=get_dist_native_list,Dno=sign,Check=diff #FIXME find out how to get oracle
- # echo Fn=get_dist_native_list,Dno=,Check=diff #FIXME find out how to get oracle
- } | xcase__per Lang py sh
- }
-
- xcase__setup() {
- #
- # Prepare for subtest
- #
- # Dropped files:
- #
- # * r_native -- result: native package name list from get_dist_native_list()
- # * r_es -- result: exit status after calling is_dist_native()
- #
- local t_dno # dist_native override
-
- # Create module
- #
- {
- echo "[MODULE]"
- echo "GROUP = api"
- echo "NAME = nativity"
- echo "LANG = $Lang"
- #shellcheck disable=SC2028
- case $Lang in
- sh)
- case $Fn:$Check in
- is_dist_native:foreign) echo "CODE = is_dist_native '$FOREIGN'; echo \$? > '$PWD/r_es'" ;;
- is_dist_native:native) echo "CODE = is_dist_native '$NATIVE'; echo \$? > '$PWD/r_es'" ;;
- is_dist_native:ninst) echo "CODE = is_dist_native '$NINST'; echo \$? > '$PWD/r_es'" ;;
- get_dist_native_list:*) echo "CODE = get_dist_native_list | LC_ALL=C sort > '$PWD/r_native'" ;;
- esac
- echo "CODE = exit_pass"
- ;;
- py)
- echo "CODE = def keep_native(lst):"
- echo "CODE = with open('$PWD/r_native', 'w') as fh:"
- echo "CODE = fh.write('\\n'.join(sorted(lst)) + '\\n')"
- echo "CODE = "
- echo "CODE = def keep_es(bit):"
- echo "CODE = with open('$PWD/r_es', 'w') as fh:"
- echo "CODE = fh.write('0\\n' if bit else '1\\n')"
- echo "CODE = "
- case $Fn:$Check in
- is_dist_native:foreign) echo "CODE = keep_es(is_dist_native('$FOREIGN'))" ;;
- is_dist_native:native) echo "CODE = keep_es(is_dist_native('$NATIVE'))" ;;
- is_dist_native:ninst) echo "CODE = keep_es(is_dist_native('$NINST'))" ;;
- get_dist_native_list:*) echo "CODE = keep_native(get_dist_native_list())" ;;
- esac
- echo "CODE = exit_pass()"
- ;;
- esac
- } > module.ini
- preupg_fupath RHEL6_7 module.ini
-
- # prepare dist_native override
- #
- case $Dno in
- all) t_dno="all" ;;
- excluded) t_dno="-$EXCLUDED" ;;
- sign) t_dno="sign" ;;
- "") t_dno="" ;;
- esac
-
- # ensure package (non-)presence)
- #
- case $Check in
- foreign) rlAssertRpm "$FOREIGN" ;;
- native) rlAssertRpm "$NATIVE" ;;
- ninst) rlAssertNotRpm "$NINST" ;;
- esac
-
- # collect data
- #
- PREUPG__DISTNATIVE="$t_dno" \
- PREUPG__UPATH="RHEL6_7/all-xccdf.xml" \
- preupg__run1
- }
-
- xcase__test() {
- #
- # Do the work
- #
- # Dropped files:
- #
- # * rpm_qa -- all installed package names
- # * o_native -- oracle: all native package names
- # * r_diff -- result: diff of above two (dumped as error)
- #
-
- # prepare oracles
- #
- rpm -qa --qf='%{NAME}\n' | LC_ALL=C sort > rpm_qa
- case $Dno:$Check in
- all:diff) cp rpm_qa o_native ;;
- excluded:diff) <rpm_qa grep -vxF "$EXCLUDED" > o_native ;;
- sign:diff) echo 42 >o_native ;; #FIXME: who knows?
- :diff) echo 42 >o_native ;; #FIXME: who knows?
- esac
-
- # do the test
- #
- case $Check in
- diff) rlRun "diff -U 1 o_native r_native > r_diff" \
- 0 "native list is same as expected" \
- || jat_dump__file -E r_diff ;;
- native) assert_native "$NATIVE" ;;
- excluded) assert_foreign "$EXCLUDED" ;;
- foreign) assert_foreign "$FOREIGN" ;;
- ninst) assert_foreign "$NINST" ;;
- esac
- }
-
- xcase__diag() {
- #
- # Burp up some diag
- #
- jat_dump__file module.ini
- preupg__get_messages | jat_dump__pipe MESSAGE
- }
-
- xcase__cleanup() {
- #
- # Clean up after subtest
- #
- preupg__rmresult
- }
-
-
- jat__log_info "EXCLUDED='$EXCLUDED'"
- jat__log_info "FOREIGN='$FOREIGN'"
- jat__log_info "NATIVE='$NATIVE'"
- jat__log_info "NINST='$NINST'"
-
- xcase__run -v
- preupg__Cleanup
|