framework 37 KB

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