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