framework 38 KB

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