framework 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. # cleanup the environment a bit
  231. # prefer our apt binaries over the system apt binaries
  232. export PATH="${BUILDDIRECTORY}:${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
  233. export LC_ALL=C.UTF-8
  234. unset LANGUAGE APT_CONFIG
  235. unset GREP_OPTIONS DEB_BUILD_PROFILES
  236. msgdone "info"
  237. }
  238. getarchitecture() {
  239. if [ "$1" = "native" -o -z "$1" ]; then
  240. eval `aptconfig shell ARCH APT::Architecture`
  241. if [ -n "$ARCH" ]; then
  242. echo $ARCH
  243. else
  244. dpkg --print-architecture
  245. fi
  246. else
  247. echo $1
  248. fi
  249. }
  250. getarchitectures() {
  251. echo "$(aptconfig dump | grep APT::Architecture | cut -d'"' -f 2 | sed '/^$/ d' | sort | uniq | tr '\n' ' ')"
  252. }
  253. getarchitecturesfromcommalist() {
  254. echo "$1" | sed -e 's#,#\n#g' | sed -e "s/^native\$/$(getarchitecture 'native')/"
  255. }
  256. configarchitecture() {
  257. {
  258. echo "APT::Architecture \"$(getarchitecture $1)\";"
  259. while [ -n "$1" ]; do
  260. echo "APT::Architectures:: \"$(getarchitecture $1)\";"
  261. shift
  262. done
  263. } >rootdir/etc/apt/apt.conf.d/01multiarch.conf
  264. configdpkg
  265. }
  266. configdpkg() {
  267. if [ ! -e rootdir/var/lib/dpkg/status ]; then
  268. local STATUSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/status-/' -e 's/^skip-/status-/')
  269. if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
  270. cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
  271. else
  272. echo -n > rootdir/var/lib/dpkg/status
  273. fi
  274. fi
  275. rm -f rootdir/etc/apt/apt.conf.d/00foreigndpkg
  276. if command dpkg --assert-multi-arch >/dev/null 2>&1 ; then
  277. local ARCHS="$(getarchitectures)"
  278. if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
  279. DPKGARCH="$(dpkg --print-architecture)"
  280. for ARCH in ${ARCHS}; do
  281. if [ "${ARCH}" != "${DPKGARCH}" ]; then
  282. if ! dpkg --add-architecture ${ARCH} >/dev/null 2>&1; then
  283. # old-style used e.g. in Ubuntu-P – and as it seems travis
  284. echo "DPKG::options:: \"--foreign-architecture\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  285. echo "DPKG::options:: \"${ARCH}\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  286. fi
  287. fi
  288. done
  289. if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
  290. # dpkg doesn't really check the version as long as it is fully installed,
  291. # but just to be sure we choose one above the required version
  292. insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
  293. fi
  294. fi
  295. fi
  296. }
  297. configcompression() {
  298. while [ -n "$1" ]; do
  299. case "$1" in
  300. '.') printf ".\t.\tcat\n";;
  301. 'gz') printf "gzip\tgz\tgzip\n";;
  302. 'bz2') printf "bzip2\tbz2\tbzip2\n";;
  303. 'lzma') printf "lzma\tlzma\txz --format=lzma\n";;
  304. 'xz') printf "xz\txz\txz\n";;
  305. *) printf "$1\t$1\t$1\n";;
  306. esac
  307. shift
  308. done > ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf
  309. }
  310. forcecompressor() {
  311. COMPRESSOR="$1"
  312. COMPRESSOR_CMD="$1"
  313. case $COMPRESSOR in
  314. gzip) COMPRESS='gz';;
  315. bzip2) COMPRESS='bz2';;
  316. lzma) COMPRESS='lzma';;
  317. xz) COMPRESS='xz';;
  318. *) msgdie "Compressor $COMPRESSOR is unknown to framework, so can't be forced by forcecompressor!";;
  319. esac
  320. local CONFFILE="${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/00force-compressor"
  321. echo "Acquire::CompressionTypes::Order { \"${COMPRESS}\"; };
  322. Dir::Bin::uncompressed \"/does/not/exist\";
  323. Dir::Bin::gzip \"/does/not/exist\";
  324. Dir::Bin::bzip2 \"/does/not/exist\";
  325. Dir::Bin::lzma \"/does/not/exist\";
  326. Dir::Bin::xz \"/does/not/exist\";" > "$CONFFILE"
  327. if [ -e "/bin/${COMPRESSOR}" ]; then
  328. echo "Dir::Bin::${COMPRESSOR} \"/bin/${COMPRESSOR}\";" >> "$CONFFILE"
  329. elif [ -e "/usr/bin/${COMPRESSOR}" ]; then
  330. echo "Dir::Bin::${COMPRESSOR} \"/usr/bin/${COMPRESSOR}\";" >> "$CONFFILE"
  331. elif [ "${COMPRESSOR}" = 'lzma' ]; then
  332. echo 'Dir::Bin::xz "/usr/bin/xz";' >> "$CONFFILE"
  333. COMPRESSOR_CMD='xz --format=lzma'
  334. else
  335. msgtest 'Test for availability of compressor' "${COMPRESSOR}"
  336. msgfail
  337. fi
  338. }
  339. setupsimplenativepackage() {
  340. local NAME="$1"
  341. local ARCH="$2"
  342. local VERSION="$3"
  343. local RELEASE="${4:-unstable}"
  344. local DEPENDENCIES="$5"
  345. local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  346. If you find such a package installed on your system,
  347. something went horribly wrong! They are autogenerated
  348. und used only by testcases and surf no other propose…"}"
  349. local SECTION="${7:-others}"
  350. local DISTSECTION
  351. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  352. DISTSECTION="main"
  353. else
  354. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  355. fi
  356. local BUILDDIR=incoming/${NAME}-${VERSION}
  357. mkdir -p ${BUILDDIR}/debian/source
  358. cd ${BUILDDIR}
  359. echo "* most suckless software product ever" > FEATURES
  360. test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
  361. test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
  362. * Initial release
  363. -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
  364. test -e debian/control || echo "Source: $NAME
  365. Section: $SECTION
  366. Priority: optional
  367. Maintainer: Joe Sixpack <joe@example.org>
  368. Build-Depends: debhelper (>= 7)
  369. Standards-Version: 3.9.1
  370. Package: $NAME" > debian/control
  371. if [ "$ARCH" = 'all' ]; then
  372. echo "Architecture: all" >> debian/control
  373. else
  374. echo "Architecture: any" >> debian/control
  375. fi
  376. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
  377. echo "Description: $DESCRIPTION" >> debian/control
  378. test -e debian/compat || echo "7" > debian/compat
  379. test -e debian/source/format || echo "3.0 (native)" > debian/source/format
  380. test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
  381. cd - > /dev/null
  382. }
  383. buildsimplenativepackage() {
  384. local NAME="$1"
  385. local ARCH="$2"
  386. local VERSION="$3"
  387. local RELEASE="${4:-unstable}"
  388. local DEPENDENCIES="$5"
  389. local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  390. If you find such a package installed on your system,
  391. something went horribly wrong! They are autogenerated
  392. und used only by testcases and surf no other propose…"}"
  393. local SECTION="${7:-others}"
  394. local PRIORITY="${8:-optional}"
  395. local FILE_TREE="$9"
  396. local COMPRESS_TYPE="${10:-gzip}"
  397. local DISTSECTION
  398. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  399. DISTSECTION="main"
  400. else
  401. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  402. fi
  403. local BUILDDIR=${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}
  404. msgninfo "Build package ${NAME} in ${VERSION} for ${RELEASE} in ${DISTSECTION}… "
  405. mkdir -p $BUILDDIR/debian/source
  406. echo "* most suckless software product ever" > ${BUILDDIR}/FEATURES
  407. echo "#!/bin/sh
  408. echo '$NAME says \"Hello!\"'" > ${BUILDDIR}/${NAME}
  409. echo "Copyleft by Joe Sixpack $(date +%Y)" > ${BUILDDIR}/debian/copyright
  410. echo "$NAME ($VERSION) $RELEASE; urgency=low
  411. * Initial release
  412. -- Joe Sixpack <joe@example.org> $(date -R)" > ${BUILDDIR}/debian/changelog
  413. echo "Source: $NAME
  414. Section: $SECTION
  415. Priority: $PRIORITY
  416. Maintainer: Joe Sixpack <joe@example.org>
  417. Standards-Version: 3.9.3" > ${BUILDDIR}/debian/control
  418. local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
  419. test -z "$BUILDDEPS" || echo "$BUILDDEPS" >> ${BUILDDIR}/debian/control
  420. echo "
  421. Package: $NAME" >> ${BUILDDIR}/debian/control
  422. if [ "$ARCH" = 'all' ]; then
  423. echo "Architecture: all" >> ${BUILDDIR}/debian/control
  424. else
  425. echo "Architecture: any" >> ${BUILDDIR}/debian/control
  426. fi
  427. local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
  428. test -z "$DEPS" || echo "$DEPS" >> ${BUILDDIR}/debian/control
  429. echo "Description: $DESCRIPTION" >> ${BUILDDIR}/debian/control
  430. echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
  431. (cd ${BUILDDIR}/..; dpkg-source -b ${NAME}-${VERSION} 2>&1) | sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' \
  432. | while read SRC; do
  433. echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
  434. # if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
  435. # aptkey --keyring ./keys/joesixpack.pub --secret-keyring ./keys/joesixpack.sec --quiet --readonly \
  436. # adv --yes --default-key 'Joe Sixpack' \
  437. # --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  438. # mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  439. # fi
  440. done
  441. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  442. rm -rf ${BUILDDIR}/debian/tmp
  443. mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
  444. cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
  445. cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
  446. if [ -n "$FILE_TREE" ]; then
  447. cp -ar "$FILE_TREE" ${BUILDDIR}/debian/tmp
  448. fi
  449. (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
  450. (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
  451. local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
  452. # ensure the right permissions as dpkg-deb ensists
  453. chmod 755 ${BUILDDIR}/debian/tmp/DEBIAN
  454. if ! dpkg-deb -Z${COMPRESS_TYPE} --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/.. >$LOG 2>&1; then
  455. cat $LOG
  456. false
  457. fi
  458. rm $LOG
  459. echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
  460. done
  461. mkdir -p ${BUILDDIR}/../${NAME}_${VERSION}
  462. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}/
  463. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}.changelog
  464. rm -rf "${BUILDDIR}"
  465. msgdone "info"
  466. }
  467. buildpackage() {
  468. local BUILDDIR=$1
  469. local RELEASE=$2
  470. local SECTION=$3
  471. local ARCH=$(getarchitecture $4)
  472. local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
  473. local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
  474. msgninfo "Build package ${PKGNAME} for ${RELEASE} in ${SECTION}… "
  475. cd $BUILDDIR
  476. if [ "$ARCH" = "all" ]; then
  477. ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
  478. fi
  479. if ! dpkg-buildpackage -uc -us -a$ARCH >$BUILDLOG 2>&1 ; then
  480. cat $BUILDLOG
  481. false
  482. fi
  483. local PKGS="$(grep '^dpkg-deb: building package' $BUILDLOG | cut -d'/' -f 2 | sed -e "s#'\.##")"
  484. local SRCS="$(grep '^dpkg-source: info: building' $BUILDLOG | grep -o '[a-z0-9._+~-]*$')"
  485. cd - > /dev/null
  486. for PKG in $PKGS; do
  487. echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
  488. done
  489. for SRC in $SRCS; do
  490. echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
  491. done
  492. msgdone "info"
  493. }
  494. buildaptarchive() {
  495. if [ -d incoming ]; then
  496. buildaptarchivefromincoming "$@"
  497. else
  498. buildaptarchivefromfiles "$@"
  499. fi
  500. }
  501. createaptftparchiveconfig() {
  502. local COMPRESSORS="$(cut -d' ' -f 1 ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | tr '\n' ' ')"
  503. COMPRESSORS="${COMPRESSORS%* }"
  504. 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' ' ')"
  505. if [ -z "$ARCHS" ]; then
  506. # the pool is empty, so we will operate on faked packages - let us use the configured archs
  507. ARCHS="$(getarchitectures)"
  508. fi
  509. echo -n 'Dir {
  510. ArchiveDir "' >> ftparchive.conf
  511. echo -n $(readlink -f .) >> ftparchive.conf
  512. echo -n '";
  513. CacheDir "' >> ftparchive.conf
  514. echo -n $(readlink -f ..) >> ftparchive.conf
  515. echo -n '";
  516. FileListDir "' >> ftparchive.conf
  517. echo -n $(readlink -f pool/) >> ftparchive.conf
  518. echo -n '";
  519. };
  520. Default {
  521. Packages::Compress "'"$COMPRESSORS"'";
  522. Sources::Compress "'"$COMPRESSORS"'";
  523. Contents::Compress "'"$COMPRESSORS"'";
  524. Translation::Compress "'"$COMPRESSORS"'";
  525. LongDescription "false";
  526. };
  527. TreeDefault {
  528. Directory "pool/";
  529. SrcDirectory "pool/";
  530. };
  531. APT {
  532. FTPArchive {
  533. Release {
  534. Origin "joesixpack";
  535. Label "apttestcases";
  536. Suite "unstable";
  537. Description "repository with dummy packages";
  538. Architectures "' >> ftparchive.conf
  539. echo -n "$ARCHS" >> ftparchive.conf
  540. echo 'source";
  541. };
  542. };
  543. };' >> ftparchive.conf
  544. for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
  545. echo -n 'tree "dists/' >> ftparchive.conf
  546. echo -n "$DIST" >> ftparchive.conf
  547. echo -n '" {
  548. Architectures "' >> ftparchive.conf
  549. echo -n "$ARCHS" >> ftparchive.conf
  550. echo -n 'source";
  551. FileList "' >> ftparchive.conf
  552. echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
  553. echo -n '";
  554. SourceFileList "' >> ftparchive.conf
  555. echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
  556. echo -n '";
  557. Sections "' >> ftparchive.conf
  558. 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
  559. echo '";
  560. };' >> ftparchive.conf
  561. done
  562. }
  563. buildaptftparchivedirectorystructure() {
  564. local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
  565. for DIST in $DISTS; do
  566. local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
  567. for SECTION in $SECTIONS; do
  568. local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
  569. for ARCH in $ARCHS; do
  570. mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
  571. done
  572. mkdir -p dists/${DIST}/${SECTION}/source
  573. mkdir -p dists/${DIST}/${SECTION}/i18n
  574. done
  575. done
  576. }
  577. insertpackage() {
  578. local RELEASE="$1"
  579. local NAME="$2"
  580. local ARCH="$3"
  581. local VERSION="$4"
  582. local DEPENDENCIES="$5"
  583. local PRIORITY="${6:-optional}"
  584. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  585. If you find such a package installed on your system,
  586. something went horribly wrong! They are autogenerated
  587. und used only by testcases and surf no other propose…"}"
  588. local ARCHS=""
  589. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  590. if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
  591. ARCHS="$(getarchitectures)"
  592. else
  593. ARCHS="$arch"
  594. fi
  595. for BUILDARCH in $ARCHS; do
  596. local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
  597. mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
  598. touch aptarchive/dists/${RELEASE}/main/source/Sources
  599. local FILE="${PPATH}/Packages"
  600. echo "Package: $NAME
  601. Priority: $PRIORITY
  602. Section: other
  603. Installed-Size: 42
  604. Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
  605. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  606. echo "Version: $VERSION
  607. Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
  608. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  609. echo "Description: $DESCRIPTION" >> $FILE
  610. echo >> $FILE
  611. done
  612. done
  613. }
  614. insertsource() {
  615. local RELEASE="$1"
  616. local NAME="$2"
  617. local ARCH="$3"
  618. local VERSION="$4"
  619. local DEPENDENCIES="$5"
  620. local ARCHS=""
  621. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  622. mkdir -p $SPATH
  623. local FILE="${SPATH}/Sources"
  624. echo "Package: $NAME
  625. Binary: $NAME
  626. Version: $VERSION
  627. Maintainer: Joe Sixpack <joe@example.org>
  628. Architecture: $ARCH" >> $FILE
  629. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  630. echo "Files:
  631. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
  632. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
  633. " >> $FILE
  634. }
  635. insertinstalledpackage() {
  636. local NAME="$1"
  637. local ARCH="$2"
  638. local VERSION="$3"
  639. local DEPENDENCIES="$4"
  640. local PRIORITY="${5:-optional}"
  641. local STATUS="${6:-install ok installed}"
  642. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
  643. If you find such a package installed on your system,
  644. something went horribly wrong! They are autogenerated
  645. und used only by testcases and surf no other propose…"}"
  646. local FILE='rootdir/var/lib/dpkg/status'
  647. local INFO='rootdir/var/lib/dpkg/info'
  648. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  649. echo "Package: $NAME
  650. Status: $STATUS
  651. Priority: $PRIORITY
  652. Section: other
  653. Installed-Size: 42
  654. Maintainer: Joe Sixpack <joe@example.org>
  655. Version: $VERSION" >> $FILE
  656. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  657. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  658. echo "Description: $DESCRIPTION" >> $FILE
  659. echo >> $FILE
  660. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  661. echo -n > ${INFO}/${NAME}:${arch}.list
  662. else
  663. echo -n > ${INFO}/${NAME}.list
  664. fi
  665. done
  666. }
  667. buildaptarchivefromincoming() {
  668. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  669. cd aptarchive
  670. [ -e pool ] || ln -s ../incoming pool
  671. [ -e ftparchive.conf ] || createaptftparchiveconfig
  672. [ -e dists ] || buildaptftparchivedirectorystructure
  673. msgninfo "\tGenerate Packages, Sources and Contents files… "
  674. aptftparchive -qq generate ftparchive.conf
  675. cd - > /dev/null
  676. msgdone "info"
  677. generatereleasefiles "$@"
  678. }
  679. buildaptarchivefromfiles() {
  680. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  681. find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
  682. msgninfo "\t${line} file… "
  683. compressfile "$line" "$1"
  684. msgdone "info"
  685. done
  686. generatereleasefiles "$@"
  687. }
  688. compressfile() {
  689. cat ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | while read compressor extension command; do
  690. if [ "$compressor" = '.' ]; then
  691. if [ -n "$2" ]; then
  692. touch -d "$2" "$1"
  693. fi
  694. continue
  695. fi
  696. cat "$1" | $command > "${1}.${extension}"
  697. if [ -n "$2" ]; then
  698. touch -d "$2" "${1}.${extension}"
  699. fi
  700. done
  701. }
  702. # can be overridden by testcases for their pleasure
  703. getcodenamefromsuite() {
  704. case "$1" in
  705. unstable) echo 'sid';;
  706. *) echo -n "$1";;
  707. esac
  708. }
  709. getreleaseversionfromsuite() { true; }
  710. getlabelfromsuite() { true; }
  711. generatereleasefiles() {
  712. # $1 is the Date header and $2 is the ValidUntil header to be set
  713. # both should be given in notation date/touch can understand
  714. msgninfo "\tGenerate Release files… "
  715. if [ -e aptarchive/dists ]; then
  716. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  717. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  718. local CODENAME="$(getcodenamefromsuite $SUITE)"
  719. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  720. local LABEL="$(getlabelfromsuite $SUITE)"
  721. if [ -n "$VERSION" ]; then
  722. VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
  723. fi
  724. if [ -n "$LABEL" ]; then
  725. LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
  726. fi
  727. aptftparchive -qq release $dir \
  728. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  729. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  730. ${LABEL} \
  731. ${VERSION} \
  732. | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  733. if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
  734. sed -i '/^Date: / a\
  735. NotAutomatic: yes' $dir/Release
  736. fi
  737. if [ -n "$1" -a "$1" != "now" ]; then
  738. sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
  739. fi
  740. if [ -n "$2" ]; then
  741. sed -i "/^Date: / a\
  742. Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
  743. fi
  744. done
  745. else
  746. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  747. fi
  748. if [ -n "$1" -a "$1" != "now" ]; then
  749. for release in $(find ./aptarchive -name 'Release'); do
  750. touch -d "$1" $release
  751. done
  752. fi
  753. msgdone "info"
  754. }
  755. setupdistsaptarchive() {
  756. local APTARCHIVE=$(readlink -f ./aptarchive)
  757. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  758. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  759. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  760. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  761. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  762. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  763. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  764. msgdone "info"
  765. done
  766. }
  767. setupflataptarchive() {
  768. local APTARCHIVE=$(readlink -f ./aptarchive)
  769. if [ -f ${APTARCHIVE}/Packages ]; then
  770. msgninfo "\tadd deb sources.list line… "
  771. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  772. msgdone "info"
  773. else
  774. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  775. fi
  776. if [ -f ${APTARCHIVE}/Sources ]; then
  777. msgninfo "\tadd deb-src sources.list line… "
  778. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  779. msgdone "info"
  780. else
  781. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  782. fi
  783. }
  784. setupaptarchive() {
  785. local NOUPDATE=0
  786. if [ "$1" = '--no-update' ]; then
  787. NOUPDATE=1
  788. shift
  789. fi
  790. buildaptarchive "$@"
  791. if [ -e aptarchive/dists ]; then
  792. setupdistsaptarchive
  793. else
  794. setupflataptarchive
  795. fi
  796. signreleasefiles 'Joe Sixpack' "$@"
  797. if [ "1" != "$NOUPDATE" ]; then
  798. testsuccess aptget update -o Debug::pkgAcquire::Worker=true -o Debug::Acquire::gpgv=true
  799. fi
  800. }
  801. signreleasefiles() {
  802. local SIGNER="${1:-Joe Sixpack}"
  803. local KEY="keys/$(echo "$SIGNER" | tr 'A-Z' 'a-z' | sed 's# ##g')"
  804. local GPG="aptkey --quiet --keyring ${KEY}.pub --secret-keyring ${KEY}.sec --readonly adv --batch --yes"
  805. msgninfo "\tSign archive with $SIGNER key $KEY… "
  806. local REXKEY='keys/rexexpired'
  807. local SECEXPIREBAK="${REXKEY}.sec.bak"
  808. local PUBEXPIREBAK="${REXKEY}.pub.bak"
  809. if [ "${SIGNER}" = 'Rex Expired' ]; then
  810. # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
  811. # option doesn't exist anymore (and using faketime would add a new obscure dependency)
  812. # therefore we 'temporary' make the key not expired and restore a backup after signing
  813. cp ${REXKEY}.sec $SECEXPIREBAK
  814. cp ${REXKEY}.pub $PUBEXPIREBAK
  815. local SECUNEXPIRED="${REXKEY}.sec.unexpired"
  816. local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
  817. if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
  818. cp $SECUNEXPIRED ${REXKEY}.sec
  819. cp $PUBUNEXPIRED ${REXKEY}.pub
  820. else
  821. if ! printf "expire\n1w\nsave\n" | $GPG --default-key "$SIGNER" --command-fd 0 --edit-key "${SIGNER}" >setexpire.gpg 2>&1; then
  822. cat setexpire.gpg
  823. exit 1
  824. fi
  825. cp ${REXKEY}.sec $SECUNEXPIRED
  826. cp ${REXKEY}.pub $PUBUNEXPIRED
  827. fi
  828. fi
  829. for RELEASE in $(find aptarchive/ -name Release); do
  830. $GPG --default-key "$SIGNER" --armor --detach-sign --sign --output ${RELEASE}.gpg ${RELEASE}
  831. local INRELEASE="$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')"
  832. $GPG --default-key "$SIGNER" --clearsign --output $INRELEASE $RELEASE
  833. # we might have set a specific date for the Release file, so copy it
  834. touch -d "$(stat --format "%y" ${RELEASE})" ${RELEASE}.gpg ${INRELEASE}
  835. done
  836. if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
  837. mv -f $SECEXPIREBAK ${REXKEY}.sec
  838. mv -f $PUBEXPIREBAK ${REXKEY}.pub
  839. fi
  840. msgdone "info"
  841. }
  842. webserverconfig() {
  843. msgtest "Set webserver config option '${1}' to" "$2"
  844. local DOWNLOG='rootdir/tmp/download-testfile.log'
  845. local STATUS='rootdir/tmp/webserverconfig.status'
  846. rm -f "$STATUS" "$DOWNLOG"
  847. if downloadfile "http://localhost:8080/_config/set/${1}/${2}" "$STATUS" > "$DOWNLOG"; then
  848. msgpass
  849. else
  850. cat "$DOWNLOG" "$STATUS"
  851. msgfail
  852. fi
  853. testwebserverlaststatuscode '200'
  854. }
  855. rewritesourceslist() {
  856. local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive")"
  857. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  858. sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#http://localhost:8080/#${1}#" -e "s#http://localhost:4433/#${1}#"
  859. done
  860. }
  861. # wait for up to 10s for a pid file to appear to avoid possible race
  862. # when a helper is started and dosn't write the PID quick enough
  863. waitforpidfile() {
  864. local PIDFILE="$1"
  865. for i in $(seq 10); do
  866. if test -s "$PIDFILE"; then
  867. return 0
  868. fi
  869. sleep 1
  870. done
  871. msgdie "waiting for $PIDFILE failed"
  872. return 1
  873. }
  874. changetowebserver() {
  875. if [ "$1" != '--no-rewrite' ]; then
  876. rewritesourceslist 'http://localhost:8080/'
  877. else
  878. shift
  879. fi
  880. if test -x ${APTWEBSERVERBINDIR}/aptwebserver; then
  881. cd aptarchive
  882. local LOG="webserver.log"
  883. if ! aptwebserver -o aptwebserver::fork=1 "$@" >$LOG 2>&1 ; then
  884. cat $LOG
  885. false
  886. fi
  887. waitforpidfile aptwebserver.pid
  888. local PID="$(cat aptwebserver.pid)"
  889. if [ -z "$PID" ]; then
  890. msgdie 'Could not fork aptwebserver successfully'
  891. fi
  892. addtrap "kill $PID;"
  893. cd - > /dev/null
  894. else
  895. msgdie 'You have to build aptwerbserver or install a webserver'
  896. fi
  897. }
  898. changetohttpswebserver() {
  899. if ! which stunnel4 >/dev/null; then
  900. msgdie 'You need to install stunnel4 for https testcases'
  901. fi
  902. if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
  903. changetowebserver --no-rewrite "$@"
  904. fi
  905. echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
  906. cert = ${TESTDIRECTORY}/apt.pem
  907. output = /dev/null
  908. [https]
  909. accept = 4433
  910. connect = 8080
  911. " > ${TMPWORKINGDIRECTORY}/stunnel.conf
  912. stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
  913. waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
  914. local PID="$(cat ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid)"
  915. if [ -z "$PID" ]; then
  916. msgdie 'Could not fork stunnel4 successfully'
  917. fi
  918. addtrap 'prefix' "kill ${PID};"
  919. rewritesourceslist 'https://localhost:4433/'
  920. }
  921. changetocdrom() {
  922. mkdir -p rootdir/media/cdrom/.disk
  923. local CD="$(readlink -f rootdir/media/cdrom)"
  924. echo "acquire::cdrom::mount \"${CD}\";
  925. acquire::cdrom::${CD}/::mount \"mv ${CD}-unmounted ${CD}\";
  926. acquire::cdrom::${CD}/::umount \"mv ${CD} ${CD}-unmounted\";
  927. acquire::cdrom::autodetect 0;" > rootdir/etc/apt/apt.conf.d/00cdrom
  928. echo -n "$1" > ${CD}/.disk/info
  929. if [ ! -d aptarchive/dists ]; then
  930. msgdie 'Flat file archive cdroms can not be created currently'
  931. return 1
  932. fi
  933. mv aptarchive/dists "$CD"
  934. ln -s "$(readlink -f ./incoming)" $CD/pool
  935. find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
  936. # start with an unmounted disk
  937. mv "${CD}" "${CD}-unmounted"
  938. # we don't want the disk to be modifiable
  939. addtrap 'prefix' "chmod -f -R +w $PWD/rootdir/media/cdrom/dists/ $PWD/rootdir/media/cdrom-unmounted/dists/ || true;"
  940. chmod -R -w rootdir/media/cdrom-unmounted/dists
  941. }
  942. downloadfile() {
  943. local PROTO="$(echo "$1" | cut -d':' -f 1 )"
  944. apthelper -o Debug::Acquire::${PROTO}=1 \
  945. download-file "$1" "$2" 2>&1 || true
  946. # only if the file exists the download was successful
  947. if [ -e "$2" ]; then
  948. return 0
  949. else
  950. return 1
  951. fi
  952. }
  953. checkdiff() {
  954. local DIFFTEXT="$(command diff -u "$@" | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  955. if [ -n "$DIFFTEXT" ]; then
  956. echo >&2
  957. echo >&2 "$DIFFTEXT"
  958. return 1
  959. else
  960. return 0
  961. fi
  962. }
  963. testfileequal() {
  964. local FILE="$1"
  965. shift
  966. msgtest "Test for correctness of file" "$FILE"
  967. if [ -z "$*" ]; then
  968. echo -n "" | checkdiff $FILE - && msgpass || msgfail
  969. else
  970. echo "$*" | checkdiff $FILE - && msgpass || msgfail
  971. fi
  972. }
  973. testempty() {
  974. msgtest "Test for no output of" "$*"
  975. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
  976. if $* >$COMPAREFILE 2>&1 && test ! -s $COMPAREFILE; then
  977. msgpass
  978. else
  979. cat $COMPAREFILE
  980. msgfail
  981. fi
  982. }
  983. testequal() {
  984. local MSG='Test of equality of'
  985. if [ "$1" = '--nomsg' ]; then
  986. MSG=''
  987. shift
  988. fi
  989. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
  990. echo "$1" > $COMPAREFILE
  991. shift
  992. if [ -n "$MSG" ]; then
  993. msgtest "$MSG" "$*"
  994. fi
  995. $* 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  996. }
  997. testequalor2() {
  998. local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
  999. local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
  1000. local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
  1001. echo "$1" > $COMPAREFILE1
  1002. echo "$2" > $COMPAREFILE2
  1003. shift 2
  1004. msgtest "Test for equality OR of" "$*"
  1005. $* >$COMPAREAGAINST 2>&1 || true
  1006. if checkdiff $COMPAREFILE1 $COMPAREAGAINST >/dev/null 2>&1 || \
  1007. checkdiff $COMPAREFILE2 $COMPAREAGAINST >/dev/null 2>&1
  1008. then
  1009. msgpass
  1010. else
  1011. echo -n "\n${CINFO}Diff against OR 1${CNORMAL}"
  1012. checkdiff $COMPAREFILE1 $COMPAREAGAINST || true
  1013. echo -n "${CINFO}Diff against OR 2${CNORMAL}"
  1014. checkdiff $COMPAREFILE2 $COMPAREAGAINST || true
  1015. msgfail
  1016. fi
  1017. }
  1018. testshowvirtual() {
  1019. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  1020. local PACKAGE="$1"
  1021. shift
  1022. while [ -n "$1" ]; do
  1023. VIRTUAL="${VIRTUAL}
  1024. N: Can't select versions from package '$1' as it is purely virtual"
  1025. PACKAGE="${PACKAGE} $1"
  1026. shift
  1027. done
  1028. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  1029. VIRTUAL="${VIRTUAL}
  1030. N: No packages found"
  1031. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
  1032. local ARCH="$(getarchitecture 'native')"
  1033. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  1034. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1035. }
  1036. testnopackage() {
  1037. msgtest "Test for non-existent packages" "apt-cache show $*"
  1038. local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
  1039. if [ -n "$SHOWPKG" ]; then
  1040. echo >&2
  1041. echo >&2 "$SHOWPKG"
  1042. msgfail
  1043. else
  1044. msgpass
  1045. fi
  1046. }
  1047. testdpkginstalled() {
  1048. msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
  1049. local PKGS="$(dpkg -l "$@" 2>/dev/null | grep '^i' | wc -l)"
  1050. if [ "$PKGS" != $# ]; then
  1051. echo >&2 $PKGS
  1052. dpkg -l "$@" | grep '^[a-z]' >&2
  1053. msgfail
  1054. else
  1055. msgpass
  1056. fi
  1057. }
  1058. testdpkgnotinstalled() {
  1059. msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
  1060. local PKGS="$(dpkg -l "$@" 2> /dev/null | grep '^i' | wc -l)"
  1061. if [ "$PKGS" != 0 ]; then
  1062. echo
  1063. dpkg -l "$@" | grep '^[a-z]' >&2
  1064. msgfail
  1065. else
  1066. msgpass
  1067. fi
  1068. }
  1069. testmarkedauto() {
  1070. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
  1071. if [ -n "$1" ]; then
  1072. msgtest 'Test for correctly marked as auto-installed' "$*"
  1073. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  1074. else
  1075. msgtest 'Test for correctly marked as auto-installed' 'no package'
  1076. echo -n > $COMPAREFILE
  1077. fi
  1078. aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1079. }
  1080. testsuccess() {
  1081. if [ "$1" = '--nomsg' ]; then
  1082. shift
  1083. else
  1084. msgtest 'Test for successful execution of' "$*"
  1085. fi
  1086. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
  1087. if $@ >${OUTPUT} 2>&1; then
  1088. msgpass
  1089. else
  1090. echo >&2
  1091. cat >&2 $OUTPUT
  1092. msgfail
  1093. fi
  1094. }
  1095. testfailure() {
  1096. if [ "$1" = '--nomsg' ]; then
  1097. shift
  1098. else
  1099. msgtest 'Test for failure in execution of' "$*"
  1100. fi
  1101. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
  1102. if $@ >${OUTPUT} 2>&1; then
  1103. echo >&2
  1104. cat >&2 $OUTPUT
  1105. msgfail
  1106. else
  1107. msgpass
  1108. fi
  1109. }
  1110. testaccessrights() {
  1111. msgtest "Test that file $1 has access rights set to" "$2"
  1112. if [ "$2" = "$(stat --format '%a' "$1")" ]; then
  1113. msgpass
  1114. else
  1115. echo >&2
  1116. ls -l >&2 "$1"
  1117. echo -n >&2 "stat(1) reports access rights: "
  1118. stat --format '%a' "$1"
  1119. msgfail
  1120. fi
  1121. }
  1122. testwebserverlaststatuscode() {
  1123. local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
  1124. local STATUS='rootdir/tmp/webserverstatus-statusfile.log'
  1125. rm -f "$DOWNLOG" "$STATUS"
  1126. msgtest 'Test last status code from the webserver was' "$1"
  1127. downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG"
  1128. if [ "$(cat "$STATUS")" = "$1" ]; then
  1129. msgpass
  1130. else
  1131. echo >&2
  1132. if [ -n "$2" ]; then
  1133. shift
  1134. echo >&2 '#### Additionally provided output files contain:'
  1135. cat >&2 "$@"
  1136. fi
  1137. echo >&2 '#### Download log of the status code:'
  1138. cat >&2 "$DOWNLOG"
  1139. msgfail "Status was $(cat "$STATUS")"
  1140. fi
  1141. }
  1142. pause() {
  1143. echo "STOPPED execution. Press enter to continue"
  1144. local IGNORE
  1145. read IGNORE
  1146. }