summaryrefslogtreecommitdiff
path: root/spec/classes/ntp_config_spec.rb
blob: 51db491a5d167756ab71a6e1c9cb30eed6c88bea (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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
require 'spec_helper'

describe 'ntp::config' do

  def param_value(subject, type, title, param)
    catalogue.resource(type, title).send(:parameters)[param.to_sym]
  end

  let(:params) {{:servers => 'fake.pool.ntp.org'}  }

  describe 'test platform specific resources' do

    describe "for operating system family Debian" do

      let(:params) {{}}
      let(:facts) {{ :osfamily => 'debian' }}

      it 'should use the debian ntp servers by default' do
        content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
        expected_lines = ['server 0.debian.pool.ntp.org iburst',
         'server 1.debian.pool.ntp.org iburst',
         'server 2.debian.pool.ntp.org iburst',
         'server 3.debian.pool.ntp.org iburst']
        (content.split("\n") & expected_lines).should == expected_lines
      end
    end

    describe "for operating system family RedHat" do

      let(:params) {{}}
      let(:facts) {{ :osfamily => 'redhat' }}

      it 'should use the redhat ntp servers by default' do
        content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
        expected_lines = [
         'server 0.centos.pool.ntp.org',
         'server 1.centos.pool.ntp.org',
         'server 2.centos.pool.ntp.org']
        (content.split("\n") & expected_lines).should == expected_lines
      end
    end

    describe "for operating system family SuSE" do

      let(:params) {{}}
      let(:facts) {{ :osfamily => 'suse' }}

      it 'should use the opensuse ntp servers by default' do
        content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
        expected_lines = [
         'server 0.opensuse.pool.ntp.org',
         'server 1.opensuse.pool.ntp.org',
         'server 2.opensuse.pool.ntp.org',
         'server 3.opensuse.pool.ntp.org']
        (content.split("\n") & expected_lines).should == expected_lines
      end
    end

    describe "for operating system family FreeBSD" do

      let(:params) {{}}
      let(:facts) {{ :osfamily => 'freebsd' }}

      it 'should use the freebsd ntp servers by default' do
        content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
        expected_lines = [
          "server 0.freebsd.pool.ntp.org iburst maxpoll 9",
          "server 1.freebsd.pool.ntp.org iburst maxpoll 9",
          "server 2.freebsd.pool.ntp.org iburst maxpoll 9",
          "server 3.freebsd.pool.ntp.org iburst maxpoll 9"]
        (content.split("\n") & expected_lines).should == expected_lines
      end

      describe "for operating system family Archlinux" do
  
        let(:params) {{}}
        let(:facts) {{ :osfamily => 'Archlinux' }}
  
        it 'should use the NTP pool servers by default' do
          content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
          expected_lines = [
            "server 0.pool.ntp.org",
            "server 1.pool.ntp.org",
            "server 2.pool.ntp.org"]
          (content.split("\n") & expected_lines).should == expected_lines
        end
      end

      describe "for operating system family unsupported" do
        let(:facts) {{
          :osfamily  => 'unsupported',
        }}

        it { expect{ subject }.to raise_error(
          /^The ntp module is not supported on an unsupported based system./
        )}
      end

    end

    describe 'for virtual machines' do

      let(:params) {{}}
      let(:facts) {{ :osfamily        => 'Archlinux',
                     :is_virtual      => 'true' }}

      it 'should not use local clock as a time source' do
        content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
        expected_lines = [
          'server  127.127.1.0  # local clock',
          'fudge  127.127.1.0 stratum 10' ]
        (content.split("\n") & expected_lines).should_not == expected_lines
      end

      it 'allows large clock skews' do
        content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
        expected_lines = [ 'tinker panic 0' ]
        (content.split("\n") & expected_lines).should == expected_lines
      end

    end

    describe 'for physical machines' do

      let(:params) {{}}
      let(:facts) {{ :osfamily        => 'Archlinux',
                     :is_virtual      => 'false' }}

      it 'disallows large clock skews' do
        content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
        expected_lines = [ 'tinker panic 0' ]
        (content.split("\n") & expected_lines).should_not == expected_lines
      end

    end

    describe "for operating system Gentoo" do

      let(:params) {{}}
      let(:facts) {{ :operatingsystem => 'Gentoo',
                      :osfamily        => 'Linux' }}


      it 'should use the NTP pool servers by default' do
        content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
        expected_lines = [
          "server 0.gentoo.pool.ntp.org",
          "server 1.gentoo.pool.ntp.org",
          "server 2.gentoo.pool.ntp.org",
          "server 3.gentoo.pool.ntp.org"]
        (content.split("\n") & expected_lines).should == expected_lines
      end
    end

    ['Debian', 'RedHat','SuSE', 'FreeBSD', 'Archlinux'].each do |osfamily|
      describe "for operating system family #{osfamily}" do

        let(:facts) {{ :osfamily => osfamily }}

        it { should contain_file('/etc/ntp.conf').with_owner('0') }
        it { should contain_file('/etc/ntp.conf').with_group('0') }
        it { should contain_file('/etc/ntp.conf').with_mode('0644') }
        it 'should allow template to be overridden' do
          params[:config_template] = 'my_ntp/ntp.conf.erb'
          content = param_value(subject, 'file', '/etc/ntp.conf', 'content')
          expected_lines = ['server foobar']
          (content.split("\n") & expected_lines).should == expected_lines
        end
      end

      ['Debian', 'RedHat','SuSE', 'FreeBSD', 'Archlinux'].each do |osfamily|

        describe "keys for osfamily #{osfamily}" do
          context "when enabled" do
            let(:facts) {{ :osfamily => osfamily }}
            let(:params) {{
              :keys_enable     => true,
              :keys_file       => '/etc/ntp/ntp.keys',
              :keys_trusted    => ['1', '2', '3'],
              :keys_controlkey => '2',
              :keys_requestkey => '3',
            }}

            it { should contain_file('/etc/ntp').with({
              'ensure'  => 'directory'})
            }
            it { should contain_file('/etc/ntp.conf').with({
              'content' => /trustedkey 1 2 3/})
            }
            it { should contain_file('/etc/ntp.conf').with({
              'content' => /controlkey 2/})
            }
            it { should contain_file('/etc/ntp.conf').with({
              'content' => /requestkey 3/})
            }
          end
        end

        context "when disabled" do
          let(:facts) {{ :osfamily => osfamily }}
          let(:params) {{
            :keys_enable     => false,
            :keys_file       => '/etc/ntp/ntp.keys',
            :keys_trusted    => ['1', '2', '3'],
            :keys_controlkey => '2',
            :keys_requestkey => '3',
          }}

          it { should_not contain_file('/etc/ntp').with({
            'ensure'  => 'directory'})
          }
          it { should_not contain_file('/etc/ntp.conf').with({
            'content' => /trustedkey 1 2 3/})
          }
          it { should_not contain_file('/etc/ntp.conf').with({
            'content' => /controlkey 2/})
          }
          it { should_not contain_file('/etc/ntp.conf').with({
            'content' => /requestkey 3/})
          }
        end
      end

      describe 'preferred servers' do
        context "when set" do
          let(:facts) {{ :osfamily => osfamily }}
          let(:params) {{
            :servers           => ['a', 'b', 'c', 'd'],
            :preferred_servers => ['a', 'b']
          }}

          it { should contain_file('/etc/ntp.conf').with({
            'content' => /server a prefer\nserver b prefer\nserver c\nserver d/})
          }
        end
        context "when not set" do
          let(:facts) {{ :osfamily => osfamily }}
          let(:params) {{
            :servers           => ['a', 'b', 'c', 'd'],
            :preferred_servers => []
          }}

          it { should_not contain_file('/etc/ntp.conf').with({
            'content' => /server a prefer/})
          }
        end
      end
    end
  end

end