framework 36 KB

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