Try   HackMD
tags: Autopkgtest LXC Ubuntu QEMU

Autopkgtest 初體驗

在 Ubuntu 安裝 lxc 跟 autopkgtest

$ sudo apt install lxc-templates lxc lxc-utils autopkgtest

建立 Ubuntu 18.04 的 LXC container

$ MIRROR=http://tw.archive.ubuntu.com/ubuntu/ \
  sudo autopkgtest-build-lxc ubuntu bionic amd64 auto.sh \
  # 可以使用自己準備的 auto.sh 來設定環境

可以先試試看 file 這個軟體套件

$ apt source file
$ sudo autopkgtest file_5.38-4.dsc -- lxc autopkgtest-bionic-amd64
$ sudo autopkgtest file_5.38-4.dsc --summary-file=summary.log \
	--log-file=complete.log -- lxc autopkgtest-bionic-amd64 # 另存紀錄

將 LXC container 清掉

$ sudo lxc-destroy -n autopkgtest-bionic-amd64

改用 lxd

$ sudo apt install lxd
$ autopkgtest-build-lxd images:ubuntu/bionic/amd64
$ sudo autopkgtest file_5.38-4.dsc -- lxd autopkgtest/ubuntu/bionic/amd64

改用 qemu

$ sudo apt install vmdb2 --no-install-recommends
$ autopkgtest-build-qemu
usage: /usr/bin/autopkgtest-build-qemu [<options...>] <release> <image> [<mirror>] [<architecture>] [<script>] [<size>]

--apt-proxy=http://PROXY:PORT     Set apt proxy [default: auto]
--arch=ARCH, --architecture=ARCH  Set architecture, e.g. i386
                                  [default: amd64]
--mirror=URL                      Set apt mirror [default:
                                  http://deb.debian.org/debian]
--script=SCRIPT                   Run an extra customization script
--size=SIZE                       Set image size [default: 25G]
$ sudo vi /usr/lib/python3/dist-packages/vmdb/plugins/grub_plugin.py +174
#                '--force-extra-removable',
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=951766
$ sudo vi /usr/lib/python3/dist-packages/vmdb/plugins/debootstrap_plugin.py +44
        vmdb.runcmd(['debootstrap', '--components=main,universe', '--variant', variant, suite, target, mirror])
# https://paste.ubuntu.com/p/psV77MzHbq/
$ sudo autopkgtest-build-qemu bionic ~/ubuntu-bionic.img \
		--arch=amd64 \
		--mirror=http://tw.archive.ubuntu.com/ubuntu \
		--size=8G \
		--script=$(readlink -f auto.sh)
$ sudo autopkgtest file_5.38-4.dsc -- qemu ~/ubuntu-bionic.img

注意事項

  • 要測試的軟體套件的 source list 要先在 vm 內準備好
  • auto.sh 要設成可執行才能使用在 autopkgtest-build-qemu
  • vm 內的 apt 不會自動更新
    • 可以使用 apt-upgrade | -U 來更新,例如 sudo autopkgtest -U file_5.38-4.dsc
  • 任何 stderr 的輸出都會被當成測試失敗
    • 除非在 Restrictions: restriction-name [, another-restriction-name ] 加上 allow-stderr
  • autopkgtest-build-lxc distribution release [architecture] [script]
    • The script must be a POSIX shell script, and should not depend on bash-specific features.
  • 有些測試只能在 qemu 裡面跑,就可以使用 Restrictions: isolation-machine
  • 有些測試在 chroot/schroot 裡面跑會失敗,就可以使用 Restrictions: isolation-container
  • 有時候測試的 lxc container 會遺留下來造成問題,測試前可先觀察一下 sudo lxc-ls -f 輸出是否正常
  • lxc 所使用的 mirror site 會在第一次建立 container 時固定下來,除非使用 sudo lxc-create -t ubuntu -n temporary -- --mirror http://mirror01.idc.hinet.net/ubuntu --flush-cache 當中的 flush-cache 來將 mirror site 改動一下
$ sudo lxc-ls -f
NAME                     STATE   AUTOSTART GROUPS IPV4       IPV6 UNPRIVILEGED 
autopkgtest-bionic-amd64 STOPPED 0         -      -          -    false        
autopkgtest-focal-amd64  STOPPED 0         -      -          -    false        
autopkgtest-lxc-aazmgf   RUNNING 0         -      10.0.3.102 -    false   

我實際遇過 autopkgtest-lxc-aazmgf 引起問題

小技巧

  1. 可以在 debian/tests/control 加上 Test-Command: false
    這樣就可以用 sudo autopkgtest -U test-name=false shell-fail lxc autopkgtest-bionic-amd64 進入測試環境

參考資料

https://people.debian.org/~mpitt/autopkgtest/README.running-tests.html
https://people.debian.org/~mpitt/autopkgtest/README.package-tests.html
https://autopkgtest.ubuntu.com/
https://ci.debian.net/
https://wiki.debian.org/ContinuousIntegration/autopkgtest
https://packaging.ubuntu.com/html/auto-pkg-test.html
https://ci.debian.net/doc/file.TUTORIAL.html
https://kali.org/docs/development/contributing-runtime-tests/