Test Suite - KVM: Difference between revisions

From Libreswan
Jump to navigation Jump to search
(replace make ... with ./kvm ...; overhaul; add kvm workflow section)
m (tweak formatting)
Line 4: Line 4:


To access files on the host file system:
To access files on the host file system:
* linux guests use the PLAN9 filesystem (9p)
* linux guests use the PLAN9 filesystem (9p)
* BSD guests (well openbsd) use NFS via the NAT interface
* BSD guests (well openbsd) use NFS via the NAT interface
Line 15: Line 16:
Some of the test scrips need to be run as root.  The test environment assumes this can be done using <tt>sudo</tt> without a password vis:
Some of the test scrips need to be run as root.  The test environment assumes this can be done using <tt>sudo</tt> without a password vis:


<pre>
sudo pwd
sudo pwd
</pre>


XXX: Surely qemu can be driven without root?
XXX: Surely qemu can be driven without root?
Line 25: Line 24:
To set this up, add your account to the wheel group:
To set this up, add your account to the wheel group:


<pre>
sudo usermod -a -G wheel $(id -u -n)
sudo usermod -a -G wheel $(id -u -n)
</pre>


and permit wheel to have no-password access:
and permit wheel to have no-password access:


<pre>
echo '%wheel ALL=(ALL) NOPASSWD: ALL' | sudo dd of=/etc/sudoers.d/wheel
echo '%wheel ALL=(ALL) NOPASSWD: ALL' | sudo dd of=/etc/sudoers.d/wheel
sudo chmod ug=r,o= /etc/sudoers.d/wheel
sudo chmod ug=r,o= /etc/sudoers.d/wheel
sudo chown root.root /etc/sudoers.d/wheel
sudo chown root.root /etc/sudoers.d/wheel
</pre>


=== Fight SELinux ===
=== Fight SELinux ===
Line 43: Line 38:
* set SELinux to permissive (recommended)
* set SELinux to permissive (recommended)


<pre>
sudo sed --in-place=.ORIG -e 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
sudo sed --in-place=.ORIG -e 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
sudo setenforce Permissive
sudo setenforce Permissive
</pre>


* disable SELinux
* disable SELinux


<pre>
sudo sed --in-place=.ORIG -e 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
sudo sed --in-place=.ORIG -e 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
sudo reboot
sudo reboot
</pre>


* (experimental) label source tree for SELinux
* (experimental) label source tree for SELinux
Line 59: Line 50:
The source tree on the host is shared with the virtual machines.  SELinux considers this a bug unless the tree is labelled with type svirt_image_t.
The source tree on the host is shared with the virtual machines.  SELinux considers this a bug unless the tree is labelled with type svirt_image_t.


<pre>
sudo dnf install policycoreutils-python-utils
sudo dnf install policycoreutils-python-utils
sudo semanage fcontext -a -t svirt_image_t "$(pwd)"'(/.*)?'
sudo semanage fcontext -a -t svirt_image_t "$(pwd)"'(/.*)?'
sudo restorecon -vR /home/build/libreswan
sudo restorecon -vR /home/build/libreswan
</pre>


There may be other things that SELinux objects to.
There may be other things that SELinux objects to.
Line 75: Line 64:
To get qemu working (while virt-manager isn't strictly required it's useful on a desktop):
To get qemu working (while virt-manager isn't strictly required it's useful on a desktop):


<pre>
sudo dnf install -y make git
sudo dnf install -y make git
sudo dnf install -y qemu virt-manager virt-install libvirt-daemon-kvm libvirt-daemon-qemu
sudo dnf install -y qemu virt-manager virt-install libvirt-daemon-kvm libvirt-daemon-qemu
sudo dnf install -y python3-pexpect
sudo dnf install -y python3-pexpect
sudo dnf install -y dvd+rw-tools          # for building OpenBSDs boot disk
sudo dnf install -y dvd+rw-tools          # for building OpenBSDs boot disk
sudo dnf install -y jq nodejs-typescript  # for generating web pages
sudo dnf install -y jq nodejs-typescript  # for generating web pages
</pre>


Once all is installed start libvirtd and then check it is running:
Once all is installed start libvirtd and then check it is running:


<pre>
sudo systemctl enable libvirtd
sudo systemctl enable libvirtd
sudo systemctl start libvirtd
sudo systemctl start libvirtd
sudo systemctl status libvirtd
sudo systemctl status libvirtd
</pre>


There should be no errors and warnings.
There should be no errors and warnings.
Line 95: Line 80:
On testing and F29, this failed with the error:
On testing and F29, this failed with the error:


<pre>
error : virQEMUCapsNewForBinaryInternal:4664 : internal error: Failed to probe QEMU binary with QMP: /usr/bin/qemu-system-xtensa: error while loading shared libraries: libbrlapi.so.0.6: cannot open shared object file: No such file or directory
error : virQEMUCapsNewForBinaryInternal:4664 : internal error: Failed to probe QEMU binary with QMP: /usr/bin/qemu-system-xtensa: error while loading shared libraries: libbrlapi.so.0.6: cannot open shared object file: No such file or directory
</pre>


and it was found that 'brlapi' needed to be manually installed.
and it was found that 'brlapi' needed to be manually installed.
Line 113: Line 96:
You need to add yourself to the qemu group.  For instance:
You need to add yourself to the qemu group.  For instance:


<pre>
sudo usermod -a -G qemu $(id -u -n)
sudo usermod -a -G qemu $(id -u -n)
</pre>


You will need to re-login for this to take effect.  A cheat is to re-login the current shell:
You will need to re-login for this to take effect.  A cheat is to re-login the current shell:


<pre>
sudo su - $(id -u -n)
sudo su - $(id -u -n)
</pre>
 


The path to your build needs to be accessible (executable) by root, assuming things are under home:
The path to your build needs to be accessible (executable) by root, assuming things are under home:


<pre>
chmod a+x $HOME
chmod a+x $HOME
</pre>


=== Fix /var/lib/libvirt/qemu ===
=== Fix /var/lib/libvirt/qemu ===
Line 134: Line 110:
{{ ambox | nocat=true | type=important | text = Because our VMs don't run as qemu, /var/lib/libvirt/qemu needs to be changed using chmod g+w to make it writable for the qemu group. This needs to be repeated if the libvirtd package is updated on the system }}
{{ ambox | nocat=true | type=important | text = Because our VMs don't run as qemu, /var/lib/libvirt/qemu needs to be changed using chmod g+w to make it writable for the qemu group. This needs to be repeated if the libvirtd package is updated on the system }}


<pre>
sudo chmod g+w /var/lib/libvirt/qemu
sudo chmod g+w /var/lib/libvirt/qemu
</pre>


=== Create /etc/modules-load.d/virtio.conf ===
=== Create /etc/modules-load.d/virtio.conf ===
Line 142: Line 116:
Several virtio modules need to be loaded into the host's kernel.  This could be done by modprobe ahead of running any virtual machines but it is easier to install them whenever the host boots.  This is arranged by listing the modules in a file within /etc/modules-load.d.  The host must be rebooted for this to take effect.
Several virtio modules need to be loaded into the host's kernel.  This could be done by modprobe ahead of running any virtual machines but it is easier to install them whenever the host boots.  This is arranged by listing the modules in a file within /etc/modules-load.d.  The host must be rebooted for this to take effect.


<pre>
sudo dd <<EOF of=/etc/modules-load.d/virtio.conf
sudo dd <<EOF of=/etc/modules-load.d/virtio.conf
virtio_blk
virtio_blk
virtio-rng
virtio-rng
virtio_console
virtio_console
virtio_net
virtio_net
virtio_scsi
virtio_scsi
virtio
virtio
virtio_balloon
virtio_balloon
virtio_input
virtio_input
virtio_pci
virtio_pci
virtio_ring
virtio_ring
9pnet_virtio
9pnet_virtio
EOF
EOF
</pre>


As of Fedora 28, several of these modules are now built into the kernel and will not show up in /proc/modules (virtio, virtio_rng, virtio_pci, virtio_ring).
As of Fedora 28, several of these modules are built into the kernel and will not show up in /proc/modules (virtio, virtio_rng, virtio_pci, virtio_ring).


=== Ensure that the host has enough entropy ===
=== Ensure that the host has enough entropy ===
Line 175: Line 147:


Fedora commands for using rngd:
Fedora commands for using rngd:
<pre>
 
sudo dnf install rng-tools
sudo dnf install rng-tools
sudo systemctl enable rngd
sudo systemctl enable rngd
sudo systemctl start rngd
sudo systemctl start rngd
</pre>


Fedora commands for using havegd:
Fedora commands for using havegd:


<pre>
sudo dnf install haveged
sudo dnf install haveged
sudo systemctl enable haveged
sudo systemctl enable haveged
sudo systemctl start haveged
sudo systemctl start haveged
</pre>


== Download and configure libreswan ==
== Download and configure libreswan ==
Line 195: Line 164:
The libreswan source tree includes all the components that are used on the host and inside the test VMs. To get the latest source code using git:
The libreswan source tree includes all the components that are used on the host and inside the test VMs. To get the latest source code using git:


<pre>
git clone https://github.com/libreswan/libreswan
git clone https://github.com/libreswan/libreswan
cd libreswan
cd libreswan
</pre>


=== Create the Pool directory for storing VM disk images - $(KVM_POOLDIR) ===
=== Create the Pool directory for storing VM disk images - $(KVM_POOLDIR) ===
Line 206: Line 173:
To change the location of the pool directory, set the KVM_POOLDIR make variable in Makefile.inc.local.  For instance:
To change the location of the pool directory, set the KVM_POOLDIR make variable in Makefile.inc.local.  For instance:


<pre>
$ grep KVM_POOLDIR Makefile.inc.local
$ grep KVM_POOLDIR Makefile.inc.local
KVM_POOLDIR=/home/libreswan/pool
KVM_POOLDIR=/home/libreswan/pool
</pre>


=== (optional) Use /tmp/pool (tmpfs) to store test VM disk images - $(KVM_LOCALDIR) ===
=== (optional) Use /tmp/pool (tmpfs) to store test VM disk images - $(KVM_LOCALDIR) ===
Line 215: Line 180:
By default, all disk mages are stored in $(KVM_POOLDIR) (see above).  That is both the base VM disk image, and the build VM and test VM disk images.  Since only the base VM image needs long-term storage, $(KVM_LOCALDIR) can be used to specify that the build and test images are stored in /tmp:
By default, all disk mages are stored in $(KVM_POOLDIR) (see above).  That is both the base VM disk image, and the build VM and test VM disk images.  Since only the base VM image needs long-term storage, $(KVM_LOCALDIR) can be used to specify that the build and test images are stored in /tmp:


<pre>
$ grep KVM_LOCALDIR Makefile.inc.local
$ grep KVM_LOCALDIR Makefile.inc.local
KVM_LOCALDIR=/tmp/pool
KVM_LOCALDIR=/tmp/pool
</pre>


This has the advantage of eliminating physical disk I/O as a bottle neck when accessing VM disk images; but the disadvantage of needing to re-build the images after a reboot.
This has the advantage of eliminating physical disk I/O as a bottle neck when accessing VM disk images; but the disadvantage of needing to re-build the images after a reboot.
Line 224: Line 187:
=== (optional) Run tests in parallel - $(KVM_PREFIXES) ===
=== (optional) Run tests in parallel - $(KVM_PREFIXES) ===


By default only one test is run at a time.  This can be changed using KVM_PREFIXES make variable which specifies the prefix to prepend to test domains.  The default value is:
By default only one test is run at a time.  This can be changed using the $(KVM_PREFIXES) make variable.  This provides a list of prefixes to be pretended to test domains creating multiple test groups.  The default value is:


<pre>
KVM_PREFIXES=''
KVM_PREFIXES=''
</pre>


which creates the domains ''east, ''west, et.al. (i.e., after expansion east, west, et.al.).
which creates the build domains ''fedora-build, ''netbsd-build, et.al., and the test domains ''east, ''west, et.al. (i.e., after expansion east, west, et.al.).


Multiple tests can be run in parallel by specifying more prefixes - a rule of thumb is one prefix per two CPU cores.  For instance, on a 4-core machine, two prefixes can be specified using:
To run tests in parallel, specify multiple prefixes.  For instance two tests can be run in parallel by specifying:


<pre>
KVM_PREFIXES='' 1.
KVM_PREFIXES='' 1.
</pre>


which creates, after expansion, the domains east, west, et.al. and 1.east, 1.west, et.al.
This will create the build domains ''fedora-build, ''netbsd-build, et.al., and the test domains ''east, ''west, et.al., and separately 1.east, 1.west, et.al.


=== (very optional) Boot VMs in parallel - $(KVM_WORKERS) ===
{{ ambox | nocat=true | type=important | text = TODO: generate $(KVM_PREFIXES) from $(KVM_PREFIX) and $(KVM_WORKERS) so that the build domains are prefixed by $(KVM_PREFIX) and the test domains are prefixed by $(KVM_PREFIX), $(KVM_PREFIX)2, ..., $(KVM_PREFIX)$(KVM_WORKERS); only create the first test domains and then create the rest as runtime snapshots. }}


By default one thread is dedicated to booting VMs.  Since booting a VM is very CPU intensive, trying to boot multiple VMs can quickly boog down the machine causing tests being run in parallel to become so slow that they timeout.
=== (optional) Parallel builds - $(KVM_WORKERS) ===


So while not recommended, this can be changed using the make variable KVM_WORKERS:
By default, build domains only have on virtual CPU.  Since building is very CPU intensive, this can be increased using $(KVM_WORKERS).


<pre>
<pre>
KVM_WORKERS=2
KVM_WORKERS=2
</pre>
</pre>
{{ ambox | nocat=true | type=important | text = In the past, because many tests were racy (results were sensitive to CPU load) KVM_WORKERS was used throttle the number of domains been booted in parallel (it is very CPU intensive).  That is no longer true.  See notes under KVM_PREFIXES above. }}


=== (optional) Generate a web page of the test results ===
=== (optional) Generate a web page of the test results ===
Line 266: Line 227:
Alternatively, a web server can be installed and configured:
Alternatively, a web server can be installed and configured:


<pre>
sudo dnf install httpd
sudo dnf install httpd
sudo systemctl enable httpd
sudo systemctl enable httpd
sudo systemctl start httpd
sudo systemctl start httpd
sudo mkdir /var/www/html/results/
sudo mkdir /var/www/html/results/
sudo chown $(id -un) /var/www/html/results/
sudo chown $(id -un) /var/www/html/results/
sudo chmod 755 /var/www/html/results/
sudo chmod 755 /var/www/html/results/
sudo sh -c 'echo "AddType text/plain .diff" >/etc/httpd/conf.d/diff.conf'
sudo sh -c 'echo "AddType text/plain .diff" >/etc/httpd/conf.d/diff.conf'
</pre>


and then $(WEB_SUMMARYDIR) used to specify that the web pages should be published under the server directory:
and then $(WEB_SUMMARYDIR) used to specify that the web pages should be published under the server directory:


<pre>
$ grep WEB_SUMMARYDIR Makefile.inc.local
$ grep WEB_SUMMARYDIR Makefile.inc.local
WEB_SUMMARYDIR=/var/www/html/results
WEB_SUMMARYDIR=/var/www/html/results
</pre>


If you want it to be the main page of the website, you can create the file /var/www/html/index.html containing:
If you want it to be the main page of the website, you can create the file /var/www/html/index.html containing:
Line 307: Line 264:
To build the VMs, and build and install (or update) libreswan, and then run the tests, use:
To build the VMs, and build and install (or update) libreswan, and then run the tests, use:


<pre>
./kvm install check
./kvm install check
</pre>


=== Setting up <tt>./kvm</tt> (tab completion) ===
=== Setting up <tt>./kvm</tt> (tab completion) ===
Line 315: Line 270:
If this:
If this:


<pre>
complete -o filenames -C './kvm' ./kvm
complete -o filenames -C './kvm' ./kvm
</pre>


is added to  <tt>.bashrc</tt> then tab completion with <tt>./kvm</tt> will include both commands and directories.
is added to  <tt>.bashrc</tt> then tab completion with <tt>./kvm</tt> will include both commands and directories.
Line 340: Line 293:
the operations can be combined on a single line:
the operations can be combined on a single line:


<pre>
./kvm install check recheck diff
./kvm install check recheck diff
</pre>


and individual tests can be selected:
and individual tests can be selected:


<pre>
./kvm install check diff testing/pluto/*ikev2*
./kvm install check diff testing/pluto/*ikev2*
</pre>


To stop <tt>./kvm</tt> use control-c.
To stop <tt>./kvm</tt> use control-c.
Line 401: Line 350:
<tt>./kvm sh HOST</tt> is a wrapper around "virsh" that automatically handles things like booting the machine, logging in, and correctly configuring the terminal.  It's big advantage is that it always works.  For instance:
<tt>./kvm sh HOST</tt> is a wrapper around "virsh" that automatically handles things like booting the machine, logging in, and correctly configuring the terminal.  It's big advantage is that it always works.  For instance:


<pre>
$ ./testing/utils/kvmsh.py east
$ ./testing/utils/kvmsh.py east
[...]
[...]
Escape character is ^]
Escape character is ^]
[root@east ~]# printenv TERM
[root@east ~]# printenv TERM
xterm
xterm
[root@east ~]# stty -a
[root@east ~]# stty -a
...; rows 52; columns 185; ...  
...; rows 52; columns 185; ...  
[root@east ~]#
[root@east ~]#
</pre>


The script "kvmsh.py" can also be used directly to invoke commands on a guest (this is how <tt>./kvm install</tt> works):
The script "kvmsh.py" can also be used directly to invoke commands on a guest (this is how <tt>./kvm install</tt> works):


<pre>
$ ./testing/utils/kvmsh.py east ls
$ ./testing/utils/kvmsh.py east ls
[root@east ~]# ls
[root@east ~]# ls
anaconda-ks.cfg
anaconda-ks.cfg
</pre>


When KVM_PREFIXES has multiple prefixes, the first is always used.
When $(KVM_PREFIXES) contains multiple prefixes, <tt>./kvm sh east</tt> always logs into the first prefixe's domain.


Limitations:
Limitations:


* no file transfer but files can be accessed via /testing
* no file transfer but files can be accessed via <tt>/pool</tt> and </tt>/testing</tt>


=== Graphical Console access using virt-manager ===
=== Graphical Console access using virt-manager ===
Line 438: Line 383:
Since you will be using ssh a lot to login to these machines, it is recommended to either put their names in /etc/hosts:
Since you will be using ssh a lot to login to these machines, it is recommended to either put their names in /etc/hosts:


<pre>
# /etc/hosts entries for libreswan test suite
# /etc/hosts entries for libreswan test suite
192.1.2.45 west
192.1.2.45 west
192.1.2.23 east
192.1.2.23 east
192.0.3.254 north
192.0.3.254 north
192.1.3.209 road
192.1.3.209 road
192.1.2.254 nic
192.1.2.254 nic
</pre>


or add entries to .ssh/config such as:
or add entries to .ssh/config such as:


<pre>
Host west
Host west
         Hostname 192.1.2.45
         Hostname 192.1.2.45
</pre>


If you wish to be able to ssh into all the VMs created without using a password, add your ssh public key to '''testing/baseconfigs/all/etc/ssh/authorized_keys'''. This file is installed as /root/.ssh/authorized_keys on all VMs
If you wish to be able to ssh into all the VMs created without using a password, add your ssh public key to '''testing/baseconfigs/all/etc/ssh/authorized_keys'''. This file is installed as /root/.ssh/authorized_keys on all VMs


Using ssh becomes easier if you are running ssh-agent (you probably are) and your public key is known to the virtual machine.  This command, run on the host, installs your public key on the root account of the guest machines west.  This assumes that west is up (it might not be, but you can put this off until you actually need ssh, at which time the machine would need to be up anyway).  Remember that the root password on each guest machine is "swan".
Using ssh becomes easier if you are running ssh-agent (you probably are) and your public key is known to the virtual machine.  This command, run on the host, installs your public key on the root account of the guest machines west.  This assumes that west is up (it might not be, but you can put this off until you actually need ssh, at which time the machine would need to be up anyway).  Remember that the root password on each guest machine is "swan".
<pre>
 
ssh-copy-id root@west
ssh-copy-id root@west
</pre>
 
You can use ssh-copy for any VM.  Unfortunately, the key is forgotten when the VM is restarted.
You can use ssh-copy for any VM.  Unfortunately, the key is forgotten when the VM is restarted.


Line 496: Line 437:
this workflow comes into its own, when updating tests en-mass using sed, for instance:
this workflow comes into its own, when updating tests en-mass using sed, for instance:


<pre>
sed -i -e 's/PARENT_//' testing/pluto/*/*.console.txt
sed -i -e 's/PARENT_//' testing/pluto/*/*.console.txt
./kvm modified check
./kvm modified check
</pre>


=== Checking for regressions ===
=== Checking for regressions ===
Line 505: Line 444:
Start by setting up a base directory.  Give the KVMs unique bN prefixes (only "b1" is needed, but we're in a hurry so add "b2 b3 b4", 4 boot workers, and /tmp/pool for KVM disk images) and kick off a test run:
Start by setting up a base directory.  Give the KVMs unique bN prefixes (only "b1" is needed, but we're in a hurry so add "b2 b3 b4", 4 boot workers, and /tmp/pool for KVM disk images) and kick off a test run:


<pre>
$ git clone https://github.com/libreswan/libreswan base
$ git clone https://github.com/libreswan/libreswan base
$ cd base
$ cd base
base$ # base - use bN as the prefix
base$ # base - use bN as the prefix
base$ echo KVM_PREFIXES=b1 b2 b3 b4 >> base/Makefile.inc.local
base$ echo KVM_PREFIXES=b1 b2 b3 b4 >> base/Makefile.inc.local
base$ echo KVM_WORKERS=4            >> base/Makefile.inc.local
base$ echo KVM_WORKERS=4            >> base/Makefile.inc.local
base$ echo KVM_LOCALDIR=/tmp/pool  >> base/Makefile.inc.local
base$ echo KVM_LOCALDIR=/tmp/pool  >> base/Makefile.inc.local
base$ mkdir -p ../pool
base$ mkdir -p ../pool
base$ nohup ./kvm install check &
base$ nohup ./kvm install check &
base$ tail -f nohup.out
base$ tail -f nohup.out
</pre>


Next, set up a working directory.  This time the KVMs are given the unique wN prefix, and point KVM_BASELINE back at base:
Next, set up a working directory.  This time the KVMs are given the unique wN prefix, and point KVM_BASELINE back at base:


<pre>
$ git clone https://github.com/libreswan/libreswan work
$ git clone https://github.com/libreswan/libreswan work
$ cd work
$ cd work
work$ # work - use wN as the prefix
work$ # work - use wN as the prefix
work$ echo KVM_PREFIXES=w1 w2 w3 w4 >> work/Makefile.inc.local
work$ echo KVM_PREFIXES=w1 w2 w3 w4 >> work/Makefile.inc.local
work$ echo KVM_WORKERS=4            >> work/Makefile.inc.local
work$ echo KVM_WORKERS=4            >> work/Makefile.inc.local
work$ echo KVM_BASELINE=../base    >> work/Makefile.inc.local
work$ echo KVM_BASELINE=../base    >> work/Makefile.inc.local
work$ echo KVM_LOCALDIR=/tmp/pool  >> work/Makefile.inc.local
work$ echo KVM_LOCALDIR=/tmp/pool  >> work/Makefile.inc.local
work$ mkdir -p ../pool
work$ mkdir -p ../pool
</pre>


work then then progress in the work directory, and when ready the test run started (here in the background):
work then then progress in the work directory, and when ready the test run started (here in the background):


<pre>
work$ ed programs/pluto/plutomain.c
work$ ed programs/pluto/plutomain.c
/static bool selftest_only = false/ s/false/true/
/static bool selftest_only = false/ s/false/true/
w
w
q
q
work$ gmake && nohup ./kvm install check &
work$ gmake && nohup ./kvm install check &
<pre>


as the tests progress, the results can be monitored:
as the tests progress, the results can be monitored:


<pre>
work$ ./kvm baseline results
work$ ./kvm baseline results
testing/pluto/basic-pluto-01 failed east:baseline-passed,output-different west:baseline-passed,output-different
testing/pluto/basic-pluto-01 failed east:baseline-passed,output-different west:baseline-passed,output-different
...
...
work$ ./kvm baseline diffs testing/pluto/basic-pluto-01
work$ ./kvm baseline diffs testing/pluto/basic-pluto-01
+whack: Pluto is not running (no "/run/pluto/pluto.ctl")
+whack: Pluto is not running (no "/run/pluto/pluto.ctl")
</pre>


and then the test run aborted, and the problem fixed and tested, and the test run restarted:
and then the test run aborted, and the problem fixed and tested, and the test run restarted:


<pre>
work$ ./kvm kill
work$ ./kvm kill
work$ git checkout -- programs/pluto/plutomain.c
work$ git checkout -- programs/pluto/plutomain.c
work$ ./kvm install check diff testing/pluto/basic-pluto-01
work$ ./kvm install check diff testing/pluto/basic-pluto-01
work$ nohup recheck &
work$ nohup recheck &
</pre>


The output can be fine tuned using baseline-failed (show differences when the baseline failed, ignoring passed and unresolved) baseline-passed (show differences when the baseline passed, ignoring failed and unresolved).
The output can be fine tuned using baseline-failed (show differences when the baseline failed, ignoring passed and unresolved) baseline-passed (show differences when the baseline passed, ignoring failed and unresolved).
Line 567: Line 496:
The command <tt>./kvm install check diff</tt> will exit with <tt>git bisect</tt> friendly status codes (See Bisect run).  This can be exploited with a sequence like:
The command <tt>./kvm install check diff</tt> will exit with <tt>git bisect</tt> friendly status codes (See Bisect run).  This can be exploited with a sequence like:


<pre>
git bisect start main ^<suspect-commit>
git bisect start main ^<suspect-commit>
git bisect run ./kvm install check diff testing/pluto/basic-pluto-01
git bisect run ./kvm install check diff testing/pluto/basic-pluto-01
git bisect visualize
git bisect visualize
# finally
# finally
git bisect reset
git bisect reset
</pre>


Also, before starting, consider making a copy of the test directory as it can help avoid problems with tests changing with the commits.
Also, before starting, consider making a copy of the test directory as it can help avoid problems with tests changing with the commits.
Line 581: Line 508:
Start the testsuite in the background:
Start the testsuite in the background:


<pre>
nohup ./kvm install check &
nohup ./kvm install check &
</pre>


To determine if the testsuite is still running:
To determine if the testsuite is still running:


<pre>
./kvm status
./kvm status
</pre>


and to stop the running testsuite:
and to stop the running testsuite:


<pre>
./kvm kill
./kvm kill
</pre>


=== Debugging inside the VM (pluto on east) ===
=== Debugging inside the VM (pluto on east) ===
Line 601: Line 522:
Terminal 1 - east: log into east, start pluto, and attach gdb
Terminal 1 - east: log into east, start pluto, and attach gdb


<pre>
./kvm sh east
./kvm sh east
east# cd /testing/pluto/basic-pluto-01
east# cd /testing/pluto/basic-pluto-01
east# sh -x ./eastinit.sh
east# sh -x ./eastinit.sh
east# gdb /usr/local/libexec/ipsec/pluto $(pidof pluto)
east# gdb /usr/local/libexec/ipsec/pluto $(pidof pluto)
(gdb) c
(gdb) c
</pre>


If pluto isn't running then gdb will complain with: ''<code>--p requires an argument</code>''
If pluto isn't running then gdb will complain with: ''<code>--p requires an argument</code>''
Line 613: Line 532:
Terminal 2 - west: log into west, start pluto and the test
Terminal 2 - west: log into west, start pluto and the test


<pre>
./kvm sh west
./kvm sh west
west# sh -x ./westinit.sh ; sh -x westrun.sh
west# sh -x ./westinit.sh ; sh -x westrun.sh
</pre>


When pluto crashes, gdb will show that and await commands.  For example, the <tt>bt</tt> command will show a backtrace.
When pluto crashes, gdb will show that and await commands.  For example, the <tt>bt</tt> command will show a backtrace.

Revision as of 03:16, 15 December 2021

KVM Test framework

Libreswan's test framework can be run using KVM guests, and the kvm scripts. It is strongly recommended to run the test suite on a host machine that has a CPU wth virtualisation instructions.

To access files on the host file system:

  • linux guests use the PLAN9 filesystem (9p)
  • BSD guests (well openbsd) use NFS via the NAT interface

For an overview of the tests see Test_Suite

Preparing the host machine

Add Yourself to sudo

Some of the test scrips need to be run as root. The test environment assumes this can be done using sudo without a password vis:

sudo pwd

XXX: Surely qemu can be driven without root?

This is done by creating a no-pasword rule to /etc/sudoers.d/.

To set this up, add your account to the wheel group:

sudo usermod -a -G wheel $(id -u -n)

and permit wheel to have no-password access:

echo '%wheel ALL=(ALL) NOPASSWD: ALL' | sudo dd of=/etc/sudoers.d/wheel
sudo chmod ug=r,o= /etc/sudoers.d/wheel
sudo chown root.root /etc/sudoers.d/wheel

Fight SELinux

SELinux blocks some actions that we need. We have not created any SELinux rules to avoid this. The options are:

  • set SELinux to permissive (recommended)
sudo sed --in-place=.ORIG -e 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
sudo setenforce Permissive
  • disable SELinux
sudo sed --in-place=.ORIG -e 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
sudo reboot
  • (experimental) label source tree for SELinux

The source tree on the host is shared with the virtual machines. SELinux considers this a bug unless the tree is labelled with type svirt_image_t.

sudo dnf install policycoreutils-python-utils
sudo semanage fcontext -a -t svirt_image_t "$(pwd)"'(/.*)?'
sudo restorecon -vR /home/build/libreswan

There may be other things that SELinux objects to.

Install Required Dependencies

Now we are ready to install the various components of libvirtd, qemu and kvm and then start the libvirtd service.

Fedora

To get qemu working (while virt-manager isn't strictly required it's useful on a desktop):

sudo dnf install -y make git
sudo dnf install -y qemu virt-manager virt-install libvirt-daemon-kvm libvirt-daemon-qemu
sudo dnf install -y python3-pexpect
sudo dnf install -y dvd+rw-tools           # for building OpenBSDs boot disk
sudo dnf install -y jq nodejs-typescript   # for generating web pages

Once all is installed start libvirtd and then check it is running:

sudo systemctl enable libvirtd
sudo systemctl start libvirtd
sudo systemctl status libvirtd

There should be no errors and warnings.

On testing and F29, this failed with the error:

error : virQEMUCapsNewForBinaryInternal:4664 : internal error: Failed to probe QEMU binary with QMP: /usr/bin/qemu-system-xtensa: error while loading shared libraries: libbrlapi.so.0.6: cannot open shared object file: No such file or directory

and it was found that 'brlapi' needed to be manually installed.

Debian

Anyone?

BSD

Anyone?

Setting Users and Groups

You need to add yourself to the qemu group. For instance:

sudo usermod -a -G qemu $(id -u -n)

You will need to re-login for this to take effect. A cheat is to re-login the current shell:

sudo su - $(id -u -n)

The path to your build needs to be accessible (executable) by root, assuming things are under home:

chmod a+x $HOME

Fix /var/lib/libvirt/qemu

sudo chmod g+w /var/lib/libvirt/qemu

Create /etc/modules-load.d/virtio.conf

Several virtio modules need to be loaded into the host's kernel. This could be done by modprobe ahead of running any virtual machines but it is easier to install them whenever the host boots. This is arranged by listing the modules in a file within /etc/modules-load.d. The host must be rebooted for this to take effect.

sudo dd <<EOF of=/etc/modules-load.d/virtio.conf
virtio_blk
virtio-rng
virtio_console
virtio_net
virtio_scsi
virtio
virtio_balloon
virtio_input
virtio_pci
virtio_ring
9pnet_virtio
EOF

As of Fedora 28, several of these modules are built into the kernel and will not show up in /proc/modules (virtio, virtio_rng, virtio_pci, virtio_ring).

Ensure that the host has enough entropy

Entropy matters

Fedora 32 comes with rng-tools pre-installed.

With KVM, a guest systems uses entropy from the host through the kernel module "virtio_rng" in the guest's kernel (set above). This has advantages:

  • entropy only needs to be gathered on one machine (the host) rather than all machines (the host and the guests)
  • the host is in the Real World and thus has more sources of real entropy
  • any hacking to make entropy available need only be done on one machine

To ensure the host has enough randomness, run either rngd or havegd. The old jitterentropy-rngd code has been merged into rng-tools' rngd.

Fedora commands for using rngd:

sudo dnf install rng-tools
sudo systemctl enable rngd
sudo systemctl start rngd

Fedora commands for using havegd:

sudo dnf install haveged
sudo systemctl enable haveged
sudo systemctl start haveged

Download and configure libreswan

Fetch Libreswan

The libreswan source tree includes all the components that are used on the host and inside the test VMs. To get the latest source code using git:

git clone https://github.com/libreswan/libreswan
cd libreswan

Create the Pool directory for storing VM disk images - $(KVM_POOLDIR)

The pool directory is used used to store VM disk images and other configuration files. By default $(top_srcdir)/../pool is used (that is, adjacent to your source tree).

To change the location of the pool directory, set the KVM_POOLDIR make variable in Makefile.inc.local. For instance:

$ grep KVM_POOLDIR Makefile.inc.local
KVM_POOLDIR=/home/libreswan/pool

(optional) Use /tmp/pool (tmpfs) to store test VM disk images - $(KVM_LOCALDIR)

By default, all disk mages are stored in $(KVM_POOLDIR) (see above). That is both the base VM disk image, and the build VM and test VM disk images. Since only the base VM image needs long-term storage, $(KVM_LOCALDIR) can be used to specify that the build and test images are stored in /tmp:

$ grep KVM_LOCALDIR Makefile.inc.local
KVM_LOCALDIR=/tmp/pool

This has the advantage of eliminating physical disk I/O as a bottle neck when accessing VM disk images; but the disadvantage of needing to re-build the images after a reboot.

(optional) Run tests in parallel - $(KVM_PREFIXES)

By default only one test is run at a time. This can be changed using the $(KVM_PREFIXES) make variable. This provides a list of prefixes to be pretended to test domains creating multiple test groups. The default value is:

KVM_PREFIXES=

which creates the build domains fedora-build, netbsd-build, et.al., and the test domains east, west, et.al. (i.e., after expansion east, west, et.al.).

To run tests in parallel, specify multiple prefixes. For instance two tests can be run in parallel by specifying:

KVM_PREFIXES= 1.

This will create the build domains fedora-build, netbsd-build, et.al., and the test domains east, west, et.al., and separately 1.east, 1.west, et.al.

(optional) Parallel builds - $(KVM_WORKERS)

By default, build domains only have on virtual CPU. Since building is very CPU intensive, this can be increased using $(KVM_WORKERS).

 KVM_WORKERS=2

(optional) Generate a web page of the test results

See the nightly test results for an example.

To create the web directory RESULTS/ and populate it with the current test results use:

make web

The files can the be viewed using http://file.

To disable web page generation, delete the directory RESULTS/.

Alternatively, a web server can be installed and configured:

sudo dnf install httpd
sudo systemctl enable httpd
sudo systemctl start httpd
sudo mkdir /var/www/html/results/
sudo chown $(id -un) /var/www/html/results/
sudo chmod 755 /var/www/html/results/
sudo sh -c 'echo "AddType text/plain .diff" >/etc/httpd/conf.d/diff.conf'

and then $(WEB_SUMMARYDIR) used to specify that the web pages should be published under the server directory:

$ grep WEB_SUMMARYDIR Makefile.inc.local
WEB_SUMMARYDIR=/var/www/html/results

If you want it to be the main page of the website, you can create the file /var/www/html/index.html containing:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <meta http-equiv="REFRESH" content="0;url=/results/">
  </head>
  <BODY>
  </BODY>
</HTML>

Running the testsuite

In the past, the testsuite was driven using make kvm-... commands. That's largely been replaced by the top-level wrapper script ./kvm which has several advantages over make:

  • it is file (file) completion friendly
  • it is shell script friendly

For the impatient: ./kvm install check

To build the VMs, and build and install (or update) libreswan, and then run the tests, use:

./kvm install check

Setting up ./kvm (tab completion)

If this:

complete -o filenames -C './kvm' ./kvm

is added to .bashrc then tab completion with ./kvm will include both commands and directories.

Running the testsuite

./kvm install
update the KVMs ready for a new test run
./kvm check
run the testsuite, previous results are saved in BACKUP/-date-
./kvm recheck
run the testsuite, but skip tests that already passed
./kvm results
list the results from the test run
./kvm diffs
display differences between the test results and the expected results, exit non-zero if there are any

the operations can be combined on a single line:

./kvm install check recheck diff

and individual tests can be selected:

./kvm install check diff testing/pluto/*ikev2*

To stop ./kvm use control-c.

Updating Certificates

The full testsuite requires a number of certificates. If not present, then ./kvm check will automatically generate them using the domain build. Just note that the certificates have a limited lifetime. Should the test system detects out-of-date certificates then ./kvm check will barf.

To rebuild the certificates:

./kvm keys

can be used to force the generation of new certificates.

Cleaning up (and general maintenance)

./kvm check-clean
delete the test results
./kvm uninstall
delete the KVM build and test domains (but don't touch the build tree or test results)
./kvm clean
delete the test results, the KVM build and test domains, the build tree, and the certificates
./kvm purge
also delete the test networks (is purge still useful?)
./kvm demolish
also delete the KVM base domain that was used to create the other domains
./kvm upgrade
delete all KVM build and test domains, and then upgrade and transmogrify the base domain ready for a fresh install
./kvm transmogrify
run a fresh transmogrify on the base domain (the base domain is reverted to before the last transmogrify)
./kvm downgrade
revert the base domain back to before it was upgraded (useful when debugging upgrade and transmogrify)

Shell and Console Access (Logging In)

There are several different ways to gain shell access to the domains.

Each method, depending on the situation, has both advantages and disadvantages. For instance:

  • while make kvmsh-host provide quick access to the console, it doesn't support file copy
  • while SSH takes more to set up, it supports things like proper terminal configuration and file copy

Serial Console access using ./kvm sh HOST (kvmsh.py)

./kvm sh HOST is a wrapper around "virsh" that automatically handles things like booting the machine, logging in, and correctly configuring the terminal. It's big advantage is that it always works. For instance:

$ ./testing/utils/kvmsh.py east
[...]
Escape character is ^]
[root@east ~]# printenv TERM
xterm
[root@east ~]# stty -a
...; rows 52; columns 185; ... 
[root@east ~]#

The script "kvmsh.py" can also be used directly to invoke commands on a guest (this is how ./kvm install works):

$ ./testing/utils/kvmsh.py east ls
[root@east ~]# ls
anaconda-ks.cfg

When $(KVM_PREFIXES) contains multiple prefixes, ./kvm sh east always logs into the first prefixe's domain.

Limitations:

  • no file transfer but files can be accessed via /pool and /testing

Graphical Console access using virt-manager

"virt-manager", a gnome tool can be used to access individual domains.

While easy to use, it doesn't support cut/paste or mechanisms for copying files.

Shell access using SSH

While requiring more effort to set up, it provides full shell access to the domains.

Since you will be using ssh a lot to login to these machines, it is recommended to either put their names in /etc/hosts:

# /etc/hosts entries for libreswan test suite
192.1.2.45 west
192.1.2.23 east
192.0.3.254 north
192.1.3.209 road
192.1.2.254 nic

or add entries to .ssh/config such as:

Host west
       Hostname 192.1.2.45

If you wish to be able to ssh into all the VMs created without using a password, add your ssh public key to testing/baseconfigs/all/etc/ssh/authorized_keys. This file is installed as /root/.ssh/authorized_keys on all VMs

Using ssh becomes easier if you are running ssh-agent (you probably are) and your public key is known to the virtual machine. This command, run on the host, installs your public key on the root account of the guest machines west. This assumes that west is up (it might not be, but you can put this off until you actually need ssh, at which time the machine would need to be up anyway). Remember that the root password on each guest machine is "swan".

ssh-copy-id root@west

You can use ssh-copy for any VM. Unfortunately, the key is forgotten when the VM is restarted.

Limitations:

  • this only works with the default east, et.al. (it does not work with KVM_PREFIXES and/or multiple test directories)

kvm workflows

(seeing as everyone has a "flow", why not kvm) here are some common workflows, the following commands are used:

./kvm modified
list the test directories that have been modified
./kvm baseline
compare test results against a baseline
./kvm patch
update the expected test results
./kvm add
git add the modified test results
./kvm status
show the status of the currently running testsuite
./kvm kill
kill the currently running testsuite

Working on individual tests

The modified command can be used to limit the test run to just tests with modified files (according to git):

./kvm modified install check diff
install libreswan and then run the testsuite against just the modified tests, display differences differences
./kvm modified recheck diff
re-run the modified tests that are failing, display differences
./kvm modified patch add
update the modified tests applying the latest output and add them to git

this workflow comes into its own, when updating tests en-mass using sed, for instance:

sed -i -e 's/PARENT_//' testing/pluto/*/*.console.txt
./kvm modified check

Checking for regressions

Start by setting up a base directory. Give the KVMs unique bN prefixes (only "b1" is needed, but we're in a hurry so add "b2 b3 b4", 4 boot workers, and /tmp/pool for KVM disk images) and kick off a test run:

$ git clone https://github.com/libreswan/libreswan base
$ cd base
base$ # base - use bN as the prefix
base$ echo KVM_PREFIXES=b1 b2 b3 b4 >> base/Makefile.inc.local
base$ echo KVM_WORKERS=4            >> base/Makefile.inc.local
base$ echo KVM_LOCALDIR=/tmp/pool   >> base/Makefile.inc.local
base$ mkdir -p ../pool
base$ nohup ./kvm install check &
base$ tail -f nohup.out

Next, set up a working directory. This time the KVMs are given the unique wN prefix, and point KVM_BASELINE back at base:

$ git clone https://github.com/libreswan/libreswan work
$ cd work
work$ # work - use wN as the prefix
work$ echo KVM_PREFIXES=w1 w2 w3 w4 >> work/Makefile.inc.local
work$ echo KVM_WORKERS=4            >> work/Makefile.inc.local
work$ echo KVM_BASELINE=../base     >> work/Makefile.inc.local
work$ echo KVM_LOCALDIR=/tmp/pool   >> work/Makefile.inc.local
work$ mkdir -p ../pool

work then then progress in the work directory, and when ready the test run started (here in the background):

work$ ed programs/pluto/plutomain.c
/static bool selftest_only = false/ s/false/true/
w
q
work$ gmake && nohup ./kvm install check &

as the tests progress, the results can be monitored:

work$ ./kvm baseline results
testing/pluto/basic-pluto-01 failed east:baseline-passed,output-different west:baseline-passed,output-different
...
work$ ./kvm baseline diffs testing/pluto/basic-pluto-01
+whack: Pluto is not running (no "/run/pluto/pluto.ctl")

and then the test run aborted, and the problem fixed and tested, and the test run restarted:

work$ ./kvm kill
work$ git checkout -- programs/pluto/plutomain.c
work$ ./kvm install check diff testing/pluto/basic-pluto-01
work$ nohup recheck &

The output can be fine tuned using baseline-failed (show differences when the baseline failed, ignoring passed and unresolved) baseline-passed (show differences when the baseline passed, ignoring failed and unresolved).

To override the KVM_BASELINE make variable, use --baseline DIRECTORY

Bisecting a test regression

The command ./kvm install check diff will exit with git bisect friendly status codes (See Bisect run). This can be exploited with a sequence like:

git bisect start main ^<suspect-commit>
git bisect run ./kvm install check diff testing/pluto/basic-pluto-01
git bisect visualize
# finally
git bisect reset

Also, before starting, consider making a copy of the test directory as it can help avoid problems with tests changing with the commits.

Controlling a test run remotely

Start the testsuite in the background:

nohup ./kvm install check &

To determine if the testsuite is still running:

./kvm status

and to stop the running testsuite:

./kvm kill

Debugging inside the VM (pluto on east)

Terminal 1 - east: log into east, start pluto, and attach gdb

./kvm sh east
east# cd /testing/pluto/basic-pluto-01
east# sh -x ./eastinit.sh
east# gdb /usr/local/libexec/ipsec/pluto $(pidof pluto)
(gdb) c

If pluto isn't running then gdb will complain with: --p requires an argument

Terminal 2 - west: log into west, start pluto and the test

./kvm sh west
west# sh -x ./westinit.sh ; sh -x westrun.sh

When pluto crashes, gdb will show that and await commands. For example, the bt command will show a backtrace.

TODO:

  • stop watchdog eventually killing pluto
  • notes for west