File: spec_helper.rb

package info (click to toggle)
serverspec-runner 1.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220 kB
  • sloc: ruby: 463; makefile: 10
file content (119 lines) | stat: -rw-r--r-- 3,530 bytes parent folder | download | duplicates (3)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require 'serverspec'
require 'pathname'
require 'net/ssh'
require 'net/ssh/proxy/command'
require 'yaml'
require 'csv'
require 'serverspec-runner/util/hash'

# require extension libraries
Dir.glob([
  ENV['specroot'] + '/lib/extension/serverspec/**/*.rb',
  ENV['specroot'] + '/lib/extension/specinfra/**/*.rb']).each {|f| require f}

ssh_opts_default = YAML.load_file(ENV['ssh_options'])
csv_path = ENV['result_csv']
explains = []
results = []
row_num = []
spacer_char = '  ' unless ENV['tableformat'] == 'csv'
spacer_char = ',' if ENV['tableformat'] == 'csv'

def get_example_desc(example_group, descriptions)

  descriptions << example_group[:description]
  return descriptions if example_group[:parent_example_group] == nil

  get_example_desc(example_group[:parent_example_group], descriptions)
end

RSpec.configure do |c|

  c.expose_current_running_example_as :example
  c.path = ENV['EXEC_PATH']

  run_path = c.files_to_run[0].split('/')

  speck_i = 0
  run_path.reverse.each_with_index do |r,i|
    if r == 'spec'
      speck_i = ((run_path.size - 1) - i)
    end
  end
  sliced = run_path.slice((speck_i + 1)..(run_path.size - 2))
  role_name = sliced.join('/')

  if ENV['ASK_SUDO_PASSWORD']
    require 'highline/import'
    c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
  else
    c.sudo_password = ENV['SUDO_PASSWORD']
  end

  set_property (YAML.load_file(ENV['platforms_tmp']))[ENV['TARGET_HOST'].to_sym]

  if ENV['TARGET_CONNECTION'] == 'ssh' || ENV['TARGET_SSH_HOST'] !~ /localhost|127\.0\.0\.1/
    c.host = ENV['TARGET_SSH_HOST']
    options = Net::SSH::Config.for(c.host, files=["~/.ssh/config"])
    ssh_opts ||= ssh_opts_default
    property[:ssh_opts].each { |k, v| ssh_opts[k.to_sym] = v } if property[:ssh_opts]
    ssh_opts[:proxy] = Kernel.eval(ssh_opts[:proxy]) if ssh_opts[:proxy]
    user    = options[:user] || ssh_opts[:user] || Etc.getlogin
    options.merge!(ssh_opts)
    set :ssh_options, options
    set :backend, :ssh
    set :request_pty, true
  else
    set :backend, :exec
  end

  prev_desc_hierarchy = nil

  c.before(:suite) do
    entity_host = (((ENV['TARGET_HOST'] != ENV['TARGET_SSH_HOST']) && (ENV['TARGET_SSH_HOST'] != nil)) ? "(#{ENV['TARGET_SSH_HOST']})" : "")
    puts "\e[33m"
    puts "### start [#{role_name}@#{ENV['TARGET_HOST']}] #{entity_host} serverspec... ###"
    print "\e[m"

    explains << "#{role_name}@#{ENV['TARGET_HOST']}#{entity_host}"
    results << ""
    row_num << 1
  end

  c.after(:each) do

    if ENV['explain'] == 'long'
      explains << spacer_char + example.metadata[:full_description]
      results << (self.example.exception ? 'NG' : 'OK')
      row_num << 1
    else

      spacer = ''
      desc_hierarchy = get_example_desc(self.example.metadata[:example_group], []).reverse
      desc_hierarchy.each_with_index do |ex, i|
        spacer += spacer_char

        if prev_desc_hierarchy != nil && prev_desc_hierarchy.length > i && prev_desc_hierarchy[i] == desc_hierarchy[i]
        else
          explains << spacer + ex
          results << ''
          row_num << i + 1
        end
      end

      explains << spacer + spacer_char + (self.example.metadata[:description] || '')
      results << (self.example.exception ? 'NG' : 'OK')
      row_num << desc_hierarchy.length + 1

      prev_desc_hierarchy = desc_hierarchy
    end
  end

  c.after(:suite) do
    CSV.open(csv_path, 'a') do |writer|
      explains.each_with_index do |v, i|
        writer << [v, results[i], row_num[i]]
      end
    end
  end
end