-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from ahelal/feature/inventory_tests
Feature/inventory tests
- Loading branch information
Showing
15 changed files
with
307 additions
and
168 deletions.
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
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 | ||
|
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,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 |
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,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 |
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,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 |
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,5 +1,5 @@ | ||
module Kitchen | ||
module AnsiblePush | ||
VERSION = '0.5.3'.freeze | ||
VERSION = '0.6.0'.freeze | ||
end | ||
end |
Oops, something went wrong.