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.



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

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 = ENV['TASK_ENV']
  load_properties properties
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



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

def properties
  @properties
end

#target_propertiesObject (readonly)

Returns the value of attribute target_properties.



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

def target_properties
  @target_properties
end

#target_variablesObject (readonly)

Returns the value of attribute target_variables.



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

def target_variables
  @target_variables
end

Class Method Details

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



155
156
157
158
159
160
161
# File 'lib/serverspec_launcher/spec_helper.rb', line 155

def self.load(host = ENV['TARGET_HOST'], target = ENV['TARGET'], properties = nil, task_source = ENV['TASK_SOURCE'], environment = '')
  helper = SpecHelper.new host, target, 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



127
128
129
130
131
132
133
# File 'lib/serverspec_launcher/spec_helper.rb', line 127

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/serverspec_launcher/spec_helper.rb', line 33

def load_properties(properties = nil)
  @properties = properties ? properties.deep_symbolize_keys : YAML.load_file('properties.yml').deep_symbolize_keys
  if @source == 'environment'
    @target_properties = @properties[:environments][@environment.to_sym][:targets][@target.to_sym]
  else
    @target_properties = @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

#set_docker_containerObject



135
136
137
138
# File 'lib/serverspec_launcher/spec_helper.rb', line 135

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/serverspec_launcher/spec_helper.rb', line 140

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



56
57
58
59
60
61
62
63
64
65
# File 'lib/serverspec_launcher/spec_helper.rb', line 56

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

#ssh_backendObject



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

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

  options[:user] ||= ssh_user
  options[:keys] = [@target_properties[:identity_file]] if @target_properties[:identity_file]

  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



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/serverspec_launcher/spec_helper.rb', line 99

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/serverspec_launcher/spec_helper.rb', line 112

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
  `vagrant up #{@host}`

  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