framework 37 KB

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