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

split of mock into transport and connection #7

Merged
merged 1 commit into from
Oct 14, 2015
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
62 changes: 40 additions & 22 deletions lib/train/transports/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,17 @@ module Train::Transports
class Mock < Train.plugin(1)
name 'mock'

attr_accessor :files, :commands, :os

def initialize(conf = nil)
@conf = conf || {}
@files = {}
@os = {}
@commands = {}
trace_calls if @conf[:verbose]
end

def mock_os(value)
@os = OS.new(self, value)
end

def mock_command(cmd, stdout = nil, stderr = nil, exit_status = 0)
@commands[cmd] = Command.new(stdout, stderr, exit_status)
end

def run_command(cmd)
@commands[cmd] || mock_command(cmd)
end

def file(path)
@files[path] ||= File.new(self, path)
def connection
@connection ||= Connection.new
end

def to_s
'Mock Backend'
'Mock Transport'
end

private
Expand Down Expand Up @@ -71,10 +54,45 @@ def trace_calls
end

class Train::Transports::Mock
class Connection < BaseConnection
attr_accessor :files, :commands
attr_reader :os

def initialize(conf = nil)
@conf = conf || {}
@files = {}
@os = OS.new(self, :unknown)
@commands = {}
trace_calls if @conf[:verbose]
end

def mock_os(value)
@os = OS.new(self, value)
end

def mock_command(cmd, stdout = nil, stderr = nil, exit_status = 0)
@commands[cmd] = Command.new(stdout, stderr, exit_status)
end

def run_command(cmd)
@commands[cmd] || mock_command(cmd)
end

def file(path)
@files[path] ||= File.new(self, path)
end

def to_s
'Mock Connection'
end
end
end

class Train::Transports::Mock::Connection
Command = Struct.new(:stdout, :stderr, :exit_status)
end

class Train::Transports::Mock
class Train::Transports::Mock::Connection
class OS < OSCommon
def initialize(backend, desc)
super(backend, desc)
Expand All @@ -86,7 +104,7 @@ def detect_family
end
end

class Train::Transports::Mock
class Train::Transports::Mock::Connection
class File < FileCommon
%w{
exist? mode owner group link_target link_path content mtime size
Expand Down
2 changes: 1 addition & 1 deletion test/unit/extras/linux_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
describe 'file common' do
let(:cls) { Train::Extras::LinuxFile }
let(:backend) {
backend = Train::Transports::Mock.new
backend = Train::Transports::Mock.new.connection
backend.mock_os({ family: 'linux' })
backend
}
Expand Down
30 changes: 18 additions & 12 deletions test/unit/transports/mock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,54 @@

describe 'mock transport' do
let(:transport) { Train::Transports::Mock.new }
let(:connection) { transport.connection }

it 'can be instantiated' do
transport.wont_be_nil
end

it 'can create a connection' do
connection.wont_be_nil
end

describe 'when running a mocked command' do
let(:mock_cmd) { }

it 'has a simple mock command creator' do
out = rand
res = Train::Transports::Mock::Command.new(out, nil, 0)
transport.mock_command('test', out).must_equal res
cls = Train::Transports::Mock::Connection::Command
res = cls.new(out, nil, 0)
connection.mock_command('test', out).must_equal res
end

it 'gets results for stdout' do
out = rand
cmd = rand
transport.mock_command(cmd, out)
transport.run_command(cmd).stdout.must_equal(out)
connection.mock_command(cmd, out)
connection.run_command(cmd).stdout.must_equal(out)
end

it 'gets results for stderr' do
err = rand
cmd = rand
transport.mock_command(cmd, nil, err)
transport.run_command(cmd).stderr.must_equal(err)
connection.mock_command(cmd, nil, err)
connection.run_command(cmd).stderr.must_equal(err)
end

it 'gets results for exit_status' do
code = rand
cmd = rand
transport.mock_command(cmd, nil, nil, code)
transport.run_command(cmd).exit_status.must_equal(code)
connection.mock_command(cmd, nil, nil, code)
connection.run_command(cmd).exit_status.must_equal(code)
end
end

describe 'when accessing a mocked os' do
it 'sets the OS to the mocked value' do
transport.mock_os({ family: 'centos' })
transport.os.linux?.must_equal true
transport.os.redhat?.must_equal true
transport.os[:family].must_equal 'centos'
connection.mock_os({ family: 'centos' })
connection.os.linux?.must_equal true
connection.os.redhat?.must_equal true
connection.os[:family].must_equal 'centos'
end
end

Expand Down