From 4337c139afbcdf729e6a9f8c1a67c335e1fca658 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Mon, 24 Apr 2017 08:23:42 -0600 Subject: [PATCH] Always assume a string for run_powershell_script. Added some basic run_powershell_script specs. --- .../microsoft/infra_manager/powershell.rb | 6 +-- .../microsoft/infra_manager/refresh_parser.rb | 3 +- .../infra_manager/powershell_spec.rb | 51 +++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/app/models/manageiq/providers/microsoft/infra_manager/powershell.rb b/app/models/manageiq/providers/microsoft/infra_manager/powershell.rb index 449e4c88ebe..081e1f79622 100644 --- a/app/models/manageiq/providers/microsoft/infra_manager/powershell.rb +++ b/app/models/manageiq/providers/microsoft/infra_manager/powershell.rb @@ -13,12 +13,11 @@ def execute_powershell(connection, script) def run_powershell_script(connection, script) log_header = "MIQ(#{self.class.name}.#{__method__})" - script_string = IO.read(script) results = [] begin with_winrm_shell(connection) do |shell| - results = shell.run(script_string) + results = shell.run(script) log_dos_error_results(results.stderr) end rescue Errno::ECONNREFUSED => err @@ -119,8 +118,7 @@ def run_powershell_script(script) _result, timings = Benchmark.realtime_block(:execution) do with_winrm_shell do |shell| - script_string = IO.read(script) - results = shell.run(script_string) + results = shell.run(script) self.class.log_dos_error_results(results.stderr) end end diff --git a/app/models/manageiq/providers/microsoft/infra_manager/refresh_parser.rb b/app/models/manageiq/providers/microsoft/infra_manager/refresh_parser.rb index 82a56d3db45..f241837641c 100644 --- a/app/models/manageiq/providers/microsoft/infra_manager/refresh_parser.rb +++ b/app/models/manageiq/providers/microsoft/infra_manager/refresh_parser.rb @@ -20,7 +20,8 @@ def ems_inv_to_hashes log_header = "MIQ(#{self.class.name}.#{__method__}) Collecting data for EMS name: [#{@ems.name}] id: [#{@ems.id}]" $scvmm_log.info("#{log_header}...") - @inventory = ManageIQ::Providers::Microsoft::InfraManager.execute_powershell_json(@connection, INVENTORY_SCRIPT) + script = IO.read(INVENTORY_SCRIPT) + @inventory = ManageIQ::Providers::Microsoft::InfraManager.execute_powershell_json(@connection, script) if @inventory.empty? $scvmm_log.warn("#{log_header}...Empty inventory set returned from SCVMM.") diff --git a/spec/models/manageiq/providers/microsoft/infra_manager/powershell_spec.rb b/spec/models/manageiq/providers/microsoft/infra_manager/powershell_spec.rb index 942196c3e9c..b220cda1e90 100644 --- a/spec/models/manageiq/providers/microsoft/infra_manager/powershell_spec.rb +++ b/spec/models/manageiq/providers/microsoft/infra_manager/powershell_spec.rb @@ -1,6 +1,16 @@ describe ManageIQ::Providers::Microsoft::InfraManager::Powershell do before(:all) do class PowershellTemp; end + class Connection; end + + class Shell + def close; end + end + + class Results + def stdout; "stdout"; end + def stderr; "stderr"; end + end end before(:each) do @@ -110,4 +120,45 @@ class PowershellTemp; end expect(powershell.parse_json_results(json).first).to be_kind_of(Hash) end end + + context "run_powershell_script" do + before(:all) do + @connection = Connection.new + @shell = Shell.new + @results = Results.new + end + + let(:connection) { @connection } + let(:shell) { @shell } + let(:results) { @results } + let(:tempfile) { @tempfile } + + let(:ps_script) do + <<-PS_SCRIPT + Import-Module VirtualMachineManager | Out-Null; \ + Get-SCVMMServer localhost | Out-Null;\ + + $vm = New-SCVirtualMachine \ + -Name 'foo_test-1a' \ + -VMHost some_host \ + -Path 'C:\\foo\\bar' \ + -VMTemplate some_template; \ + + $vm | Select-Object ID | ConvertTo-Json + PS_SCRIPT + end + + it "requires two arguments" do + expect { powershell.run_powershell_script }.to raise_error(ArgumentError) + expect { powershell.run_powershell_script(connection) }.to raise_error(ArgumentError) + end + + it "accepts a string argument for a script" do + allow(powershell).to receive(:with_winrm_connection).and_return(connection) + allow(connection).to receive(:shell).and_return(shell) + allow(shell).to receive(:run).and_return(results) + + expect(powershell.run_powershell_script(connection, ps_script)).to eql("stdout") + end + end end