require ruby library in serverspec - ruby

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.

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'

Related

how to invoke bundler commands from within a Serverspec/RSpec test

I have a project to create a template ruby project.
I am using serverspec and want to verify the behaviour of the template.
However, using command(`rake -T`) fails. If I execute the command manually, it works as expected.
Debugging, when the test is running in Serverspec, it finds the wrong Gemfile - it is using the Gemfile from my project (.), not the generated directory (target/sample_project).
How can I invoke rake or bundler commands withing a Serverspec/Rspec test?
sample code:
require "spec_helper"
require 'serverspec'
require 'fileutils'
set :backend, :exec
set :login_shell, true
describe "Generated Template" do
output_dir='target'
project_dir="#{output_dir}/sample_project"
# Hooks omitted to create the sample_project
# and change working directory to `project_dir`
describe command('rake -T') do
its(:stdout) { should include "rake serverspec:localhost" }
its(:stdout) { should include "rake serverspec:my_app" }
end
end
Bundler has provision for running external shell commands documented here: http://bundler.io/v1.3/man/bundle-exec.1.html
Running bundler/rake tasks is possible using rspec using Bundler.with_clean_env, instead of Serverspec.
require 'bundler'
require 'rspec'
RSpec.describe "Generated Template" do
output_dir='target'
project_dir="#{output_dir}/sample_project"
around(:example) do |example|
#Change context, so we are in the generated project directory
orig_dir=Dir.pwd
Dir.chdir(project_dir)
example.run
Dir.chdir(orig_dir)
end
around(:example) do |example|
Bundler.with_clean_env do
example.run
end
end
it "should include localhost" do
expect(`rake -T 2>&1`).to include "rake serverspec:localhost"
end
end

All SauceLabs tests run with Capybara result in "Unnamed Ruby job" and no metadata

I'm setting up a standalone RSpec/Capybara test suite integrated with SauceLabs, but the instructions in the documentation don't seem to be working for me.
Here are the relevant parts of my spec_helper.rb:
require 'capybara'
require 'capybara/rspec'
require 'sauce/capybara'
Sauce.config do |config|
config[:browsers] = [
[ "OSX 10.10", "Safari", "8" ]
]
end
Capybara.default_driver = :sauce
And here's the feature (not_found_spec.rb):
feature 'Enroll: 404', :sauce => true do
before :each do
#nonexistent_curriculum = FactoryGirl.build :curriculum
#enroll = Enroll.new
end
context 'When I visit a page that does not exist' do
scenario 'I see a Not Found message' do
#enroll.go #nonexistent_curriculum
expect(#enroll.not_found).to be_visible
end
end
end
When I then run rspec, the specs run and pass, but no metadata of any kind is recorded. All I see on SauceLabs is "Unnamed Ruby job".
What am I missing?
When you run
bundle exec rake sauce:install:spec
it creates a sauce_helper.rb which is then typically required from the end of your rails_helper.rb or spec_helper.rb depending on what *_helper.rb files you have. It looks like you copied the Sauce config part from sauce_helper into your spec_helper but you haven't shown that you have
require "sauce"
in there which is in the generated sauce_helper. Without requiring "sauce" it may be that sauce/rspec/rspec.rb is not getting required which is where all the hooks into rspec for tests with sauce: true are set up.

Load Error in file when running with minitest

I'm somewhat new to Ruby so I apologize if this is a stupid question. I've searched pretty hard, and can't really find anything that works.
My issue, I am trying to unit test a framework i'm writing that is located in the lib directory of a rails project. I'm using the minitest unit testing framework (executing via a rake task) in my unit test i have a require that is referencing "File A". That loads fine. However "File A" has a require to "File B"
like this:
require './FileB'
when that runs from minitest I get the cannot load such file error
this is how my rake task looks
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs = ["lib", "test"]
t.name = "test:file_a_tests"
t.warning = true
t.test_files = FileList['test/file_a_test.rb']
end
Whether you want to use a relative path during require, use:
require_relative 'FileB'
Hope it helps.

How to embed a screenshot with minitest-reporters

I am using minitest with Jenkins to produce test reports. At the moment I am using Minitest::Reporters::JUnitReporter - https://github.com/kern/minitest-reporters. This seems to give nice concise results.
The only thing missing is an embedded screenshot, specifically from failed tests.
How can I produce a Jenkins friendly test report that includes a screenshot? I am open to use one of the other Minitest::Reporters if it helps.
Thanks
You can use the capybara-screenshot gem for this: https://github.com/mattheworiordan/capybara-screenshot
Gemfile:
group :test do
gem 'capybara', '2.6.0'
gem 'selenium-webdriver', '2.49.0'
gem 'poltergeist', '1.8.1'
gem 'capybara-screenshot', '1.0.11'
end
test_helper.rb:
# default rails test configuration
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
# configure webtests with capybara and poltergeist (headless)
require 'capybara/rails'
require 'capybara/poltergeist'
require 'capybara-screenshot/minitest'
if ENV['HEADLESS'] == 'true'
# headless driver configuration
Capybara.default_driver = :poltergeist
else
Capybara.default_driver = :selenium
end
Capybara::Screenshot.prune_strategy = :keep_last_run # keep screenshots only from the last test run
Capybara.save_and_open_page_path = File.join(Rails.root, "test/screenshots") # where to save screenshots
# default test class for unit tests
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
# default test class for webtests
class ActionDispatch::IntegrationTest
include Capybara::DSL
include Capybara::Screenshot::MiniTestPlugin
end

How to skip require in ruby?

Can I conditionally skip requiring a file in Ruby?
begin
require 'aws-sdk'
rescue LoadError
puts "aws-sdk gem not found"
end
namespace :db do
desc "import local postgres database to heroku. user and database name is hardcoded"
task :import_to_heroku => [:environment, "db:dump_for_heroku"] do
# code using aws-sdk gem
end
end
In the above code, can I ask Ruby not to read after rescue LoadError
I can wrap the whole code in an conditional but that is inelegant.
I tried next and return.
EDIT: added a new question at Can I conditionally skip loading "further" ruby code in the same file?. sorry. Did not ask this question properly
Maybe add an exit after the log:
begin
require 'aws-sdk'
rescue LoadError
puts "aws-sdk gem not found"
exit
end
namespace :db do
desc "import local postgres database to heroku. user and database name is hardcoded"
task :import_to_heroku => [:environment, "db:dump_for_heroku"] do
# code using aws-sdk gem
end
end
Also the abort function is to log and exit in the same call:
abort("aws-sdk gem not found")
i have rescued LoadError but i want that if LoadError is executed., further code should not be executed. In the example given, the rake task db:import_to_heroku should not be called
Then do:
begin
require 'aws-sdk'
namespace :db do
desc "import local postgres database to heroku. user and database name is hardcoded"
task :import_to_heroku => [:environment, "db:dump_for_heroku"] do
# code using aws-sdk gem
end
end
rescue LoadError
puts "aws-sdk gem not found"
end
The "top-level return" feature has been added.
It is now possible to use the return keyword at the top level, which as you say, did not work at the time the question was asked. Further discussion here.

Resources