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

Improve selinux support #243

Merged
merged 4 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@
'0.5.2_windows_386' => '2e866812de16f1a6138a0fd1eebc76143f1314826e3b52597a55ac510ae94be6',
'0.5.2_web_ui' => 'ad883aa52e1c0136ab1492bbcedad1210235f26d59719fb6de3ef6464f1ff3b1'
}

###
# selinux
###

default['selinux']['state'] = 'permissive'

This comment was marked as outdated.

2 changes: 1 addition & 1 deletion recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# Copyright 2014, 2015 Bloomberg Finance L.P.
#
include_recipe 'selinux::disabled' if node['os'] == 'linux'
include_recipe 'selinux::default' if node['os'] == 'linux'

if node['firewall']['allow_consul']
include_recipe 'firewall::default'
Expand Down
55 changes: 55 additions & 0 deletions test/spec/recipes/default_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'spec_helper'

describe 'consul::default' do
context 'When all attributes are default, on an unspecified platform' do
let(:chef_run) do
ChefSpec::SoloRunner.new.converge(described_recipe)
end

it 'converges successfully' do
chef_run # This should not raise an error
end
end

context 'When selinux is set to be permissive, on a RHEL distribution' do
let(:chef_run) do
ChefSpec::SoloRunner.new do |node, server|
node.automatic['os'] = 'linux'
node.automatic['platform_family'] = 'rhel'
node.set['selinux']['state'] = 'permissive'
end.converge(described_recipe)
end

it 'selinux_state action is permissive' do
expect(chef_run).to permissive_selinux_state('SELinux Permissive')
end
end

context 'When selinux is set to be disabled, on a RHEL distribution' do
let(:chef_run) do
ChefSpec::SoloRunner.new do |node, server|
node.automatic['os'] = 'linux'
node.automatic['platform_family'] = 'rhel'
node.set['selinux']['state'] = 'disabled'
end.converge(described_recipe)
end

it 'selinux_state action is disabled' do
expect(chef_run).to disabled_selinux_state('SELinux Disabled')
end
end

context 'When selinux is set to be enforcing, on a RHEL distribution' do
let(:chef_run) do
ChefSpec::SoloRunner.new do |node, server|
node.automatic['os'] = 'linux'
node.automatic['platform_family'] = 'rhel'
node.set['selinux']['state'] = 'enforcing'
end.converge(described_recipe)
end

it 'selinux_state action is enforcing' do
expect(chef_run).to enforcing_selinux_state('SELinux Enforcing')
end
end
end
1 change: 1 addition & 0 deletions test/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'chefspec'
require 'chefspec/berkshelf'
require 'poise_boiler/spec_helper'
require_relative('support/chefspec_extensions/automatic_resource_matcher')
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Modified for styling
module ChefSpec
# https://github.com/lynx44/chefspec_extensions
module AutomaticResourceMatcher
def method_missing(meth, *args, &block)
method_name = meth.to_s
if resource_matcher_candidate?(method_name, args)
cookbook_candidates = get_cookbook_candidates(method_name)
cookbook_matches = find_cookbooks_with_matching_resources(cookbook_candidates, method_name)
if cookbook_matches.length == 1
cookbook = cookbook_matches.first
return create_matcher(args, cookbook[:name], method_name)
end
end
super
end

private

def resource_matcher_candidate?(method_name, args)
method_name.count('_') >= 1 && args.length == 1
end

def cookbooks
@@cookbooks ||=
cookbook_paths
.map { |cookbook_path| Dir.glob("#{cookbook_path}/*") }
.flatten
.select { |c| File.directory? c }
.map { |c| { name: Pathname.new(c).basename.to_s, path: c } }
.flatten
end

def cookbook_paths
Chef::Config[:cookbook_path].is_a?(Array) ? Chef::Config[:cookbook_path] : [Chef::Config[:cookbook_path]]
end

def get_cookbook_candidates(method_name)
cookbooks.select { |c| method_name.include? c[:name] }
end

def parse_lwrp(cookbook, method_name)
if (method_name.count('_') == 1)
method_name = "#{method_name}_default"
end

parts = method_name.split("_#{cookbook}_")
{ action: parts[0], cookbook: cookbook, resource_name: parts[1] }
end

def find_cookbooks_with_matching_resources(cookbook_candidates, method_name)
cookbook_matches = []
cookbook_candidates.each do |cookbook|
resource_parts = parse_lwrp(cookbook[:name], method_name)
cookbook_matches.push(cookbook) if cookbook_has_resource?(cookbook, resource_parts[:resource_name])
end
cookbook_matches
end

def cookbook_has_resource?(cookbook, resource_name)
Dir.glob("#{cookbook[:path]}/resources/#{resource_name}.rb").length == 1
end

def create_matcher(args, cookbook, method_name)
resource_definition = parse_lwrp(cookbook, method_name)
resource_name = "#{cookbook}_#{resource_definition[:resource_name]}"
if (resource_definition[:resource_name] == 'default')
resource_name = cookbook
end
ChefSpec::Matchers::ResourceMatcher.new(resource_name.to_sym, resource_definition[:action].to_sym, args[0])
end
end
end

RSpec.configure do |c|
c.include ChefSpec::AutomaticResourceMatcher
end