aboutsummaryrefslogtreecommitdiff
path: root/spec/spec_helper_acceptance.rb
blob: 660c1ce5f50b29227b2ebcd828caffa3545bf31c (plain)
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
require 'beaker-rspec'
require 'beaker/module_install_helper'
require 'beaker/puppet_install_helper'
require 'voxpupuli/acceptance/spec_helper_acceptance'

$LOAD_PATH << File.join(__dir__, 'acceptance/lib')

# TODO: This should be added to Beaker
def assert_matching_arrays(expected, actual, message = '')
  assert_equal(expected.sort, actual.sort, message)
end

# TODO: Remove the wrappers to user_present
# and user_absent if Beaker::Host's user_present
# and user_absent functions are fixed to work with
# Arista (EOS).

def user_present(host, username)
  case host['platform']
  when %r{eos}
    on(host, "useradd #{username}")
  else
    host.user_present(username)
  end
end

def user_absent(host, username)
  case host['platform']
  when %r{eos}
    on(host, "userdel #{username}", acceptable_exit_codes: [0, 1])
  else
    host.user_absent(username)
  end
end

def beaker_opts
  { debug: true, trace: true, expect_failures: true, acceptable_exit_codes: (0...256) }
  # { expect_failures: true, acceptable_exit_codes: (0...256) }
end

def compatible_agents
  agents.reject { |agent| agent['platform'].include?('windows') || agent['platform'].include?('eos-') }
end

def clean(agent, o = {})
  o = { user: 'tstuser' }.merge(o)
  run_cron_on(agent, :remove, o[:user])
  apply_manifest_on(agent, %([user{'%s': ensure => absent, managehome => false }]) % o[:user])
end

def setup(agent, o = {})
  o = { user: 'tstuser' }.merge(o)
  apply_manifest_on(agent, %(user { '%s': ensure => present, managehome => false }) % o[:user])
  apply_manifest_on(agent, %(case $facts['os']['name'] {
                                centos, redhat, fedora: {$cron = 'cronie'}
                                solaris: { $cron = 'core-os' }
                                default: {$cron ='cron'} }
                                package {'cron': name=> $cron, ensure=>present, }))
end

# Returns all of the lines that correspond to crontab entries
# on the agent. For simplicity, these are any lines that do
# not begin with '#'.
def crontab_entries_of(host, username)
  crontab_contents = run_cron_on(host, :list, username).stdout.strip
  crontab_contents.lines.map(&:chomp).reject { |line| line =~ %r{^#} }
end

def resource_manifest(resource, title, params = {})
  params_str = params.map { |param, value|
    # This is not quite correct for all parameter values,
    # but it is good enough for most purposes.
    value_str = value.to_s
    value_str = "\"#{value_str}\"" if value.is_a?(String)

    "  #{param} => #{value_str}"
  }.join(",\n")

  <<-MANIFEST
  #{resource} { '#{title}':
  #{params_str}
}
   MANIFEST
end

def file_manifest(path, params = {})
  resource_manifest('file', path, params)
end

def cron_manifest(entry_name, params = {})
  resource_manifest('cron', entry_name, params)
end

def user_manifest(username, params = {})
  resource_manifest('user', username, params)
end

RSpec.configure do |c|
  c.before :suite do
    unless ENV['BEAKER_provision'] == 'no'
      run_puppet_install_helper
      install_module_on(hosts_as('default'))
      install_module_dependencies_on(hosts)
    end
  end
end