Skip to content

Commit

Permalink
Merge pull request #57 from chef/chris-rock/fix-local-exec-on-win
Browse files Browse the repository at this point in the history
Properly wrap commands in powershell for local backend
  • Loading branch information
srenatus committed Jan 25, 2016
2 parents 5d3e21f + b466555 commit 602a2cb
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ puts conn.file('/proc/version').content
conn.close
```

# Testing

We perform `unit`, `integration` and `windows` tests.

* `unit` tests ensure the intended behaviour of the implementation
* `integration` tests run against VMs and docker containers
* `windows` tests that run on appveyor for windows integration tests

## Windows

```
# run windows tests
bundle exec rake test:windows
# run single tests
bundle exec ruby -I .\test\windows\ .\test\windows\local_test.rb
```


# Kudos and Contributors

Train is heavily based on the work of:
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace :test do

task :windows do
Dir.glob('test/windows/*_test.rb').all? do |file|
sh(Gem.ruby, '-w', '-Ilib:test', file)
sh(Gem.ruby, '-w', '-I .\test\windows', file)
end or fail 'Failures'
end

Expand Down
14 changes: 11 additions & 3 deletions lib/train/extras/command_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,20 @@ class PowerShellCommand < CommandWrapperBase
def initialize(backend, options)
@backend = backend
validate_options(options)
end

@prefix = 'powershell '
def run(script)
# wrap the script to ensure we always run it via powershell
# especially in local mode, we cannot be sure that we get a Powershell
# we may just get a `cmd`.
# TODO: we may want to opt for powershell.exe -command instead of `encodeCommand`
"powershell -encodedCommand #{WinRM::PowershellScript.new(safe_script(script)).encoded}"
end

def run(command)
@prefix + command
# reused from https://github.com/WinRb/WinRM/blob/master/lib/winrm/command_executor.rb
# suppress the progress stream from leaking to stderr
def safe_script(script)
"$ProgressPreference='SilentlyContinue';" + script
end

def to_s
Expand Down
2 changes: 1 addition & 1 deletion lib/train/transports/winrm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class WinRMFailed < Train::TransportError; end
# @author Matt Wrock <matt@mattwrock.com>
# @author Salim Afiune <salim@afiunemaya.com.mx>
# @author Fletcher Nichol <fnichol@nichol.ca>
class WinRM < Train.plugin(1) # rubocop:disable Metrics/ClassLength
class WinRM < Train.plugin(1)
name 'winrm'

autoload :Connection, 'train/transports/winrm_connection'
Expand Down
3 changes: 2 additions & 1 deletion test/unit/extras/command_wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

it 'wraps commands in powershell' do
lc = cls.new(backend, {})
lc.run(cmd).must_equal "powershell #{cmd}"
tmp =
lc.run(cmd).must_equal "powershell -encodedCommand #{WinRM::PowershellScript.new('$ProgressPreference=\'SilentlyContinue\';' + cmd).encoded}"
end
end
12 changes: 12 additions & 0 deletions test/windows/local_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
os[:arch].must_equal nil
end

it 'run echo test' do
cmd = conn.run_command('Write-Output "test"')
cmd.stdout.must_equal "test\r\n"
cmd.stderr.must_equal ''
end

it 'use powershell piping' do
cmd = conn.run_command("New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name A -Value (Write-Output 'PropertyA') -PassThru | Add-Member -MemberType NoteProperty -Name B -Value (Write-Output 'PropertyB') -PassThru | ConvertTo-Json")
cmd.stdout.must_equal "{\r\n \"A\": \"PropertyA\",\r\n \"B\": \"PropertyB\"\r\n}\r\n"
cmd.stderr.must_equal ''
end

after do
# close the connection
conn.close
Expand Down
12 changes: 12 additions & 0 deletions test/windows/winrm_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
os[:arch].must_equal nil
end

it 'run echo test' do
cmd = conn.run_command('Write-Output "test"')
cmd.stdout.must_equal "test\r\n"
cmd.stderr.must_equal ''
end

it 'use powershell piping' do
cmd = conn.run_command("New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name A -Value (Write-Output 'PropertyA') -PassThru | Add-Member -MemberType NoteProperty -Name B -Value (Write-Output 'PropertyB') -PassThru | ConvertTo-Json")
cmd.stdout.must_equal "{\r\n \"A\": \"PropertyA\",\r\n \"B\": \"PropertyB\"\r\n}\r\n"
cmd.stderr.must_equal ''
end

after do
# close the connection
conn.close
Expand Down

0 comments on commit 602a2cb

Please sign in to comment.