Testing multiple hosts with the same test using serverspec - ruby

The Advanced Tips section of the Serverspec site shows an example of testing multiple hosts with the same test set. I've built an example of my own (https://gist.github.com/neilhwatson/81249ad393800a76a8ad), but there are problems.
The first problem is that the tests stop at the first failure rather than proceeding through the lot and keeping a tally. The second is that the failure output does not indicate on which host the failure occurred. What can I do to fix these problems and produce a final report for all hosts?

For the first issue, ServerSpec by default will run all your tests. However, since you have a loop that executes a Rake task for each environment, the first environment to have a failure causes the task to fails and so an exception is raised and the rest of your tasks don't run.
I've forked your gist and updated the Rake task to surround it with a begin/rescue.
...
begin
desc "Run serverspec to #{host}"
RSpec::Core::RakeTask.new(host) do |t|
ENV['TARGET_HOST'] = host
t.pattern = "spec/base,cfengine3/*_spec.rb"
end
rescue
end
...
For the second problem, it doesn't look like ServerSpec will output which environment the tests are running in. But since the updated Gist shows that the host gets set in the spec_helper.rb we can use that to add an RSpec configuration that sets up an after(:each) and only output the host on errors. The relevant code changes are in a fork of the gist, but basically you'll just need the below snippet in your spec_helper.rb:
RSpec.configure do |c|
c.after(:each) do |example|
if example.exception
puts "Failed on #{host_run_on}"
end
end
end

Related

Order of Rake Test Task

I have a rake file with three tasks, which I need to execute in order.
require 'rake/testtask'
file 'some_binary_file.elf' do
puts 'fetching file from server ...'
# this task connects to a server and downloads some binaries
# it takes a few seconds to run
end
task flash_application: 'some_binary_file.elf' do
puts 'flashing the file to the hardware ...'
# this task copies a binary file to the flash memory
# of some external hardware, also takes a few seconds
end
Rake::TestTask(:hardware) do |t|
puts 'running tests ...'
f.test_files = FileList['test/**/*_test.rb']
end
rake default: [:flash_application, :hardware]
when I run $ rake in a terminal, it produces the following output.
running tests ... < ---- (not actually running)
fetching file from server ...
flashing the file to the hardware ...
I would expect rake to run the tasks in the order I specified, but It seems to always execute the test task first. It is remarkable that the tests do not run - but the output of the task creation is produced anyway.
When you want to run the tasks in a specific order, you must depend them on each other. In your case, :flash_application should then depend on :hardware
Found the Bug - This problem was not ruby / rake specific. The flash_application task changes the working directory. Because of that, there is no Rakefile with a task 'hardware' in the current working directory. But researching for this bug yielded some interesting insights.
Ruby arrays are ordered, if one want to execute task in an order it is sufficient to define them in execution order in an array i.e.
task some_task: [:first, :second, :third]
Rake::TestTask.new defines a plain old rake task when called. That means, when rake is called, ruby creates an instance of a Rake::TestTask. All code passed into the constructor is executed / yielded during this phase. This yields the described behavior from the original question.

Database cleaner not working with vim-rspec plugin

I use vim-rspec plugin to be able to run rspec tests from within vim, and it was working very well so far. But suddenly the database_cleaner gem stopped working.
Here is my configuration:
# spec/rspec_rails.rb
Rspec.configure do |config|
config.before(:suite) do
puts "Setting up the database cleaner."
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.around(:each) do |example|
puts "Cleaning the database"
DatabaseCleaner.cleaning do
example.run
end
end
end
I put those two messages to find out if the two blocks run. but they don't. Even if I stop spring than I run again it does not correct it. The strange thing is that if I run the rspec command from the command line every thing works well and I get both of the messages and the database cleaned, the first one time on running, and the second on every example run.
Problem might be in spring itself, remove it and try again. Also you can take a look at a g:rspec_command in your .vimrc file, maybe you bind any specific script to it?

Is there a way to hook a before and after suite, in a Rakefile?

My current Rakefile looks like this:
# ...
task :test do
# build testing environment
RSpec::Core::RakeTask.new(:spec) do |t|
# ...
end
Rake::Task["spec"].execute
# remove testing environment
end
Unfortunately this does not execute anything after Rake::Task["spec"].execute for some reasons that I can't possibly conceive.
So, is there a way to specify something to execute before and after the test suite runs, within the Rakefile?
If your specs failed, maybe that's why nothing after Rake::Task["spec"].execute gets executed. Instead of using the Rakefile, why not use Rspec's before(:suite) and after(:suite) hooks? You can put that code in your spec_helper.rb if you have one.
https://www.relishapp.com/rspec/rspec-core/v/2-0/docs/hooks/before-and-after-hooks#before/after-blocks-defined-in-config-are-run-in-order

Buildr, RSpec—pass build parameter to test

I have »buildr« »buildfile« which triggers some »rspec« tests. I would like to pass some path parameters to the tests, so that It wont cause trouble to load test-resources files. In the »buildfile« I have got this code to trigger the tests:
RSpec.configure do |config|
config.add_setting :spec_resources_dir, :default => _(:src, 'spec', 'ruby', 'resources')
end
RSpec::Core::RakeTask.new(:run_rspec) do |t|
t.pattern = 'src/spec/**/*_spec.rb'
end
task test => [:run_rspec]
But if I try to retrieve the value in the specfile like this:
RSpec.configuration.spec_resources_dir
I get this error
undefined method `spec_resources_dir' […] (NoMethodError)
Any ideas?
RSpec's rake task runs the specs in a separate process, so configuration you do with RSpec.configure in the buildfile will not be visible to the running specs.
Two suggestions for passing info from the buildfile to your spec task:
Generate a spec_helper and require it from your specs (or via rspec's -r command line option and the rspec_opts config parameter on RSpec::Core::RakeTask). You could use buildr's filtering to substitute values from the buildfile into the helper.
Set values in ENV and then read them out from your specs. Environment variables are shared from parent to child processes.
By request, an example for #1:
RSpec::Core::RakeTask.new do |t|
t.rspec_opts = "-r '#{_(:target, 'spec_helper.rb')}'"
end
This assumes that you (probably in another task) generate the spec helper into _(:target, 'spec_helper.rb')

run Ruby Unit Tests with Buildr

I am using buildr for some time now, but today I came over a little problem in connection to unit testing of ruby code.
So in my buildfile I have these lines:
define "ruby-project" do
project.version = VERSION_NUMBER
project.group = GROUP
Rake::TestTask.new(:test_rb) do |t|
t.warning = true
t.verbose = true
t.test_files = FileList['test/*.rb']
end
task test => [:test_rb]
end
running buildr test actually runs the tests, what is nice. The test is actually just that:
require 'test/unit'
class TestFileParse < Test::Unit::TestCase
def test_fail
assert(false, 'test to fail')
end
end
As expected it fails, BUT what is strange for me is that buildr quits the build with that message:
sh: 2: Syntax error: Unterminated quoted string
Buildr aborted!
RuntimeError : Command failed with status (1): [/usr/bin/ruby1.9.1 -w -I"lib" -I"/var/lib/...]
Running ruby file-with-failing-test-from-above.rp does not throw a runtime error, instead it prints the test report on screen, what is what is what I want.
Question
How can I make Buildr run the unit tests without quitting with an RuntimeError if a test fails?
Greetings Philipp
Since I am not too familiar with Ruby development, what involves Rake, I was looking for the wrong question. Instead of looking for: »how to run unit test with buildr«, the question should have been »how to run unit tests with rake«, because buildr is a kind of extended Rake (similar to the »maven-ant-relationship«). So everything one can do in Rake, one can do in buildr, too. Therefore on good SO answer to run ruby unit tests in buildr is here.
Additionally it is possible to run RSpec's with buildr, therefore one has two options:
set project.test.using :rspec, what involves the use of JRuby, so one has to set JRUBY_HOME (in my case ~/.rvm/rubies/jruby-1.7.9/), or run buildr within jruby. In my case this slowed test execution down, because each time a jvm needed to be started.
Or one can use rspec's rake task in this manner. I choose this method since my tests run much faster without the jvm overhead.
n.b. as the answer implies, I switched over to rspec.
Happy Testing/Speccing!

Resources