framework 39 KB

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