framework 37 KB

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