Skip to content

Commit

Permalink
Fix the firewalld_version fact
Browse files Browse the repository at this point in the history
* Work around a bug in firewall-cmd and use raw python to get the
  firewalld version if firewalld-cmd does not return anything
* Incorporated the simp-beaker-helpers gem to get access to the helper
  methods

Fixes voxpupuli#263
  • Loading branch information
trevor-vaughan committed Feb 28, 2020
1 parent a1f6ec1 commit befefc3
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 23 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--format documentation
--color
--fail-fast
2 changes: 1 addition & 1 deletion .sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gemfile:
- gem: 'mocha'
version: '~> 1.10.0'
':system_tests':
- gem: 'beaker-vagrant'
- gem: 'simp-beaker-helpers'

.rubocop.yml:
default_configs:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ group :system_tests do
gem 'rbnacl-libsodium', :require => false
gem 'bcrypt_pbkdf', :require => false
gem 'ed25519', :require => false
gem 'beaker-vagrant', :require => false
gem 'simp-beaker-helpers', :require => false
end

group :release do
Expand Down
28 changes: 27 additions & 1 deletion lib/facter/firewalld_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@
confine { @firewall_cmd }

setcode do
Facter::Core::Execution.execute(%(#{@firewall_cmd} --version), on_fail: :failed).strip
def failed_value?(value)
!value || (value == :failed) || value.empty?
end

value = Facter::Core::Execution.execute(%(#{@firewall_cmd} --version), on_fail: :failed)

if failed_value?(value)
# Python gets stuck in some weird places
python = Facter::Util::Resolution.which('python')
python ||= Facter::Util::Resolution.which('platform-python')

python_path = '/usr/libexec/platform-python'
python ||= python_path if File.exist?(python_path)

# Because firewall-cmd fails if firewalld is not running
workaround_command = %{#{python} -c 'import firewall.config; print(firewall.config.__dict__["VERSION"])'}

value = Facter::Core::Execution.execute(workaround_command, on_fail: :failed)
end

if failed_value?(value)
value = nil
else
value.strip!
end

value
end
end
3 changes: 3 additions & 0 deletions rakelib/simp.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'simp/rake/beaker'

Simp::Rake::Beaker.new(File.join(File.dirname(__FILE__), '..'))
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class { 'firewalld':
test_services = on(host, 'firewall-cmd --list-services --zone=test').output.strip.split(%r{\s+})
expect(test_services).to include('test_sshd')
end

it 'returns a fact when firewalld is not running' do
on(host, 'puppet resource service firewalld ensure=stopped')
expect(pfact_on(host, 'firewalld_version')).to match(%r{^\d})
end
end
end
end
44 changes: 24 additions & 20 deletions spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
require 'beaker-rspec'
require 'pry'
require 'tmpdir'
require 'yaml'
require 'simp/beaker_helpers'
include Simp::BeakerHelpers

UNSUPPORTED_PLATFORMS = %w[windows Darwin].freeze

unless ENV['RS_PROVISION'] == 'no' || ENV['BEAKER_provision'] == 'no'

require 'beaker/puppet_install_helper'

run_puppet_install_helper('agent', ENV['PUPPET_VERSION'])

RSpec.configure do |c|
# Project root
proj_root = File.expand_path(File.join(__dir__, '..'))
unless ENV['BEAKER_provision'] == 'no'
hosts.each do |host|
# Install Puppet
if host.is_pe?
install_pe
else
install_puppet
end
end
end

# Readable test descriptions
c.formatter = :documentation
RSpec.configure do |c|
# ensure that environment OS is ready on each host
fix_errata_on hosts

# Don't burn resources if we don't have to
c.fail_fast = true
# Readable test descriptions
c.formatter = :documentation

# Configure all nodes in nodeset
c.before :suite do
hosts.each do |host|
install_dev_puppet_module_on(host, source: proj_root, module_name: 'firewalld')
install_puppet_module_via_pmt_on(host, module_name: 'puppetlabs/stdlib')
end
# Configure all nodes in nodeset
c.before :suite do
begin
# Install modules and dependencies from spec/fixtures/modules
copy_fixture_modules_to(hosts)
end
end
end
7 changes: 7 additions & 0 deletions spec/unit/facter/firewalld_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
Facter::Core::Execution.stubs(:execute).with('/usr/bin/firewall-cmd --version', on_fail: :failed).returns(firewalld_version)
end

let(:python_args) do
%{-c 'import firewall.config; print(firewall.config.__dict__["VERSION"])'}
end

context 'as a regular user' do
let(:firewalld_version) { "0.7.0\n" }

Expand All @@ -32,6 +36,9 @@
let(:firewalld_version) { :failed }

it 'does not return a fact' do
Facter::Util::Resolution.stubs(:which).with('python').returns('/usr/bin/python')
Facter::Core::Execution.stubs(:execute).with("/usr/bin/python #{python_args}", on_fail: :failed).returns(:failed)

expect(Facter.fact('firewalld_version').value).to be_nil
end
end
Expand Down

0 comments on commit befefc3

Please sign in to comment.