framework 36 KB

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