forked from Normation/rudder-tests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec_helper.rb
84 lines (65 loc) · 2.1 KB
/
spec_helper.rb
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'serverspec'
require 'net/ssh'
require 'tempfile'
host = ENV['TARGET_HOST']
if host == "localhost"
set :backend, :exec
set :host, host
set :disable_sudo, true
else
set :backend, :ssh
`vagrant up #{host}`
config = Tempfile.new('', Dir.tmpdir)
`vagrant ssh-config #{host} > #{config.path}`
options = Net::SSH::Config.for(host, [config.path])
options[:user] ||= Etc.getlogin
set :host, options[:host_name] || host
set :ssh_options, options
end
# Set environment variables
set :env, :LANG => 'C', :LC_MESSAGES => 'C'
# Set PATH
# set :path, '/sbin:/usr/local/sbin:$PATH'
# Some common rudder test elements
$params = {}
ENV.each { |key, value|
if key.start_with?("RUDDER_")
key2 = key[7..-1]
$params[key2] = value
end
}
url = $params['SERVER']
token = $params['TOKEN']
$rudderCli = 'rudder-cli --skip-verify --url=' + url.to_s + ' --token=' + token.to_s
# Functions that can be used in tests
def send_file(from, to)
`mkdir -p sendfile/`
`cp #{from} sendfile/`
host = ENV['TARGET_HOST']
`vagrant ssh #{host} -c 'mv /vagrant/sendfile/#{from} #{to}'`
end
## monkeypatching serverspec
if defined? RSpec::Core::Formatters::BaseTextFormatter
# print test duration in dicumentation format
module RSpec
module Core
module Formatters
# @private
class DocumentationFormatter < BaseTextFormatter
def example_passed(passed)
output.puts passed_output(passed.example)
output.puts "#{current_indentation}time: #{passed.example.execution_result.run_time}s"
end
def example_pending(pending)
output.puts pending_output(pending.example, pending.example.execution_result.pending_message)
output.puts "#{current_indentation}time: #{pending.example.execution_result.run_time}s"
end
def example_failed(failure)
output.puts failure_output(failure.example, failure.example.execution_result.exception)
output.puts "#{current_indentation}time: #{failure.example.execution_result.run_time}s"
end
end
end
end
end
end