diff --git a/Gemfile b/Gemfile index d69d176..7deefd0 100644 --- a/Gemfile +++ b/Gemfile @@ -7,5 +7,6 @@ group :development do gem 'rubocop' gem 'simplecov' gem 'test-kitchen' + gem 'fakefs' end diff --git a/Gemfile.lock b/Gemfile.lock index d79357b..d8363a3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -152,6 +153,7 @@ PLATFORMS ruby DEPENDENCIES + fakefs kitchen-ansiblepush! kitchen-docker pry diff --git a/lib/kitchen/provisioner/ansible_push.rb b/lib/kitchen/provisioner/ansible_push.rb index 567ade1..cb9f082 100644 --- a/lib/kitchen/provisioner/ansible_push.rb +++ b/lib/kitchen/provisioner/ansible_push.rb @@ -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" @@ -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] @@ -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 diff --git a/spec/kitchen/provisioner/ansible_push_options_spec.rb b/spec/kitchen/provisioner/ansible_push_options_spec.rb index 0a35272..282036f 100644 --- a/spec/kitchen/provisioner/ansible_push_options_spec.rb +++ b/spec/kitchen/provisioner/ansible_push_options_spec.rb @@ -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, @@ -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, @@ -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, diff --git a/spec/kitchen/provisioner/ansible_push_spec.rb b/spec/kitchen/provisioner/ansible_push_spec.rb index 6b9b9f2..a1a4aa9 100644 --- a/spec/kitchen/provisioner/ansible_push_spec.rb +++ b/spec/kitchen/provisioner/ansible_push_spec.rb @@ -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 } @@ -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 @@ -39,17 +36,45 @@ expect(provisioner.diagnose_plugin[:api_version]).to eq(2) end + it 'should find playbook file in /' 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 ' 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 ' 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' } @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f29e096..15341f0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,3 +12,7 @@ config.tty = true config.color = true end + +def kitchen_root + $spec_dir ||= File.expand_path(File.join(__dir__, 'assets')) +end