diff options
| author | Shulhan <ms@kilabit.info> | 2023-12-02 15:29:15 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-12-04 01:27:56 +0700 |
| commit | c80efd1a2f6574c4253ab435f682599146ac9f55 (patch) | |
| tree | 390513bac54ce3a832b26285786d116c333f6ef4 | |
| parent | 775af78cc6c8ebe9e258b81dd9442bbe6b8b36d4 (diff) | |
| download | awwan-c80efd1a2f6574c4253ab435f682599146ac9f55.tar.xz | |
all: refactoring container for integration tests
Instead of building the container multiple times, create it one time only
that importable to the system /var/lib/machines.
So future development can enable or start them manually and run the
test-integration or test-all tasks without rebuilding them again.
This changes require moving the image output to /data/awwan because if
the test image is located under the sub directory of the source
directory and we bind the source directory into container it will cause
recursive mount that makes its impossible to use the image inside
/var/lib/machines.
We also set the network zone to "awwan", to allow multiple containers
shared the same subnet on the host later.
| -rw-r--r-- | Makefile | 51 | ||||
| -rw-r--r-- | _mkosi/mkosi.conf | 3 | ||||
| -rwxr-xr-x | _mkosi/mkosi.finalize | 10 | ||||
| -rwxr-xr-x | _mkosi/mkosi.finalize.chroot | 7 | ||||
| -rw-r--r-- | _mkosi/mkosi.nspawn | 11 | ||||
| -rwxr-xr-x | _mkosi/mkosi.prepare | 26 | ||||
| -rwxr-xr-x | _mkosi/mkosi.prepare.chroot | 18 | ||||
| -rw-r--r-- | _mkosi/mkosi.skeleton/etc/pacman.conf | 101 |
8 files changed, 159 insertions, 68 deletions
@@ -45,46 +45,41 @@ lint-www: -cd _wui && eslint --fix . #}}} -#{{{ Testing with container using mkosi. +#{{{ Unit and integration tests using container. Linux only. -.PHONY: setup-mkosi -setup-mkosi: - @echo ">>> Creating symlinks to simplify binding ..." - ln -sf $(shell go env GOCACHE) _mkosi/mkosi.cache/gocache - ln -sf $(shell go env GOMODCACHE) _mkosi/mkosi.cache/gomodcache - @echo ">>> Booting awwan-test container ..." - sudo mkosi --directory=_mkosi/ boot +.PHONY: build-awwan-test +build-awwan-test: + @echo ">>> Stopping container ..." + -sudo machinectl stop awwan-test -.PHONY: test-with-mkosi -test-with-mkosi: - go test -tags=integration -c . - machinectl shell awwan@awwan-test \ - /bin/sh -c "cd src; ./awwan.test -test.v" + @echo ">>> Creating binding ..." + mkdir -p /data/awwan/ + ln -sTf $$(pwd) /data/awwan/src + ln -sTf $(shell go env GOCACHE) /data/awwan/gocache + ln -sTf $(shell go env GOMODCACHE) /data/awwan/gomodcache -.PHONY: test-all-mkosi -test-all-mkosi: - rm -f _coverage/* - go test -cover ./... -test.gocoverdir=_coverage - machinectl shell awwan@awwan-test \ - /bin/sh -c "cd src; \ - go test -cover -tags=integration ./... -test.gocoverdir=_coverage" - go tool covdata textfmt -i=_coverage -o cover.txt - go tool cover -html=cover.txt -o cover.html - go tool covdata percent -i=_coverage + @echo ">>> Building container awwan-test ..." + sudo mkosi --directory=_mkosi/ --force build + + sudo machinectl --force import-tar /data/awwan/awwan-test.tar + sudo machinectl start awwan-test -## The following tasks must be executed inside the container. + ## Once the container is imported, we can enable and run them any + ## time without rebuilding again. .PHONY: test-integration test-integration: - go test -cover -tags=integration ./... -test.gocoverdir=_coverage - go tool covdata textfmt -i=_coverage -o cover.txt - go tool cover -html=cover.txt -o cover.html + go test -tags=integration -c . + machinectl shell awwan@awwan-test \ + /bin/sh -c "cd src; ./awwan.test -test.v" .PHONY: test-all test-all: rm -f _coverage/* go test -cover ./... -test.gocoverdir=_coverage - go test -cover -tags=integration ./... -test.gocoverdir=_coverage + machinectl shell awwan@awwan-test \ + /bin/sh -c "cd src; \ + go test -cover -tags=integration ./... -test.gocoverdir=_coverage" go tool covdata textfmt -i=_coverage -o cover.txt go tool cover -html=cover.txt -o cover.html go tool covdata percent -i=_coverage diff --git a/_mkosi/mkosi.conf b/_mkosi/mkosi.conf index a543a04..2f535e8 100644 --- a/_mkosi/mkosi.conf +++ b/_mkosi/mkosi.conf @@ -1,6 +1,7 @@ [Output] -Format=directory +Format=tar Output=awwan-test +OutputDirectory=/data/awwan/ [Content] Bootable=no diff --git a/_mkosi/mkosi.finalize b/_mkosi/mkosi.finalize deleted file mode 100755 index 5cca699..0000000 --- a/_mkosi/mkosi.finalize +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - -if [ "$container" != "mkosi" ]; then - exec mkosi-chroot "$CHROOT_SCRIPT" "$@" -fi - -systemctl enable sshd.service - -chown -R awwan:awwan /home/awwan/.ssh/ -chown -R awwanssh:awwanssh /home/awwanssh/.ssh/ diff --git a/_mkosi/mkosi.finalize.chroot b/_mkosi/mkosi.finalize.chroot new file mode 100755 index 0000000..84db413 --- /dev/null +++ b/_mkosi/mkosi.finalize.chroot @@ -0,0 +1,7 @@ +#!/bin/sh + +systemctl enable sshd.service + +chmod 0600 /etc/ssh/*_key +chown -R awwan:awwan /home/awwan/ +chown -R awwanssh:awwanssh /home/awwanssh/ diff --git a/_mkosi/mkosi.nspawn b/_mkosi/mkosi.nspawn index c22d6a3..f701c4a 100644 --- a/_mkosi/mkosi.nspawn +++ b/_mkosi/mkosi.nspawn @@ -1,4 +1,9 @@ [Files] -Bind=../:/home/awwan/src -Bind=mkosi.cache/gocache:/home/awwan/.cache/go-build -Bind=mkosi.cache/gomodcache:/home/awwan/go/pkg/mod +Bind=/data/awwan/src:/home/awwan/src:idmap +Bind=/data/awwan/gocache:/home/awwan/.cache/go-build:idmap +Bind=/data/awwan/gomodcache:/home/awwan/go/pkg/mod:idmap + +[Network] +Private = yes +VirtualEthernet = yes +Zone = awwan diff --git a/_mkosi/mkosi.prepare b/_mkosi/mkosi.prepare deleted file mode 100755 index 5582886..0000000 --- a/_mkosi/mkosi.prepare +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/sh - -echo "--- mkosi.prepare: args=$@" -echo "--- mkosi.prepare: container=$container" - -if [ "$container" != "mkosi" ]; then - exec mkosi-chroot "$CHROOT_SCRIPT" "$@" -fi - -if [ "$1" == "final" ]; then - set -x - ## User testing sudo with password prompt. - ## The UID of user in container must equal with UID in host, for - ## better compatibility. - ## The password is "awwan". - useradd --create-home --user-group \ - --uid $MKOSI_UID \ - --password '$2a$10$XVhjfOB4Un5DJE4TQEBPrOHfBVGVWP4iA3ElUMzcbJ7jdc2zZPgZ2' \ - awwan - - ## User testing with ssh. - useradd --create-home --user-group --groups wheel \ - --uid $((MKOSI_UID+1)) \ - --password '$2a$10$XVhjfOB4Un5DJE4TQEBPrOHfBVGVWP4iA3ElUMzcbJ7jdc2zZPgZ2' \ - awwanssh -fi diff --git a/_mkosi/mkosi.prepare.chroot b/_mkosi/mkosi.prepare.chroot new file mode 100755 index 0000000..0c401fb --- /dev/null +++ b/_mkosi/mkosi.prepare.chroot @@ -0,0 +1,18 @@ +#!/bin/sh + +set -x + +## User testing sudo with password prompt. +## The UID of user in container must equal with UID in host, for +## better compatibility. +## The password is "awwan". +useradd --create-home --user-group \ + --uid $MKOSI_UID \ + --password '$2a$10$XVhjfOB4Un5DJE4TQEBPrOHfBVGVWP4iA3ElUMzcbJ7jdc2zZPgZ2' \ + awwan + +## User testing with ssh. +useradd --create-home --user-group --groups wheel \ + --uid $((MKOSI_UID+1)) \ + --password '$2a$10$XVhjfOB4Un5DJE4TQEBPrOHfBVGVWP4iA3ElUMzcbJ7jdc2zZPgZ2' \ + awwanssh diff --git a/_mkosi/mkosi.skeleton/etc/pacman.conf b/_mkosi/mkosi.skeleton/etc/pacman.conf new file mode 100644 index 0000000..e288913 --- /dev/null +++ b/_mkosi/mkosi.skeleton/etc/pacman.conf @@ -0,0 +1,101 @@ +# +# /etc/pacman.conf +# +# See the pacman.conf(5) manpage for option and repository directives + +# +# GENERAL OPTIONS +# +[options] +# The following paths are commented out with their default values listed. +# If you wish to use different paths, uncomment and update the paths. +#RootDir = / +#DBPath = /var/lib/pacman/ +CacheDir = /home/var/cache/pacman/pkg/ +#LogFile = /var/log/pacman.log +#GPGDir = /etc/pacman.d/gnupg/ +#HookDir = /etc/pacman.d/hooks/ +HoldPkg = pacman glibc +#XferCommand = /usr/bin/curl -L -C - -f -o %o %u +#XferCommand = /usr/bin/wget --passive-ftp -c -O %o %u +CleanMethod = KeepInstalled +Architecture = auto + +# Pacman won't upgrade packages listed in IgnorePkg and members of IgnoreGroup +IgnorePkg = go +#IgnoreGroup = + +#NoUpgrade = +#NoExtract = + +# Misc options +#UseSyslog +#Color +#NoProgressBar +CheckSpace +VerbosePkgLists +#ParallelDownloads = 5 +ILoveCandy + +# By default, pacman accepts packages signed by keys that its local keyring +# trusts (see pacman-key and its man page), as well as unsigned packages. +SigLevel = Required DatabaseOptional +LocalFileSigLevel = Optional +#RemoteFileSigLevel = Required + +# NOTE: You must run `pacman-key --init` before first using pacman; the local +# keyring can then be populated with the keys of all official Arch Linux +# packagers with `pacman-key --populate archlinux`. + +# +# REPOSITORIES +# - can be defined here or included from another file +# - pacman will search repositories in the order defined here +# - local/custom mirrors can be added here or in separate files +# - repositories listed first will take precedence when packages +# have identical names, regardless of version number +# - URLs will have $repo replaced by the name of the current repo +# - URLs will have $arch replaced by the name of the architecture +# +# Repository entries are of the format: +# [repo-name] +# Server = ServerName +# Include = IncludePath +# +# The header [repo-name] is crucial - it must be present and +# uncommented to enable the repo. +# + +# The testing repositories are disabled by default. To enable, uncomment the +# repo name header and Include lines. You can add preferred servers immediately +# after the header, and they will be used before the default mirrors. + +#[core-testing] +#Include = /etc/pacman.d/mirrorlist + +[core] +Server = https://mirror.0x.sg/archlinux/$repo/os/$arch + +#[extra-testing] +#Include = /etc/pacman.d/mirrorlist + +[extra] +Server = https://mirror.0x.sg/archlinux/$repo/os/$arch + +# If you want to run 32 bit applications on your x86_64 system, +# enable the multilib repositories as required here. + +#[multilib-testing] +#Include = /etc/pacman.d/mirrorlist + +#[multilib] +#Include = /etc/pacman.d/mirrorlist + +# An example of a custom package repository. See the pacman manpage for +# tips on creating your own repositories. +#[custom] +#SigLevel = Optional TrustAll +#Server = file:///home/custompkgs + +[build.kilabit.info] +Server = https://build.kilabit.info/aur |
