Using Rake to ServerSpec test
If you don't like kitchen, or your team is using Rake you may choose to use the rake spec command for serverspec testing. The directory layout is simpler than Kitchen, but requires more configuration as you need to create the following files;
- Rakefile
- spec_helper.rb
- Vagrantfile
- Provisioning script for the Vagrant VMs
These files need further configuration to make serverspec work using Rake. The Rakefile and the spec_helper.rb files will live within the test directory, but spec_helper.rb will live in a spec subdirectory.
You'll notice the key difference compared to RSpec is the localhost directory. Each host will need it's spec test included. For example;
Vagrantfile test ├── coretests │ ├── nginx_spec.rb │ └── dns_spec.rb ├── Rakefile └── spec └── web │ ├── nginx_spec.rb -> ../../coretests/nginx_spec.rb └── webserver1.al.local │ └── nginx_spec.rb -> ../../coretests/nginx_spec.rb └── spec_helper.rb
Example diagramatic view of a project and where the directory structure fits;
Above we now have an SSH host for web and weberver1.al.local. The difference between the localhost and the SSH version is in the spec_helper.rb file, so be sure to check your configurations.
In the above we symlink each host with the required tests in the coretests folder.
Rakefile
Here is an example Rake file to perform build, config and run;
The important elements about the above file are;
- hosts = %w (
- Each line in this list will be a host in your Vagrantfile
- The name specified here will be the same as the name of the define :web for example, which is the beginning of the definition of the VM
- task :spec => 'spec:all'
- This line defines what tasks to run within the file. This file will run the all task.
- task :all
- This is the main task that tells rake to use the hosts variable to loop through
- The hosts.each starts the real work
- If you have more than one host you will need to add them to the hosts array
- t.pattern
- This line defines the name of the directory that will be used to target the tests to run for the current host in the loop
spec/spec_helper.rb
This file defines how the VM will be created and the connection to it, as well as other environmental information to be used within the VM.
Example;
Vagrantfile
This will live above the test directory. For our example so far we would need a VM with the name of web;
The define :web matches the web in the Rakefile. The bin/web.sh will call the necessary commands to provision the VM, prior to serverspec running.
Running the test
To run a serverspec test using rake simply run the following;
Unlike kitchen, rake does not show the build and configuration of the VM, so you will be waiting some time until it gets to the test phase, which means the output is cleaner, since it is only the tests.