aboutsummaryrefslogtreecommitdiff
path: root/spec/acceptance/tests/should_query_all.rb
blob: cc925667c5ef7a7de3161f06c1a5a40aa07db57b (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
test_name 'should query all hosts from hosts file'

tag 'audit:low',
    'audit:refactor',  # Use block style `test_name`
    'audit:acceptance' # Could be done at the integration (or unit) layer though
# actual changing of resources could irreparably damage a
# host running this, or require special permissions.

content = '127.0.0.1 test1 test1.local
127.0.0.2 test2 test2.local
127.0.0.3 test3 test3.local
127.0.0.4 test4 test4.local
'

agents.each do |agent|
  backup = agent.tmpfile('host-query-all')

  step 'configure the system for testing (including file backups)'
  on agent, "cp /etc/hosts #{backup}"
  on agent, 'cat > /etc/hosts', stdin: content

  step 'query all host records using puppet'
  on(agent, puppet_resource('host')) do
    found = stdout.scan(%r{host \{ '([^']+)'}).flatten.sort
    fail_test "the list of returned hosts was wrong: #{found.join(', ')}" unless
      found == ['test1', 'test2', 'test3', 'test4']

    count = stdout.scan(%r{ensure\s+=>\s+'present'}).length
    fail_test "found #{count} records, wanted 4" unless count == 4
  end

  step 'clean up the system afterwards'
  on agent, "cat #{backup} > /etc/hosts && rm -f #{backup}"
end