From 9d87be51daa69abea4cc82457a96dfdb9b5bfd92 Mon Sep 17 00:00:00 2001 From: Dan Dunckel Date: Mon, 12 Oct 2015 17:55:48 -0700 Subject: [PATCH] Small refactor on conditional check and add tests --- plugins/provisioners/shell/config.rb | 2 +- .../communicators/winrm/communicator_test.rb | 9 +++++++++ test/unit/plugins/provisioners/shell/config_test.rb | 13 +++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/plugins/provisioners/shell/config.rb b/plugins/provisioners/shell/config.rb index 19d5180a9..aaadb8081 100755 --- a/plugins/provisioners/shell/config.rb +++ b/plugins/provisioners/shell/config.rb @@ -81,7 +81,7 @@ module VagrantPlugins errors << I18n.t("vagrant.provisioners.shell.args_bad_type") end - if @elevated_interactive == true && @privileged == false + if elevated_interactive && !privileged errors << I18n.t("vagrant.provisioners.shell.interactive_not_elevated") end diff --git a/test/unit/plugins/communicators/winrm/communicator_test.rb b/test/unit/plugins/communicators/winrm/communicator_test.rb index e98f646b3..829170bd6 100644 --- a/test/unit/plugins/communicators/winrm/communicator_test.rb +++ b/test/unit/plugins/communicators/winrm/communicator_test.rb @@ -93,6 +93,15 @@ describe VagrantPlugins::CommunicatorWinRM::Communicator do expect(subject.execute("dir", { elevated: true })).to eq(0) end + it "wraps command in elevated and interactive shell script when elevated and interactive are true" do + expect(shell).to receive(:upload).with(kind_of(String), "c:/tmp/vagrant-elevated-shell.ps1") + expect(shell).to receive(:powershell) do |cmd| + expect(cmd).to eq("powershell -executionpolicy bypass -file \"c:/tmp/vagrant-elevated-shell.ps1\" " + + "-username \"vagrant\" -password \"password\" -encoded_command \"ZABpAHIAOwAgAGUAeABpAHQAIAAkAEwAQQBTAFQARQBYAEkAVABDAE8ARABFAA==\"") + end.and_return({ exitcode: 0 }) + expect(subject.execute("dir", { elevated: true, interactive: true })).to eq(0) + end + it "can use cmd shell" do expect(shell).to receive(:cmd).with(kind_of(String)).and_return({ exitcode: 0 }) expect(subject.execute("dir", { shell: :cmd })).to eq(0) diff --git a/test/unit/plugins/provisioners/shell/config_test.rb b/test/unit/plugins/provisioners/shell/config_test.rb index 946b4c2a8..582740162 100644 --- a/test/unit/plugins/provisioners/shell/config_test.rb +++ b/test/unit/plugins/provisioners/shell/config_test.rb @@ -85,6 +85,19 @@ describe "VagrantPlugins::Shell::Config" do I18n.t("vagrant.provisioners.shell.args_bad_type") ]) end + + it "returns an error if elevated_interactive is true but privileged is false" do + subject.path = file_that_exists + subject.elevated_interactive = true + subject.privileged = false + subject.finalize! + + result = subject.validate(machine) + + expect(result["shell provisioner"]).to eq([ + I18n.t("vagrant.provisioners.shell.interactive_not_elevated") + ]) + end end describe 'finalize!' do