Cross-Platform Testing with Vagrant & VirtualBox

Environment testing verifies that an application installs and runs correctly in every supported operating system, distribution and version, and that it downloads the packages it needs. For a cross-platform product the matrix includes Linux, Windows and macOS, and the only sustainable way to cover it is with disposable machines created from code.

With Vagrant by HashiCorp I describe any Linux distribution, and many versions of each one, in a single file that anyone can reproduce. When a check needs a desktop environment, I use VirtualBox directly. The configuration below defines three machines (RedHat, SUSE and Debian) with fixed resources, a provisioning step that updates the system and a shared folder to exchange installers and results. It is driven from the Windows command line and plugs into a Jenkins pipeline.

# -*- mode: ruby -*-
# vi: set ft=ruby :

# -> RedHat
Vagrant.configure("2") do |config|
  config.vm.define "redhat9" do |redhat9|
  redhat9.vm.provider "virtualbox" do |vb|
    vb.name = "box-redhat9"
    vb.memory = "2048"
    vb.cpus = 2	
  redhat9.vm.box = "generic/rhel9"
  redhat9.vm.hostname = "test-redhat9"
  redhat9.vm.provision "shell", 
    inline: <<-SCRIPT
      sudo yum -y update
      sudo yum -y upgrade
    SCRIPT
# redhat9.vm.network "private_network", type: "dhcp"
  redhat9.vm.synced_folder "C:/Users/antonio/Downloads", "/home/vagrant/shared"
  end
end

# --> SUSE
  config.vm.define "suse-sles12sp1" do |sles12sp1|
  sles12sp1.vm.provider "virtualbox" do |vb|
    vb.name = "Suse-SLES-12SP1"
    vb.memory = "2048"
    vb.cpus = 2	
  sles12sp1.vm.box = "wvera/sles12sp1"
  sles12sp1.vm.hostname = "test-SUSE-sles12sp1"
  sles12sp1.vm.provision "shell", 
    inline: <<-SCRIPT
      sudo zypper update --no-confirm
      sudo zypper upgrade --no-confirm
    SCRIPT
# sles12sp1.vm.network "private_network", type: "dhcp"
  sles12sp1.vm.synced_folder "C:/Users/abgro/Downloads", "/home/vagrant/shared"
  end
end

# --> Debian
  config.vm.define "debian9" do |debian9|
  debian9.vm.provider "virtualbox" do |vb|
    vb.name = "box-debian9"
    vb.memory = "2048"
    vb.cpus = 2
  debian9.vm.box = "generic/debian9"
  debian9.vm.hostname = "test-debian-9"
  debian9.vm.provision "shell", 
    inline: <<-SCRIPT
      sudo apt -y update
      sudo apt -y upgrade
    SCRIPT
# debian9.vm.network "private_network", type: "dhcp"
  debian9.vm.synced_folder "C:/Users/abgro/Downloads", "/home/vagrant/shared"
  end
end
  • Infrastructure as code: the environment is a file under version control, so a failure can be reproduced by anyone on the team.
  • Disposable by design: vagrant up creates a clean machine and vagrant destroy -f removes it, so no test inherits the leftovers of the previous one.
  • Version coverage: adding a distribution or a release is one more block in the file, which keeps the support matrix honest.
  • Shared folder: the installer goes in, and the results come out for verification by the rest of the pipeline.