framework 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. local DOWNLOG='rootdir/tmp/download-testfile.log'
  810. local STATUS='rootdir/tmp/webserverconfig.status'
  811. rm -f "$STATUS" "$DOWNLOG"
  812. if downloadfile "http://localhost:8080/_config/set/${1}/${2}" "$STATUS" > "$DOWNLOG"; then
  813. msgpass
  814. else
  815. cat "$DOWNLOG" "$STATUS"
  816. msgfail
  817. fi
  818. testwebserverlaststatuscode '200'
  819. }
  820. rewritesourceslist() {
  821. local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive")"
  822. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  823. sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#http://localhost:8080/#${1}#" -e "s#http://localhost:4433/#${1}#"
  824. done
  825. }
  826. changetowebserver() {
  827. if [ "$1" != '--no-rewrite' ]; then
  828. rewritesourceslist 'http://localhost:8080/'
  829. else
  830. shift
  831. fi
  832. if test -x ${APTWEBSERVERBINDIR}/aptwebserver; then
  833. cd aptarchive
  834. local LOG="webserver.log"
  835. if ! aptwebserver -o aptwebserver::fork=1 "$@" >$LOG 2>&1 ; then
  836. cat $LOG
  837. false
  838. fi
  839. local PID="$(cat aptwebserver.pid)"
  840. if [ -z "$PID" ]; then
  841. msgdie 'Could not fork aptwebserver successfully'
  842. fi
  843. addtrap "kill $PID;"
  844. cd - > /dev/null
  845. else
  846. msgdie 'You have to build aptwerbserver or install a webserver'
  847. fi
  848. }
  849. changetohttpswebserver() {
  850. if ! which stunnel4 >/dev/null; then
  851. msgdie 'You need to install stunnel4 for https testcases'
  852. fi
  853. if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
  854. changetowebserver --no-rewrite "$@"
  855. fi
  856. echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
  857. cert = ${TESTDIRECTORY}/apt.pem
  858. output = /dev/null
  859. [https]
  860. accept = 4433
  861. connect = 8080
  862. " > ${TMPWORKINGDIRECTORY}/stunnel.conf
  863. stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
  864. local PID="$(cat ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid)"
  865. addtrap 'prefix' "kill ${PID};"
  866. rewritesourceslist 'https://localhost:4433/'
  867. }
  868. changetocdrom() {
  869. mkdir -p rootdir/media/cdrom/.disk
  870. local CD="$(readlink -f rootdir/media/cdrom)"
  871. echo "acquire::cdrom::mount \"${CD}\";
  872. acquire::cdrom::${CD}/::mount \"mv ${CD}-unmounted ${CD}\";
  873. acquire::cdrom::${CD}/::umount \"mv ${CD} ${CD}-unmounted\";
  874. 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. # start with an unmounted disk
  884. mv "${CD}" "${CD}-unmounted"
  885. # we don't want the disk to be modifiable
  886. addtrap 'prefix' "chmod -f -R +w $PWD/rootdir/media/cdrom/dists/ $PWD/rootdir/media/cdrom-unmounted/dists/ || true;"
  887. chmod -R -w rootdir/media/cdrom-unmounted/dists
  888. }
  889. downloadfile() {
  890. local PROTO="$(echo "$1" | cut -d':' -f 1 )"
  891. apthelper -o Acquire::https::CaInfo=${TESTDIR}/apt.pem \
  892. -o Debug::Acquire::${PROTO}=1 \
  893. download-file "$1" "$2" 2>&1 || true
  894. # only if the file exists the download was successful
  895. if [ -e "$2" ]; then
  896. return 0
  897. else
  898. return 1
  899. fi
  900. }
  901. checkdiff() {
  902. local DIFFTEXT="$(command diff -u "$@" | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  903. if [ -n "$DIFFTEXT" ]; then
  904. echo >&2
  905. echo >&2 "$DIFFTEXT"
  906. return 1
  907. else
  908. return 0
  909. fi
  910. }
  911. testfileequal() {
  912. local FILE="$1"
  913. shift
  914. msgtest "Test for correctness of file" "$FILE"
  915. if [ -z "$*" ]; then
  916. echo -n "" | checkdiff $FILE - && msgpass || msgfail
  917. else
  918. echo "$*" | checkdiff $FILE - && msgpass || msgfail
  919. fi
  920. }
  921. testempty() {
  922. msgtest "Test for no output of" "$*"
  923. test -z "$($* 2>&1)" && msgpass || msgfail
  924. }
  925. testequal() {
  926. local MSG='Test of equality of'
  927. if [ "$1" = '--nomsg' ]; then
  928. MSG=''
  929. shift
  930. fi
  931. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
  932. echo "$1" > $COMPAREFILE
  933. shift
  934. if [ -n "$MSG" ]; then
  935. msgtest "$MSG" "$*"
  936. fi
  937. $* 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  938. }
  939. testequalor2() {
  940. local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
  941. local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
  942. local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
  943. echo "$1" > $COMPAREFILE1
  944. echo "$2" > $COMPAREFILE2
  945. shift 2
  946. msgtest "Test for equality OR of" "$*"
  947. $* >$COMPAREAGAINST 2>&1 || true
  948. if checkdiff $COMPAREFILE1 $COMPAREAGAINST >/dev/null 2>&1 || \
  949. checkdiff $COMPAREFILE2 $COMPAREAGAINST >/dev/null 2>&1
  950. then
  951. msgpass
  952. else
  953. echo -n "\n${CINFO}Diff against OR 1${CNORMAL}"
  954. checkdiff $COMPAREFILE1 $COMPAREAGAINST || true
  955. echo -n "${CINFO}Diff against OR 2${CNORMAL}"
  956. checkdiff $COMPAREFILE2 $COMPAREAGAINST || true
  957. msgfail
  958. fi
  959. }
  960. testshowvirtual() {
  961. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  962. local PACKAGE="$1"
  963. shift
  964. while [ -n "$1" ]; do
  965. VIRTUAL="${VIRTUAL}
  966. N: Can't select versions from package '$1' as it is purely virtual"
  967. PACKAGE="${PACKAGE} $1"
  968. shift
  969. done
  970. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  971. VIRTUAL="${VIRTUAL}
  972. N: No packages found"
  973. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
  974. local ARCH="$(getarchitecture 'native')"
  975. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  976. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  977. }
  978. testnopackage() {
  979. msgtest "Test for non-existent packages" "apt-cache show $*"
  980. local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
  981. if [ -n "$SHOWPKG" ]; then
  982. echo >&2
  983. echo >&2 "$SHOWPKG"
  984. msgfail
  985. else
  986. msgpass
  987. fi
  988. }
  989. testdpkginstalled() {
  990. msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
  991. local PKGS="$(dpkg -l "$@" 2>/dev/null | grep '^i' | wc -l)"
  992. if [ "$PKGS" != $# ]; then
  993. echo >&2 $PKGS
  994. dpkg -l "$@" | grep '^[a-z]' >&2
  995. msgfail
  996. else
  997. msgpass
  998. fi
  999. }
  1000. testdpkgnotinstalled() {
  1001. msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
  1002. local PKGS="$(dpkg -l "$@" 2> /dev/null | grep '^i' | wc -l)"
  1003. if [ "$PKGS" != 0 ]; then
  1004. echo
  1005. dpkg -l "$@" | grep '^[a-z]' >&2
  1006. msgfail
  1007. else
  1008. msgpass
  1009. fi
  1010. }
  1011. testmarkedauto() {
  1012. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
  1013. if [ -n "$1" ]; then
  1014. msgtest 'Test for correctly marked as auto-installed' "$*"
  1015. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  1016. else
  1017. msgtest 'Test for correctly marked as auto-installed' 'no package'
  1018. echo -n > $COMPAREFILE
  1019. fi
  1020. aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1021. }
  1022. testsuccess() {
  1023. if [ "$1" = '--nomsg' ]; then
  1024. shift
  1025. else
  1026. msgtest 'Test for successful execution of' "$*"
  1027. fi
  1028. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
  1029. if $@ >${OUTPUT} 2>&1; then
  1030. msgpass
  1031. else
  1032. echo >&2
  1033. cat >&2 $OUTPUT
  1034. msgfail
  1035. fi
  1036. }
  1037. testfailure() {
  1038. if [ "$1" = '--nomsg' ]; then
  1039. shift
  1040. else
  1041. msgtest 'Test for failure in execution of' "$*"
  1042. fi
  1043. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
  1044. if $@ >${OUTPUT} 2>&1; then
  1045. echo >&2
  1046. cat >&2 $OUTPUT
  1047. msgfail
  1048. else
  1049. msgpass
  1050. fi
  1051. }
  1052. testwebserverlaststatuscode() {
  1053. local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
  1054. local STATUS='rootdir/tmp/webserverstatus-statusfile.log'
  1055. rm -f "$DOWNLOG" "$STATUS"
  1056. msgtest 'Test last status code from the webserver was' "$1"
  1057. downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG"
  1058. if [ "$(cat "$STATUS")" = "$1" ]; then
  1059. msgpass
  1060. else
  1061. echo >&2
  1062. if [ -n "$2" ]; then
  1063. shift
  1064. echo >&2 '#### Additionally provided output files contain:'
  1065. cat >&2 "$@"
  1066. fi
  1067. echo >&2 '#### Download log of the status code:'
  1068. cat >&2 "$DOWNLOG"
  1069. msgfail "Status was $(cat "$STATUS")"
  1070. fi
  1071. }
  1072. pause() {
  1073. echo "STOPPED execution. Press enter to continue"
  1074. local IGNORE
  1075. read IGNORE
  1076. }