I was trying some TDD with Tesk-Kitchen and ServerSpec when I found myself in the following case scenario:
I have a integration test like this:
# cookbook_webtest/test/integration/default/serverspec/localhost/webtest_spec.rb
require 'spec_helper'
require 'faraday'
describe "Web server" do
let(:host) { URI.parse('http://localhost') }
it "is listening on port 80" do
expect(port(80)).to be_listening
end
it "is showing a page with the text 'Hello world'" do
connection = Faraday.new host
page = connection.get('/').body
expect(page).to match /Hello world/
end
end
The problem here is that to require the library faraday
I need to have the gem already installed in the system.
Test-kitchen install the gem serverspec
vía busser, but just that gem and its dependencies.
I was looking through the documentation but I didn't found a official way to install dependencies for your tests.
Searching for a solution the my problem through the code I found two solutions, but I'm not sure which one is more official/elegant. Here are my two solutions:
I found how the busser plugin install its own gem and I did the same from the spec_helper.rb
:
# cookbook_webtest/test/integration/default/serverspec/spec_helper.rb
# The workaround
require 'busser/rubygems'
Busser::RubyGems.install_gem('faraday', '~> 0.9.0')
# The normal stuff
require 'serverspec'
include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS
RSpec.configure do |c|
c.before :all do
c.path = '/sbin:/usr/sbin'
end
end
Later I found that there is a hook in the Busser to setup the enviroment before to test a suite. You need to place a shell script named prepare.sh
at the busser's level:
#!/bin/sh
# cookbook_webtest/test/integration/default/serverspec/prepare.sh
/opt/chef/embedded/bin/gem install faraday --no-rdoc --no-ri --install-dir /tmp/busser/gems/ --version 0.9.0
I believe that this is not so uncommon case, so I'd like to know which one is the better way to solve this problem. Maybe there is another way I didn't find... Any light to this subject will be much appreciate 😄
You can add Gemfile now in serverspec.
See https://github.com/test-kitchen/busser-serverspec#-usage