QEMU-Based System Test Automation (Part 1/2)

This article focuses on automating end-to-end testing using virtual machines. The article discusses such issues as the automation of the deployment and configuration of virtual benches, as well as the automation of the launch of processes inside virtual machines, followed by monitoring the results. At the end of the article, we will receive, albeit imperfect (we will return to this later), but a simple and understandable script with which you can run system tests with one button, even without having a single virtual machine on your computer.



The article assumes that the reader has the following skills:



  • Confident use of the Linux operating system;
  • Basic understanding of the principles of virtualization;
  • Introducing the QEMU hypervisor and virt-manager graphical client


The article is divided into two parts: in the first part we will get acquainted with the basic tools that will allow us to create, deploy and manage virtual machines using exclusively the command line. This knowledge will be useful to us for the second part of the article (which can be found here: https://habr.com/ru/post/520648/ ), where we will connect these tools together and try to automate tests of a specific network application.



Disclaimer

. . ( .. ). , , , , .



- , , - .



What is system testing



System testing (or, as it is sometimes called, end-to-end testing) is testing a program (or even an entire system), taking into account the environment in which the program will have to work. In addition to system testing, there is also unit testing (testing specific functions and modules), integration testing (testing large independent components of a program or an entire program) and many other types of testing. But why should we be interested in the systemic one?



, , , - . , , , . , . :



  • ;
  • (, );
  • ( .. );
  • ;
  • .


, , , . , , , , - .



?



, ? (, ). , . , , :



  1. Linux, ;
  2. GUI ( -GUI);
  3. .


1 2 , . 3 , . ( ) , , .



?



, , , . QEMU, , . , , (, VirtualBox).



?



, , :



  1. ;
  2. "" ;
  3. . , , .


-, , , , : , . , , . , . , , , .



, !





, . , , , . , QEMU virt-install. :



virt-install \
    --name my_super_vm \
    --ram 1024 \
    --disk my_super_vm.qcow2,size=8 \
    --cdrom /path/to/ubuntu_server.iso


my_super_vm, 1024 , my_super_vm.qcow2 8 . CD- ubuntu_server.iso (, ), , , .



, , VNC-, . Ubuntu Server 18.04. , Ubuntu Server, , .



, -.





( ) . ( ) , , . . , . , , VirtualBox .



virt-install, , : . - , .



, . , . libguestfs . , virt-builder, "" .



libguestfs

Libguestfs โ€” , . , . , , . ( Unix-way), , virt-copy-in. , -, , virt-builder.



, virt-builder? "" Ubuntu Server. :



virt-builder ubuntu-18.04 \
    --format qcow2 \
    --output my_super_disk.qcow2


? , qcow2 ( ) ubuntu-18.04, libguestfs. virt-builder , Ubuntu Server!



virt-builder , ,



, () my_super_disk.qcow2:



virt-install \
    --import \
    --name my_super_vm \
    --ram 1024 \
    --disk my_super_vm.qcow2


, --cdrom, . --import. , cdrom, ( Bios Boot Options ). .. Ubuntu Server, .



my_super_vm. , Ubuntu Server 18.04 .





. .



, : , . , ( ) . , GUI, bash- .



, , , : SSH- .



ssh . , , - . , ssh-. , ? , , .



. (pipe), . Linux- unix-socket, โ€” . , , . , ssh, , . qemu-guest-agent.



, Hyper-V, KVP (Key-Value Pairs) Hyper-V Sockets.



:



  1. ;
  2. ;
  3. root ;
  4. SSH- , SSH .


ssh

, , . . , โ€” , sudo. , .



. :



  1. ;
  2. ( -).


, .



virsh, โ€” libvirt.



libvirt

Libvirt โ€” , , , . libvirt โ€” , . , . , QEMU, . , - QEMU, libvirt virsh, , .



libvirt (, , ) XML-. XML- :



<network>
    <name>net_for_ssh</name>
    <bridge name='net_for_ssh'/>
    <ip address='192.168.100.1' netmask='255.255.255.0'/>
</network>


192.168.100.1 โ€” , , .



, โ€” xml :



virsh net-define net_for_ssh.xml


, :



virsh net-start net_for_ssh


. --network:



virt-install \
    --import \
    --name my_super_vm \
    --ram 1024 \
    --disk my_super_vm.qcow2 \
    --network network=net_for_ssh \
    --noautoconsole


--noautoconsole, VNC- (, - , virt-manager).



, : .





, (SSH- )? libguestfs virt-builder.



, , , . , Ubuntu Server 18.04 netplan, , , .yaml /etc/netplan. virt-builder --copy-in:



netcfg_ssh.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      addresses:
        - 192.168.100.2/24


virt-builder ubuntu-18.04 \
    --format qcow2 \
    --output my_super_disk.qcow2 \
    --copy-in netcfg_ssh.yaml:/etc/netplan/


Ubuntu Server 18.04 virt-builder netcfg_ssh.yaml /etc/netplan/ .



, :



ping 192.168.100.2 -c5


, SSH.



SSH



:



  1. root- ;
  2. SSH-, ;
  3. SSH- .


root. virt-builder, , , , โ€” root-:



virt-builder ubuntu-18.04 \
    --format qcow2 \
    --output my_super_disk.qcow2 \
    --root-password password:1111 \
    --copy-in netcfg_ssh.yaml:/etc/netplan/


SSH . - :



ssh-keygen -A
sed -i \"s/.*PermitRootLogin.*/PermitRootLogin yes/g\" /etc/ssh/sshd_config


, ? virt-builder , . , virt-builder . --run-command:



virt-builder ubuntu-18.04 \
    --format qcow2 \
    --output my_super_disk.qcow2 \
    --root-password password:1111 \
    --run-command "ssh-keygen -A" \
    --run-command "sed -i \"s/.*PermitRootLogin.*/PermitRootLogin yes/g\" /etc/ssh/sshd_config" \
    --copy-in netcfg_ssh.yaml:/etc/netplan/


?

. libguestfs , . , , User Space Linux Kernel. , . :



  • --run-command , ;
  • . โ€” apt install.


โ€ฆ ! ! , "". virt-install - . ssh . ssh :



#!/bin/bash
SSH_CMD="sshpass -p 1111 ssh -o StrictHostKeyChecking=no"

while ! $SSH_CMD root@192.168.100.2 echo Hello world from my super vm!
do
    echo "Waiting for my super vm ..."
    sleep 1
done


, -o StrictHostKeyChecking=no ssh , . sshpass , .





In the first part of the article, we have not yet written a single real system test, but we got acquainted with a serious arsenal of utilities for working with virtual machines, learned how to automatically create virtual machines, roll out OS on them, configure them, and also establish a control channel via SSH. With this stock of knowledge, we can now safely move on to the most important and interesting thing: how, after all, to automate system tests on virtual machines.




All Articles