1

I am a newbie in ruby and trying to get my hands dirty in chef. I have written a wrapper cookbook on postgresql community cookbook and wish to test it using test kitchen. Following is the spec.rb file I have written:

require 'serverspec'
require 'pg'

include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS


RSpec.configure do |c|
  c.before :all do
    c.path = '/sbin:/usr/sbin'
    c.os = backend(Serverspec::Commands::Base).check_os
  end
end

describe "Postgresql server" do
    it "should connect to database" do
        conn = PG::Connection.open(:dbname => "db",:user => "user1",:password => "password")
        conn.status == "CONNECTION_OK"
    end
end

Through this test I wish to check if the user and database have been created properly. However this test is unable to resolve the dependency of "pg". Where do I mention this dependency in serverspec? I have used kitchen verify [node name] to run the test.

Vaibhav
  • 569
  • 6
  • 31

1 Answers1

2

Create the Ruby code necessary to install the gem prior to requiring it in your spec_helper.rb file (or on the top of the spec file if it makes more sense):

begin
  Gem::Specification.find_by_name('pg')
rescue Gem::LoadError
  require 'rubygems/dependency_installer'
  Gem::DependencyInstaller.new(Gem::DependencyInstaller::DEFAULT_OPTIONS).install('pg')
end
require 'pg'
cassianoleal
  • 2,556
  • 19
  • 22
  • I am running this spec file using test kitchen. Test kitchen is responsible for installing the required gems in the node. I am not aware of how to include this `bundle install` command in test kitchen's life cycle. – Vaibhav Mar 02 '14 at 11:02
  • I tried your method. Test kitchen does start installing pg gem. However it errors out with "Can't find the PostgreSQL client library (libpq)". There is a lot of discussion on this error and I found that using "env ARCHFLAGS="-arch x86_64" gem install pg" solves the problem. I have run this command manually and it is successful. However, how do I pass this parameter in DependencyInstaller? – Vaibhav Mar 20 '14 at 09:28