From 6c4db8faed34cb7380cccfef8f2db958ac6d77c9 Mon Sep 17 00:00:00 2001 From: Andrey Tuzhilin Date: Thu, 28 Jan 2021 14:00:35 +0300 Subject: [PATCH 1/4] feat: default playbook path --- Gemfile | 1 + Gemfile.lock | 2 + lib/kitchen/provisioner/ansible_push.rb | 31 +++++++-- .../provisioner/ansible_push_options_spec.rb | 12 ++-- spec/kitchen/provisioner/ansible_push_spec.rb | 63 ++++++++++++++++--- spec/spec_helper.rb | 4 ++ 6 files changed, 93 insertions(+), 20 deletions(-) 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..d091054 100644 --- a/lib/kitchen/provisioner/ansible_push.rb +++ b/lib/kitchen/provisioner/ansible_push.rb @@ -61,14 +61,16 @@ 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 File.exist?(playbook) 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 +101,10 @@ def conf @validated_config = config end + def playbook + @playbook ||= config[:playbook] || calculate_path("converge.yml") + end + def machine_name return @machine_name if defined? @machine_name @machine_name = if config[:use_instance_name] @@ -170,7 +176,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 @@ -308,6 +314,23 @@ def verbosity_argument '-v' end end + + def calculate_path(path, type = :file) + unless instance + fail 'Please ensure that an instance is provided before calling calculate_path' + end + base = config[:test_base_path] + candidates = [] + candidates << File.join(base, instance.suite.name, 'ansible', path) + candidates << File.join(base, instance.suite.name, path) + candidates << File.join(base, path) + candidates << File.join(config[:kitchen_root], path) + candidates << File.join(Dir.pwd, path) + + candidates.find do |c| + type == :file ? File.file?(c) : File.directory?(c) + end + end end end 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..a4429cb 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,55 @@ expect(provisioner.diagnose_plugin[:api_version]).to eq(2) end - it 'Should fail with no playbook file' do - expect { provisioner.prepare_command }.to raise_error(Kitchen::UserError) + 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 - describe 'Baisc config' do + it 'should find playbook file in //ansible' do + FakeFS.with_fresh do + create_playbook("/b/fries/ansible/converge.yml") + expect(provisioner.playbook).to match("fries/ansible/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 find playbook file in kitchen_root' do + FakeFS.with_fresh do + create_playbook("/r/converge.yml") + expect(provisioner.playbook).to match("r/converge.yml") + end + end + + 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 +93,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 From 36b336d8e74a3d0a3c2863a84572fda7805bd6d0 Mon Sep 17 00:00:00 2001 From: Andrey Tuzhilin Date: Thu, 28 Jan 2021 14:23:45 +0300 Subject: [PATCH 2/4] chore: add log for playbook discovery --- lib/kitchen/provisioner/ansible_push.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/kitchen/provisioner/ansible_push.rb b/lib/kitchen/provisioner/ansible_push.rb index d091054..493cc00 100644 --- a/lib/kitchen/provisioner/ansible_push.rb +++ b/lib/kitchen/provisioner/ansible_push.rb @@ -71,6 +71,7 @@ def conf return @validated_config if defined? @validated_config raise UserError, "playbook '#{config[:playbook]}' could not be found. Please check path" unless File.exist?(playbook) + info("Using #{playbook} play book 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" From 905aed3fc9a44508d11c1e16188ea1e0d5522f4d Mon Sep 17 00:00:00 2001 From: Andrey Tuzhilin Date: Thu, 28 Jan 2021 14:28:10 +0300 Subject: [PATCH 3/4] typo --- lib/kitchen/provisioner/ansible_push.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kitchen/provisioner/ansible_push.rb b/lib/kitchen/provisioner/ansible_push.rb index 493cc00..51ee2b4 100644 --- a/lib/kitchen/provisioner/ansible_push.rb +++ b/lib/kitchen/provisioner/ansible_push.rb @@ -71,7 +71,7 @@ def conf return @validated_config if defined? @validated_config raise UserError, "playbook '#{config[:playbook]}' could not be found. Please check path" unless File.exist?(playbook) - info("Using #{playbook} play book to converge") + 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" From 25632f7b1cd96b31b16e8df824772c9a61dd901e Mon Sep 17 00:00:00 2001 From: Andrey Tuzhilin Date: Sat, 30 Jan 2021 00:53:54 +0300 Subject: [PATCH 4/4] fix: no playbook found error --- lib/kitchen/provisioner/ansible_push.rb | 21 ++----------------- spec/kitchen/provisioner/ansible_push_spec.rb | 14 ++----------- 2 files changed, 4 insertions(+), 31 deletions(-) diff --git a/lib/kitchen/provisioner/ansible_push.rb b/lib/kitchen/provisioner/ansible_push.rb index 51ee2b4..cb9f082 100644 --- a/lib/kitchen/provisioner/ansible_push.rb +++ b/lib/kitchen/provisioner/ansible_push.rb @@ -70,7 +70,7 @@ class AnsiblePush < Base def conf return @validated_config if defined? @validated_config - raise UserError, "playbook '#{config[:playbook]}' could not be found. Please check path" unless File.exist?(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]) @@ -103,7 +103,7 @@ def conf end def playbook - @playbook ||= config[:playbook] || calculate_path("converge.yml") + @playbook ||= config[:playbook] || calculate_path("converge.yml", :type => :file) end def machine_name @@ -315,23 +315,6 @@ def verbosity_argument '-v' end end - - def calculate_path(path, type = :file) - unless instance - fail 'Please ensure that an instance is provided before calling calculate_path' - end - base = config[:test_base_path] - candidates = [] - candidates << File.join(base, instance.suite.name, 'ansible', path) - candidates << File.join(base, instance.suite.name, path) - candidates << File.join(base, path) - candidates << File.join(config[:kitchen_root], path) - candidates << File.join(Dir.pwd, path) - - candidates.find do |c| - type == :file ? File.file?(c) : File.directory?(c) - end - end end end end diff --git a/spec/kitchen/provisioner/ansible_push_spec.rb b/spec/kitchen/provisioner/ansible_push_spec.rb index a4429cb..a1a4aa9 100644 --- a/spec/kitchen/provisioner/ansible_push_spec.rb +++ b/spec/kitchen/provisioner/ansible_push_spec.rb @@ -57,13 +57,6 @@ end end - it 'should find playbook file in //ansible' do - FakeFS.with_fresh do - create_playbook("/b/fries/ansible/converge.yml") - expect(provisioner.playbook).to match("fries/ansible/converge.yml") - end - end - it 'should find playbook file in current dir' do FakeFS.with_fresh do create_playbook("/cwd/converge.yml") @@ -71,11 +64,8 @@ end end - it 'should find playbook file in kitchen_root' do - FakeFS.with_fresh do - create_playbook("/r/converge.yml") - expect(provisioner.playbook).to match("r/converge.yml") - end + it 'Should fail with no playbook file' do + expect { provisioner.prepare_command }.to raise_error(Kitchen::UserError) end describe 'Basic config' do