基于QEMU的系统测试自动化(第1/2部分)

本文重点介绍使用虚拟机自动进行端到端测试。本文讨论了诸如虚拟工作台的部署和配置自动化以及虚拟机内部的流程启动自动化等问题,然后监视结果。在本文的结尾,尽管不完美(尽管稍后会再介绍),但我们将收到一个简单易懂的脚本,您可以使用该脚本通过一个按钮运行系统测试,即使您的计算机上没有单个虚拟机也是如此。



本文假定读者具有以下技能:



  • 自信地使用Linux操作系统;
  • 对虚拟化原理有基本了解;
  • 引入QEMU虚拟机管理程序和virt-manager图形客户端


本文分为两个部分:在第一部分中,我们将熟悉基本工具,这些工具将使我们能够仅使用命令行来创建,部署和管理虚拟机。对于本文的第二部分(可以在这里找到:https : //habr.com/ru/post/520648/),这一知识将对我们有用。在本文中,我们将把这些工具连接在一起,并尝试自动执行特定网络应用程序的测试。



免责声明

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



- , , - .



什么是系统测试



系统测试(或有时称为端到端测试)是在考虑程序必须在其中运行的环境的情况下测试程序(甚至是整个系统)。除了系统测试之外,还包括单元测试(特定功能和模块的测试),集成测试(测试程序或整个程序的大型独立组件)以及许多其他类型的测试。但是,为什么系统的人会引起我们的兴趣呢?



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



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


, , , . , , , , - .



?



, ? (, ). , . , , :



  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 , .





在本文的第一部分中,我们尚未编写单个真实的系统测试,但是我们熟悉用于处理虚拟机的大量实用工具,了解了如何自动创建虚拟机,在其上部署OS,配置它们以及通过SSH建立控制通道。有了这些知识储备,我们现在就可以安全地进行最重要和有趣的事情:毕竟,如何在虚拟机上自动化系统测试。




All Articles