Skip to content

Instantly share code, notes, and snippets.

@Sam-Martin
Last active March 21, 2020 03:52
Show Gist options
  • Save Sam-Martin/fd97d39b285b97eab88c to your computer and use it in GitHub Desktop.
Save Sam-Martin/fd97d39b285b97eab88c to your computer and use it in GitHub Desktop.
Test Kitchen VagrantFile.rb to add disks & test_helper.bash to format them

Use these two files to add disks to your VMs (here adding three disks) when Kitchen spins up Vagrant boxes.

  • test_helper.bash (add to your bats test with load test_helper)
  • vagrantfile.rb (add to your .kitchen.yml using)
---
driver:
  name: vagrant
  vagrantfiles: 
      - vagrantfile.rb
#!/bin/bash
echo "Setting up test disks..."
{
# Unmount all disks in case they're already mounted (surpress exit code as they won't be the first time!)
sudo umount /media/disk1 || true
sudo umount /media/disk2 || true
sudo umount /media/disk3 || true
# Partition all three new disks
(echo o; echo n; echo p; echo 1; echo ; echo; echo w) | sudo fdisk /dev/sdb
(echo o; echo n; echo p; echo 1; echo ; echo; echo w) | sudo fdisk /dev/sdc
(echo o; echo n; echo p; echo 1; echo ; echo; echo w) | sudo fdisk /dev/sdd
# Format all three new disks
sudo mkfs.ext4 /dev/sdb1 -E lazy_itable_init=1
sudo mkfs.ext4 /dev/sdc1 -E lazy_itable_init=1
sudo mkfs.ext4 /dev/sdd1 -E lazy_itable_init=1
# Create mount points for all three new disks (surpress exit code as they may already exist)
sudo mkdir /media/disk1 || true
sudo mkdir /media/disk2 || true
sudo mkdir /media/disk3 || true
# Mount all three new disks
sudo mount -t ext4 /dev/sdb1 /media/disk1
sudo mount -t ext4 /dev/sdc1 /media/disk2
sudo mount -t ext4 /dev/sdd1 /media/disk3
# Create some files
sudo dd if=/dev/urandom of=/media/disk1/bigfile1.not bs=1M count=2
sudo dd if=/dev/urandom of=/media/disk1/bigfile2.not bs=1M count=2
sudo dd if=/dev/urandom of=/media/disk1/bigfile3.not bs=1M count=2
} &> /dev/null
echo "Disk setup complete!"
Vagrant.configure('2') do |config|
# Add additional hard disks for functional testing
config.vm.provider 'virtualbox' do |v|
# Attach SATA controller for ease of setup
v.customize ['storagectl', :id,
'--name', 'SATAController',
'--add', 'sata',
'--controller', 'IntelAhci',
'--portcount', 4
]
# Create and attach disks to SATA controller
file_to_disk = [
'tmp/disk1.vdi',
'tmp/disk2.vdi',
'tmp/disk3.vdi'
]
file_to_disk.each_with_index do |disk_file, i|
# Delete the disk if it already exists
if File.exist?(disk_file)
v.customize ['closemedium', 'disk', disk_file, '--delete']
end
# Create a fresh disk
v.customize ['createhd', '--filename', disk_file, '--size', 500 * 1024]
# Attach the disk
v.customize ['storageattach', :id,
'--storagectl', 'SATAController',
'--port', i,
'--device', 0,
'--type', 'hdd',
'--medium', disk_file
]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment