Extract cucumber env and steps to ease code reuse
producer extensions like producer-rails must not reimplement all the testing infrastructure, this extraction allows to setup everything with just one require call in cucumber env: require 'producer/core/testing/cucumber'
This commit is contained in:
24
lib/producer/core/testing/aruba_program_wrapper.rb
Normal file
24
lib/producer/core/testing/aruba_program_wrapper.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
module Producer
|
||||
module Core
|
||||
module Testing
|
||||
class ArubaProgramWrapper
|
||||
def initialize(argv, stdin = $stdin, stdout = $stdout, stderr = $stderr,
|
||||
kernel = Kernel)
|
||||
@argv = argv
|
||||
@stdin = stdin
|
||||
@stdout = stdout
|
||||
@stderr = stderr
|
||||
@kernel = kernel
|
||||
end
|
||||
|
||||
def execute!
|
||||
Producer::Core::CLI.run!(
|
||||
@argv.dup, stdin: @stdin, stdout: @stdout, stderr: @stderr
|
||||
)
|
||||
rescue SystemExit => e
|
||||
@kernel.exit e.status
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
39
lib/producer/core/testing/cucumber.rb
Normal file
39
lib/producer/core/testing/cucumber.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
require 'aruba/cucumber'
|
||||
require 'aruba/in_process'
|
||||
|
||||
require 'producer/core'
|
||||
require 'producer/core/testing/aruba_program_wrapper'
|
||||
require 'producer/core/testing/cucumber/etc_steps'
|
||||
require 'producer/core/testing/cucumber/execution_steps'
|
||||
require 'producer/core/testing/cucumber/output_steps'
|
||||
require 'producer/core/testing/cucumber/recipe_steps'
|
||||
require 'producer/core/testing/cucumber/remote_steps'
|
||||
require 'producer/core/testing/cucumber/ssh_steps'
|
||||
|
||||
# Raise aruba default timeout so test suite can run on a slow machine.
|
||||
Before do
|
||||
@aruba_timeout_seconds = 8
|
||||
end
|
||||
|
||||
# Use aruba "in process" optimization only for scenarios not tagged @exec.
|
||||
# We need a real process in a few cases: real program name, interactive usage…
|
||||
Before('@exec') do
|
||||
Aruba.process = Aruba::SpawnProcess
|
||||
end
|
||||
|
||||
Before('~@exec') do
|
||||
Aruba::InProcess.main_class = Producer::Core::Testing::ArubaProgramWrapper
|
||||
Aruba.process = Aruba::InProcess
|
||||
end
|
||||
|
||||
# Fake home directory for @fake_home tagged scenarios.
|
||||
Before('@fake_home') do
|
||||
ENV['HOME'] = File.expand_path(current_dir)
|
||||
end
|
||||
|
||||
# Enable cucumber-sshd "fast" mode (persists sshd across scenarios), and
|
||||
# register hooks for @sshd tagged scenarios.
|
||||
Before do
|
||||
@_sshd_fast = true
|
||||
end
|
||||
require 'cucumber/sshd/cucumber'
|
3
lib/producer/core/testing/cucumber/etc_steps.rb
Normal file
3
lib/producer/core/testing/cucumber/etc_steps.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
Then /^the output must contain my current login name$/ do
|
||||
assert_partial_output Etc.getlogin, all_output
|
||||
end
|
3
lib/producer/core/testing/cucumber/execution_steps.rb
Normal file
3
lib/producer/core/testing/cucumber/execution_steps.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
Then /^the exit status must be (\d+)$/ do |exit_status|
|
||||
assert_exit_status exit_status.to_i
|
||||
end
|
35
lib/producer/core/testing/cucumber/output_steps.rb
Normal file
35
lib/producer/core/testing/cucumber/output_steps.rb
Normal file
@@ -0,0 +1,35 @@
|
||||
Then /^the output must match \/([^\/]+)\/$/ do |pattern|
|
||||
assert_matching_output pattern, all_output
|
||||
end
|
||||
|
||||
Then /^the output must contain "([^"]+)"$/ do |content|
|
||||
assert_partial_output content, all_output
|
||||
end
|
||||
|
||||
Then /^the output must contain:$/ do |content|
|
||||
assert_partial_output content, all_output
|
||||
end
|
||||
|
||||
Then /^the output must not contain "([^"]+)"$/ do |content|
|
||||
assert_no_partial_output content, all_output
|
||||
end
|
||||
|
||||
Then /^the output must contain exactly "([^"]+)"$/ do |content|
|
||||
assert_exact_output content, all_output
|
||||
end
|
||||
|
||||
Then /^the output must contain exactly:$/ do |content|
|
||||
assert_exact_output content, all_output
|
||||
end
|
||||
|
||||
Then /^the error output must contain exactly "([^"]+)"$/ do |content|
|
||||
assert_exact_output content, all_stderr
|
||||
end
|
||||
|
||||
Then /^the output must contain ruby lib directory$/ do
|
||||
assert_partial_output RbConfig::CONFIG['rubylibdir'], all_output
|
||||
end
|
||||
|
||||
Then /^the output must not contain ruby lib directory$/ do
|
||||
assert_no_partial_output RbConfig::CONFIG['rubylibdir'], all_output
|
||||
end
|
70
lib/producer/core/testing/cucumber/recipe_steps.rb
Normal file
70
lib/producer/core/testing/cucumber/recipe_steps.rb
Normal file
@@ -0,0 +1,70 @@
|
||||
def run_recipe(remote: false, options: nil, check: false, rargv: nil)
|
||||
command = %w[producer recipe.rb]
|
||||
case remote
|
||||
when :unknown then command += %w[-t unknown_host.test]
|
||||
when true then command += %w[-t some_host.test]
|
||||
end
|
||||
command << options if options
|
||||
command << ['--', *rargv] if rargv
|
||||
|
||||
run_simple command.join(' '), false
|
||||
|
||||
assert_exit_status 0 if check
|
||||
assert_matching_output '\ASocketError', all_output if remote == :unknown
|
||||
end
|
||||
|
||||
Given /^a recipe with:$/ do |recipe_body|
|
||||
write_file 'recipe.rb', recipe_body
|
||||
end
|
||||
|
||||
Given /^a recipe with an error$/ do
|
||||
write_file 'recipe.rb', "fail 'some error'\n"
|
||||
end
|
||||
|
||||
Given /^a recipe using a remote$/ do
|
||||
write_file 'recipe.rb', "task(:some_task) { sh 'echo hello' }\n"
|
||||
end
|
||||
|
||||
Given /^a recipe named "([^"]+)" with:$/ do |recipe_path, recipe_body|
|
||||
write_file recipe_path, recipe_body
|
||||
end
|
||||
|
||||
When /^I execute the recipe$/ do
|
||||
run_recipe
|
||||
end
|
||||
|
||||
When /^I execute the recipe on remote target$/ do
|
||||
run_recipe remote: true
|
||||
end
|
||||
|
||||
When /^I execute the recipe on unknown remote target$/ do
|
||||
run_recipe remote: :unknown
|
||||
end
|
||||
|
||||
When /^I execute the recipe with options? (-.+)$/ do |options|
|
||||
run_recipe options: options
|
||||
end
|
||||
|
||||
When /^I execute the recipe on unknown remote target with options? (-.+)$/ do |options|
|
||||
run_recipe remote: :unknown, options: options
|
||||
end
|
||||
|
||||
When /^I successfully execute the recipe$/ do
|
||||
run_recipe check: true
|
||||
end
|
||||
|
||||
When /^I successfully execute the recipe on remote target$/ do
|
||||
run_recipe remote: true, check: true
|
||||
end
|
||||
|
||||
When /^I successfully execute the recipe with option? (-.+)$/ do |options|
|
||||
run_recipe options: options, check: true
|
||||
end
|
||||
|
||||
When /^I successfully execute the recipe with arguments "([^"]+)"$/ do |rargv|
|
||||
run_recipe rargv: rargv, check: true
|
||||
end
|
||||
|
||||
When /^I execute the recipe interactively$/ do
|
||||
run_interactive 'producer recipe.rb'
|
||||
end
|
41
lib/producer/core/testing/cucumber/remote_steps.rb
Normal file
41
lib/producer/core/testing/cucumber/remote_steps.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
def stat_mode(path)
|
||||
in_current_dir do
|
||||
('%o' % [File::Stat.new(path).mode])[-4, 4]
|
||||
end
|
||||
end
|
||||
|
||||
Given /^a remote directory named "([^"]+)"$/ do |path|
|
||||
create_dir path
|
||||
end
|
||||
|
||||
Given /^a remote file named "([^"]+)"$/ do |file_name|
|
||||
write_file file_name, ''
|
||||
end
|
||||
|
||||
Given /^a remote file named "([^"]+)" with "([^"]+)"$/ do |file_name, content|
|
||||
write_file file_name, content
|
||||
end
|
||||
|
||||
Then /^the remote directory "([^"]+)" must exist$/ do |path|
|
||||
check_directory_presence [path], true
|
||||
end
|
||||
|
||||
Then /^the remote file "([^"]+)" must contain "([^"]+)"$/ do |path, content|
|
||||
check_file_content path, content, true
|
||||
end
|
||||
|
||||
Then /^the remote file "([^"]+)" must contain exactly "([^"]+)"$/ do |path, content|
|
||||
check_file_content path, content
|
||||
end
|
||||
|
||||
Then /^the remote file "([^"]+)" must match \/([^\/]+)\/$/ do |path, pattern|
|
||||
check_file_content path, /#{pattern}/, true
|
||||
end
|
||||
|
||||
Then /^the remote file "([^"]+)" must have (\d+) mode$/ do |path, mode|
|
||||
expect(stat_mode path).to eq mode
|
||||
end
|
||||
|
||||
Then /^the remote directory "([^"]+)" must have (\d+) mode$/ do |path, mode|
|
||||
expect(stat_mode path).to eq mode
|
||||
end
|
3
lib/producer/core/testing/cucumber/ssh_steps.rb
Normal file
3
lib/producer/core/testing/cucumber/ssh_steps.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
Given /^an SSH config with:$/ do |config|
|
||||
write_file '.ssh/config', config
|
||||
end
|
Reference in New Issue
Block a user