framework 38 KB

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