framework 37 KB

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