Class: SpecHelper

Inherits:
Object
  • Object
show all
Includes:
ExampleHelper
Defined in:
lib/serverspec_launcher/spec_helper.rb

Overview

Does all the setup fo r the serverspec tests

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExampleHelper

#load_bundled_examples, #load_examples_from_gem, #load_shared_examples, #shared_examples

Constructor Details

#initialize(host = ENV['TARGET_HOST'], target = ENV['TARGET'], properties = nil, task_source = ENV['TASK_SOURCE'], environment = ENV['TASK_ENV']) ⇒ SpecHelper

Returns a new instance of SpecHelper.



26
27
28
29
30
31
32
# File 'lib/serverspec_launcher/spec_helper.rb', line 26

def initialize(host = ENV['TARGET_HOST'], target = ENV['TARGET'], properties = nil, task_source = ENV['TASK_SOURCE'], environment = ENV['TASK_ENV'])
  @host = host
  @target = target
  @source = task_source
  @environment = environment
  load_properties properties
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



24
25
26
# File 'lib/serverspec_launcher/spec_helper.rb', line 24

def properties
  @properties
end

#target_propertiesObject (readonly)

Returns the value of attribute target_properties.



24
25
26
# File 'lib/serverspec_launcher/spec_helper.rb', line 24

def target_properties
  @target_properties
end

#target_variablesObject (readonly)

Returns the value of attribute target_variables.



24
25
26
# File 'lib/serverspec_launcher/spec_helper.rb', line 24

def target_variables
  @target_variables
end

Class Method Details

.load(host = , target = , properties = nil, task_source = , environment = ) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/serverspec_launcher/spec_helper.rb', line 191

def self.load(host = ENV['TARGET_HOST'], target = ENV['TARGET'], properties = nil, task_source = ENV['TASK_SOURCE'], environment = ENV['TASK_ENV'])
  props = PropertiesLoader.new properties
  helper = SpecHelper.new host, target, props.properties, task_source, environment
  props = helper.properties[:shared_example_gems] || []
  helper.load_shared_examples props
  helper.setup_backend
  helper
end

Instance Method Details

#docker_backendObject



163
164
165
166
167
168
169
# File 'lib/serverspec_launcher/spec_helper.rb', line 163

def docker_backend
  set :backend, :docker

  set_docker_image
  set_docker_container
  set :docker_url, @target_properties[:docker_url] if @target_properties[:docker_url]
end

#load_properties(properties = nil) ⇒ Object



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
# File 'lib/serverspec_launcher/spec_helper.rb', line 34

def load_properties(properties = nil)
  @properties = if properties
                  properties.deep_symbolize_keys
                else
                  YAML.load_file('properties.yml').deep_symbolize_keys
                end
  @target_properties = if @source == 'environment'
   @properties[:environments][@environment.to_sym][:targets][@target.to_sym]
  else
    @properties[:targets][@target.to_sym]
  end
  if @source == 'environment'
    vars = @properties[:environments][@environment.to_sym][:variables] ? @properties[:environments][@environment.to_sym][:variables] : {}
    @target_variables =  @properties[:variables] ? @properties[:variables].deep_merge(vars) : {}.deep_merge(vars)
  else
    @target_variables = @properties[:variables] ? @properties[:variables] : {}
  end

  @backend = @target_properties[:backend] || 'ssh'
  @target_properties[:target] = ENV['TASK_NAME']

  @target_properties[:variables] =  @target_variables.deep_merge(@target_properties[:variables])
  @target_properties[:environment] =  @properties[:environment] ? @properties[:variables].deep_merge(@target_properties[:environment]) : {}.deep_merge(@target_properties[:environment])

  set_property @target_properties
end

#password_checks(options) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/serverspec_launcher/spec_helper.rb', line 123

def password_checks(options)
  if ENV['ASK_LOGIN_PASSWORD']
    begin
      require 'highline/import'
    rescue LoadError
      raise 'highline is not available. Try installing it.'
    end
    options[:password] = ask('Enter login password: ') { |q| q.echo = false }
  else
    options[:password] =  ENV['LOGIN_PASSWORD']
  end
end

#set_docker_containerObject



171
172
173
174
# File 'lib/serverspec_launcher/spec_helper.rb', line 171

def set_docker_container
  set :docker_container, @target_properties[:docker_container] if @target_properties[:docker_container]
  set :docker_container_create_options, @target_properties[:docker_options] if @target_properties[:docker_options]
end

#set_docker_imageObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/serverspec_launcher/spec_helper.rb', line 176

def set_docker_image
  if @target_properties[:dockerfile]
    puts "Building Docker Image from #{@target_properties[:dockerfile]}"
    commands = File.read(@target_properties[:dockerfile])
    image = Docker::Image.build(commands)
    set :docker_image, image.id
  elsif @target_properties[:docker_build_dir]
    puts "Building Docker Image in dir #{@target_properties[:docker_build_dir]}"
    image = Docker::Image.build_from_dir(@target_properties[:docker_build_dir])
    set :docker_image, image.id
  else
    set :docker_image, @target_properties[:docker_image] if @target_properties[:docker_image]
  end
end

#setup_backendObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/serverspec_launcher/spec_helper.rb', line 61

def setup_backend
  if @backend == 'exec'
    set :backend, :exec
  elsif @backend == 'docker'
    docker_backend
  elsif @backend == 'inspec'
  else
    ssh_backend
  end
end

#ssh_backendObject



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
# File 'lib/serverspec_launcher/spec_helper.rb', line 72

def ssh_backend
  set :backend, :ssh

  sudo_checks



  ssh_user = @target_properties[:user] || Etc.getlogin

  options = if @backend == 'vagrant'
              vagrant_backend
            else
              Net::SSH::Config.for(@host)
            end
  password_checks(options)
  options[:user] ||= ssh_user
  options[:keys] = [@target_properties[:identity_file]] if @target_properties[:identity_file]
  options[:port] = @target_properties[:ssh_port] if @target_properties[:ssh_port]
  host_key_checking(options)


  set :host, options[:host_name] || @host
  set :ssh_options, options


  # Disable sudo
  # set :disable_sudo, true

  # Set environment variables
  return unless @target_properties[:environment]

  env = @target_properties[:environment].map do |en|
    variable, value = en.split('=')
    { variable.to_sym => value }
  end.reduce({}, :merge)
  set :env, env
end

#sudo_checksObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/serverspec_launcher/spec_helper.rb', line 110

def sudo_checks
  if ENV['ASK_SUDO_PASSWORD']
    begin
      require 'highline/import'
    rescue LoadError
      raise 'highline is not available. Try installing it.'
    end
    set :sudo_password, ask('Enter sudo password: ') { |q| q.echo = false }
  else
    set :sudo_password, ENV['SUDO_PASSWORD']
  end
end

#vagrant_backendObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/serverspec_launcher/spec_helper.rb', line 136

def vagrant_backend
  @host = @target_properties[:vagrant_host] || 'default'
  vagrant_dir = @target_properties[:vagrant_dir]

  old_dir = Dir.pwd
  Dir.chdir(vagrant_dir) if vagrant_dir
  reprovision = @target_properties[:vagrant_reprovision] ? '--provision' : ''
  vagrant_up = "vagrant up #{@host} #{reprovision}"
  if @target_properties[:vagrant_output]
    Open3.popen2e vagrant_up do |_stdin, stdout_and_stderr, _wait_thr|
      while (line = stdout_and_stderr.gets) do
        puts line
      end
    end
  elsif @target_properties[:vagrant_errors]
    system(vagrant_up, out: File::NULL)
  else
    system(vagrant_up, out: File::NULL, err: File::NULL)
  end

  config = Tempfile.new('', Dir.tmpdir)
  config.write(`vagrant ssh-config #{@host}`)
  config.close
  Dir.chdir old_dir
  Net::SSH::Config.for(@host, [config.path])
end