aboutsummaryrefslogtreecommitdiff
path: root/spec/defines/dhcp_failover_spec.rb
blob: d8400b0c933452a7a2af1c1f72a8fae3101c43cf (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
199
200
201
202
203
204
require 'spec_helper'

describe 'dhcp::failover' do
  let (:title) { 'failover-dhcp' }

  on_supported_os.each do |os, facts|
    context "on #{os}" do
      let(:facts) do
        facts.merge({
          :concat_basedir => '/var/lib/puppet/concat',
          :domain         => 'example.com',
          :ipaddress      => '1.2.3.3',
        })
      end

      context 'when passing wrong value for ensure' do
        let (:params) { {
          :ensure       => 'foo',
          :peer_address => '1.2.3.4',
        } }

        it 'should fail' do
          expect {
            should contain_file('/etc/dhcp/failover/failover-dhcp.conf')
          }.to raise_error(Puppet::Error, /"foo" does not match \["present", "absent"\]/)
        end
      end

      context 'when passing wrong type for address' do
        let (:params) { {
          :address      => true,
          :peer_address => '1.2.3.4',
        } }

        it 'should fail' do
          expect {
            should contain_file('/etc/dhcp/failover/failover-dhcp.conf')
          }.to raise_error(Puppet::Error, /true is not a string\./)
        end
      end

      context 'when passing wrong value for address' do
        let (:params) { {
          :address      => 'foo',
          :peer_address => '1.2.3.4',
        } }

        it 'should fail' do
          expect {
            should contain_file('/etc/dhcp/failover/failover-dhcp.conf')
          }.to raise_error(Puppet::Error, /is not a valid IPv4 address\./)
        end
      end

      context 'when not passing peer_address' do
        it 'should fail' do
          expect {
            should contain_file('/etc/dhcp/failover/failover-dhcp.conf')
          }.to raise_error(Puppet::Error, /Must pass peer_address to Dhcp::Failover/)
        end
      end

      context 'when passing wrong type for peer_address' do
        let (:params) { {
          :peer_address => true,
        } }

        it 'should fail' do
          expect {
            should contain_file('/etc/dhcp/failover/failover-dhcp.conf')
          }.to raise_error(Puppet::Error, /true is not a string\./)
        end
      end

      context 'when passing wrong value for peer_address' do
        let (:params) { {
          :peer_address => 'foo',
        } }

        it 'should fail' do
          expect {
            should contain_file('/etc/dhcp/failover/failover-dhcp.conf')
          }.to raise_error(Puppet::Error, /is not a valid IPv4 address\./)
        end
      end

      context 'when passing wrong type for peer_port' do
        let (:params) { {
          :peer_port    => 'foo',
          :peer_address => '1.2.3.4',
        } }

        it 'should fail' do
          expect {
            should contain_file('/etc/dhcp/failover/failover-dhcp.conf')
          }.to raise_error(Puppet::Error, /Expected first argument to be an Integer/)
        end
      end

      context 'when passing wrong type for port' do
        let (:params) { {
          :peer_address => '1.2.3.4',
          :port         => 'foo',
        } }

        it 'should fail' do
          expect {
            should contain_file('/etc/dhcp/failover/failover-dhcp.conf')
          }.to raise_error(Puppet::Error, /Expected first argument to be an Integer/)
        end
      end



      context 'when using defaults' do
        let (:params) { {
          :peer_address => '1.2.3.4',
        } }

        it { should contain_file('/etc/dhcp/failover/failover-dhcp.conf').with(
          :ensure  => 'file',
          :owner   => 'root',
          :group   => 'root'
        ).with_content(
          /failover peer "failover-dhcp" \{\n/
        ).with_content(
          /primary;\n/
        ).with_content(
          /address 1.2.3.3;\n/
        ).with_content(
          /port 647;\n/
        ).with_content(
          /peer address 1.2.3.4;\n/
        ).with_content(
          /peer port 647;\n/
        ) }
        it { should contain_concat__fragment('dhcp.failover.failover-dhcp').with({
          :content => "include \"/etc/dhcp/failover/failover-dhcp.conf\";\n",
          :target  => '/etc/dhcp/dhcpd.conf',
        })}
      end

      context 'when passing options as a hash' do
        let (:params) { {
          :peer_address => '1.2.3.4',
          :options      => {
            'max-response-delay'       => '30',
            'max-unacked-updates'      => '10',
            'load balance max seconds' => '3',
            'mclt'                     => '1800',
            'split'                    => '128',
          },
        } }

        it { should contain_file('/etc/dhcp/failover/failover-dhcp.conf').with({
          :ensure  => 'file',
          :owner   => 'root',
          :group   => 'root'
        }).with_content(
          /failover peer "failover-dhcp" \{\n/
        ).with_content(
          /  load balance max seconds 3;\n/
        ).with_content(
          /  max-response-delay 30;\n/
        ).with_content(
          /  max-unacked-updates 10;\n/
        ).with_content(
          /  mclt 1800;\n/
        ).with_content(
          /  split 128;\n/
        ) }
      end

      context 'when overriding all parameters' do
        let (:params) { {
          :peer_address => '1.2.3.4',
          :address      => '1.2.3.5',
          :peer_port    => 847,
          :port         => 847,
          :options      => {
            'mclt' => 1800,
          },
          :role         => 'secondary',
        } }

        it { should contain_file('/etc/dhcp/failover/failover-dhcp.conf').with({
          :ensure  => 'file',
          :owner   => 'root',
          :group   => 'root'
        }).with_content(
          /secondary;\n/
        ).with_content(
          /address 1.2.3.5;\n/
        ).with_content(
          /port 847;\n/
        ).with_content(
          /peer port 847;\n/
        ).with_content(
          /mclt 1800;\n/
        ) }
      end
    end
  end
end