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