framework 44 KB

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