framework 39 KB

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