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

Feature/default playbook path #64

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ group :development do
gem 'rubocop'
gem 'simplecov'
gem 'test-kitchen'
gem 'fakefs'
end

2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ GEM
docile (1.3.5)
ed25519 (1.2.4)
erubi (1.10.0)
fakefs (1.3.2)
ffi (1.14.2)
gssapi (1.3.1)
ffi (>= 1.0.1)
Expand Down Expand Up @@ -152,6 +153,7 @@ PLATFORMS
ruby

DEPENDENCIES
fakefs
kitchen-ansiblepush!
kitchen-docker
pry
Expand Down
15 changes: 11 additions & 4 deletions lib/kitchen/provisioner/ansible_push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ class AnsiblePush < Base
# For tests disable if not needed
default_config :chef_bootstrap_url, 'https://omnitruck.chef.io/install.sh'

expand_path_for :playbook
expand_path_for :vault_password_file
expand_path_for :ansible_config

# Validates the config and returns it. Has side-effect of
# possibly setting @extra_vars which doesn't seem to be used
def conf
return @validated_config if defined? @validated_config

raise UserError, 'No playbook defined. Please specify one in .kitchen.yml' unless config[:playbook]

raise UserError, "playbook '#{config[:playbook]}' could not be found. Please check path" unless File.exist?(config[:playbook])
raise UserError, "playbook '#{config[:playbook]}' could not be found. Please check path" unless playbook && File.exist?(playbook)
info("Using #{playbook} playbook to converge")

if config[:vault_password_file] && !File.exist?(config[:vault_password_file])
raise UserError, "Vault password '#{config[:vault_password_file]}' could not be found. Please check path"
Expand Down Expand Up @@ -99,6 +102,10 @@ def conf
@validated_config = config
end

def playbook
@playbook ||= config[:playbook] || calculate_path("converge.yml", :type => :file)
end

def machine_name
return @machine_name if defined? @machine_name
@machine_name = if config[:use_instance_name]
Expand Down Expand Up @@ -170,7 +177,7 @@ def options
def command
return @command if defined? @command
@command = [conf[:ansible_playbook_bin]]
@command = (@command << options << conf[:playbook]).flatten.join(' ')
@command = (@command << options << playbook).flatten.join(' ')
debug("Ansible push command= #{@command}")
@command
end
Expand Down
12 changes: 6 additions & 6 deletions spec/kitchen/provisioner/ansible_push_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
let(:config) do
{
test_base_path: '/b',
kitchen_root: '/r',
kitchen_root: kitchen_root,
log_level: :info,
playbook: 'spec/assets/ansible_test.yml',
playbook: 'ansible_test.yml',
generate_inv: false,
remote_user: 'test',
sudo: true,
Expand All @@ -46,9 +46,9 @@
let(:config) do
{
test_base_path: '/b',
kitchen_root: '/r',
kitchen_root: kitchen_root,
log_level: :info,
playbook: 'spec/assets/ansible_test.yml',
playbook: 'ansible_test.yml',
generate_inv: false,
remote_user: 'test2',
become: true,
Expand Down Expand Up @@ -102,9 +102,9 @@
let(:config) do
{
test_base_path: '/b',
kitchen_root: '/r',
kitchen_root: kitchen_root,
log_level: :info,
playbook: 'spec/assets/ansible_test.yml',
playbook: 'ansible_test.yml',
generate_inv: false,
remote_user: 'test2',
become: true,
Expand Down
49 changes: 41 additions & 8 deletions spec/kitchen/provisioner/ansible_push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'kitchen'
require 'kitchen/provisioner/ansible_push'
require 'kitchen/errors'
require 'fakefs/safe'

describe Kitchen::Provisioner::AnsiblePush do
let(:logged_output) { StringIO.new }
Expand All @@ -27,10 +28,6 @@
instance_double('Kitchen::Instance', name: 'coolbeans', logger: logger, suite: suite, platform: platform)
end

# let(:machine_name) do
# 'test'
# end

let(:provisioner) do
Kitchen::Provisioner::AnsiblePush.new(config).finalize_config!(instance)
end
Expand All @@ -39,17 +36,45 @@
expect(provisioner.diagnose_plugin[:api_version]).to eq(2)
end

it 'should find playbook file in <test_base_path>/<suite>' do
FakeFS.with_fresh do
create_playbook("/b/fries/converge.yml")
expect(provisioner.playbook).to match("fries/converge.yml")
end
end

it 'should find playbook file in <test_base_path>' do
FakeFS.with_fresh do
create_playbook("/b/converge.yml")
expect(provisioner.playbook).to match("converge.yml")
end
end

it 'should find playbook file in <test_base_path>' do
FakeFS.with_fresh do
create_playbook("/b/converge.yml")
expect(provisioner.playbook).to match("converge.yml")
end
end

it 'should find playbook file in current dir' do
FakeFS.with_fresh do
create_playbook("/cwd/converge.yml")
expect(provisioner.playbook).to match("cwd/converge.yml")
end
end

it 'Should fail with no playbook file' do
expect { provisioner.prepare_command }.to raise_error(Kitchen::UserError)
end

describe 'Baisc config' do
describe 'Basic config' do
let(:config) do
{
test_base_path: '/b',
kitchen_root: '/r',
kitchen_root: kitchen_root,
log_level: :info,
playbook: 'spec/assets/ansible_test.yml',
playbook: 'ansible_test.yml',
generate_inv: false,
remote_user: 'test'
}
Expand All @@ -58,10 +83,18 @@
expect(provisioner.prepare_command).to be
end
it 'should set playbookname' do
expect(config[:playbook]).to match('spec/assets/ansible_test.yml')
expect(config[:playbook]).to match('ansible_test.yml')
end
it 'User should be set' do
expect(config[:remote_user]).to match('test')
end
end
end

def create_playbook(playbook_path)
Dir.mkdir("/r")
Dir.mkdir("/b")
Dir.mkdir("/cwd")
FakeFS::FileSystem.add(playbook_path, FakeFS::FakeFile.new)
FakeFS::FileSystem.chdir("/cwd")
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
config.tty = true
config.color = true
end

def kitchen_root
$spec_dir ||= File.expand_path(File.join(__dir__, 'assets'))
end