framework 37 KB

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