-
-
Notifications
You must be signed in to change notification settings - Fork 244
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
johnbellone
merged 4 commits into
sous-chefs:master
from
visioncritical:improve-selinux-support
Dec 4, 2015
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
77 changes: 77 additions & 0 deletions
77
test/spec/support/chefspec_extensions/automatic_resource_matcher.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as outdated.
Sorry, something went wrong.