-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jared Quick <jquick@chef.io>
- Loading branch information
Showing
9 changed files
with
115 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# encoding: utf-8 | ||
|
||
class Train::Plugins::Transport | ||
class CacheConnection < BaseConnection | ||
# Create a new CacheConnection instance. This instance will cache | ||
# file and command operations on the underline connection. | ||
# | ||
# @param connection [Connection] connection object | ||
def initialize(connection) | ||
@connection = connection | ||
@file_cache = {} | ||
@cmd_cache = {} | ||
|
||
@connection.class.instance_methods(false).each do |m| | ||
next if %i(run_command file).include?(m) | ||
define_singleton_method m do |*args| | ||
@connection.send(m, *args) | ||
end | ||
end | ||
end | ||
|
||
def file(path) | ||
if @file_cache.key?(path) | ||
@file_cache[path] | ||
else | ||
@file_cache[path] = @connection.file(path) | ||
end | ||
end | ||
|
||
def run_command(cmd) | ||
if @cmd_cache.key?(cmd) | ||
@cmd_cache[cmd] | ||
else | ||
@cmd_cache[cmd] = @connection.run_command(cmd) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ def initialize(conf = nil) | |
super(conf) | ||
mock_os | ||
@commands = {} | ||
@files = {} | ||
end | ||
|
||
def uri | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# encoding: utf-8 | ||
require 'helper' | ||
|
||
describe 'train cache connection' do | ||
let(:cls) { Train::Plugins::Transport::BaseConnection } | ||
let(:connection) { cls.new({}) } | ||
let(:cache_connection) { Train::Plugins::Transport::CacheConnection.new(connection) } | ||
|
||
describe 'create cache connection' do | ||
it 'return new cache connection' do | ||
cache_connection.must_be_kind_of Train::Plugins::Transport::CacheConnection | ||
cache_connection.respond_to?(:run_command).must_equal true | ||
end | ||
end | ||
|
||
describe 'check instance method fallback' do | ||
it 'check os method' do | ||
cache_connection.respond_to?(:os).must_equal true | ||
cache_connection.respond_to?(:platform).must_equal true | ||
end | ||
|
||
it 'check close method' do | ||
cache_connection.respond_to?(:close).must_equal true | ||
end | ||
end | ||
|
||
describe 'load file' do | ||
it 'load file with caching' do | ||
connection.expects(:file).once.returns('test_file') | ||
cache_connection.file('/tmp/test').must_equal('test_file') | ||
cache_connection.file('/tmp/test').must_equal('test_file') | ||
assert = { '/tmp/test' => 'test_file' } | ||
cache_connection.instance_variable_get(:@file_cache).must_equal(assert) | ||
end | ||
end | ||
|
||
describe 'run command' do | ||
it 'run command with caching' do | ||
connection.expects(:run_command).once.returns('test_user') | ||
cache_connection.run_command('whoami').must_equal('test_user') | ||
cache_connection.run_command('whoami').must_equal('test_user') | ||
assert = { 'whoami' => 'test_user' } | ||
cache_connection.instance_variable_get(:@cmd_cache).must_equal(assert) | ||
end | ||
end | ||
end |