framework 39 KB

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