Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCVMM] Always assume a string for run_powershell_script #14859

Merged
merged 1 commit into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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