framework 35 KB

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