You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1319 lines
43 KiB

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