framework 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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. EXIT_CODE=$((EXIT_CODE+1));
  46. }
  47. # enable / disable Debugging
  48. MSGLEVEL=${MSGLEVEL:-3}
  49. if [ $MSGLEVEL -le 0 ]; then
  50. msgdie() { true; }
  51. fi
  52. if [ $MSGLEVEL -le 1 ]; then
  53. msgwarn() { true; }
  54. msgnwarn() { true; }
  55. fi
  56. if [ $MSGLEVEL -le 2 ]; then
  57. msgmsg() { true; }
  58. msgnmsg() { true; }
  59. msgtest() { true; }
  60. msgpass() { printf " ${CPASS}P${CNORMAL}"; }
  61. msgskip() { printf " ${CWARNING}S${CNORMAL}" >&2; }
  62. if [ -n "$CFAIL" ]; then
  63. msgfail() { printf " ${CFAIL}FAIL${CNORMAL}" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
  64. else
  65. msgfail() { printf " ###FAILED###" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
  66. fi
  67. fi
  68. if [ $MSGLEVEL -le 3 ]; then
  69. msginfo() { true; }
  70. msgninfo() { true; }
  71. fi
  72. if [ $MSGLEVEL -le 4 ]; then
  73. msgdebug() { true; }
  74. msgndebug() { true; }
  75. fi
  76. msgdone() {
  77. if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
  78. [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
  79. [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
  80. [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
  81. [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
  82. true;
  83. else
  84. printf "${CDONE}DONE${CNORMAL}\n";
  85. fi
  86. }
  87. getaptconfig() {
  88. if [ -f ./aptconfig.conf ]; then
  89. echo "./aptconfig.conf"
  90. elif [ -f ../aptconfig.conf ]; then
  91. echo "../aptconfig.conf"
  92. fi
  93. }
  94. runapt() {
  95. msgdebug "Executing: ${CCMD}$*${CDEBUG} "
  96. local CMD="$1"
  97. shift
  98. case $CMD in
  99. sh|aptitude|*/*) ;;
  100. *) CMD="${BUILDDIRECTORY}/$CMD";;
  101. esac
  102. MALLOC_PERTURB_=21 MALLOC_CHECK_=2 APT_CONFIG="$(getaptconfig)" LD_LIBRARY_PATH=${BUILDDIRECTORY} $CMD "$@"
  103. }
  104. aptconfig() { runapt apt-config "$@"; }
  105. aptcache() { runapt apt-cache "$@"; }
  106. aptcdrom() { runapt apt-cdrom "$@"; }
  107. aptget() { runapt apt-get "$@"; }
  108. aptftparchive() { runapt apt-ftparchive "$@"; }
  109. aptkey() { runapt apt-key "$@"; }
  110. aptmark() { runapt apt-mark "$@"; }
  111. aptsortpkgs() { runapt apt-sortpkgs "$@"; }
  112. apt() { runapt apt "$@"; }
  113. apthelper() { runapt "${APTHELPERBINDIR}/apt-helper" "$@"; }
  114. aptwebserver() { runapt "${APTWEBSERVERBINDIR}/aptwebserver" "$@"; }
  115. aptitude() { runapt aptitude "$@"; }
  116. aptextracttemplates() { runapt apt-extracttemplates "$@"; }
  117. aptinternalsolver() { runapt "${APTINTERNALSOLVER}" "$@"; }
  118. dpkg() {
  119. command dpkg --root=${TMPWORKINGDIRECTORY}/rootdir --force-not-root --force-bad-path --log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log "$@"
  120. }
  121. dpkgcheckbuilddeps() {
  122. command dpkg-checkbuilddeps --admindir=${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg "$@"
  123. }
  124. gdb() {
  125. echo "gdb: run »$*«"
  126. CMD="$1"
  127. shift
  128. APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${LIBRARYPATH} command gdb ${BUILDDIRECTORY}/$CMD --args ${BUILDDIRECTORY}/$CMD "$@"
  129. }
  130. gpg() {
  131. # see apt-key for the whole trickery. Setup is done in setupenvironment
  132. command gpg --ignore-time-conflict --no-options --no-default-keyring \
  133. --homedir "${TMPWORKINGDIRECTORY}/gnupghome" \
  134. --no-auto-check-trustdb --trust-model always \
  135. "$@"
  136. }
  137. exitwithstatus() {
  138. # error if we about to overflow, but ...
  139. # "255 failures ought to be enough for everybody"
  140. if [ $EXIT_CODE -gt 255 ]; then
  141. msgdie "Total failure count $EXIT_CODE too big"
  142. fi
  143. exit $((EXIT_CODE <= 255 ? EXIT_CODE : 255));
  144. }
  145. shellsetedetector() {
  146. local exit_status=$?
  147. if [ "$exit_status" != '0' ]; then
  148. printf >&2 "${CERROR}E: Looks like the testcases ended prematurely with exitcode: ${exit_status}${CNORMAL}\n"
  149. if [ "$EXIT_CODE" = '0' ]; then
  150. EXIT_CODE="$exit_status"
  151. fi
  152. fi
  153. }
  154. addtrap() {
  155. if [ "$1" = 'prefix' ]; then
  156. CURRENTTRAP="$2 $CURRENTTRAP"
  157. else
  158. CURRENTTRAP="$CURRENTTRAP $1"
  159. fi
  160. trap "shellsetedetector; $CURRENTTRAP exitwithstatus;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  161. }
  162. setupenvironment() {
  163. TMPWORKINGDIRECTORY=$(mktemp -d)
  164. TESTDIRECTORY=$(readlink -f $(dirname $0))
  165. msgninfo "Preparing environment for ${CCMD}$(basename $0)${CINFO} in ${TMPWORKINGDIRECTORY}… "
  166. # allow overriding the default BUILDDIR location
  167. BUILDDIRECTORY=${APT_INTEGRATION_TESTS_BUILD_DIR:-"${TESTDIRECTORY}/../../build/bin"}
  168. LIBRARYPATH=${APT_INTEGRATION_TESTS_LIBRARY_PATH:-"${BUILDDIRECTORY}"}
  169. METHODSDIR=${APT_INTEGRATION_TESTS_METHODS_DIR:-"${BUILDDIRECTORY}/methods"}
  170. APTHELPERBINDIR=${APT_INTEGRATION_TESTS_LIBEXEC_DIR:-"${BUILDDIRECTORY}"}
  171. APTWEBSERVERBINDIR=${APT_INTEGRATION_TESTS_WEBSERVER_BIN_DIR:-"${BUILDDIRECTORY}"}
  172. APTINTERNALSOLVER=${APT_INTEGRATION_TESTS_INTERNAL_SOLVER:-"${BUILDDIRECTORY}/apt-internal-solver"}
  173. test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
  174. # -----
  175. addtrap "cd /; rm -rf $TMPWORKINGDIRECTORY;"
  176. cd $TMPWORKINGDIRECTORY
  177. mkdir rootdir aptarchive keys
  178. cd rootdir
  179. mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
  180. mkdir -p var/cache var/lib/apt var/log tmp
  181. mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
  182. touch var/lib/dpkg/available
  183. mkdir -p usr/lib/apt
  184. ln -s ${METHODSDIR} usr/lib/apt/methods
  185. if [ "$BUILDDIRECTORY" = "$LIBRARYPATH" ]; then
  186. mkdir -p usr/lib/apt/solvers
  187. ln -s "${BUILDDIRECTORY}/apt-dump-solver" usr/lib/apt/solvers/dump
  188. ln -s "${BUILDDIRECTORY}/apt-internal-solver" usr/lib/apt/solvers/apt
  189. echo "Dir::Bin::Solvers \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/solvers\";" > etc/apt/apt.conf.d/externalsolver.conf
  190. fi
  191. # use the autoremove from the BUILDDIRECTORY if its there, otherwise
  192. # system
  193. if [ -e ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove ]; then
  194. ln -s ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove etc/apt/apt.conf.d/01autoremove
  195. else
  196. ln -s /etc/apt/apt.conf.d/01autoremove etc/apt/apt.conf.d/01autoremove
  197. fi
  198. cd ..
  199. local PACKAGESFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Packages-/' -e 's/^skip-/Packages-/')
  200. if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
  201. cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
  202. fi
  203. local SOURCESSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Sources-/' -e 's/^skip-/Sources-/')
  204. if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
  205. cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
  206. fi
  207. cp $(find $TESTDIRECTORY -name '*.pub' -o -name '*.sec') keys/
  208. ln -s ${TMPWORKINGDIRECTORY}/keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  209. echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
  210. echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
  211. echo "Debug::NoLocking \"true\";" >> aptconfig.conf
  212. echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
  213. echo "Dir::Bin::Methods \"${METHODSDIR}\";" >> aptconfig.conf
  214. echo "Dir::Bin::dpkg \"fakeroot\";" >> aptconfig.conf
  215. echo "DPKG::options:: \"dpkg\";" >> aptconfig.conf
  216. echo "DPKG::options:: \"--root=${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
  217. echo "DPKG::options:: \"--force-not-root\";" >> aptconfig.conf
  218. echo "DPKG::options:: \"--force-bad-path\";" >> aptconfig.conf
  219. if ! command dpkg --assert-multi-arch >/dev/null 2>&1; then
  220. echo "DPKG::options:: \"--force-architecture\";" >> aptconfig.conf # Added to test multiarch before dpkg is ready for it…
  221. fi
  222. echo "DPKG::options:: \"--log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log\";" >> aptconfig.conf
  223. echo 'quiet::NoUpdate "true";' >> aptconfig.conf
  224. echo "Acquire::https::CaInfo \"${TESTDIR}/apt.pem\";" > rootdir/etc/apt/apt.conf.d/99https
  225. echo "Apt::Cmd::Disable-Script-Warning \"1\";" > rootdir/etc/apt/apt.conf.d/apt-binary
  226. configcompression '.' 'gz' #'bz2' 'lzma' 'xz'
  227. # gpg needs a trustdb to function, but it can't be invalid (not even empty)
  228. # see also apt-key where this trickery comes from:
  229. local TRUSTDBDIR="${TMPWORKINGDIRECTORY}/gnupghome"
  230. mkdir "$TRUSTDBDIR"
  231. chmod 700 "$TRUSTDBDIR"
  232. # We also don't use a secret keyring, of course, but gpg panics and
  233. # implodes if there isn't one available - and writeable for imports
  234. local SECRETKEYRING="${TRUSTDBDIR}/secring.gpg"
  235. touch $SECRETKEYRING
  236. # now create the trustdb with an (empty) dummy keyring
  237. # newer gpg versions are fine without it, but play it safe for now
  238. gpg --quiet --check-trustdb --secret-keyring $SECRETKEYRING --keyring $SECRETKEYRING >/dev/null 2>&1
  239. # cleanup the environment a bit
  240. export PATH="${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
  241. export LC_ALL=C.UTF-8
  242. unset LANGUAGE APT_CONFIG
  243. unset GREP_OPTIONS DEB_BUILD_PROFILES
  244. msgdone "info"
  245. }
  246. getarchitecture() {
  247. if [ "$1" = "native" -o -z "$1" ]; then
  248. eval `aptconfig shell ARCH APT::Architecture`
  249. if [ -n "$ARCH" ]; then
  250. echo $ARCH
  251. else
  252. dpkg --print-architecture
  253. fi
  254. else
  255. echo $1
  256. fi
  257. }
  258. getarchitectures() {
  259. echo "$(aptconfig dump | grep APT::Architecture | cut -d'"' -f 2 | sed '/^$/ d' | sort | uniq | tr '\n' ' ')"
  260. }
  261. getarchitecturesfromcommalist() {
  262. echo "$1" | sed -e 's#,#\n#g' | sed -e "s/^native\$/$(getarchitecture 'native')/"
  263. }
  264. configarchitecture() {
  265. {
  266. echo "APT::Architecture \"$(getarchitecture $1)\";"
  267. while [ -n "$1" ]; do
  268. echo "APT::Architectures:: \"$(getarchitecture $1)\";"
  269. shift
  270. done
  271. } >rootdir/etc/apt/apt.conf.d/01multiarch.conf
  272. configdpkg
  273. }
  274. configdpkg() {
  275. if [ ! -e rootdir/var/lib/dpkg/status ]; then
  276. local STATUSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/status-/' -e 's/^skip-/status-/')
  277. if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
  278. cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
  279. else
  280. echo -n > rootdir/var/lib/dpkg/status
  281. fi
  282. fi
  283. rm -f rootdir/etc/apt/apt.conf.d/00foreigndpkg
  284. if command dpkg --assert-multi-arch >/dev/null 2>&1 ; then
  285. local ARCHS="$(getarchitectures)"
  286. if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
  287. DPKGARCH="$(dpkg --print-architecture)"
  288. for ARCH in ${ARCHS}; do
  289. if [ "${ARCH}" != "${DPKGARCH}" ]; then
  290. if ! dpkg --add-architecture ${ARCH} >/dev/null 2>&1; then
  291. # old-style used e.g. in Ubuntu-P – and as it seems travis
  292. echo "DPKG::options:: \"--foreign-architecture\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  293. echo "DPKG::options:: \"${ARCH}\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  294. fi
  295. fi
  296. done
  297. if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
  298. # dpkg doesn't really check the version as long as it is fully installed,
  299. # but just to be sure we choose one above the required version
  300. insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
  301. fi
  302. fi
  303. fi
  304. }
  305. configcompression() {
  306. while [ -n "$1" ]; do
  307. case "$1" in
  308. '.') printf ".\t.\tcat\n";;
  309. 'gz') printf "gzip\tgz\tgzip\n";;
  310. 'bz2') printf "bzip2\tbz2\tbzip2\n";;
  311. 'lzma') printf "lzma\tlzma\txz --format=lzma\n";;
  312. 'xz') printf "xz\txz\txz\n";;
  313. *) printf "$1\t$1\t$1\n";;
  314. esac
  315. shift
  316. done > ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf
  317. }
  318. forcecompressor() {
  319. COMPRESSOR="$1"
  320. COMPRESSOR_CMD="$1"
  321. case $COMPRESSOR in
  322. gzip) COMPRESS='gz';;
  323. bzip2) COMPRESS='bz2';;
  324. lzma) COMPRESS='lzma';;
  325. xz) COMPRESS='xz';;
  326. *) msgdie "Compressor $COMPRESSOR is unknown to framework, so can't be forced by forcecompressor!";;
  327. esac
  328. local CONFFILE="${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/00force-compressor"
  329. echo "Acquire::CompressionTypes::Order { \"${COMPRESS}\"; };
  330. Dir::Bin::uncompressed \"/does/not/exist\";
  331. Dir::Bin::gzip \"/does/not/exist\";
  332. Dir::Bin::bzip2 \"/does/not/exist\";
  333. Dir::Bin::lzma \"/does/not/exist\";
  334. Dir::Bin::xz \"/does/not/exist\";" > "$CONFFILE"
  335. if [ -e "/bin/${COMPRESSOR}" ]; then
  336. echo "Dir::Bin::${COMPRESSOR} \"/bin/${COMPRESSOR}\";" >> "$CONFFILE"
  337. elif [ -e "/usr/bin/${COMPRESSOR}" ]; then
  338. echo "Dir::Bin::${COMPRESSOR} \"/usr/bin/${COMPRESSOR}\";" >> "$CONFFILE"
  339. elif [ "${COMPRESSOR}" = 'lzma' ]; then
  340. echo 'Dir::Bin::xz "/usr/bin/xz";' >> "$CONFFILE"
  341. COMPRESSOR_CMD='xz --format=lzma'
  342. else
  343. msgtest 'Test for availability of compressor' "${COMPRESSOR}"
  344. msgfail
  345. fi
  346. }
  347. setupsimplenativepackage() {
  348. local NAME="$1"
  349. local ARCH="$2"
  350. local VERSION="$3"
  351. local RELEASE="${4:-unstable}"
  352. local DEPENDENCIES="$5"
  353. local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  354. If you find such a package installed on your system,
  355. something went horribly wrong! They are autogenerated
  356. und used only by testcases and surf no other propose…"}"
  357. local SECTION="${7:-others}"
  358. local DISTSECTION
  359. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  360. DISTSECTION="main"
  361. else
  362. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  363. fi
  364. local BUILDDIR=incoming/${NAME}-${VERSION}
  365. mkdir -p ${BUILDDIR}/debian/source
  366. cd ${BUILDDIR}
  367. echo "* most suckless software product ever" > FEATURES
  368. test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
  369. test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
  370. * Initial release
  371. -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
  372. test -e debian/control || echo "Source: $NAME
  373. Section: $SECTION
  374. Priority: optional
  375. Maintainer: Joe Sixpack <joe@example.org>
  376. Build-Depends: debhelper (>= 7)
  377. Standards-Version: 3.9.1
  378. Package: $NAME" > debian/control
  379. if [ "$ARCH" = 'all' ]; then
  380. echo "Architecture: all" >> debian/control
  381. else
  382. echo "Architecture: any" >> debian/control
  383. fi
  384. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
  385. echo "Description: $DESCRIPTION" >> debian/control
  386. test -e debian/compat || echo "7" > debian/compat
  387. test -e debian/source/format || echo "3.0 (native)" > debian/source/format
  388. test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
  389. cd - > /dev/null
  390. }
  391. buildsimplenativepackage() {
  392. local NAME="$1"
  393. local ARCH="$2"
  394. local VERSION="$3"
  395. local RELEASE="${4:-unstable}"
  396. local DEPENDENCIES="$5"
  397. local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  398. If you find such a package installed on your system,
  399. something went horribly wrong! They are autogenerated
  400. und used only by testcases and surf no other propose…"}"
  401. local SECTION="${7:-others}"
  402. local PRIORITY="${8:-optional}"
  403. local FILE_TREE="$9"
  404. local COMPRESS_TYPE="${10:-gzip}"
  405. local DISTSECTION
  406. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  407. DISTSECTION="main"
  408. else
  409. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  410. fi
  411. local BUILDDIR=${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}
  412. msgninfo "Build package ${NAME} in ${VERSION} for ${RELEASE} in ${DISTSECTION}… "
  413. mkdir -p $BUILDDIR/debian/source
  414. echo "* most suckless software product ever" > ${BUILDDIR}/FEATURES
  415. echo "#!/bin/sh
  416. echo '$NAME says \"Hello!\"'" > ${BUILDDIR}/${NAME}
  417. echo "Copyleft by Joe Sixpack $(date +%Y)" > ${BUILDDIR}/debian/copyright
  418. echo "$NAME ($VERSION) $RELEASE; urgency=low
  419. * Initial release
  420. -- Joe Sixpack <joe@example.org> $(date -R)" > ${BUILDDIR}/debian/changelog
  421. echo "Source: $NAME
  422. Section: $SECTION
  423. Priority: $PRIORITY
  424. Maintainer: Joe Sixpack <joe@example.org>
  425. Standards-Version: 3.9.3" > ${BUILDDIR}/debian/control
  426. local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
  427. test -z "$BUILDDEPS" || echo "$BUILDDEPS" >> ${BUILDDIR}/debian/control
  428. echo "
  429. Package: $NAME" >> ${BUILDDIR}/debian/control
  430. if [ "$ARCH" = 'all' ]; then
  431. echo "Architecture: all" >> ${BUILDDIR}/debian/control
  432. else
  433. echo "Architecture: any" >> ${BUILDDIR}/debian/control
  434. fi
  435. local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
  436. test -z "$DEPS" || echo "$DEPS" >> ${BUILDDIR}/debian/control
  437. echo "Description: $DESCRIPTION" >> ${BUILDDIR}/debian/control
  438. echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
  439. (cd ${BUILDDIR}/..; dpkg-source -b ${NAME}-${VERSION} 2>&1) | sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' \
  440. | while read SRC; do
  441. echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
  442. # if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
  443. # gpg --yes --secret-keyring ./keys/joesixpack.sec \
  444. # --keyring ./keys/joesixpack.pub --default-key 'Joe Sixpack' \
  445. # --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  446. # mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  447. # fi
  448. done
  449. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  450. rm -rf ${BUILDDIR}/debian/tmp
  451. mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
  452. cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
  453. cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
  454. if [ -n "$FILE_TREE" ]; then
  455. cp -ar "$FILE_TREE" ${BUILDDIR}/debian/tmp
  456. fi
  457. (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
  458. (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
  459. local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
  460. # ensure the right permissions as dpkg-deb ensists
  461. chmod 755 ${BUILDDIR}/debian/tmp/DEBIAN
  462. if ! dpkg-deb -Z${COMPRESS_TYPE} --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/.. >$LOG 2>&1; then
  463. cat $LOG
  464. false
  465. fi
  466. rm $LOG
  467. echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
  468. done
  469. mkdir -p ${BUILDDIR}/../${NAME}_${VERSION}
  470. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}/
  471. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}.changelog
  472. rm -rf "${BUILDDIR}"
  473. msgdone "info"
  474. }
  475. buildpackage() {
  476. local BUILDDIR=$1
  477. local RELEASE=$2
  478. local SECTION=$3
  479. local ARCH=$(getarchitecture $4)
  480. local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
  481. local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
  482. msgninfo "Build package ${PKGNAME} for ${RELEASE} in ${SECTION}… "
  483. cd $BUILDDIR
  484. if [ "$ARCH" = "all" ]; then
  485. ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
  486. fi
  487. if ! dpkg-buildpackage -uc -us -a$ARCH >$BUILDLOG 2>&1 ; then
  488. cat $BUILDLOG
  489. false
  490. fi
  491. local PKGS="$(grep '^dpkg-deb: building package' $BUILDLOG | cut -d'/' -f 2 | sed -e "s#'\.##")"
  492. local SRCS="$(grep '^dpkg-source: info: building' $BUILDLOG | grep -o '[a-z0-9._+~-]*$')"
  493. cd - > /dev/null
  494. for PKG in $PKGS; do
  495. echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
  496. done
  497. for SRC in $SRCS; do
  498. echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
  499. done
  500. msgdone "info"
  501. }
  502. buildaptarchive() {
  503. if [ -d incoming ]; then
  504. buildaptarchivefromincoming "$@"
  505. else
  506. buildaptarchivefromfiles "$@"
  507. fi
  508. }
  509. createaptftparchiveconfig() {
  510. local COMPRESSORS="$(cut -d' ' -f 1 ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | tr '\n' ' ')"
  511. COMPRESSORS="${COMPRESSORS%* }"
  512. 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' ' ')"
  513. if [ -z "$ARCHS" ]; then
  514. # the pool is empty, so we will operate on faked packages - let us use the configured archs
  515. ARCHS="$(getarchitectures)"
  516. fi
  517. echo -n 'Dir {
  518. ArchiveDir "' >> ftparchive.conf
  519. echo -n $(readlink -f .) >> ftparchive.conf
  520. echo -n '";
  521. CacheDir "' >> ftparchive.conf
  522. echo -n $(readlink -f ..) >> ftparchive.conf
  523. echo -n '";
  524. FileListDir "' >> ftparchive.conf
  525. echo -n $(readlink -f pool/) >> ftparchive.conf
  526. echo -n '";
  527. };
  528. Default {
  529. Packages::Compress "'"$COMPRESSORS"'";
  530. Sources::Compress "'"$COMPRESSORS"'";
  531. Contents::Compress "'"$COMPRESSORS"'";
  532. Translation::Compress "'"$COMPRESSORS"'";
  533. LongDescription "false";
  534. };
  535. TreeDefault {
  536. Directory "pool/";
  537. SrcDirectory "pool/";
  538. };
  539. APT {
  540. FTPArchive {
  541. Release {
  542. Origin "joesixpack";
  543. Label "apttestcases";
  544. Suite "unstable";
  545. Description "repository with dummy packages";
  546. Architectures "' >> ftparchive.conf
  547. echo -n "$ARCHS" >> ftparchive.conf
  548. echo 'source";
  549. };
  550. };
  551. };' >> ftparchive.conf
  552. for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
  553. echo -n 'tree "dists/' >> ftparchive.conf
  554. echo -n "$DIST" >> ftparchive.conf
  555. echo -n '" {
  556. Architectures "' >> ftparchive.conf
  557. echo -n "$ARCHS" >> ftparchive.conf
  558. echo -n 'source";
  559. FileList "' >> ftparchive.conf
  560. echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
  561. echo -n '";
  562. SourceFileList "' >> ftparchive.conf
  563. echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
  564. echo -n '";
  565. Sections "' >> ftparchive.conf
  566. 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
  567. echo '";
  568. };' >> ftparchive.conf
  569. done
  570. }
  571. buildaptftparchivedirectorystructure() {
  572. local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
  573. for DIST in $DISTS; do
  574. local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
  575. for SECTION in $SECTIONS; do
  576. local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
  577. for ARCH in $ARCHS; do
  578. mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
  579. done
  580. mkdir -p dists/${DIST}/${SECTION}/source
  581. mkdir -p dists/${DIST}/${SECTION}/i18n
  582. done
  583. done
  584. }
  585. insertpackage() {
  586. local RELEASE="$1"
  587. local NAME="$2"
  588. local ARCH="$3"
  589. local VERSION="$4"
  590. local DEPENDENCIES="$5"
  591. local PRIORITY="${6:-optional}"
  592. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  593. If you find such a package installed on your system,
  594. something went horribly wrong! They are autogenerated
  595. und used only by testcases and surf no other propose…"}"
  596. local ARCHS=""
  597. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  598. if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
  599. ARCHS="$(getarchitectures)"
  600. else
  601. ARCHS="$arch"
  602. fi
  603. for BUILDARCH in $ARCHS; do
  604. local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
  605. mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
  606. touch aptarchive/dists/${RELEASE}/main/source/Sources
  607. local FILE="${PPATH}/Packages"
  608. echo "Package: $NAME
  609. Priority: $PRIORITY
  610. Section: other
  611. Installed-Size: 42
  612. Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
  613. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  614. echo "Version: $VERSION
  615. Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
  616. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  617. echo "Description: $DESCRIPTION" >> $FILE
  618. echo >> $FILE
  619. done
  620. done
  621. }
  622. insertsource() {
  623. local RELEASE="$1"
  624. local NAME="$2"
  625. local ARCH="$3"
  626. local VERSION="$4"
  627. local DEPENDENCIES="$5"
  628. local ARCHS=""
  629. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  630. mkdir -p $SPATH
  631. local FILE="${SPATH}/Sources"
  632. echo "Package: $NAME
  633. Binary: $NAME
  634. Version: $VERSION
  635. Maintainer: Joe Sixpack <joe@example.org>
  636. Architecture: $ARCH" >> $FILE
  637. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  638. echo "Files:
  639. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
  640. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
  641. " >> $FILE
  642. }
  643. insertinstalledpackage() {
  644. local NAME="$1"
  645. local ARCH="$2"
  646. local VERSION="$3"
  647. local DEPENDENCIES="$4"
  648. local PRIORITY="${5:-optional}"
  649. local STATUS="${6:-install ok installed}"
  650. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
  651. If you find such a package installed on your system,
  652. something went horribly wrong! They are autogenerated
  653. und used only by testcases and surf no other propose…"}"
  654. local FILE='rootdir/var/lib/dpkg/status'
  655. local INFO='rootdir/var/lib/dpkg/info'
  656. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  657. echo "Package: $NAME
  658. Status: $STATUS
  659. Priority: $PRIORITY
  660. Section: other
  661. Installed-Size: 42
  662. Maintainer: Joe Sixpack <joe@example.org>
  663. Version: $VERSION" >> $FILE
  664. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  665. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  666. echo "Description: $DESCRIPTION" >> $FILE
  667. echo >> $FILE
  668. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  669. echo -n > ${INFO}/${NAME}:${arch}.list
  670. else
  671. echo -n > ${INFO}/${NAME}.list
  672. fi
  673. done
  674. }
  675. buildaptarchivefromincoming() {
  676. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  677. cd aptarchive
  678. [ -e pool ] || ln -s ../incoming pool
  679. [ -e ftparchive.conf ] || createaptftparchiveconfig
  680. [ -e dists ] || buildaptftparchivedirectorystructure
  681. msgninfo "\tGenerate Packages, Sources and Contents files… "
  682. aptftparchive -qq generate ftparchive.conf
  683. cd - > /dev/null
  684. msgdone "info"
  685. generatereleasefiles
  686. }
  687. buildaptarchivefromfiles() {
  688. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  689. find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
  690. msgninfo "\t${line} file… "
  691. compressfile "$line" "$1"
  692. msgdone "info"
  693. done
  694. generatereleasefiles "$@"
  695. }
  696. compressfile() {
  697. cat ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | while read compressor extension command; do
  698. if [ "$compressor" = '.' ]; then
  699. if [ -n "$2" ]; then
  700. touch -d "$2" "$1"
  701. fi
  702. continue
  703. fi
  704. cat "$1" | $command > "${1}.${extension}"
  705. if [ -n "$2" ]; then
  706. touch -d "$2" "${1}.${extension}"
  707. fi
  708. done
  709. }
  710. # can be overridden by testcases for their pleasure
  711. getcodenamefromsuite() {
  712. case "$1" in
  713. unstable) echo 'sid';;
  714. *) echo -n "$1";;
  715. esac
  716. }
  717. getreleaseversionfromsuite() { true; }
  718. getlabelfromsuite() { true; }
  719. generatereleasefiles() {
  720. # $1 is the Date header and $2 is the ValidUntil header to be set
  721. # both should be given in notation date/touch can understand
  722. msgninfo "\tGenerate Release files… "
  723. if [ -e aptarchive/dists ]; then
  724. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  725. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  726. local CODENAME="$(getcodenamefromsuite $SUITE)"
  727. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  728. local LABEL="$(getlabelfromsuite $SUITE)"
  729. if [ -n "$VERSION" ]; then
  730. VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
  731. fi
  732. if [ -n "$LABEL" ]; then
  733. LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
  734. fi
  735. aptftparchive -qq release $dir \
  736. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  737. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  738. ${LABEL} \
  739. ${VERSION} \
  740. | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  741. if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
  742. sed -i '/^Date: / a\
  743. NotAutomatic: yes' $dir/Release
  744. fi
  745. if [ -n "$1" -a "$1" != "now" ]; then
  746. sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
  747. fi
  748. if [ -n "$2" ]; then
  749. sed -i "/^Date: / a\
  750. Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
  751. fi
  752. done
  753. else
  754. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  755. fi
  756. if [ -n "$1" -a "$1" != "now" ]; then
  757. for release in $(find ./aptarchive -name 'Release'); do
  758. touch -d "$1" $release
  759. done
  760. fi
  761. msgdone "info"
  762. }
  763. setupdistsaptarchive() {
  764. local APTARCHIVE=$(readlink -f ./aptarchive)
  765. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  766. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  767. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  768. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  769. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  770. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  771. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  772. msgdone "info"
  773. done
  774. }
  775. setupflataptarchive() {
  776. local APTARCHIVE=$(readlink -f ./aptarchive)
  777. if [ -f ${APTARCHIVE}/Packages ]; then
  778. msgninfo "\tadd deb sources.list line… "
  779. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  780. msgdone "info"
  781. else
  782. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  783. fi
  784. if [ -f ${APTARCHIVE}/Sources ]; then
  785. msgninfo "\tadd deb-src sources.list line… "
  786. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  787. msgdone "info"
  788. else
  789. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  790. fi
  791. }
  792. setupaptarchive() {
  793. buildaptarchive
  794. if [ -e aptarchive/dists ]; then
  795. setupdistsaptarchive
  796. else
  797. setupflataptarchive
  798. fi
  799. signreleasefiles
  800. if [ "$1" != '--no-update' ]; then
  801. msgninfo "\tSync APT's cache with the archive… "
  802. aptget update -qq
  803. msgdone "info"
  804. fi
  805. }
  806. signreleasefiles() {
  807. local SIGNER="${1:-Joe Sixpack}"
  808. local GPG="gpg --batch --yes"
  809. msgninfo "\tSign archive with $SIGNER key… "
  810. local REXKEY='keys/rexexpired'
  811. local SECEXPIREBAK="${REXKEY}.sec.bak"
  812. local PUBEXPIREBAK="${REXKEY}.pub.bak"
  813. if [ "${SIGNER}" = 'Rex Expired' ]; then
  814. # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
  815. # option doesn't exist anymore (and using faketime would add a new obscure dependency)
  816. # therefore we 'temporary' make the key not expired and restore a backup after signing
  817. cp ${REXKEY}.sec $SECEXPIREBAK
  818. cp ${REXKEY}.pub $PUBEXPIREBAK
  819. local SECUNEXPIRED="${REXKEY}.sec.unexpired"
  820. local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
  821. if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
  822. cp $SECUNEXPIRED ${REXKEY}.sec
  823. cp $PUBUNEXPIRED ${REXKEY}.pub
  824. else
  825. printf "expire\n1w\nsave\n" | $GPG --keyring ${REXKEY}.pub --secret-keyring ${REXKEY}.sec --command-fd 0 --edit-key "${SIGNER}" >/dev/null 2>&1 || true
  826. cp ${REXKEY}.sec $SECUNEXPIRED
  827. cp ${REXKEY}.pub $PUBUNEXPIRED
  828. fi
  829. fi
  830. for KEY in $(find keys/ -name '*.sec'); do
  831. GPG="$GPG --secret-keyring $KEY"
  832. done
  833. for KEY in $(find keys/ -name '*.pub'); do
  834. GPG="$GPG --keyring $KEY"
  835. done
  836. for RELEASE in $(find aptarchive/ -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. echo >&2
  1098. cat >&2 $OUTPUT
  1099. msgfail
  1100. fi
  1101. }
  1102. testfailure() {
  1103. if [ "$1" = '--nomsg' ]; then
  1104. shift
  1105. else
  1106. msgtest 'Test for failure in execution of' "$*"
  1107. fi
  1108. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
  1109. if $@ >${OUTPUT} 2>&1; then
  1110. echo >&2
  1111. cat >&2 $OUTPUT
  1112. msgfail
  1113. else
  1114. msgpass
  1115. fi
  1116. }
  1117. testwebserverlaststatuscode() {
  1118. local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
  1119. local STATUS='rootdir/tmp/webserverstatus-statusfile.log'
  1120. rm -f "$DOWNLOG" "$STATUS"
  1121. msgtest 'Test last status code from the webserver was' "$1"
  1122. downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG"
  1123. if [ "$(cat "$STATUS")" = "$1" ]; then
  1124. msgpass
  1125. else
  1126. echo >&2
  1127. if [ -n "$2" ]; then
  1128. shift
  1129. echo >&2 '#### Additionally provided output files contain:'
  1130. cat >&2 "$@"
  1131. fi
  1132. echo >&2 '#### Download log of the status code:'
  1133. cat >&2 "$DOWNLOG"
  1134. msgfail "Status was $(cat "$STATUS")"
  1135. fi
  1136. }
  1137. pause() {
  1138. echo "STOPPED execution. Press enter to continue"
  1139. local IGNORE
  1140. read IGNORE
  1141. }