diff --git a/lib/kitchen/busser.rb b/lib/kitchen/busser.rb index 34d3bf98c..c9ce52c3c 100644 --- a/lib/kitchen/busser.rb +++ b/lib/kitchen/busser.rb @@ -56,6 +56,8 @@ def initialize(suite_name, opts = {}) @config[:root_path] = opts.fetch(:root_path, DEFAULT_ROOT_PATH) @config[:version] = opts.fetch(:version, "busser") @config[:busser_bin] = opts.fetch(:busser_bin, File.join(@config[:root_path], "bin/busser")) + @config[:remote_transfer_path] = opts.fetch(:remote_transfer_path, File.join(@config[:root_path], "transfer")) + @config[:local_transfer_path] = nil end # Returns the name of this busser, suitable for display in a CLI. @@ -89,6 +91,53 @@ def diagnose result end + # Clears local and constructs a remote command to clean-up busser transfer directory + # + # @return [string] a shell transfer specific directory delete command + def cleanup_cmd + #Empty the previous local transfer folder + FileUtils.rm_rf(config[:local_transfer_path]) if config[:local_transfer_path] + + case shell + when "bourne" + cmd = [ + sudo("rm -rf #{config[:remote_transfer_path]}"), + sudo("mkdir -p #{config[:remote_transfer_path]}"), + sudo("chmod 777 #{config[:remote_transfer_path]}") + ].join("\n").concat("\n").gsub(/^ {10}/, '') + + when "powershell" + cmd = <<-CMD.gsub(/^ {10}/, "") + rmdir -Recurse -Force -ea 0 #{config[:remote_transfer_path]} | out-null + exit 0 + CMD + else + raise "[#{self}] Unsupported shell: #{shell}" + end + Util.wrap_command(cmd, shell) + end + + # Prepare busser test files to be transferred to the target system + # + # @return [Hash] a hash list of local transfer files with its remote target directory + def sync_files + # Initialize a new transfer folder + config[:local_transfer_path] = Dir.mktmpdir("#{instance.name}-busser-transfer") + + transfer_list = [] + local_suite_files.each do |f| + raw_content = IO.read(f) + md5 = Digest::MD5.hexdigest(raw_content) + remote_dir = config[:remote_transfer_path] + temp_file = File.join(config[:local_transfer_path], md5) + encoded_content = Base64.encode64(raw_content).gsub("\n", '') + IO.binwrite(temp_file, encoded_content) + + transfer_list.push({local: temp_file , remote: remote_dir}) + end + transfer_list + end + # Returns a command string which installs Busser, and installs all # required Busser plugins for the suite. # @@ -119,7 +168,10 @@ def setup_cmd when "powershell" cmd = <<-CMD.gsub(/^ {10}/, "") #{busser_setup_env} + echo 'Cleaning up busser...' + gem uninstall #{plugins.join(" ")} + echo 'Setting up busser...' if ((gem list busser -i) -eq \"false\") { gem install #{gem_install_args} } @@ -312,8 +364,11 @@ def remote_file(file, dir) # @api private def stream_file(local_path, remote_path) local_file = IO.read(local_path) - encoded_file = Base64.encode64(local_file).gsub("\n", "") + + #encoded_file = Base64.encode64(local_file).gsub("\n", "") md5 = Digest::MD5.hexdigest(local_file) + transfer_path = File.join(File.join(config[:root_path],'transfer'),md5) + perms = format("%o", File.stat(local_path).mode)[2, 4] stream_cmd = [ sudo(config[:busser_bin]), @@ -325,7 +380,7 @@ def stream_file(local_path, remote_path) [ %{echo "Uploading #{remote_path} (mode=#{perms})"}, - %{echo "#{encoded_file}" | #{stream_cmd}} + %{cat "#{transfer_path}" | #{stream_cmd}} ].join("\n").concat("\n") end diff --git a/lib/kitchen/data_munger.rb b/lib/kitchen/data_munger.rb index bec46147d..534c45eba 100644 --- a/lib/kitchen/data_munger.rb +++ b/lib/kitchen/data_munger.rb @@ -96,6 +96,7 @@ def transport_data_for(suite, platform) set_kitchen_config_at!(tdata, :kitchen_root) set_kitchen_config_at!(tdata, :test_base_path) set_kitchen_config_at!(tdata, :log_level) + tdata[:ssh_key] = data[:driver][:ssh_key] combine_arrays!(tdata, :run_list, :platform, :suite) end end diff --git a/lib/kitchen/driver/base.rb b/lib/kitchen/driver/base.rb index a59d8245d..802b4f690 100644 --- a/lib/kitchen/driver/base.rb +++ b/lib/kitchen/driver/base.rb @@ -20,6 +20,7 @@ require "kitchen/lazy_hash" + module Kitchen module Driver @@ -86,14 +87,23 @@ def setup(state) end end - # Verifies a converged instance. + # Uploads latest test files and verifies a converged instance. # # @param state [Hash] mutable instance and driver state # @raise [ActionFailed] if the action could not be completed def verify(state) transport.connection(state) do |conn| + + conn.execute(busser.cleanup_cmd) + + busser.sync_files.each do |file| + conn.upload!(file[:local], file[:remote]) + end + conn.execute(busser.sync_cmd) conn.execute(busser.run_cmd) + + conn.execute(busser.cleanup_cmd) end end diff --git a/lib/kitchen/transport/ssh.rb b/lib/kitchen/transport/ssh.rb index 168caed0b..f41a216b1 100644 --- a/lib/kitchen/transport/ssh.rb +++ b/lib/kitchen/transport/ssh.rb @@ -46,6 +46,8 @@ def execute(command) # (see Base#upload!) def upload!(local, remote, options = {}, &progress) + return if local.nil? || local.empty? + options = { :recursive => true }.merge(options) if progress.nil? @@ -56,6 +58,8 @@ def upload!(local, remote, options = {}, &progress) } end + + local = Array.new(1) { local } if local.is_a? String local.each do |path| session.scp.upload!(path, remote, options, &progress) end diff --git a/lib/kitchen/transport/winrm.rb b/lib/kitchen/transport/winrm.rb index 75c781fea..0d20dec4e 100644 --- a/lib/kitchen/transport/winrm.rb +++ b/lib/kitchen/transport/winrm.rb @@ -28,9 +28,9 @@ end require "logger" - require "kitchen/errors" require "kitchen/login_command" + module Kitchen module Transport @@ -78,6 +78,8 @@ def wql(query) # (see Base#upload!) def upload!(local, remote) + return if local.nil? || local.empty? + logger.info("Concurrent threads set to :max_threads => #{config[:max_threads]}") logger.debug("Upload: #{local} -> #{remote}") local = Array.new(1) { local } if local.is_a? String @@ -351,12 +353,16 @@ def should_upload_file?(local, remote) $file.Dispose() } if ($guest_md5 -eq '#{local_md5}') { - exit 0 + [Environment]::ExitCode = 0 + return 0 } } -exit 1 +[Environment]::ExitCode = 1 +return 1 EOH - powershell(command)[:exitcode] == 1 + result = powershell(command) + should = (result[:data][0][:stdout] == "1") + should end # Uploads the given file to a new temp file on the guest