framework 38 KB

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