Skip to content

Commit

Permalink
Merge 8abde51 into 8baea6f
Browse files Browse the repository at this point in the history
  • Loading branch information
zenspider committed May 29, 2019
2 parents 8baea6f + 8abde51 commit 91a3550
Show file tree
Hide file tree
Showing 21 changed files with 59 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
train-*.gem
r-train-*.gem
Gemfile.lock
Gemfile.local
.kitchen/
TAGS
terraform.tfstate.backup
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ group :tools do
gem 'rb-readline'
gem 'license_finder'
end

# add these additional dependencies into Gemfile.local
eval_gemfile(__FILE__ + ".local") if File.exist?(__FILE__ + ".local")
4 changes: 2 additions & 2 deletions Rakefile
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ task lint: [:rubocop]
task default: [:test, :lint]

Rake::TestTask.new do |t|
t.libs << 'test/unit'
t.libs << 'test'
t.pattern = 'test/unit/**/*_test.rb'
t.warning = true
t.warning = false
t.verbose = true
t.ruby_opts = ['--dev'] if defined?(JRUBY_VERSION)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/train/platforms/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def to_hash
@platform
end

def cisco_ios? # TODO: kinda a hack. needed to prevent tests from corrupting.
false
end

# Add generic family? and platform methods to an existing platform
#
# This is done later to add any custom
Expand Down
2 changes: 1 addition & 1 deletion lib/train/plugins/base_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class BaseConnection
# @yield [self] yields itself for block-style invocation
def initialize(options = nil)
@options = options || {}
@logger = @options.delete(:logger) || Logger.new(STDOUT)
@logger = @options.delete(:logger) || Logger.new($stdout, level: :fatal)
Train::Platforms::Detect::Specifications::OS.load
Train::Platforms::Detect::Specifications::Api.load

Expand Down
2 changes: 1 addition & 1 deletion lib/train/plugins/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Transport
# @return [Transport] the transport object
def initialize(options = {})
@options = merge_options({}, options || {})
@logger = @options[:logger] || Logger.new(STDOUT)
@logger = @options[:logger] || Logger.new($stdout, level: :fatal)
end

# Create a connection to the target. Options may be provided
Expand Down
8 changes: 4 additions & 4 deletions lib/train/transports/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ def mock_command(cmd, stdout = nil, stderr = nil, exit_status = 0)

def command_not_found(cmd)
if @options[:verbose]
STDERR.puts('Command not mocked:')
STDERR.puts(' '+cmd.to_s.split("\n").join("\n "))
STDERR.puts(' SHA: ' + Digest::SHA256.hexdigest(cmd.to_s))
$stderr.puts('Command not mocked:')
$stderr.puts(' '+cmd.to_s.split("\n").join("\n "))
$stderr.puts(' SHA: ' + Digest::SHA256.hexdigest(cmd.to_s))
end
# return a non-zero exit code
mock_command(cmd, nil, nil, 1)
end

def file_not_found(path)
STDERR.puts('File not mocked: '+path.to_s) if @options[:verbose]
$stderr.puts('File not mocked: '+path.to_s) if @options[:verbose]
File.new(self, path)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/train/transports/vmware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
module Train::Transports
class VMware < Train.plugin(1)
name 'vmware'
option :viserver, default: ENV['VISERVER']
option :username, default: ENV['VISERVER_USERNAME']
option :password, default: ENV['VISERVER_PASSWORD']
option :viserver, default: proc { ENV['VISERVER'] }
option :username, default: proc { ENV['VISERVER_USERNAME'] }
option :password, default: proc { ENV['VISERVER_PASSWORD'] }
option :insecure, default: false

def connection(_ = nil)
Expand Down
3 changes: 3 additions & 0 deletions test/unit/helper.rb → test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
require 'byebug'

require 'train'

# needed to force unix? and others to be created
Train::Platforms::Detect::Specifications::OS.load
2 changes: 1 addition & 1 deletion test/integration/test_ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
backend_conf = {
'target' => ENV['target'] || 'vagrant@localhost',
'key_files' => ENV['key_files'] || '/root/.ssh/id_rsa',
'logger' => Logger.new(STDOUT),
'logger' => Logger.new($stdout),
}

backend_conf['target'] = 'ssh://' + backend_conf['target']
Expand Down
1 change: 0 additions & 1 deletion test/unit/extras/command_wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
bpw = Base64.strict_encode64(pw + "\n")
bcmd = Base64.strict_encode64("echo #{bpw} | base64 --decode | sudo -S #{cmd}")
lc.run(cmd).must_equal "echo #{bcmd} | base64 --decode | $SHELL --login"
p bcmd
end

it 'wraps commands in a default shell when shell is true' do
Expand Down
2 changes: 1 addition & 1 deletion test/unit/plugins/connection_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# encoding: utf-8
require_relative '../helper'
require 'helper'

describe 'v1 Connection Plugin' do
describe 'empty v1 connection plugin' do
Expand Down
4 changes: 3 additions & 1 deletion test/unit/train_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# encoding: utf-8
#
# Author:: Dominik Richter (<dominik.richter@gmail.com>)
require_relative 'helper'
require 'helper'

$:.concat Dir["test/fixtures/plugins/*/lib"] # HACK? I honestly can't tell

describe Train do
before do
Expand Down
20 changes: 10 additions & 10 deletions test/unit/transports/azure_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,27 @@ def initialize(hash)
end

it 'can use azure_client default client' do
MANAGEMENT_API_CLIENT = Azure::Resources::Profiles::Latest::Mgmt::Client
management_api_client = Azure::Resources::Profiles::Latest::Mgmt::Client
client = connection.azure_client
client.class.must_equal MANAGEMENT_API_CLIENT
client.class.must_equal management_api_client
end

it 'can use azure_client graph client' do
GRAPH_API_CLIENT = Azure::GraphRbac::Profiles::Latest::Client
client = connection.azure_client(GRAPH_API_CLIENT)
client.class.must_equal GRAPH_API_CLIENT
graph_api_client = Azure::GraphRbac::Profiles::Latest::Client
client = connection.azure_client(graph_api_client)
client.class.must_equal graph_api_client
end

it 'can use azure_client vault client' do
VAULT_API_CLIENT = ::Azure::KeyVault::Profiles::Latest::Mgmt::Client
client = connection.azure_client(VAULT_API_CLIENT, vault_name: 'Test Vault')
client.class.must_equal VAULT_API_CLIENT
vault_api_client = ::Azure::KeyVault::Profiles::Latest::Mgmt::Client
client = connection.azure_client(vault_api_client, vault_name: 'Test Vault')
client.class.must_equal vault_api_client
end

it 'cannot instantiate azure_client vault client without a vault name' do
VAULT_API_CLIENT = ::Azure::KeyVault::Profiles::Latest::Mgmt::Client
vault_api_client = ::Azure::KeyVault::Profiles::Latest::Mgmt::Client
assert_raises(Train::UserError) do
connection.azure_client(VAULT_API_CLIENT)
connection.azure_client(vault_api_client)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

describe 'CiscoIOSConnection' do
let(:cls) do
Train::Platforms::Detect::Specifications::OS.load
plat = Train::Platforms.name('mock').in_family('cisco_ios')
plat.add_platform_methods
plat.stubs(:cisco_ios?).returns(true)
Expand Down
1 change: 0 additions & 1 deletion test/unit/transports/local_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class TransportHelper

def initialize(user_opts = {})
opts = {platform_name: 'mock', family_hierarchy: ['mock']}.merge(user_opts)
Train::Platforms::Detect::Specifications::OS.load
plat = Train::Platforms.name(opts[:platform_name])
plat.family_hierarchy = opts[:family_hierarchy]
plat.add_platform_methods
Expand Down
14 changes: 9 additions & 5 deletions test/unit/transports/mock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
end

it 'handles nil commands' do
connection.run_command(nil).stdout.must_equal('')
assert_output "", /Command not mocked/ do
connection.run_command(nil).stdout.must_equal('')
end
end

it 'can mock up nil commands' do
Expand Down Expand Up @@ -131,10 +133,12 @@
describe 'when accessing a mocked file' do
it 'handles a non-existing file' do
x = rand.to_s
f = connection.file(x)
f.must_be_kind_of Train::Transports::Mock::Connection::File
f.exist?.must_equal false
f.path.must_equal x
assert_output "", /File not mocked/ do
f = connection.file(x)
f.must_be_kind_of Train::Transports::Mock::Connection::File
f.exist?.must_equal false
f.path.must_equal x
end
end

# tests if all fields between the local json and resulting mock file
Expand Down
1 change: 1 addition & 0 deletions test/unit/transports/ssh_connection_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'helper'
require 'train/transports/ssh'
require 'train/transports/ssh_connection'

Expand Down
14 changes: 9 additions & 5 deletions test/unit/transports/ssh_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8

require_relative '../helper'
require 'helper'
require 'train/transports/ssh'

describe 'ssh transport' do
Expand Down Expand Up @@ -146,7 +146,7 @@
"-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
"-o", "IdentitiesOnly=yes",
"-o", "LogLevel=VERBOSE",
"-o", "LogLevel=ERROR",
"-o", "ForwardAgent=no",
"-i", conf[:key_files],
"-o", "ProxyCommand='ssh root@127.0.0.1 -W %h:%p'",
Expand Down Expand Up @@ -219,6 +219,8 @@
end

it 'wont connect if it is not possible' do
conf[:connection_timeout] = 1
conf[:connection_retries] = 1
conf[:host] = 'localhost'
conf[:port] = 1
conf.delete :proxy_command
Expand Down Expand Up @@ -295,15 +297,17 @@
connection.login_command.command.must_equal 'ssh'
end

make_my_diffs_pretty!

it 'has login command arguments' do
connection.login_command.arguments.must_equal([
"-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
"-o", "IdentitiesOnly=yes",
"-o", "LogLevel=VERBOSE",
"-o", "LogLevel=ERROR",
"-o", "ForwardAgent=no",
"-i", conf[:key_files],
"-o", "ProxyCommand='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -o LogLevel=VERBOSE -o ForwardAgent=no -i #{conf[:key_files]} root@bastion_dummy -p 22 -W %h:%p'",
"-o", "ProxyCommand='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -o LogLevel=ERROR -o ForwardAgent=no -i #{conf[:key_files]} root@bastion_dummy -p 22 -W %h:%p'",
"-p", "22",
"root@#{conf[:host]}",
])
Expand Down Expand Up @@ -333,7 +337,7 @@
mock = MiniTest::Mock.new
mock.expect(:call, true) do |hostname, username, options|
options[:proxy].kind_of?(Net::SSH::Proxy::Command) &&
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -o LogLevel=VERBOSE -o ForwardAgent=no -i #{conf[:key_files]} root@bastion_dummy -p 22 -W %h:%p" == options[:proxy].command_line_template
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -o LogLevel=ERROR -o ForwardAgent=no -i #{conf[:key_files]} root@bastion_dummy -p 22 -W %h:%p" == options[:proxy].command_line_template
end
connection.stubs(:run_command)
Net::SSH.stub(:start, mock) do
Expand Down
3 changes: 1 addition & 2 deletions test/unit/transports/vmware_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: utf-8
require 'helper'
require 'train/transports/vmware'

describe 'Train::Transports::VMware::Connection' do
def add_stubs(stub_options)
Expand Down Expand Up @@ -30,8 +31,6 @@ def create_transport(options = {})
ENV['VISERVER_USERNAME'] = 'testuser'
ENV['VISERVER_PASSWORD'] = 'supersecurepassword'

# Need to require this here as it captures the ENV variables on load
require 'train/transports/vmware'
add_stubs(options[:stub_options] || {})
Train::Transports::VMware.new(options[:transport_options])
end
Expand Down
5 changes: 2 additions & 3 deletions test/unit/transports/winrm_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
end

it 'has default endpoint' do
winrm.options[:endpoint].must_equal nil
winrm.options[:endpoint].must_be_nil
end

it 'has default path set' do
Expand Down Expand Up @@ -64,7 +64,7 @@
end
it 'has default ca_trust_path set' do
winrm.options.key?(:ca_trust_path).must_equal true
winrm.options[:ca_trust_path].must_equal nil
winrm.options[:ca_trust_path].must_be_nil
end
end

Expand All @@ -87,7 +87,6 @@

describe 'when configuring the connection' do
let(:winrm) {
puts "Insantiating cls.new with #{conf}"
cls.new(conf)
}

Expand Down

0 comments on commit 91a3550

Please sign in to comment.