framework 37 KB

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