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

Support for AIX and targeted SSH testing #41

Merged
merged 21 commits into from
Dec 17, 2015
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
train-*.gem
r-train-*.gem
Gemfile.lock
.kitchen/
24 changes: 24 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ namespace :test do
path = File.join(File.dirname(__FILE__), 'test', 'integration')
sh('sh', '-c', "cd #{path} && kitchen test -c #{concurrency}")
end

# Target required:
# rake "test:ssh[user@server]"
# sh -c cd /home/foobarbam/src/gems/train/test/integration \
# && target=user@server ruby -I ../../lib test_ssh.rb tests/*
# ...
# Turn debug logging back on:
# debug=1 rake "test:ssh[user@server]"
# Use a different ssh key:
# key_files=/home/foobarbam/.ssh/id_rsa2 rake "test:ssh[user@server]"
# Run with a specific test:
# test=path_block_device_test.rb rake "test:ssh[user@server]"
task :ssh, [:target] do |t, args|
path = File.join(File.dirname(__FILE__), 'test', 'integration')
key_files = ENV['key_files'] || File.join(ENV['HOME'], '.ssh', 'id_rsa')

sh_cmd = "cd #{path} && target=#{args[:target]} key_files=#{key_files}"

sh_cmd += " debug=#{ENV['debug']}" if ENV['debug']
sh_cmd += ' ruby -I ../../lib test_ssh.rb tests/'
sh_cmd += ENV['test'] || '*'

sh('sh', '-c', sh_cmd)
end
end

# Print the current version of this gem or update it.
Expand Down
1 change: 1 addition & 0 deletions lib/train/extras.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Train::Extras
autoload :CommandWrapper, 'train/extras/command_wrapper'
autoload :FileCommon, 'train/extras/file_common'
autoload :LinuxFile, 'train/extras/linux_file'
autoload :AixFile, 'train/extras/aix_file'
autoload :WindowsFile, 'train/extras/windows_file'
autoload :OSCommon, 'train/extras/os_common'
autoload :Stat, 'train/extras/stat'
Expand Down
37 changes: 37 additions & 0 deletions lib/train/extras/aix_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# encoding: utf-8

require 'shellwords'
require 'train/extras/stat'

module Train::Extras
class AixFile < LinuxFile
def initialize(backend, path)
super(backend, path)
end

def content
return @content if defined?(@content)
@content = case
when !exist?, directory?
nil
when size.nil?, size == 0
''
else
@backend.run_command("cat #{@spath}").stdout || ''
end
end

def link_path
return nil unless symlink?
@link_path ||= (
@backend.run_command("perl -e 'print readlink shift' #{@spath}").stdout.chomp
)
end

def mounted?
@mounted ||= (
!@backend.run_command("lsfs -c #{@spath}").stdout.empty?
)
end
end
end
3 changes: 3 additions & 0 deletions lib/train/extras/os_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def to_hash
'windows' => %w{
windows
},
'aix' => %w{
aix
},
}

OS['linux'] = %w{linux alpine arch coreos exherbo gentoo slackware} +
Expand Down
29 changes: 29 additions & 0 deletions lib/train/extras/stat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def self.find_type(mode)
end

def self.stat(shell_escaped_path, backend)
return aix_stat(shell_escaped_path, backend) if backend.os.aix?
return bsd_stat(shell_escaped_path, backend) if backend.os.bsd?
return linux_stat(shell_escaped_path, backend) if backend.os.unix?
# all other cases we don't handle
Expand Down Expand Up @@ -88,5 +89,33 @@ def self.bsd_stat(shell_escaped_path, backend)
selinux_label: fields[8],
}
end

def self.aix_stat(shell_escaped_path, backend)
# Perl here b/c it is default on AIX
stat_cmd = <<-EOP
perl -e '
@a = lstat(shift) or exit 2;
$u = getpwuid($a[4]);
$g = getgrgid($a[5]);
printf("0%o\\n%s\\n%s\\n%d\\n%d\\n", $a[2], $u, $g, $a[9], $a[7])
' #{shell_escaped_path}
EOP

res = backend.run_command(stat_cmd)
return {} if res.exit_status != 0

fields = res.stdout.split("\n")
tmask = fields[0].to_i(8)

{
type: find_type(tmask),
mode: tmask & 00777,
owner: fields[1],
group: fields[2],
mtime: fields[3].to_i,
size: fields[4].to_i,
selinux_label: nil,
}
end
end
end
8 changes: 7 additions & 1 deletion lib/train/transports/ssh_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ def os
end

def file(path)
@files[path] ||= LinuxFile.new(self, path)
@files[path] ||= \
case os[:family]
when 'aix'
AixFile.new(self, path)
else
LinuxFile.new(self, path)
end
end

# (see Base::Connection#run_command)
Expand Down
5 changes: 4 additions & 1 deletion test/integration/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ def dup(o)
end

def root_group(os)
if os[:family] == 'freebsd'
case os[:family]
when 'freebsd'
'wheel'
when 'aix'
'system'
else
'root'
end
Expand Down
19 changes: 17 additions & 2 deletions test/integration/test_ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@

require_relative 'helper'
require 'train'
require 'logger'

backends = {}
backend_conf = {
'target' => 'ssh://vagrant@localhost',
'key_files' => '/root/.ssh/id_rsa',
'target' => ENV['target'] || 'vagrant@localhost',
'key_files' => ENV['key_files'] || '/root/.ssh/id_rsa',
'logger' => Logger.new(STDOUT),
}

backend_conf['target'] = 'ssh://' + backend_conf['target']
backend_conf['logger'].level = \
if ENV.key?('debug')
case ENV['debug'].to_s
when /^false$/i, /^0$/i
Logger::INFO
else
Logger::DEBUG
end
else
Logger::INFO
end

backends[:ssh] = proc { |*args|
conf = Train.target_config(backend_conf)
Train.create('ssh', conf).connection(args[0])
Expand Down
4 changes: 3 additions & 1 deletion test/integration/tests/path_folder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
file.type.must_equal(:directory)
end

if get_backend.call.os[:family] == 'freebsd'
case get_backend.call.os[:family]
when 'freebsd'
it 'has freebsd folder content behavior' do
file.content.must_equal("\u0003\u0000")
end
Expand All @@ -30,6 +31,7 @@
it 'has an sha256sum' do
file.sha256sum.must_equal('9b4fb24edd6d1d8830e272398263cdbf026b97392cc35387b991dc0248a628f9')
end

else
it 'has no content' do
file.content.must_equal(nil)
Expand Down