common.rb
1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
gem "test-unit" # http://rubyforge.org/pipermail/test-unit-tracker/2009-July/000075.html
gem 'mocha'
require 'test/unit'
require 'mocha/setup'
require 'pty'
require 'expect'
module IntegrationTestHelpers
def sh command
puts "$ #{command}"
res = system(command)
status = $?
raise "Command: #{command} failed:#{status.exitstatus}" unless res
end
def tmpdir(&block)
Dir.mktmpdir do |dir|
yield(dir)
end
end
def set_authorized_key(user,pubkey)
authorized_key = "/home/#{user}/.ssh/authorized_keys"
sh "sudo cp #{pubkey} #{authorized_key}"
sh "sudo chown #{user} #{authorized_key}"
sh "sudo chmod 0744 #{authorized_key}"
end
def with_agent(&block)
puts "/usr/bin/ssh-agent -c"
agent_out = `/usr/bin/ssh-agent -c`
agent_out.split("\n").each do |line|
if line =~ /setenv (\S+) (\S+);/
ENV[$1] = $2
puts "ENV[#{$1}]=#{$2}"
end
end
begin
yield
ensure
sh "/usr/bin/ssh-agent -k"
end
end
def ssh_add(key,password)
command = "ssh-add #{key}"
status = nil
PTY.spawn(command) do |reader, writer, pid|
begin
reader.expect(/Enter passphrase for .*:/) { |data| puts data }
writer.puts(password)
until reader.eof? do
puts reader.readline
end
rescue Errno::EIO => e
end
pid, status = Process.wait2 pid
end
raise "Command: #{command} failed:#{status.exitstatus}" unless status
status.exitstatus
end
end