Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an apply matcher #115

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions lib/beaker-rspec/matchers/apply.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module BeakerRSpec
module Matchers
module Apply
class ApplyMatcher
# @param [Beaker::Host] host
def initialize(host)
@host = host
end

# @param [String, Pathname] manifest
def matches?(manifest)
file_path = copy_manifest(manifest)

command = Beaker::PuppetCommand.new('apply', file_path, :'detailed-exitcodes' => nil)

result = @host.exec(command, accept_all_exit_codes: true)
unless result.exit_code_in?([0, 2])
@error = "Expected to apply manifest"
@result = result
return false
end

if @idempotently
result = @host.exec(command, accept_all_exit_codes: true)
if result.exit_code != 0
@error = "Expected to apply manifest idempotently"
@result = result
return false
end
end

true
ensure
@host.rm_rf(file_path) if file_path && !file_path.empty?
end

def idempotently
@idempotently = true
self
end

def description
desc = 'apply'
desc += ' idempotently' if @idempotently
desc
end

def failure_message
message = [@error, @result.output]

begin
cmd = failure_context
rescue NameError
# Not specified
else
message << default.exec(Beaker::Command.new(cmd)).output
end

message.join("\n")
end

private

# @param [String, Pathname] manifest
def copy_manifest(manifest)
file_path = @host.tmpfile(%(apply_manifest_#{Time.now.strftime("%H%M%S%L")}.pp))

case manifest
when String
manifest
Tempfile.create do |tempfile|
tempfile.write(manifest)
tempfile.flush
@host.do_scp_to(tempfile.path, file_path, {})
end
when Pathname
@host.do_scp_to(manifest.to_s, file_path, {})
else
raise ArgumentError, 'Unsupported type'
end

file_path
end
end

def apply
ApplyMatcher.new(default)
end

def apply_on(host)
ApplyMatcher.new(host)
end
end
end
end
3 changes: 3 additions & 0 deletions lib/beaker-rspec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require 'beaker-rspec/beaker_shim'
require "beaker-rspec/helpers/serverspec"
require "beaker-rspec/matchers/apply"
include BeakerRSpec::BeakerShim

RSpec.configure do |c|
c.include BeakerRSpec::Matchers::Apply

# Enable color
c.tty = true

15 changes: 15 additions & 0 deletions spec/acceptance/example_spec.rb
Original file line number Diff line number Diff line change
@@ -29,6 +29,21 @@
step('testing that a step can be used')
end

describe "apply matcher" do
subject do
<<~PUPPET
file { '/tmp/beaker-rspec':
ensure => file,
content => 'Hello World!',
}
PUPPET
end

it { is_expected.to apply.idempotently }

specify { expect(file('/tmp/beaker-rspec')).to be_file.and(have_attributes(content: 'Hello World!')) }
end

context "has serverspec support" do
hosts.each do |node|
sshd = case node['platform']