framework 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. #!/bin/sh -- # no runable script, just for vi
  2. EXIT_CODE=0
  3. # we all like colorful messages
  4. if [ "$MSGCOLOR" != 'NO' ]; then
  5. if [ ! -t 1 ]; then # but check that we output to a terminal
  6. export MSGCOLOR='NO'
  7. fi
  8. fi
  9. if [ "$MSGCOLOR" != 'NO' ]; then
  10. CERROR="\033[1;31m" # red
  11. CWARNING="\033[1;33m" # yellow
  12. CMSG="\033[1;32m" # green
  13. CINFO="\033[1;96m" # light blue
  14. CDEBUG="\033[1;94m" # blue
  15. CNORMAL="\033[0;39m" # default system console color
  16. CDONE="\033[1;32m" # green
  17. CPASS="\033[1;32m" # green
  18. CFAIL="\033[1;31m" # red
  19. CCMD="\033[1;35m" # pink
  20. fi
  21. msgdie() { printf "${CERROR}E: $1${CNORMAL}\n" >&2; exit 1; }
  22. msgwarn() { printf "${CWARNING}W: $1${CNORMAL}\n" >&2; }
  23. msgmsg() { printf "${CMSG}$1${CNORMAL}\n"; }
  24. msginfo() { printf "${CINFO}I: $1${CNORMAL}\n"; }
  25. msgdebug() { printf "${CDEBUG}D: $1${CNORMAL}\n"; }
  26. msgdone() { printf "${CDONE}DONE${CNORMAL}\n"; }
  27. msgnwarn() { printf "${CWARNING}W: $1${CNORMAL}" >&2; }
  28. msgnmsg() { printf "${CMSG}$1${CNORMAL}"; }
  29. msgninfo() { printf "${CINFO}I: $1${CNORMAL}"; }
  30. msgndebug() { printf "${CDEBUG}D: $1${CNORMAL}"; }
  31. msgtest() {
  32. while [ -n "$1" ]; do
  33. printf "${CINFO}$1${CCMD} "
  34. printf -- "$(echo "$2" | sed -e 's#^apt\([cgfs]\)#apt-\1#')${CINFO} "
  35. shift
  36. if [ -n "$1" ]; then shift; else break; fi
  37. done
  38. printf "…${CNORMAL} "
  39. }
  40. msgpass() { printf "${CPASS}PASS${CNORMAL}\n"; }
  41. msgskip() { printf "${CWARNING}SKIP${CNORMAL}\n" >&2; }
  42. msgfail() {
  43. if [ $# -gt 0 ]; then printf "${CFAIL}FAIL: $*${CNORMAL}\n" >&2;
  44. else printf "${CFAIL}FAIL${CNORMAL}\n" >&2; fi
  45. if [ -n "$APT_DEBUG_TESTS" ]; then
  46. bash
  47. fi
  48. EXIT_CODE=$((EXIT_CODE+1));
  49. }
  50. # enable / disable Debugging
  51. MSGLEVEL=${MSGLEVEL:-3}
  52. if [ $MSGLEVEL -le 0 ]; then
  53. msgdie() { true; }
  54. fi
  55. if [ $MSGLEVEL -le 1 ]; then
  56. msgwarn() { true; }
  57. msgnwarn() { true; }
  58. fi
  59. if [ $MSGLEVEL -le 2 ]; then
  60. msgmsg() { true; }
  61. msgnmsg() { true; }
  62. msgtest() { true; }
  63. msgpass() { printf " ${CPASS}P${CNORMAL}"; }
  64. msgskip() { printf " ${CWARNING}S${CNORMAL}" >&2; }
  65. if [ -n "$CFAIL" ]; then
  66. msgfail() { printf " ${CFAIL}FAIL${CNORMAL}" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
  67. else
  68. msgfail() { printf " ###FAILED###" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
  69. fi
  70. fi
  71. if [ $MSGLEVEL -le 3 ]; then
  72. msginfo() { true; }
  73. msgninfo() { true; }
  74. fi
  75. if [ $MSGLEVEL -le 4 ]; then
  76. msgdebug() { true; }
  77. msgndebug() { true; }
  78. fi
  79. msgdone() {
  80. if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
  81. [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
  82. [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
  83. [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
  84. [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
  85. true;
  86. else
  87. printf "${CDONE}DONE${CNORMAL}\n";
  88. fi
  89. }
  90. getaptconfig() {
  91. if [ -f ./aptconfig.conf ]; then
  92. echo "./aptconfig.conf"
  93. elif [ -f ../aptconfig.conf ]; then
  94. echo "../aptconfig.conf"
  95. fi
  96. }
  97. runapt() {
  98. msgdebug "Executing: ${CCMD}$*${CDEBUG} "
  99. local CMD="$1"
  100. shift
  101. case $CMD in
  102. sh|aptitude|*/*|command) ;;
  103. *) CMD="${BUILDDIRECTORY}/$CMD";;
  104. esac
  105. MALLOC_PERTURB_=21 MALLOC_CHECK_=2 APT_CONFIG="$(getaptconfig)" LD_LIBRARY_PATH=${LIBRARYPATH} $CMD "$@"
  106. }
  107. aptconfig() { runapt apt-config "$@"; }
  108. aptcache() { runapt apt-cache "$@"; }
  109. aptcdrom() { runapt apt-cdrom "$@"; }
  110. aptget() { runapt apt-get "$@"; }
  111. aptftparchive() { runapt apt-ftparchive "$@"; }
  112. aptkey() { runapt apt-key "$@"; }
  113. aptmark() { runapt apt-mark "$@"; }
  114. aptsortpkgs() { runapt apt-sortpkgs "$@"; }
  115. apt() { runapt apt "$@"; }
  116. apthelper() { runapt "${APTHELPERBINDIR}/apt-helper" "$@"; }
  117. aptwebserver() { runapt "${APTWEBSERVERBINDIR}/aptwebserver" "$@"; }
  118. aptitude() { runapt aptitude "$@"; }
  119. aptextracttemplates() { runapt apt-extracttemplates "$@"; }
  120. aptinternalsolver() { runapt "${APTINTERNALSOLVER}" "$@"; }
  121. dpkg() {
  122. command dpkg --root=${TMPWORKINGDIRECTORY}/rootdir --force-not-root --force-bad-path --log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log "$@"
  123. }
  124. dpkgcheckbuilddeps() {
  125. command dpkg-checkbuilddeps --admindir=${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg "$@"
  126. }
  127. gdb() {
  128. local CMD="$1"
  129. shift
  130. runapt command gdb --quiet -ex run "${BUILDDIRECTORY}/$CMD" --args "${BUILDDIRECTORY}/$CMD" "$@"
  131. }
  132. exitwithstatus() {
  133. # error if we about to overflow, but ...
  134. # "255 failures ought to be enough for everybody"
  135. if [ $EXIT_CODE -gt 255 ]; then
  136. msgdie "Total failure count $EXIT_CODE too big"
  137. fi
  138. exit $((EXIT_CODE <= 255 ? EXIT_CODE : 255));
  139. }
  140. shellsetedetector() {
  141. local exit_status=$?
  142. if [ "$exit_status" != '0' ]; then
  143. printf >&2 "${CERROR}E: Looks like the testcases ended prematurely with exitcode: ${exit_status}${CNORMAL}\n"
  144. if [ "$EXIT_CODE" = '0' ]; then
  145. EXIT_CODE="$exit_status"
  146. fi
  147. fi
  148. }
  149. addtrap() {
  150. if [ "$1" = 'prefix' ]; then
  151. CURRENTTRAP="$2 $CURRENTTRAP"
  152. else
  153. CURRENTTRAP="$CURRENTTRAP $1"
  154. fi
  155. trap "shellsetedetector; $CURRENTTRAP exitwithstatus;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  156. }
  157. setupenvironment() {
  158. TMPWORKINGDIRECTORY=$(mktemp -d)
  159. addtrap "cd /; rm -rf $TMPWORKINGDIRECTORY;"
  160. msgninfo "Preparing environment for ${CCMD}$(basename $0)${CINFO} in ${TMPWORKINGDIRECTORY}… "
  161. TESTDIRECTORY=$(readlink -f $(dirname $0))
  162. # allow overriding the default BUILDDIR location
  163. BUILDDIRECTORY=${APT_INTEGRATION_TESTS_BUILD_DIR:-"${TESTDIRECTORY}/../../build/bin"}
  164. LIBRARYPATH=${APT_INTEGRATION_TESTS_LIBRARY_PATH:-"${BUILDDIRECTORY}"}
  165. METHODSDIR=${APT_INTEGRATION_TESTS_METHODS_DIR:-"${BUILDDIRECTORY}/methods"}
  166. APTHELPERBINDIR=${APT_INTEGRATION_TESTS_LIBEXEC_DIR:-"${BUILDDIRECTORY}"}
  167. APTWEBSERVERBINDIR=${APT_INTEGRATION_TESTS_WEBSERVER_BIN_DIR:-"${BUILDDIRECTORY}"}
  168. APTINTERNALSOLVER=${APT_INTEGRATION_TESTS_INTERNAL_SOLVER:-"${BUILDDIRECTORY}/apt-internal-solver"}
  169. test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
  170. # -----
  171. cd $TMPWORKINGDIRECTORY
  172. mkdir rootdir aptarchive keys
  173. cd rootdir
  174. mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
  175. mkdir -p var/cache var/lib/apt var/log tmp
  176. mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
  177. touch var/lib/dpkg/available
  178. mkdir -p usr/lib/apt
  179. ln -s ${METHODSDIR} usr/lib/apt/methods
  180. if [ "$BUILDDIRECTORY" = "$LIBRARYPATH" ]; then
  181. mkdir -p usr/lib/apt/solvers
  182. ln -s "${BUILDDIRECTORY}/apt-dump-solver" usr/lib/apt/solvers/dump
  183. ln -s "${BUILDDIRECTORY}/apt-internal-solver" usr/lib/apt/solvers/apt
  184. echo "Dir::Bin::Solvers \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/solvers\";" > etc/apt/apt.conf.d/externalsolver.conf
  185. fi
  186. # use the autoremove from the BUILDDIRECTORY if its there, otherwise
  187. # system
  188. if [ -e ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove ]; then
  189. ln -s ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove etc/apt/apt.conf.d/01autoremove
  190. else
  191. ln -s /etc/apt/apt.conf.d/01autoremove etc/apt/apt.conf.d/01autoremove
  192. fi
  193. cd ..
  194. local PACKAGESFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Packages-/' -e 's/^skip-/Packages-/')
  195. if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
  196. cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
  197. fi
  198. local SOURCESSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Sources-/' -e 's/^skip-/Sources-/')
  199. if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
  200. cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
  201. fi
  202. cp $(find $TESTDIRECTORY -name '*.pub' -o -name '*.sec') keys/
  203. chmod 644 $(find keys -name '*.pub' -o -name '*.sec')
  204. ln -s ${TMPWORKINGDIRECTORY}/keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  205. echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
  206. echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
  207. echo "Debug::NoLocking \"true\";" >> aptconfig.conf
  208. echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
  209. echo "Dir::Bin::Methods \"${METHODSDIR}\";" >> aptconfig.conf
  210. echo "Dir::Bin::apt-key \"${BUILDDIRECTORY}/apt-key\";" >> aptconfig.conf
  211. echo "Dir::Bin::dpkg \"fakeroot\";" >> aptconfig.conf
  212. echo "DPKG::options:: \"dpkg\";" >> aptconfig.conf
  213. echo "DPKG::options:: \"--root=${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
  214. echo "DPKG::options:: \"--force-not-root\";" >> aptconfig.conf
  215. echo "DPKG::options:: \"--force-bad-path\";" >> aptconfig.conf
  216. if ! command dpkg --assert-multi-arch >/dev/null 2>&1; then
  217. echo "DPKG::options:: \"--force-architecture\";" >> aptconfig.conf # Added to test multiarch before dpkg is ready for it…
  218. fi
  219. echo "DPKG::options:: \"--log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log\";" >> aptconfig.conf
  220. echo 'quiet::NoUpdate "true";' >> aptconfig.conf
  221. echo 'quiet::NoStatistic "true";' >> aptconfig.conf
  222. # too distracting for users, but helpful to detect changes
  223. echo 'Acquire::Progress::Ignore::ShowErrorText "true";' >> aptconfig.conf
  224. # in testcases, it can appear as if localhost has a rotation setup,
  225. # hide this as we can't really deal with it properly
  226. echo 'Acquire::Failure::ShowIP "false";' >> aptconfig.conf
  227. echo "Acquire::https::CaInfo \"${TESTDIR}/apt.pem\";" > rootdir/etc/apt/apt.conf.d/99https
  228. echo "Apt::Cmd::Disable-Script-Warning \"1\";" > rootdir/etc/apt/apt.conf.d/apt-binary
  229. configcompression '.' 'gz' #'bz2' 'lzma' 'xz'
  230. # Acquire::AllowInsecureRepositories=false is not yet the default
  231. # but we want it to be the default soon
  232. configallowinsecurerepositories "false";
  233. # cleanup the environment a bit
  234. # prefer our apt binaries over the system apt binaries
  235. export PATH="${BUILDDIRECTORY}:${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
  236. export LC_ALL=C.UTF-8
  237. unset LANGUAGE APT_CONFIG
  238. unset GREP_OPTIONS DEB_BUILD_PROFILES
  239. msgdone "info"
  240. }
  241. getarchitecture() {
  242. if [ "$1" = "native" -o -z "$1" ]; then
  243. eval `aptconfig shell ARCH APT::Architecture`
  244. if [ -n "$ARCH" ]; then
  245. echo $ARCH
  246. else
  247. dpkg --print-architecture
  248. fi
  249. else
  250. echo $1
  251. fi
  252. }
  253. getarchitectures() {
  254. echo "$(aptconfig dump | grep APT::Architecture | cut -d'"' -f 2 | sed '/^$/ d' | sort | uniq | tr '\n' ' ')"
  255. }
  256. getarchitecturesfromcommalist() {
  257. echo "$1" | sed -e 's#,#\n#g' | sed -e "s/^native\$/$(getarchitecture 'native')/"
  258. }
  259. configarchitecture() {
  260. {
  261. echo "APT::Architecture \"$(getarchitecture $1)\";"
  262. while [ -n "$1" ]; do
  263. echo "APT::Architectures:: \"$(getarchitecture $1)\";"
  264. shift
  265. done
  266. } >rootdir/etc/apt/apt.conf.d/01multiarch.conf
  267. configdpkg
  268. }
  269. configdpkg() {
  270. if [ ! -e rootdir/var/lib/dpkg/status ]; then
  271. local STATUSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/status-/' -e 's/^skip-/status-/')
  272. if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
  273. cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
  274. else
  275. echo -n > rootdir/var/lib/dpkg/status
  276. fi
  277. fi
  278. rm -f rootdir/etc/apt/apt.conf.d/00foreigndpkg
  279. if command dpkg --assert-multi-arch >/dev/null 2>&1 ; then
  280. local ARCHS="$(getarchitectures)"
  281. if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
  282. DPKGARCH="$(dpkg --print-architecture)"
  283. for ARCH in ${ARCHS}; do
  284. if [ "${ARCH}" != "${DPKGARCH}" ]; then
  285. if ! dpkg --add-architecture ${ARCH} >/dev/null 2>&1; then
  286. # old-style used e.g. in Ubuntu-P – and as it seems travis
  287. echo "DPKG::options:: \"--foreign-architecture\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  288. echo "DPKG::options:: \"${ARCH}\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  289. fi
  290. fi
  291. done
  292. if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
  293. # dpkg doesn't really check the version as long as it is fully installed,
  294. # but just to be sure we choose one above the required version
  295. insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
  296. fi
  297. fi
  298. fi
  299. }
  300. configallowinsecurerepositories() {
  301. echo "Acquire::AllowInsecureRepositories \"$1\";" > rootdir/etc/apt/apt.conf.d/allow-insecure-repositories.conf
  302. }
  303. configcompression() {
  304. while [ -n "$1" ]; do
  305. case "$1" in
  306. '.') printf ".\t.\tcat\n";;
  307. 'gz') printf "gzip\tgz\tgzip\n";;
  308. 'bz2') printf "bzip2\tbz2\tbzip2\n";;
  309. 'lzma') printf "lzma\tlzma\txz --format=lzma\n";;
  310. 'xz') printf "xz\txz\txz\n";;
  311. *) printf "$1\t$1\t$1\n";;
  312. esac
  313. shift
  314. done > ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf
  315. }
  316. forcecompressor() {
  317. COMPRESSOR="$1"
  318. COMPRESSOR_CMD="$1"
  319. case $COMPRESSOR in
  320. gzip) COMPRESS='gz';;
  321. bzip2) COMPRESS='bz2';;
  322. lzma) COMPRESS='lzma';;
  323. xz) COMPRESS='xz';;
  324. *) msgdie "Compressor $COMPRESSOR is unknown to framework, so can't be forced by forcecompressor!";;
  325. esac
  326. local CONFFILE="${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/00force-compressor"
  327. echo "Acquire::CompressionTypes::Order { \"${COMPRESS}\"; };
  328. Dir::Bin::uncompressed \"/does/not/exist\";
  329. Dir::Bin::gzip \"/does/not/exist\";
  330. Dir::Bin::bzip2 \"/does/not/exist\";
  331. Dir::Bin::lzma \"/does/not/exist\";
  332. Dir::Bin::xz \"/does/not/exist\";" > "$CONFFILE"
  333. if [ -e "/bin/${COMPRESSOR}" ]; then
  334. echo "Dir::Bin::${COMPRESSOR} \"/bin/${COMPRESSOR}\";" >> "$CONFFILE"
  335. elif [ -e "/usr/bin/${COMPRESSOR}" ]; then
  336. echo "Dir::Bin::${COMPRESSOR} \"/usr/bin/${COMPRESSOR}\";" >> "$CONFFILE"
  337. elif [ "${COMPRESSOR}" = 'lzma' ]; then
  338. echo 'Dir::Bin::xz "/usr/bin/xz";' >> "$CONFFILE"
  339. COMPRESSOR_CMD='xz --format=lzma'
  340. else
  341. msgtest 'Test for availability of compressor' "${COMPRESSOR}"
  342. msgfail
  343. fi
  344. }
  345. setupsimplenativepackage() {
  346. local NAME="$1"
  347. local ARCH="$2"
  348. local VERSION="$3"
  349. local RELEASE="${4:-unstable}"
  350. local DEPENDENCIES="$5"
  351. local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  352. If you find such a package installed on your system,
  353. something went horribly wrong! They are autogenerated
  354. und used only by testcases and surf no other propose…"}"
  355. local SECTION="${7:-others}"
  356. local DISTSECTION
  357. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  358. DISTSECTION="main"
  359. else
  360. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  361. fi
  362. local BUILDDIR=incoming/${NAME}-${VERSION}
  363. mkdir -p ${BUILDDIR}/debian/source
  364. cd ${BUILDDIR}
  365. echo "* most suckless software product ever" > FEATURES
  366. test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
  367. test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
  368. * Initial release
  369. -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
  370. test -e debian/control || echo "Source: $NAME
  371. Section: $SECTION
  372. Priority: optional
  373. Maintainer: Joe Sixpack <joe@example.org>
  374. Build-Depends: debhelper (>= 7)
  375. Standards-Version: 3.9.1
  376. Package: $NAME" > debian/control
  377. if [ "$ARCH" = 'all' ]; then
  378. echo "Architecture: all" >> debian/control
  379. else
  380. echo "Architecture: any" >> debian/control
  381. fi
  382. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
  383. echo "Description: $DESCRIPTION" >> debian/control
  384. test -e debian/compat || echo "7" > debian/compat
  385. test -e debian/source/format || echo "3.0 (native)" > debian/source/format
  386. test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
  387. cd - > /dev/null
  388. }
  389. buildsimplenativepackage() {
  390. local NAME="$1"
  391. local ARCH="$2"
  392. local VERSION="$3"
  393. local RELEASE="${4:-unstable}"
  394. local DEPENDENCIES="$5"
  395. local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  396. If you find such a package installed on your system,
  397. something went horribly wrong! They are autogenerated
  398. und used only by testcases and surf no other propose…"}"
  399. local SECTION="${7:-others}"
  400. local PRIORITY="${8:-optional}"
  401. local FILE_TREE="$9"
  402. local COMPRESS_TYPE="${10:-gzip}"
  403. local DISTSECTION
  404. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  405. DISTSECTION="main"
  406. else
  407. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  408. fi
  409. local BUILDDIR=${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}
  410. msgninfo "Build package ${NAME} in ${VERSION} for ${RELEASE} in ${DISTSECTION}… "
  411. mkdir -p $BUILDDIR/debian/source
  412. echo "* most suckless software product ever" > ${BUILDDIR}/FEATURES
  413. echo "#!/bin/sh
  414. echo '$NAME says \"Hello!\"'" > ${BUILDDIR}/${NAME}
  415. echo "Copyleft by Joe Sixpack $(date +%Y)" > ${BUILDDIR}/debian/copyright
  416. echo "$NAME ($VERSION) $RELEASE; urgency=low
  417. * Initial release
  418. -- Joe Sixpack <joe@example.org> $(date -R)" > ${BUILDDIR}/debian/changelog
  419. echo "Source: $NAME
  420. Section: $SECTION
  421. Priority: $PRIORITY
  422. Maintainer: Joe Sixpack <joe@example.org>
  423. Standards-Version: 3.9.3" > ${BUILDDIR}/debian/control
  424. local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
  425. test -z "$BUILDDEPS" || echo "$BUILDDEPS" >> ${BUILDDIR}/debian/control
  426. echo "
  427. Package: $NAME" >> ${BUILDDIR}/debian/control
  428. if [ "$ARCH" = 'all' ]; then
  429. echo "Architecture: all" >> ${BUILDDIR}/debian/control
  430. else
  431. echo "Architecture: any" >> ${BUILDDIR}/debian/control
  432. fi
  433. local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
  434. test -z "$DEPS" || echo "$DEPS" >> ${BUILDDIR}/debian/control
  435. echo "Description: $DESCRIPTION" >> ${BUILDDIR}/debian/control
  436. echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
  437. (cd ${BUILDDIR}/..; dpkg-source -b ${NAME}-${VERSION} 2>&1) | sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' \
  438. | while read SRC; do
  439. echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
  440. # if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
  441. # aptkey --keyring ./keys/joesixpack.pub --secret-keyring ./keys/joesixpack.sec --quiet --readonly \
  442. # adv --yes --default-key 'Joe Sixpack' \
  443. # --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  444. # mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  445. # fi
  446. done
  447. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  448. rm -rf ${BUILDDIR}/debian/tmp
  449. mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
  450. cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
  451. cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
  452. if [ -n "$FILE_TREE" ]; then
  453. cp -ar "$FILE_TREE" ${BUILDDIR}/debian/tmp
  454. fi
  455. (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
  456. (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
  457. local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
  458. # ensure the right permissions as dpkg-deb ensists
  459. chmod 755 ${BUILDDIR}/debian/tmp/DEBIAN
  460. if ! dpkg-deb -Z${COMPRESS_TYPE} --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/.. >$LOG 2>&1; then
  461. cat $LOG
  462. false
  463. fi
  464. rm $LOG
  465. echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
  466. done
  467. mkdir -p ${BUILDDIR}/../${NAME}_${VERSION}
  468. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}/
  469. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}.changelog
  470. rm -rf "${BUILDDIR}"
  471. msgdone "info"
  472. }
  473. buildpackage() {
  474. local BUILDDIR=$1
  475. local RELEASE=$2
  476. local SECTION=$3
  477. local ARCH=$(getarchitecture $4)
  478. local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
  479. local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
  480. msgninfo "Build package ${PKGNAME} for ${RELEASE} in ${SECTION}… "
  481. cd $BUILDDIR
  482. if [ "$ARCH" = "all" ]; then
  483. ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
  484. fi
  485. if ! dpkg-buildpackage -uc -us -a$ARCH >$BUILDLOG 2>&1 ; then
  486. cat $BUILDLOG
  487. false
  488. fi
  489. local PKGS="$(grep '^dpkg-deb: building package' $BUILDLOG | cut -d'/' -f 2 | sed -e "s#'\.##")"
  490. local SRCS="$(grep '^dpkg-source: info: building' $BUILDLOG | grep -o '[a-z0-9._+~-]*$')"
  491. cd - > /dev/null
  492. for PKG in $PKGS; do
  493. echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
  494. done
  495. for SRC in $SRCS; do
  496. echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
  497. done
  498. msgdone "info"
  499. }
  500. buildaptarchive() {
  501. if [ -d incoming ]; then
  502. buildaptarchivefromincoming "$@"
  503. else
  504. buildaptarchivefromfiles "$@"
  505. fi
  506. }
  507. createaptftparchiveconfig() {
  508. local COMPRESSORS="$(cut -d' ' -f 1 ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | tr '\n' ' ')"
  509. COMPRESSORS="${COMPRESSORS%* }"
  510. local ARCHS="$(find pool/ -name '*.deb' | grep -oE '_[a-z0-9-]+\.deb$' | sort | uniq | sed -e '/^_all.deb$/ d' -e 's#^_\([a-z0-9-]*\)\.deb$#\1#' | tr '\n' ' ')"
  511. if [ -z "$ARCHS" ]; then
  512. # the pool is empty, so we will operate on faked packages - let us use the configured archs
  513. ARCHS="$(getarchitectures)"
  514. fi
  515. echo -n 'Dir {
  516. ArchiveDir "' >> ftparchive.conf
  517. echo -n $(readlink -f .) >> ftparchive.conf
  518. echo -n '";
  519. CacheDir "' >> ftparchive.conf
  520. echo -n $(readlink -f ..) >> ftparchive.conf
  521. echo -n '";
  522. FileListDir "' >> ftparchive.conf
  523. echo -n $(readlink -f pool/) >> ftparchive.conf
  524. echo -n '";
  525. };
  526. Default {
  527. Packages::Compress "'"$COMPRESSORS"'";
  528. Sources::Compress "'"$COMPRESSORS"'";
  529. Contents::Compress "'"$COMPRESSORS"'";
  530. Translation::Compress "'"$COMPRESSORS"'";
  531. LongDescription "false";
  532. };
  533. TreeDefault {
  534. Directory "pool/";
  535. SrcDirectory "pool/";
  536. };
  537. APT {
  538. FTPArchive {
  539. Release {
  540. Origin "joesixpack";
  541. Label "apttestcases";
  542. Suite "unstable";
  543. Description "repository with dummy packages";
  544. Architectures "' >> ftparchive.conf
  545. echo -n "$ARCHS" >> ftparchive.conf
  546. echo 'source";
  547. };
  548. };
  549. };' >> ftparchive.conf
  550. for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
  551. echo -n 'tree "dists/' >> ftparchive.conf
  552. echo -n "$DIST" >> ftparchive.conf
  553. echo -n '" {
  554. Architectures "' >> ftparchive.conf
  555. echo -n "$ARCHS" >> ftparchive.conf
  556. echo -n 'source";
  557. FileList "' >> ftparchive.conf
  558. echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
  559. echo -n '";
  560. SourceFileList "' >> ftparchive.conf
  561. echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
  562. echo -n '";
  563. Sections "' >> ftparchive.conf
  564. echo -n "$(find ./pool/ -maxdepth 1 -name "${DIST}.*.pkglist" -type f | cut -d'/' -f 3 | cut -d'.' -f 2 | sort | uniq | tr '\n' ' ')" >> ftparchive.conf
  565. echo '";
  566. };' >> ftparchive.conf
  567. done
  568. }
  569. buildaptftparchivedirectorystructure() {
  570. local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
  571. for DIST in $DISTS; do
  572. local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
  573. for SECTION in $SECTIONS; do
  574. local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
  575. for ARCH in $ARCHS; do
  576. mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
  577. done
  578. mkdir -p dists/${DIST}/${SECTION}/source
  579. mkdir -p dists/${DIST}/${SECTION}/i18n
  580. done
  581. done
  582. }
  583. insertpackage() {
  584. local RELEASE="$1"
  585. local NAME="$2"
  586. local ARCH="$3"
  587. local VERSION="$4"
  588. local DEPENDENCIES="$5"
  589. local PRIORITY="${6:-optional}"
  590. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  591. If you find such a package installed on your system,
  592. something went horribly wrong! They are autogenerated
  593. und used only by testcases and surf no other propose…"}"
  594. local ARCHS=""
  595. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  596. if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
  597. ARCHS="$(getarchitectures)"
  598. else
  599. ARCHS="$arch"
  600. fi
  601. for BUILDARCH in $ARCHS; do
  602. local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
  603. mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
  604. touch aptarchive/dists/${RELEASE}/main/source/Sources
  605. local FILE="${PPATH}/Packages"
  606. echo "Package: $NAME
  607. Priority: $PRIORITY
  608. Section: other
  609. Installed-Size: 42
  610. Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
  611. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  612. echo "Version: $VERSION
  613. Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
  614. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  615. echo "Description: $DESCRIPTION" >> $FILE
  616. echo >> $FILE
  617. done
  618. done
  619. }
  620. insertsource() {
  621. local RELEASE="$1"
  622. local NAME="$2"
  623. local ARCH="$3"
  624. local VERSION="$4"
  625. local DEPENDENCIES="$5"
  626. local ARCHS=""
  627. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  628. mkdir -p $SPATH
  629. local FILE="${SPATH}/Sources"
  630. echo "Package: $NAME
  631. Binary: $NAME
  632. Version: $VERSION
  633. Maintainer: Joe Sixpack <joe@example.org>
  634. Architecture: $ARCH" >> $FILE
  635. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  636. echo "Files:
  637. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
  638. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
  639. " >> $FILE
  640. }
  641. insertinstalledpackage() {
  642. local NAME="$1"
  643. local ARCH="$2"
  644. local VERSION="$3"
  645. local DEPENDENCIES="$4"
  646. local PRIORITY="${5:-optional}"
  647. local STATUS="${6:-install ok installed}"
  648. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
  649. If you find such a package installed on your system,
  650. something went horribly wrong! They are autogenerated
  651. und used only by testcases and surf no other propose…"}"
  652. local FILE='rootdir/var/lib/dpkg/status'
  653. local INFO='rootdir/var/lib/dpkg/info'
  654. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  655. echo "Package: $NAME
  656. Status: $STATUS
  657. Priority: $PRIORITY
  658. Section: other
  659. Installed-Size: 42
  660. Maintainer: Joe Sixpack <joe@example.org>
  661. Version: $VERSION" >> $FILE
  662. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  663. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  664. echo "Description: $DESCRIPTION" >> $FILE
  665. echo >> $FILE
  666. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  667. echo -n > ${INFO}/${NAME}:${arch}.list
  668. else
  669. echo -n > ${INFO}/${NAME}.list
  670. fi
  671. done
  672. }
  673. buildaptarchivefromincoming() {
  674. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  675. cd aptarchive
  676. [ -e pool ] || ln -s ../incoming pool
  677. [ -e ftparchive.conf ] || createaptftparchiveconfig
  678. [ -e dists ] || buildaptftparchivedirectorystructure
  679. msgninfo "\tGenerate Packages, Sources and Contents files… "
  680. aptftparchive -qq generate ftparchive.conf
  681. cd - > /dev/null
  682. msgdone "info"
  683. generatereleasefiles "$@"
  684. }
  685. buildaptarchivefromfiles() {
  686. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  687. find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
  688. msgninfo "\t${line} file… "
  689. compressfile "$line" "$1"
  690. msgdone "info"
  691. done
  692. generatereleasefiles "$@"
  693. }
  694. compressfile() {
  695. cat ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | while read compressor extension command; do
  696. if [ "$compressor" = '.' ]; then
  697. if [ -n "$2" ]; then
  698. touch -d "$2" "$1"
  699. fi
  700. continue
  701. fi
  702. cat "$1" | $command > "${1}.${extension}"
  703. if [ -n "$2" ]; then
  704. touch -d "$2" "${1}.${extension}"
  705. fi
  706. done
  707. }
  708. # can be overridden by testcases for their pleasure
  709. getcodenamefromsuite() {
  710. case "$1" in
  711. unstable) echo 'sid';;
  712. *) echo -n "$1";;
  713. esac
  714. }
  715. getreleaseversionfromsuite() { true; }
  716. getlabelfromsuite() { true; }
  717. generatereleasefiles() {
  718. # $1 is the Date header and $2 is the ValidUntil header to be set
  719. # both should be given in notation date/touch can understand
  720. msgninfo "\tGenerate Release files… "
  721. if [ -e aptarchive/dists ]; then
  722. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  723. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  724. local CODENAME="$(getcodenamefromsuite $SUITE)"
  725. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  726. local LABEL="$(getlabelfromsuite $SUITE)"
  727. if [ -n "$VERSION" ]; then
  728. VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
  729. fi
  730. if [ -n "$LABEL" ]; then
  731. LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
  732. fi
  733. aptftparchive -qq release $dir \
  734. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  735. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  736. ${LABEL} \
  737. ${VERSION} \
  738. | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  739. if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
  740. sed -i '/^Date: / a\
  741. NotAutomatic: yes' $dir/Release
  742. fi
  743. if [ -n "$1" -a "$1" != "now" ]; then
  744. sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
  745. fi
  746. if [ -n "$2" ]; then
  747. sed -i "/^Date: / a\
  748. Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
  749. fi
  750. done
  751. else
  752. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  753. fi
  754. if [ -n "$1" -a "$1" != "now" ]; then
  755. for release in $(find ./aptarchive -name 'Release'); do
  756. touch -d "$1" $release
  757. done
  758. fi
  759. msgdone "info"
  760. }
  761. setupdistsaptarchive() {
  762. local APTARCHIVE=$(readlink -f ./aptarchive)
  763. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  764. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  765. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  766. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  767. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  768. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  769. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  770. msgdone "info"
  771. done
  772. }
  773. setupflataptarchive() {
  774. local APTARCHIVE=$(readlink -f ./aptarchive)
  775. if [ -f ${APTARCHIVE}/Packages ]; then
  776. msgninfo "\tadd deb sources.list line… "
  777. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  778. msgdone "info"
  779. else
  780. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  781. fi
  782. if [ -f ${APTARCHIVE}/Sources ]; then
  783. msgninfo "\tadd deb-src sources.list line… "
  784. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  785. msgdone "info"
  786. else
  787. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  788. fi
  789. }
  790. setupaptarchive() {
  791. local NOUPDATE=0
  792. if [ "$1" = '--no-update' ]; then
  793. NOUPDATE=1
  794. shift
  795. fi
  796. buildaptarchive "$@"
  797. if [ -e aptarchive/dists ]; then
  798. setupdistsaptarchive
  799. else
  800. setupflataptarchive
  801. fi
  802. signreleasefiles 'Joe Sixpack'
  803. if [ "1" != "$NOUPDATE" ]; then
  804. testsuccess aptget update -o Debug::pkgAcquire::Worker=true -o Debug::Acquire::gpgv=true
  805. fi
  806. }
  807. signreleasefiles() {
  808. local SIGNER="${1:-Joe Sixpack}"
  809. local REPODIR="${2:-aptarchive}"
  810. local KEY="keys/$(echo "$SIGNER" | tr 'A-Z' 'a-z' | sed 's# ##g')"
  811. local GPG="aptkey --quiet --keyring ${KEY}.pub --secret-keyring ${KEY}.sec --readonly adv --batch --yes"
  812. msgninfo "\tSign archive with $SIGNER key $KEY… "
  813. local REXKEY='keys/rexexpired'
  814. local SECEXPIREBAK="${REXKEY}.sec.bak"
  815. local PUBEXPIREBAK="${REXKEY}.pub.bak"
  816. if [ "${SIGNER}" = 'Rex Expired' ]; then
  817. # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
  818. # option doesn't exist anymore (and using faketime would add a new obscure dependency)
  819. # therefore we 'temporary' make the key not expired and restore a backup after signing
  820. cp ${REXKEY}.sec $SECEXPIREBAK
  821. cp ${REXKEY}.pub $PUBEXPIREBAK
  822. local SECUNEXPIRED="${REXKEY}.sec.unexpired"
  823. local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
  824. if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
  825. cp $SECUNEXPIRED ${REXKEY}.sec
  826. cp $PUBUNEXPIRED ${REXKEY}.pub
  827. else
  828. if ! printf "expire\n1w\nsave\n" | $GPG --default-key "$SIGNER" --command-fd 0 --edit-key "${SIGNER}" >setexpire.gpg 2>&1; then
  829. cat setexpire.gpg
  830. exit 1
  831. fi
  832. cp ${REXKEY}.sec $SECUNEXPIRED
  833. cp ${REXKEY}.pub $PUBUNEXPIRED
  834. fi
  835. fi
  836. for RELEASE in $(find ${REPODIR}/ -name Release); do
  837. $GPG --default-key "$SIGNER" --armor --detach-sign --sign --output ${RELEASE}.gpg ${RELEASE}
  838. local INRELEASE="$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')"
  839. $GPG --default-key "$SIGNER" --clearsign --output $INRELEASE $RELEASE
  840. # we might have set a specific date for the Release file, so copy it
  841. touch -d "$(stat --format "%y" ${RELEASE})" ${RELEASE}.gpg ${INRELEASE}
  842. done
  843. if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
  844. mv -f $SECEXPIREBAK ${REXKEY}.sec
  845. mv -f $PUBEXPIREBAK ${REXKEY}.pub
  846. fi
  847. msgdone "info"
  848. }
  849. webserverconfig() {
  850. msgtest "Set webserver config option '${1}' to" "$2"
  851. local DOWNLOG='rootdir/tmp/download-testfile.log'
  852. local STATUS='rootdir/tmp/webserverconfig.status'
  853. rm -f "$STATUS" "$DOWNLOG"
  854. if downloadfile "http://localhost:8080/_config/set/${1}/${2}" "$STATUS" > "$DOWNLOG"; then
  855. msgpass
  856. else
  857. cat "$DOWNLOG" "$STATUS"
  858. msgfail
  859. fi
  860. testwebserverlaststatuscode '200'
  861. }
  862. rewritesourceslist() {
  863. local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive")"
  864. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  865. sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#http://localhost:8080/#${1}#" -e "s#http://localhost:4433/#${1}#"
  866. done
  867. }
  868. # wait for up to 10s for a pid file to appear to avoid possible race
  869. # when a helper is started and dosn't write the PID quick enough
  870. waitforpidfile() {
  871. local PIDFILE="$1"
  872. for i in $(seq 10); do
  873. if test -s "$PIDFILE"; then
  874. return 0
  875. fi
  876. sleep 1
  877. done
  878. msgdie "waiting for $PIDFILE failed"
  879. return 1
  880. }
  881. changetowebserver() {
  882. if [ "$1" != '--no-rewrite' ]; then
  883. rewritesourceslist 'http://localhost:8080/'
  884. else
  885. shift
  886. fi
  887. if test -x ${APTWEBSERVERBINDIR}/aptwebserver; then
  888. cd aptarchive
  889. local LOG="webserver.log"
  890. if ! aptwebserver -o aptwebserver::fork=1 "$@" >$LOG 2>&1 ; then
  891. cat $LOG
  892. false
  893. fi
  894. waitforpidfile aptwebserver.pid
  895. local PID="$(cat aptwebserver.pid)"
  896. if [ -z "$PID" ]; then
  897. msgdie 'Could not fork aptwebserver successfully'
  898. fi
  899. addtrap "kill $PID;"
  900. cd - > /dev/null
  901. else
  902. msgdie 'You have to build aptwerbserver or install a webserver'
  903. fi
  904. }
  905. changetohttpswebserver() {
  906. if ! which stunnel4 >/dev/null; then
  907. msgdie 'You need to install stunnel4 for https testcases'
  908. fi
  909. if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
  910. changetowebserver --no-rewrite "$@"
  911. fi
  912. echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
  913. cert = ${TESTDIRECTORY}/apt.pem
  914. output = /dev/null
  915. [https]
  916. accept = 4433
  917. connect = 8080
  918. " > ${TMPWORKINGDIRECTORY}/stunnel.conf
  919. stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
  920. waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
  921. local PID="$(cat ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid)"
  922. if [ -z "$PID" ]; then
  923. msgdie 'Could not fork stunnel4 successfully'
  924. fi
  925. addtrap 'prefix' "kill ${PID};"
  926. rewritesourceslist 'https://localhost:4433/'
  927. }
  928. changetocdrom() {
  929. mkdir -p rootdir/media/cdrom/.disk
  930. local CD="$(readlink -f rootdir/media/cdrom)"
  931. echo "acquire::cdrom::mount \"${CD}\";
  932. acquire::cdrom::${CD}/::mount \"mv ${CD}-unmounted ${CD}\";
  933. acquire::cdrom::${CD}/::umount \"mv ${CD} ${CD}-unmounted\";
  934. acquire::cdrom::autodetect 0;" > rootdir/etc/apt/apt.conf.d/00cdrom
  935. echo -n "$1" > ${CD}/.disk/info
  936. if [ ! -d aptarchive/dists ]; then
  937. msgdie 'Flat file archive cdroms can not be created currently'
  938. return 1
  939. fi
  940. mv aptarchive/dists "$CD"
  941. ln -s "$(readlink -f ./incoming)" $CD/pool
  942. find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
  943. # start with an unmounted disk
  944. mv "${CD}" "${CD}-unmounted"
  945. # we don't want the disk to be modifiable
  946. addtrap 'prefix' "chmod -f -R +w $PWD/rootdir/media/cdrom/dists/ $PWD/rootdir/media/cdrom-unmounted/dists/ || true;"
  947. chmod -R -w rootdir/media/cdrom-unmounted/dists
  948. }
  949. downloadfile() {
  950. local PROTO="$(echo "$1" | cut -d':' -f 1 )"
  951. apthelper -o Debug::Acquire::${PROTO}=1 \
  952. download-file "$1" "$2" 2>&1 || true
  953. # only if the file exists the download was successful
  954. if [ -e "$2" ]; then
  955. return 0
  956. else
  957. return 1
  958. fi
  959. }
  960. checkdiff() {
  961. local DIFFTEXT="$(command diff -u "$@" | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  962. if [ -n "$DIFFTEXT" ]; then
  963. echo >&2
  964. echo >&2 "$DIFFTEXT"
  965. return 1
  966. else
  967. return 0
  968. fi
  969. }
  970. testfileequal() {
  971. local FILE="$1"
  972. shift
  973. msgtest "Test for correctness of file" "$FILE"
  974. if [ -z "$*" ]; then
  975. echo -n "" | checkdiff $FILE - && msgpass || msgfail
  976. else
  977. echo "$*" | checkdiff $FILE - && msgpass || msgfail
  978. fi
  979. }
  980. testempty() {
  981. msgtest "Test for no output of" "$*"
  982. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
  983. if $* >$COMPAREFILE 2>&1 && test ! -s $COMPAREFILE; then
  984. msgpass
  985. else
  986. cat $COMPAREFILE
  987. msgfail
  988. fi
  989. }
  990. testequal() {
  991. local MSG='Test of equality of'
  992. if [ "$1" = '--nomsg' ]; then
  993. MSG=''
  994. shift
  995. fi
  996. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
  997. echo "$1" > $COMPAREFILE
  998. shift
  999. if [ -n "$MSG" ]; then
  1000. msgtest "$MSG" "$*"
  1001. fi
  1002. $* 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1003. }
  1004. testequalor2() {
  1005. local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
  1006. local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
  1007. local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
  1008. echo "$1" > $COMPAREFILE1
  1009. echo "$2" > $COMPAREFILE2
  1010. shift 2
  1011. msgtest "Test for equality OR of" "$*"
  1012. $* >$COMPAREAGAINST 2>&1 || true
  1013. if checkdiff $COMPAREFILE1 $COMPAREAGAINST >/dev/null 2>&1 || \
  1014. checkdiff $COMPAREFILE2 $COMPAREAGAINST >/dev/null 2>&1
  1015. then
  1016. msgpass
  1017. else
  1018. echo -n "\n${CINFO}Diff against OR 1${CNORMAL}"
  1019. checkdiff $COMPAREFILE1 $COMPAREAGAINST || true
  1020. echo -n "${CINFO}Diff against OR 2${CNORMAL}"
  1021. checkdiff $COMPAREFILE2 $COMPAREAGAINST || true
  1022. msgfail
  1023. fi
  1024. }
  1025. testshowvirtual() {
  1026. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  1027. local PACKAGE="$1"
  1028. shift
  1029. while [ -n "$1" ]; do
  1030. VIRTUAL="${VIRTUAL}
  1031. N: Can't select versions from package '$1' as it is purely virtual"
  1032. PACKAGE="${PACKAGE} $1"
  1033. shift
  1034. done
  1035. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  1036. VIRTUAL="${VIRTUAL}
  1037. N: No packages found"
  1038. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
  1039. local ARCH="$(getarchitecture 'native')"
  1040. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  1041. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1042. }
  1043. testnopackage() {
  1044. msgtest "Test for non-existent packages" "apt-cache show $*"
  1045. local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
  1046. if [ -n "$SHOWPKG" ]; then
  1047. echo >&2
  1048. echo >&2 "$SHOWPKG"
  1049. msgfail
  1050. else
  1051. msgpass
  1052. fi
  1053. }
  1054. testdpkginstalled() {
  1055. msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
  1056. local PKGS="$(dpkg -l "$@" 2>/dev/null | grep '^i' | wc -l)"
  1057. if [ "$PKGS" != $# ]; then
  1058. echo >&2 $PKGS
  1059. dpkg -l "$@" | grep '^[a-z]' >&2
  1060. msgfail
  1061. else
  1062. msgpass
  1063. fi
  1064. }
  1065. testdpkgnotinstalled() {
  1066. msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
  1067. local PKGS="$(dpkg -l "$@" 2> /dev/null | grep '^i' | wc -l)"
  1068. if [ "$PKGS" != 0 ]; then
  1069. echo
  1070. dpkg -l "$@" | grep '^[a-z]' >&2
  1071. msgfail
  1072. else
  1073. msgpass
  1074. fi
  1075. }
  1076. testmarkedauto() {
  1077. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
  1078. if [ -n "$1" ]; then
  1079. msgtest 'Test for correctly marked as auto-installed' "$*"
  1080. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  1081. else
  1082. msgtest 'Test for correctly marked as auto-installed' 'no package'
  1083. echo -n > $COMPAREFILE
  1084. fi
  1085. aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1086. }
  1087. testsuccess() {
  1088. if [ "$1" = '--nomsg' ]; then
  1089. shift
  1090. else
  1091. msgtest 'Test for successful execution of' "$*"
  1092. fi
  1093. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
  1094. if $@ >${OUTPUT} 2>&1; then
  1095. msgpass
  1096. else
  1097. local EXITCODE=$?
  1098. echo >&2
  1099. cat >&2 $OUTPUT
  1100. msgfail "exitcode $EXITCODE"
  1101. fi
  1102. }
  1103. testfailure() {
  1104. if [ "$1" = '--nomsg' ]; then
  1105. shift
  1106. else
  1107. msgtest 'Test for failure in execution of' "$*"
  1108. fi
  1109. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
  1110. if $@ >${OUTPUT} 2>&1; then
  1111. local EXITCODE=$?
  1112. echo >&2
  1113. cat >&2 $OUTPUT
  1114. msgfail "exitcode $EXITCODE"
  1115. else
  1116. msgpass
  1117. fi
  1118. }
  1119. testaccessrights() {
  1120. msgtest "Test that file $1 has access rights set to" "$2"
  1121. if [ "$2" = "$(stat --format '%a' "$1")" ]; then
  1122. msgpass
  1123. else
  1124. echo >&2
  1125. ls -l >&2 "$1"
  1126. echo -n >&2 "stat(1) reports access rights: "
  1127. stat --format '%a' "$1"
  1128. msgfail
  1129. fi
  1130. }
  1131. testwebserverlaststatuscode() {
  1132. local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
  1133. local STATUS='rootdir/tmp/webserverstatus-statusfile.log'
  1134. rm -f "$DOWNLOG" "$STATUS"
  1135. msgtest 'Test last status code from the webserver was' "$1"
  1136. downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG"
  1137. if [ "$(cat "$STATUS")" = "$1" ]; then
  1138. msgpass
  1139. else
  1140. echo >&2
  1141. if [ -n "$2" ]; then
  1142. shift
  1143. echo >&2 '#### Additionally provided output files contain:'
  1144. cat >&2 "$@"
  1145. fi
  1146. echo >&2 '#### Download log of the status code:'
  1147. cat >&2 "$DOWNLOG"
  1148. msgfail "Status was $(cat "$STATUS")"
  1149. fi
  1150. }
  1151. pause() {
  1152. echo "STOPPED execution. Press enter to continue"
  1153. local IGNORE
  1154. read IGNORE
  1155. }