From fcd07e3a3789ec056ebafe3d9fdfc5a621970823 Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Thu, 30 Jan 2020 12:15:38 -0500 Subject: [PATCH] Switch to PowerShell for create_remote_directory --- plugins/communicators/winssh/communicator.rb | 9 +++-- .../communicators/winssh/communicator_test.rb | 36 ++++++++++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/plugins/communicators/winssh/communicator.rb b/plugins/communicators/winssh/communicator.rb index 5d78255c5..435d20a30 100644 --- a/plugins/communicators/winssh/communicator.rb +++ b/plugins/communicators/winssh/communicator.rb @@ -35,9 +35,12 @@ module VagrantPlugins if force_raw if shell == "powershell" - base_cmd = "powershell \"Write-Host #{CMD_GARBAGE_MARKER}; [Console]::Error.WriteLine('#{CMD_GARBAGE_MARKER}'); #{command}\"" + command = "Write-Host #{CMD_GARBAGE_MARKER}; [Console]::Error.WriteLine('#{CMD_GARBAGE_MARKER}'); #{command}" + command = Base64.strict_encode64(command.encode("UTF-16LE", "UTF-8")) + base_cmd = "powershell -encodedCommand #{command}" else - base_cmd = "cmd /q /c \"ECHO #{CMD_GARBAGE_MARKER} && ECHO #{CMD_GARBAGE_MARKER} 1>&2 && #{command}\"" + command = "ECHO #{CMD_GARBAGE_MARKER} && ECHO #{CMD_GARBAGE_MARKER} 1>&2 && #{command}" + base_cmd = "cmd /q /c \"#{command}\"" end else tfile = Tempfile.new('vagrant-ssh') @@ -227,7 +230,7 @@ SCRIPT end def create_remote_directory(dir, force_raw=false) - execute("if not exist \"#{dir}\" mkdir \"#{dir}\"", shell: "cmd", force_raw: force_raw) + execute("md -Force \"#{dir}\"", shell: "powershell", force_raw: force_raw) end end end diff --git a/test/unit/plugins/communicators/winssh/communicator_test.rb b/test/unit/plugins/communicators/winssh/communicator_test.rb index d04a3652a..7c000f839 100644 --- a/test/unit/plugins/communicators/winssh/communicator_test.rb +++ b/test/unit/plugins/communicators/winssh/communicator_test.rb @@ -17,12 +17,14 @@ describe VagrantPlugins::CommunicatorWinSSH::Communicator do ) end + let(:shell) { "cmd" } + # SSH configuration information mock let(:winssh) do double("winssh", insert_key: false, export_command_template: export_command_template, - shell: 'cmd', + shell: shell, upload_directory: "C:\\Windows\\Temp" ) end @@ -225,6 +227,22 @@ describe VagrantPlugins::CommunicatorWinSSH::Communicator do with(/ECHO #{command_garbage_marker} && ECHO #{command_garbage_marker}.*/) expect(communicator.execute("dir", force_raw: true)).to eq(0) end + + context "and shell is powershell" do + let(:shell) { "powershell" } + + it "passes the correct flags to powershell" do + expect(channel).to receive(:exec). + with(/-encodedCommand/) + expect(communicator.execute("dir", force_raw: true)).to eq(0) + end + + it "encodes the raw command" do + expect(channel).to receive(:exec). + with(/VwByAGkAdABlAC0ASABvAHMAdAAgADQAMQBlADUANwBkADMAOAAtAGIANABmADcALQA0AGUANAA2AC0AOQBjADMAOAAtADEAMwA4ADcAMwBkADMAMwA4AGIAOAA2AC0AdgBhAGcAcgBhAG4AdAAtAHMAcwBoADsAIABbAEMAbwBuAHMAbwBsAGUAXQA6ADoARQByAHIAbwByAC4AVwByAGkAdABlAEwAaQBuAGUAKAAnADQAMQBlADUANwBkADMAOAAtAGIANABmADcALQA0AGUANAA2AC0AOQBjADMAOAAtADEAMwA4ADcAMwBkADMAMwA4AGIAOAA2AC0AdgBhAGcAcgBhAG4AdAAtAHMAcwBoACcAKQA7ACAAZABpAHIA/) + expect(communicator.execute("dir", force_raw: true)).to eq(0) + end + end end end @@ -550,4 +568,20 @@ describe VagrantPlugins::CommunicatorWinSSH::Communicator do end end end + + describe "#create_remote_directory" do + it "should set the shell to powershell" do + expect(communicator).to receive(:execute).with( + anything, + hash_including(shell: "powershell")) + communicator.create_remote_directory('c:\destination') + end + + it "should use an appropriate command for powershell" do + expect(communicator).to receive(:execute).with( + /md -Force/, + anything) + communicator.create_remote_directory('c:\destination') + end + end end