framework 37 KB

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