Class: Kitchen::Verifier::Goss

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/verifier/goss.rb

Instance Method Summary collapse

Instance Method Details

#call(state) ⇒ Object

Runs the verifier on the instance.

Parameters:

  • state (Hash)

    mutable instance state

Raises:

  • (ActionFailed)

    if the action could not be completed



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/kitchen/verifier/goss.rb', line 76

def call(state)
  create_sandbox
  sandbox_dirs = Dir.glob(File.join(sandbox_path, '*'))

  instance.transport.connection(state) do |conn|
    conn.execute(install_command)
    conn.execute(init_command)
    info("Transferring files to #{instance.to_str}")
    conn.upload(sandbox_dirs, config[:root_path])
    debug('Transfer complete')
    conn.execute(prepare_command)
    conn.execute(run_command)
  end
rescue Kitchen::Transport::TransportFailed => ex
  if ex.message .include? '<TEST EXECUTION FAILED>'
    raise ActionFailed, "Action #verify failed for #{instance.to_str}."
  else
    raise ActionFailed, ex.message
  end
ensure
  cleanup_sandbox
end

#create_sandboxObject



139
140
141
142
# File 'lib/kitchen/verifier/goss.rb', line 139

def create_sandbox
  super
  prepare_suites
end

#env_varsObject



150
151
152
153
# File 'lib/kitchen/verifier/goss.rb', line 150

def env_vars
  return nil if config[:env_vars].none?
  config[:env_vars].map { |k, v| "#{k}=#{v}" }.join(' ')
end

#get_test_nameObject



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/kitchen/verifier/goss.rb', line 236

def get_test_name
  base_path = File.join(config[:test_base_path], config[:suite_name])
  remote_base_path = File.join(config[:root_path], 'suites')
  all_tests = ''
  local_suite_files.each do |test_file|
    if File.basename(test_file) != config[:goss_var_path] && File.basename(test_file).end_with?('.yml')
      all_tests += ' ' + test_file.sub(base_path, remote_base_path)
      end
  end
  all_tests
end

#goss_filename_flagsObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/kitchen/verifier/goss.rb', line 200

def goss_filename_flags
  <<-CMD
     ## Set the flags for GOSS command path
     VERSION="#{config[:goss_version]}"
     DISTRO="$(uname)"
     ## Need improvements
     if [ "$(uname -m)" = "x86_64" ]; then
       ARCH="amd64"
     else
       ARCH="386"
     fi

      if [ -f /etc/os-release ]; then

        if [ "$(grep -i 'ubuntu' /etc/os-release)" != ""  ]; then
          OS="ubuntu"
        fi
        if [ "$(grep -i 'centos' /etc/os-release)" != "" ]; then
          OS="centos"
        fi
        if [ "$(grep -i '7' /etc/os-release)" != "" ]; then
          VER='7'
        fi
        if [ "$(grep -i '16.04' /etc/os-release)" != "" ]; then
          VER='16.04'
        fi
      else
        OS="centos"
        VER="6"
      fi

      OS_VERSION=${OS}${VER}
      echo $OS_VERSION
    CMD
end

#init_commandObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kitchen/verifier/goss.rb', line 56

def init_command
  return if local_suite_files.empty?
  debug('Remove root_path on remote server.')
  <<-CMD
    suite_dir="#{config[:root_path]}"
    if [ "${suite_dir}" = "x" ]; then
      echo "root_path is not configured."
      exit 1
    fi
    ## Remove root_path
    rm -rf #{config[:root_path]}
    ## Create root_path
    mkdir -p #{config[:root_path]}
  CMD
end

#install_commandObject



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
# File 'lib/kitchen/verifier/goss.rb', line 24

def install_command
  # If cutom install
  info('Installing with custom install command') if config[:custom_install_command]
  return config[:custom_install_command] if config[:custom_install_command]

  info('Checking/Installing GOSS')
  prefix_command(wrap_shell_code(Util.outdent!(<<-CMD)))
    ## Get helper
    #{Kitchen::Util.shell_helpers}

    #{goss_filename_flags}
    download_url="#{config[:goss_link]}"
    goss_download_path="#{config[:goss_download_path]}"

    ## Check do we need to download GOSS
    if [ -f "/${goss_download_path}" ]; then
      echo "GOSS is installed in ${goss_download_path}"
    else
      echo "Checking compatibility"
      distro="$(uname)"
      if [ "x${distro}" != "xLinux" ]; then
        echo "Your distro '${distro}' is not supported."
        exit 1
      fi
      echo "Trying to download GOSS to ${goss_download_path}"
      do_download ${download_url} ${goss_download_path}
      chmod +x ${goss_download_path}
    fi
  CMD
end

#local_suite_filesArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an Array of test suite filenames for the related suite currently residing on the local workstation. Any special provisioner-specific directories (such as a Chef roles/ directory) are excluded.

Returns:

  • (Array<String>)

    array of suite files



131
132
133
134
135
136
# File 'lib/kitchen/verifier/goss.rb', line 131

def local_suite_files
  base = File.join(config[:test_base_path], config[:suite_name])
  glob = File.join(base, 'goss/**/*')
  # testfiles = Dir.glob(glob).reject { |f| File.directory?(f) }
  Dir.glob(glob).reject { |f| File.directory?(f) }
end

#prepare_suitesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Copies all test suite files into the suites directory in the sandbox.



115
116
117
118
119
120
121
122
123
# File 'lib/kitchen/verifier/goss.rb', line 115

def prepare_suites
  base = File.join(config[:test_base_path], config[:suite_name])

  local_suite_files.each do |src|
    dest = File.join(sandbox_suites_dir, src.sub("#{base}/", ''))
    FileUtils.mkdir_p(File.dirname(dest))
    FileUtils.cp(src, dest, preserve: true)
  end
end

#remote_var_fileObject



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/kitchen/verifier/goss.rb', line 155

def remote_var_file
  base_path = File.join(config[:test_base_path], config[:suite_name])
  remote_base_path = File.join(config[:root_path], 'suites')
  result = ''
  local_suite_files.each do |src|
    if File.basename(src) == config[:goss_var_path]
      result = src.sub(base_path, remote_base_path)
    end
  end
  result
end

#run_commandObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/kitchen/verifier/goss.rb', line 100

def run_command
  return if local_suite_files.empty?

  debug('Running tests')
  prefix_command(wrap_shell_code(Util.outdent!(<<-CMD)))
    set +e
    #{goss_filename_flags}
    command_validate_opts="validate --format #{config[:validate_output]}"
    #{run_test_command}
  CMD
end

#run_test_commandString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the run command to execute tests.

Returns:

  • (String)

    the run command to execute tests



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/kitchen/verifier/goss.rb', line 169

def run_test_command
  command = config[:goss_download_path]
  command = "sudo -E #{command}" if !config[:use_sudo] == true
  command = "#{env_vars} #{command}" if config[:env_vars].any?
  command = "#{command} --vars #{remote_var_file}" if config[:goss_var_path]
  puts command

  <<-CMD
    if [ ! -x "#{config[:goss_download_path]}" ]; then
        echo "Something failed cant execute '${command}'"
        exit 1
    fi

    test_failed=0
    for VARIABLE in #{get_test_name}
    do
      #{command} -g ${VARIABLE} ${command_validate_opts}
      if [ "$?" -ne 0 ]; then
        test_failed=1
      fi
    done

    # Check exit code
    if [ "$test_failed" -ne 0 ]; then
      test_failed=1
      echo "<TEST EXECUTION FAILED>"
    fi
    exit ${test_failed}
  CMD
end

#sandbox_suites_dirString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns path to suites directory under sandbox path.

Returns:

  • (String)

    path to suites directory under sandbox path



146
147
148
# File 'lib/kitchen/verifier/goss.rb', line 146

def sandbox_suites_dir
  File.join(sandbox_path, 'suites')
end

#sleep_if_setObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sleep for a period of time, if a value is set in the config.



251
252
253
254
255
256
# File 'lib/kitchen/verifier/goss.rb', line 251

def sleep_if_set
  config[:sleep].to_i.times do
    print '.'
    sleep 1
  end
end