Skip to content

Commit

Permalink
Merge pull request #37 from ahelal/feature/inventory_tests
Browse files Browse the repository at this point in the history
Feature/inventory tests
  • Loading branch information
ahelal committed Jan 1, 2017
2 parents 7d06c22 + e786a4f commit 8dcc750
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 168 deletions.
6 changes: 3 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ NumericLiterals:

# Offense count: 6
Metrics/AbcSize:
Max: 71
Max: 120

# Offense count: 1
# Configuration parameters: CountComments.
Expand All @@ -28,8 +28,8 @@ Metrics/ClassLength:

# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 18
Max: 30

# Offense count: 3
Metrics/PerceivedComplexity:
Max: 19
Max: 30
12 changes: 6 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
source "https://rubygems.org"

gemspec
source 'https://rubygems.org'
group :development do
gem 'test-kitchen'
gem 'kitchen-docker'
gem 'kitchen-localhost'
gem 'rubocop', require: false
gem 'simplecov', :require => false
gem 'pry'
gem 'rspec'
gem 'rubocop'
gem 'simplecov'
gem 'test-kitchen'
end

45 changes: 3 additions & 42 deletions bin/kitchen-ansible-inventory
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
#!/usr/bin/env ruby
require 'yaml'
require 'json'
require 'kitchen-ansible/util_inventory.rb'

all = []
groups = Hash.new
hosts = Hash.new
if File.exist?(TEMP_GROUP_FILE)
groups = YAML::load_file TEMP_GROUP_FILE
end
require 'kitchen-ansible/print_inventory_cli.rb'

Dir.glob(TEMP_INV_DIR + '/ansiblepush_host_*.yml') do |inv_yml|
vm = YAML::load_file inv_yml
vm.each do |host, host_attr|
if host_attr["mygroup"]
if host_attr["mygroup"].is_a?(Hash)
host_attr["mygroup"].each do | group |
groups[group] ||= []
groups[group] << host
end
elsif host_attr["mygroup"].kind_of?(String)
groups[host_attr["mygroup"]] ||= []
groups[host_attr["mygroup"]] << host
elsif host_attr["mygroup"].kind_of?(Array)
host_attr["mygroup"].each do | group |
groups[group] ||= []
groups[group] << host
end
end
end
host_attr.delete("mygroup")
hosts[host] = host_attr
all << host
end
end

inventory = { "all" => all, "_meta" => { "hostvars" => hosts }}



inventory = groups.merge(inventory)

# Print our inventory
puts JSON.pretty_generate(inventory)
print_inventory = PrintInventory.new
print_inventory.run
4 changes: 2 additions & 2 deletions callback/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self):
os.remove(self.change_file)
except OSError as e: # this would be "except OSError, e:" before Python 2.6
if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
raise # re-raise exception if a different error occured
raise # re-raise exception if a different error occurred


def write_changed_to_file(self, host, res, name=None):
Expand Down Expand Up @@ -108,4 +108,4 @@ def playbook_on_play_start(self, name):
pass

def playbook_on_stats(self, stats):
pass
print "Call back end"
2 changes: 0 additions & 2 deletions kitchen-ansiblepush.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,4 @@ Supports running ansible in push mode
EOF

gem.add_runtime_dependency 'test-kitchen', '~> 1.4'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'pry'
end
30 changes: 30 additions & 0 deletions lib/kitchen-ansible/idempotancy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'securerandom'

def idempotency_test
info('*************** idempotency test ***************')
file_path = "/tmp/kitchen_ansible_callback/#{SecureRandom.uuid}.changes"
exec_ansible_command(
command_env.merge(
'ANSIBLE_CALLBACK_PLUGINS' => "#{File.dirname(__FILE__)}/../../callback/",
'ANSIBLE_CALLBACK_WHITELIST' => 'changes',
'PLUGIN_CHANGES_FILE' => file_path
), command, 'ansible-playbook'
)
debug("idempotency file #{file_path}")
# Check ansible callback if changes has occured in the second run
if File.file?(file_path)
task = 0
info('idempotency test [Failed]')
File.open(file_path, 'r') do |f|
f.each_line do |line|
task += 1
info(" #{task}> #{line.strip}")
end
end
raise "idempotency test Failed. Number of non idempotent tasks: #{task}" if conf[:fail_non_idempotent]
# If we reach this point we should give a warning
info('Warning idempotency test [failed]')
else
info('idempotency test [passed]')
end
end
64 changes: 64 additions & 0 deletions lib/kitchen-ansible/print_inventory_cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env ruby
require 'yaml'
require 'json'
require 'kitchen-ansible/util_inventory.rb'

class PrintInventory
def initialize
@inventory = {}
@all = []
@groups = if File.exist?(TEMP_GROUP_FILE)
read_from_yaml TEMP_GROUP_FILE
else
{}
end
@hosts = {}
end

def read_from_yaml(yaml_file)
YAML.load_file yaml_file
end

def read_all_hosts
Dir.glob(TEMP_INV_DIR + '/ansiblepush_host_*.yml')
end

def construct
read_all_hosts.each do |inv_yml|
vm = read_from_yaml inv_yml
vm.each do |host, host_attr|
if host_attr['mygroup']
if host_attr['mygroup'].is_a?(Hash)
host_attr['mygroup'].each do |group|
@groups[group] ||= []
@groups[group] << host
end
elsif host_attr['mygroup'].is_a?(String)
@groups[host_attr['mygroup']] ||= []
@groups[host_attr['mygroup']] << host
elsif host_attr['mygroup'].is_a?(Array)
host_attr['mygroup'].each do |group|
@groups[group] ||= []
@groups[group] << host
end
end
end
host_attr.delete('mygroup')
@hosts[host] = host_attr
@all << host
end
end

@inventory = { 'all' => @all, '_meta' => { 'hostvars' => @hosts } }
@groups.merge(@inventory)
end

def output_json
puts JSON.pretty_generate(@inventory)
end

def run
@inventory = construct
output_json
end
end
2 changes: 1 addition & 1 deletion lib/kitchen-ansible/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Kitchen
module AnsiblePush
VERSION = '0.5.3'.freeze
VERSION = '0.6.0'.freeze
end
end
Loading

0 comments on commit 8dcc750

Please sign in to comment.