1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00

Cucumber tests will now run on the latest Vagrant

This removes the dependency on the `kuroko` gem and uses the Vagrant
command line application to control the virtual environment. The
`vagrant` command should be in your path, but if it isn't the path can
be set with the `VAGRANT_BIN` environment variable. This may even work
on older versions of Vagrant, but they are untested.

The `VagrantHelpers` module was added to mimic some of the API that was
provided by `kuroko`. The `RemoteCommandHelpers` module was modified to
accommodate those changes. Any non-zero exit status on a remote command
will raise a `VagrantHelpers::VagrantSSHCommandError` and should be
expected by any tests using the command helpers. All existing tests
work as expected.

In addition, a couple of minor changes were made. The TestApp utilizes
the Pathname library but does not require it. This was causing the suite
to fail for me so I added an explicit require. Also, the test for the
existence of a release directory would give a false positive on
subsequent runs if the `KEEP_RUNNING` option was used. I added an
`at_exit` that removes the test deployment directory to clean up the
box for the next run.

Documentation was also added to the README for how to run the test
suites.
This commit is contained in:
Travis Petticrew 2014-04-22 10:36:44 -05:00
parent 1be0fa546a
commit 2e13a1319c
10 changed files with 75 additions and 15 deletions

View file

@ -7,4 +7,3 @@ rvm:
- rbx - rbx
script: bundle exec rake spec script: bundle exec rake spec
cache: bundler cache: bundler
bundler_args: --without cucumber

View file

@ -12,6 +12,7 @@ https://github.com/capistrano/capistrano/compare/v3.2.1...HEAD
* Minor Changes * Minor Changes
* Added tests for after/before hooks features (@juanibiapina, @miry) * Added tests for after/before hooks features (@juanibiapina, @miry)
* Improved the output of `cap --help`. (@mbrictson) * Improved the output of `cap --help`. (@mbrictson)
* Cucumber suite now runs on the latest version of Vagrant (@tpett)
## `3.2.1` ## `3.2.1`

View file

@ -4,7 +4,6 @@ source 'https://rubygems.org'
gemspec gemspec
group :cucumber do group :cucumber do
gem 'kuroko'
gem 'cucumber' gem 'cucumber'
end end

View file

@ -274,6 +274,27 @@ connections behind the scenes in Capistrano. Depending on how deep you dig, you
might run into interfaces that come directly from SSHKit (the configuration is might run into interfaces that come directly from SSHKit (the configuration is
a good example). a good example).
## Testing
Capistrano has two test suites: an RSpec suite and a Cucumber suite. The
RSpec suite handles quick feedback unit specs. The Cucumber features are
an integration suite that uses Vagrant to deploy to a real virtual
server. In order to run the Cucumber suite you will need to install
[Vagrant](http://www.vagrantup.com/) and Vagrant supported
virtualization software like
[VirtualBox](https://www.virtualbox.org/wiki/Downloads).
```
# To run the RSpec suite
$ rake spec
# To run the Cucumber suite
$ rake features
# To run the Cucumber suite and leave the VM running (faster for subsequent runs)
$ rake features KEEP_RUNNING=1
```
## License ## License
MIT License (MIT) MIT License (MIT)

View file

@ -1,5 +1,9 @@
require "bundler/gem_tasks" require "bundler/gem_tasks"
require "cucumber/rake/task"
require "rspec/core/rake_task"
task :default => :spec task :default => :spec
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new RSpec::Core::RakeTask.new
Cucumber::Rake::Task.new(:features)

View file

@ -100,7 +100,8 @@ end
Then(/^the failure task will not run$/) do Then(/^the failure task will not run$/) do
failed = TestApp.shared_path.join('failed') failed = TestApp.shared_path.join('failed')
!run_vagrant_command(test_file_exists(failed)) expect { run_vagrant_command(test_file_exists(failed)) }
.to raise_error(VagrantHelpers::VagrantSSHCommandError)
end end
When(/^an error is raised$/) do When(/^an error is raised$/) do

View file

@ -1,12 +1,11 @@
require 'kuroko' PROJECT_ROOT = File.expand_path('../../../', __FILE__)
VAGRANT_ROOT = File.join(PROJECT_ROOT, 'spec/support')
VAGRANT_BIN = ENV['VAGRANT_BIN'] || "vagrant"
project_root = File.expand_path('../../../', __FILE__) at_exit do
vagrant_root = File.join(project_root, 'spec/support') if ENV['KEEP_RUNNING']
VagrantHelpers.run_vagrant_command("rm -rf /home/vagrant/var")
Kuroko.configure do |config| end
config.vagrant_root = 'spec/support'
end end
puts vagrant_root.inspect
require_relative '../../spec/support/test_app' require_relative '../../spec/support/test_app'

View file

@ -1,5 +1,4 @@
module RemoteCommandHelpers module RemoteCommandHelpers
def test_dir_exists(path) def test_dir_exists(path)
exists?('d', path) exists?('d', path)
end end
@ -13,11 +12,11 @@ module RemoteCommandHelpers
end end
def exists?(type, path) def exists?(type, path)
%{[ -#{type} "#{path}" ] && echo "#{path} exists." || echo "Error: #{path} does not exist."} %{[ -#{type} "#{path}" ]}
end end
def safely_remove_file(path) def safely_remove_file(path)
run_vagrant_command("rm #{test_file}") rescue Vagrant::Errors::VagrantError run_vagrant_command("rm #{test_file}") rescue VagrantHelpers::VagrantSSHCommandError
end end
end end

View file

@ -0,0 +1,35 @@
module VagrantHelpers
extend self
class VagrantSSHCommandError < RuntimeError; end
at_exit do
if ENV['KEEP_RUNNING']
puts "Vagrant vm will be left up because KEEP_RUNNING is set."
puts "Rerun without KEEP_RUNNING set to cleanup the vm."
else
vagrant_cli_command("destroy -f")
end
end
def vagrant_cli_command(command)
puts "[vagrant] #{command}"
Dir.chdir(VAGRANT_ROOT) do
`#{VAGRANT_BIN} #{command} 2>&1`.split("\n").each do |line|
puts "[vagrant] #{line}"
end
end
$?
end
def run_vagrant_command(command)
if (status = vagrant_cli_command("ssh -c #{command.inspect}")).success?
true
else
fail VagrantSSHCommandError, status
end
end
end
World(VagrantHelpers)

View file

@ -1,4 +1,6 @@
require 'fileutils' require 'fileutils'
require 'pathname'
module TestApp module TestApp
extend self extend self