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

Use named pipe to decrease local Windows runtime #220

Merged
merged 26 commits into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c8a8442
Use named pipe to decrease local Windows runtime
jerryaldrichiii Nov 21, 2017
4540c4b
Modify named pipe creation to use unique name
jerryaldrichiii Nov 22, 2017
48fa142
Remove `$ProgressPreference = 'SilentlyContinue'`
jerryaldrichiii Nov 22, 2017
bed61c5
Modify code to persist pipe usage
jerryaldrichiii Nov 22, 2017
fe773e6
Removed pipe argument (default is In/Out)
jerryaldrichiii Nov 22, 2017
ccd1a8d
Privatize pipe related functions
jerryaldrichiii Nov 23, 2017
8f2437b
Add fallback to not used pipe if creation fails
jerryaldrichiii Nov 23, 2017
82ffa18
Move Windows local command logic to separate class
jerryaldrichiii Nov 27, 2017
776b8da
Revert file code format
jerryaldrichiii Nov 27, 2017
44a7a6a
Change `@platform` to `@os` for consistency
jerryaldrichiii Nov 27, 2017
9681fe6
Split Windows runners and create a `GenericRunner`
jerryaldrichiii Nov 27, 2017
d1ad987
Add test for Windows named pipe usage
jerryaldrichiii Nov 27, 2017
18a4245
Tweak `GenericRunner` and default `run_command`
jerryaldrichiii Nov 27, 2017
d3b6f86
Remove `Local::` from `CommandWrapper`
jerryaldrichiii Nov 27, 2017
bd2c7c3
Fix test for Windows named pipe
jerryaldrichiii Nov 27, 2017
e7d7d32
Make changes to integrate @jquick's cacheing magic
jerryaldrichiii Nov 27, 2017
ecad39f
Remove unneeded logic for opening an existing pipe
jerryaldrichiii Nov 27, 2017
7517045
Add comments explaining pipe check tests
jerryaldrichiii Nov 30, 2017
d494df2
Change `Shellout` to `ShellOut`
jerryaldrichiii Nov 30, 2017
ab34ccc
Change `stubs` to `expects` where appropriate
jerryaldrichiii Nov 30, 2017
824f2e3
Modify pipe tests
jerryaldrichiii Nov 30, 2017
9965ef8
Move/Modify Unit tests and add Integration tests
jerryaldrichiii Dec 1, 2017
e63627f
Fix local tests when running with others.
jquick Dec 2, 2017
17d9de7
Minor style changes
jerryaldrichiii Dec 3, 2017
6160bde
Remove extra newline between `let`s
jerryaldrichiii Dec 4, 2017
0bfd1c1
Modify TransportHelper to accept platform options
jerryaldrichiii Dec 4, 2017
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
37 changes: 35 additions & 2 deletions test/unit/transports/local_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
class TransportHelper
attr_accessor :transport

def initialize
def initialize(type = :mock)
Train::Platforms::Detect::Specifications::OS.load
plat = Train::Platforms.name('mock').in_family('linux')
plat = Train::Platforms.name('mock')
plat.add_platform_methods
plat.stubs(:windows?).returns(true) if type == :windows
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The seems a bit brute-force. Can we accept an options hash and then we can plumb the family in as necessary and let the Train::Platforms stuff work normally without stubbing methods on this?

def initialize(opts = {})
  Train::Platforms::Detect::Specifications::OS.load
  plat = Train::Platforms.name('mock')
  plat.in_family(opts[:family]) unless opts[:family].nil?
  plat.add_platform_methods
end

Would that work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure will! Great idea. See latest commit.

Train::Platforms::Detect.stubs(:scan).returns(plat)
@transport = Train::Transports::Local.new
end
Expand Down Expand Up @@ -93,4 +94,36 @@ def mock_run_cmd(cmd, &block)
end
end
end

describe 'when running on Windows' do
let(:connection) { TransportHelper.new(:windows).transport.connection }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra newline - it's more common to group all the lets together than space them out.

let(:runner) { mock }

it 'uses `WindowsPipeRunner` by default' do
Train::Transports::Local::Connection::WindowsPipeRunner
.expects(:new)
.returns(runner)

Train::Transports::Local::Connection::WindowsShellRunner
.expects(:new)
.never

runner.expects(:run_command).with('not actually executed')
connection.run_command('not actually executed')
end

it 'uses `WindowsShellRunner` when a named pipe is not available' do
Train::Transports::Local::Connection::WindowsPipeRunner
.expects(:new)
.raises(Train::Transports::Local::PipeError)

Train::Transports::Local::Connection::WindowsShellRunner
.expects(:new)
.returns(runner)

runner.expects(:run_command).with('not actually executed')
connection.run_command('not actually executed')
end
end
end
33 changes: 19 additions & 14 deletions test/windows/local_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,36 @@
cmd.stderr.must_equal ''
end

# Verify pipe is created by using PowerShell to check pipe location. This
# works by intercepting the `SecureRandom` call which controls the pipe
# name. If this command uses a pipe, then the `stdout` of this command will
# contain the pipe name.
it 'can execute a command using a named pipe' do
SecureRandom.expects(:hex).returns('with_pipe')
SecureRandom.expects(:hex).returns('via_pipe')

cmd = conn.run_command('Get-ChildItem //./pipe/ | Where-Object { $_.Name -Match "inspec_with_pipe" }')
cmd.stdout.must_match /inspec_with_pipe/
Train::Transports::Local::Connection::WindowsShellRunner
.any_instance
.expects(:new)
.never

cmd = conn.run_command('Write-Output "Create pipe"')
File.exist?('//./pipe/inspec_via_pipe').must_equal true
cmd.stdout.must_equal "Create pipe\r\n"
cmd.stderr.must_equal ''
end

it 'when named pipe is not available it uses `WindowsShellRunner`' do
# Prevent named pipe from being created
it 'can execute a command via ShellRunner if pipe creation fails' do
# By forcing `acquire_pipe` to fail to return a pipe, any attempts to create
# a `WindowsPipeRunner` object should fail. If we can still run a command,
# then we know that it was successfully executed by `Mixlib::ShellOut`.
Train::Transports::Local::Connection::WindowsPipeRunner
.any_instance
.expects(:acquire_pipe)
.at_least_once
.returns(nil)

Train::Transports::Local::Connection::WindowsShellRunner
.any_instance
.expects(:run_command)
proc { Train::Transports::Local::Connection::WindowsPipeRunner.new }
.must_raise(Train::Transports::Local::PipeError)

# Run command to trigger our stubs/expects
conn.run_command('Write-Host "using ShellOut"')
cmd = conn.run_command('Write-Output "test"')
cmd.stdout.must_equal "test\r\n"
cmd.stderr.must_equal ''
end

describe 'file' do
Expand Down