Project

General

Profile

Download (8.13 KB) Statistics
| Branch: | Tag: | Revision:
ed2c683b Ivan Nečas
require 'test_helper'

module ForemanMaintain
describe Reporter::CLIReporter do
9f048ccd Ivan Nečas
include CliAssertions

ed2c683b Ivan Nečas
let :capture do
StringIO.new
end

ef9ee7ac Ivan Nečas
let :fake_stdin do
StringIO.new
end

ed2c683b Ivan Nečas
let :reporter do
ef9ee7ac Ivan Nečas
Reporter::CLIReporter.new(capture, fake_stdin)
ed2c683b Ivan Nečas
end

let :scenario do
1fc13b23 Ivan Nečas
Scenarios::PresentUpgrade.new
ed2c683b Ivan Nečas
end

ea60b444 Ivan Nečas
let(:warn_scenario) do
Scenarios::Dummy::Warn.new
38a0b341 Swapnil Abnave
end

ea60b444 Ivan Nečas
let(:fail_scenario) do
Scenarios::Dummy::Fail.new
end
ed2c683b Ivan Nečas
ea60b444 Ivan Nečas
let(:warn_and_fail_scenario) do
Scenarios::Dummy::WarnAndFail.new
end
ed2c683b Ivan Nečas
ea60b444 Ivan Nečas
def decision_question(description)
"Continue with step [#{description}]?, [y(yes), n(no), q(quit)]"
end
ed2c683b Ivan Nečas
ea60b444 Ivan Nečas
it 'reports human-readable info about the run' do
reporter.before_scenario_starts(fail_scenario)
run_scenario(fail_scenario)
ed2c683b Ivan Nečas
bdce5057 Swapnil Abnave
assert_equal <<-STR.strip_heredoc, captured_out
ea60b444 Ivan Nečas
Running Scenarios::Dummy::Fail
3b966152 Ivan Nečas
================================================================================
ea60b444 Ivan Nečas
check that ends up with fail: [FAIL]
this check is always causing failure
bdce5057 Swapnil Abnave
--------------------------------------------------------------------------------
ea60b444 Ivan Nečas
check that ends up with success: [OK]
bdce5057 Swapnil Abnave
--------------------------------------------------------------------------------
STR
ed2c683b Ivan Nečas
end

ef9ee7ac Ivan Nečas
it 'asks about the next steps' do
will_press('y')
163e3fff Ivan Nečas
start_step = Procedures::PresentServiceStart.new
restart_step = Procedures::PresentServiceRestart.new
019ca085 Ivan Nečas
reporter.on_next_steps([start_step])
38a0b341 Swapnil Abnave
assert_equal decision_question('start the present service'), captured_out(false).strip
ef9ee7ac Ivan Nečas
will_press('2')
019ca085 Ivan Nečas
assert_equal restart_step, reporter.on_next_steps([start_step, restart_step])
bdce5057 Swapnil Abnave
assert_equal <<-STR.strip_heredoc.strip, captured_out(false).strip
There are multiple steps to proceed:
1) start the present service
2) restart present service
38a0b341 Swapnil Abnave
Select step to continue, [n(next), q(quit)]
bdce5057 Swapnil Abnave
STR
ef9ee7ac Ivan Nečas
will_press('q')
019ca085 Ivan Nečas
assert_equal :quit, reporter.on_next_steps([start_step, restart_step])
ef9ee7ac Ivan Nečas
end

ea60b444 Ivan Nečas
it 'informs the user about failures of the last scenario' do
run_scenario(fail_scenario)
reporter.after_scenario_finishes(fail_scenario)
assert_equal <<-MESSAGE.strip_heredoc.strip, captured_out(false).strip
check that ends up with fail: [FAIL]
this check is always causing failure
--------------------------------------------------------------------------------
check that ends up with success: [OK]
--------------------------------------------------------------------------------
Scenario [Scenarios::Dummy::Fail] failed.

The following steps ended up in failing state:

[dummy-check-fail]

Resolve the failed steps and rerun
the command. In case the failures are false positives,
use --whitelist=\"dummy-check-fail\"
MESSAGE
end

it 'informs the user about warnings of the last scenario' do
run_scenario(warn_scenario)
reporter.after_scenario_finishes(warn_scenario)
assert_equal <<-MESSAGE.strip_heredoc.strip, captured_out(false).strip
check that ends up with warning: [WARNING]
this check is always causing warnings
--------------------------------------------------------------------------------
check that ends up with success: [OK]
--------------------------------------------------------------------------------
Scenario [Scenarios::Dummy::Warn] failed.

The following steps ended up in warning state:

[dummy-check-warn]

The steps in warning state itself might not mean there is an error,
but it should be reviews to ensure the behavior is expected
MESSAGE
end

181810b0 Ivan Nečas
it 'informs the user about warnings and failures of the last scenario' do
ea60b444 Ivan Nečas
run_scenario(warn_and_fail_scenario)
reporter.after_scenario_finishes(warn_and_fail_scenario)
assert_equal <<-MESSAGE.strip_heredoc.strip, captured_out(false).strip
check that ends up with warning: [WARNING]
this check is always causing warnings
--------------------------------------------------------------------------------
check that ends up with fail: [FAIL]
this check is always causing failure
--------------------------------------------------------------------------------
check that ends up with success: [OK]
--------------------------------------------------------------------------------
Scenario [Scenarios::Dummy::WarnAndFail] failed.

The following steps ended up in failing state:

[dummy-check-fail]

The following steps ended up in warning state:

[dummy-check-warn]

Resolve the failed steps and rerun
the command. In case the failures are false positives,
use --whitelist=\"dummy-check-fail\"

The steps in warning state itself might not mean there is an error,
but it should be reviews to ensure the behavior is expected
MESSAGE
end

181810b0 Ivan Nečas
it 'ignores whitelisted warnings and failures of the last scenario' do
run_scenario(warn_and_fail_scenario, :whitelisted => true)
reporter.after_scenario_finishes(warn_and_fail_scenario)
assert_equal <<-MESSAGE.strip_heredoc.strip, captured_out(false).strip
check that ends up with warning: [WARNING]
this check is always causing warnings
--------------------------------------------------------------------------------
check that ends up with fail: [FAIL]
this check is always causing failure
--------------------------------------------------------------------------------
check that ends up with success: [OK]
--------------------------------------------------------------------------------
MESSAGE
end

afbe9e6e Ivan Nečas
describe 'assumeyes' do
let(:reporter) do
Reporter::CLIReporter.new(capture, fake_stdin, :assumeyes => true)
end

it 'answers yes when assumeyes is active' do
start_step = Procedures::PresentServiceStart.new
reporter.on_next_steps([start_step])
assert_match 'start the present service', captured_out(false).strip
assert_match 'assuming yes', captured_out(false).strip
end

it 'chooses the first option when multiple options are present' do
start_step = Procedures::PresentServiceStart.new
restart_step = Procedures::PresentServiceRestart.new
assert_equal start_step, reporter.on_next_steps([start_step, restart_step])
assert_equal <<-STR.strip_heredoc.strip, captured_out(false).strip
There are multiple steps to proceed:
1) start the present service
2) restart present service
(assuming first option)
STR
end
end

38a0b341 Swapnil Abnave
describe 'skip_to_next' do
it 'option N/n is to skip the current prompted step' do
restart_step = Procedures::PresentServiceRestart.new

will_press('n')
019ca085 Ivan Nečas
assert_equal :no, reporter.on_next_steps([restart_step])
38a0b341 Swapnil Abnave
assert_equal decision_question('restart present service'), captured_out(false).strip
end
end

ef9ee7ac Ivan Nečas
def will_press(string)
fake_stdin.rewind
fake_stdin.puts(string)
fake_stdin.rewind
end

9f048ccd Ivan Nečas
def captured_out(simulate_terminal = true)
ed2c683b Ivan Nečas
capture.rewind
# simulate carriage returns to get the output as user would see it
ef9ee7ac Ivan Nečas
out = capture.read
9f048ccd Ivan Nečas
out = simulate_carriage_returns(out) if simulate_terminal
f9d53393 Ivan Nečas
out = remove_colors(out)
ef9ee7ac Ivan Nečas
capture.rewind
ed2c683b Ivan Nečas
out
end
ea60b444 Ivan Nečas
181810b0 Ivan Nečas
def run_scenario(scenario, options = {})
ea60b444 Ivan Nečas
scenario.steps.each do |step|
181810b0 Ivan Nečas
ForemanMaintain::Runner::Execution.new(step, reporter, options).tap(&:run)
ea60b444 Ivan Nečas
end
end
ed2c683b Ivan Nečas
end
end