aboutsummaryrefslogtreecommitdiff
path: root/spec/acceptance/hosts.rb
blob: 297d92334a246901948041c0c766691864c07ae6 (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
test_name 'Augeas hosts file' do
  tag 'risk:medium',
      'audit:medium',
      'audit:acceptance',
      'audit:refactor' # move to puppet types test directory, this is not testing puppet apply
  # reduce to a single manifest and apply

  skip_test 'requires augeas which is included in AIO' if @options[:type] != 'aio'

  confine :except, platform: [
    'windows',
    'cisco_ios', # PUP-7380
  ]
  confine(:to, {}, hosts.reject { |host| host[:roles].include?('master') })

  step 'Backup the hosts file' do
    on hosts, 'cp /etc/hosts /tmp/hosts.bak'
  end

  # We have a begin/ensure block here to clean up the hosts file in case
  # of test failure.
  begin
    step 'Create an entry in the hosts file' do
      manifest = <<EOF
augeas { 'add_hosts_entry':
  context => '/files/etc/hosts',
  incl    => '/etc/hosts',
  lens    => 'Hosts.lns',
  changes => [
    'set 01/ipaddr 192.168.0.1',
    'set 01/canonical pigiron.example.com',
    'set 01/alias[1] pigiron',
    'set 01/alias[2] piggy'
  ]
}
EOF
      on hosts, puppet_apply('--verbose'), stdin: manifest
      on hosts, "fgrep '192.168.0.1\tpigiron.example.com pigiron piggy' /etc/hosts"
    end

    step 'Modify an entry in the hosts file' do
      manifest = <<EOF
augeas { 'mod_hosts_entry':
  context => '/files/etc/hosts',
  incl    => '/etc/hosts',
  lens    => 'Hosts.lns',
  changes => [
    'set *[canonical = "pigiron.example.com"]/alias[last()+1] oinker'
  ]
}
EOF

      on hosts, puppet_apply('--verbose'), stdin: manifest
      on hosts, "fgrep '192.168.0.1\tpigiron.example.com pigiron piggy oinker' /etc/hosts"
    end

    step 'Remove an entry from the hosts file' do
      manifest = <<EOF
augeas { 'del_hosts_entry':
  context => '/files/etc/hosts',
  incl    => '/etc/hosts',
  lens    => 'Hosts.lns',
  changes => [
    'rm *[canonical = "pigiron.example.com"]'
  ]
}
EOF

      on hosts, puppet_apply('--verbose'), stdin: manifest
      on hosts, "fgrep 'pigiron.example.com' /etc/hosts", acceptable_exit_codes: [1]
    end
  ensure
    on hosts, 'cat /tmp/hosts.bak > /etc/hosts && rm /tmp/hosts.bak'
  end
end