framework 38 KB

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