framework 38 KB

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