framework 37 KB

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