Skip to content

Instantly share code, notes, and snippets.

@obazoud
Forked from juanje/gist:9603938
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obazoud/90cd0e2151c3453531b3 to your computer and use it in GitHub Desktop.
Save obazoud/90cd0e2151c3453531b3 to your computer and use it in GitHub Desktop.

Install gem before to require it at Test-Kitchen

Context

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

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:

Solution 1: Busser method

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

Solution 2: prepare.sh hook

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

Conclusion

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 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment