From ef93b7e5db9d495b9437dfb7ba9d9bbfd966949e Mon Sep 17 00:00:00 2001 From: Ashley Penney Date: Tue, 30 Jul 2013 13:01:14 -0400 Subject: Rewrite the spec testing. A lot of changes here, primarily to unify the spec testing in a single spec file to reflect the changes to the private ntp::config and ::install classes. As a side effect make sure we properly test each supported distribution (including Gentoo properly) and prefer to use 'content' => /blah/ rather than other methods of testing file contents. --- spec/classes/ntp_config_spec.rb | 251 ------------------------------------- spec/classes/ntp_install_spec.rb | 52 -------- spec/classes/ntp_service_spec.rb | 73 ----------- spec/classes/ntp_spec.rb | 260 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 253 insertions(+), 383 deletions(-) delete mode 100644 spec/classes/ntp_config_spec.rb delete mode 100644 spec/classes/ntp_install_spec.rb delete mode 100644 spec/classes/ntp_service_spec.rb (limited to 'spec') diff --git a/spec/classes/ntp_config_spec.rb b/spec/classes/ntp_config_spec.rb deleted file mode 100644 index 51db491..0000000 --- a/spec/classes/ntp_config_spec.rb +++ /dev/null @@ -1,251 +0,0 @@ -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 diff --git a/spec/classes/ntp_install_spec.rb b/spec/classes/ntp_install_spec.rb deleted file mode 100644 index 4ed263e..0000000 --- a/spec/classes/ntp_install_spec.rb +++ /dev/null @@ -1,52 +0,0 @@ -require 'spec_helper' - -describe 'ntp::install' do - - ['Debian', 'RedHat', 'SuSE', 'FreeBSD', 'Archlinux'].each do |osfamily| - describe "for osfamily #{osfamily}" do - - let(:facts) {{ :osfamily => osfamily }} - let(:params) {{ - :package_ensure => 'present', - :package_name => 'ntp', - }} - - it { should contain_package('ntp').with( - :ensure => 'present', - :name => 'ntp' - )} - - it 'should allow package ensure to be overridden' do - params[:package_ensure] = 'latest' - subject.should contain_package('ntp').with_ensure('latest') - end - - it 'should allow the package name to be overridden' do - params[:package_name] = 'hambaby' - subject.should contain_package('ntp').with_name('hambaby') - end - - end - end - - describe "for distribution gentoo" do - - let(:facts) {{ :osfamily => 'Linux', :operatingsystem => 'Gentoo' }} - let(:params) {{ - :package_ensure => 'present', - :package_name => 'net-misc/ntp', - }} - - it { should contain_package('ntp').with( - :ensure => 'present', - :name => 'net-misc/ntp' - )} - - it 'should allow package ensure to be overridden' do - params[:package_ensure] = 'latest' - subject.should contain_package('ntp').with_ensure('latest') - end - - end - -end diff --git a/spec/classes/ntp_service_spec.rb b/spec/classes/ntp_service_spec.rb deleted file mode 100644 index bbf9dd1..0000000 --- a/spec/classes/ntp_service_spec.rb +++ /dev/null @@ -1,73 +0,0 @@ -require 'spec_helper' - -describe 'ntp::service' do - - ['Debian', 'RedHat', 'SuSE', 'FreeBSD', 'Archlinux'].each do |osfamily| - describe "for osfamily #{osfamily}" do - - let(:facts) {{ :osfamily => osfamily }} - let(:params) {{ - :service_manage => true, - :service_enable => true, - :service_ensure => 'running', - :service_name => 'ntp' - }} - - it { should contain_service('ntp').with( - :enable => true, - :ensure => 'running', - :name => 'ntp' - )} - - it 'should allow service ensure to be overridden' do - params[:service_ensure] = 'stopped' - subject.should contain_service('ntp').with_ensure('stopped') - end - end - end - - ['Gentoo'].each do |operatingsystem| - describe "for distribution #{operatingsystem}" do - - let(:facts) {{ :osfamily => 'Linux', :operatingsystem => operatingsystem }} - let(:params) {{ - :service_manage => true, - :service_enable => true, - :service_ensure => 'running', - :service_name => 'ntpd' } - } - - it 'should contain service' do - should contain_service('ntp').with( - :enable => true, - :ensure => 'running', - :name => 'ntpd') - end - - it 'should allow service ensure to be overridden' do - params[:service_ensure] = 'stopped' - subject.should contain_service('ntp').with_ensure('stopped') - end - - end - end - - describe "isn't managed if service_manage is false" do - - let(:facts) {{ :osfamily => 'Debian' }} - - let(:params) {{ - :service_manage => false, - :service_enable => true, - :service_ensure => 'running', - :service_name => 'ntpd', - }} - - it { should_not contain_service('ntp').with( - :enable => true, - :ensure => 'running', - :name => 'ntpd' - )} - end - -end diff --git a/spec/classes/ntp_spec.rb b/spec/classes/ntp_spec.rb index 4ffd817..6c636f4 100644 --- a/spec/classes/ntp_spec.rb +++ b/spec/classes/ntp_spec.rb @@ -2,14 +2,260 @@ require 'spec_helper' describe 'ntp' do - let(:facts) {{ :osfamily => 'Debian' }} + ['Debian', 'RedHat','SuSE', 'FreeBSD', 'Archlinux', 'Gentoo'].each do |system| + if system == 'Gentoo' + let(:facts) {{ :osfamily => 'Linux', :operatingsystem => system }} + else + let(:facts) {{ :osfamily => system }} + end - it { should include_class('ntp::install') } - it { should include_class('ntp::config') } - it { should include_class('ntp::service') } + it { should include_class('ntp::install') } + it { should include_class('ntp::config') } + it { should include_class('ntp::service') } - # These are currently breaking for me. - #it { should have_class_count(3) } - #it { should have_resource_count(0) } + describe 'ntp::config on #{system}' do + 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') } + + describe 'allows template to be overridden' do + let(:params) {{ :config_template => 'my_ntp/ntp.conf.erb' }} + it { should contain_file('/etc/ntp.conf').with({ + 'content' => /server foobar/}) + } + end + + describe "keys for osfamily #{system}" do + context "when enabled" do + 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(: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 + + describe 'preferred servers' do + context "when set" do + 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(:params) {{ + :servers => ['a', 'b', 'c', 'd'], + :preferred_servers => [] + }} + + it { should_not contain_file('/etc/ntp.conf').with({ + 'content' => /server a prefer/}) + } + end + end + + describe 'ntp::install on #{system}' do + let(:params) {{ :package_ensure => 'present', :package_name => ['ntp'], }} + + it { should contain_package('ntp').with( + :ensure => 'present', + :name => 'ntp' + )} + + describe 'should allow package ensure to be overridden' do + let(:params) {{ :package_ensure => 'latest', :package_name => ['ntp'] }} + it { should contain_package('ntp').with_ensure('latest') } + end + + describe 'should allow the package name to be overridden' do + let(:params) {{ :package_ensure => 'present', :package_name => ['hambaby'] }} + it { should contain_package('ntp').with_name('hambaby') } + end + end + + describe 'ntp::service' do + let(:params) {{ + :service_manage => true, + :service_enable => true, + :service_ensure => 'running', + :service_name => 'ntp' + }} + + describe 'with defaults' do + it { should contain_service('ntp').with( + :enable => true, + :ensure => 'running', + :name => 'ntp' + )} + end + + describe 'service_ensure' do + describe 'when overridden' do + let(:params) {{ :service_name => 'ntp', :service_ensure => 'stopped' }} + it { should contain_service('ntp').with_ensure('stopped') } + end + end + + describe 'service_manage' do + let(:params) {{ + :service_manage => false, + :service_enable => true, + :service_ensure => 'running', + :service_name => 'ntpd', + }} + + it 'when set to false' do + should_not contain_service('ntp').with({ + 'enable' => true, + 'ensure' => 'running', + 'name' => 'ntpd' + }) + end + end + end + end + + context 'ntp::config' do + describe "for operating system Gentoo" do + let(:facts) {{ :operatingsystem => 'Gentoo', + :osfamily => 'Linux' }} + + it 'uses the NTP pool servers by default' do + should contain_file('/etc/ntp.conf').with({ + 'content' => /server \d.gentoo.pool.ntp.org/, + }) + end + end + describe "on osfamily Debian" do + let(:facts) {{ :osfamily => 'debian' }} + + it 'uses the debian ntp servers by default' do + should contain_file('/etc/ntp.conf').with({ + 'content' => /server \d.debian.pool.ntp.org iburst/, + }) + end + end + + describe "on osfamily RedHat" do + let(:facts) {{ :osfamily => 'RedHat' }} + + it 'uses the redhat ntp servers by default' do + should contain_file('/etc/ntp.conf').with({ + 'content' => /server \d.centos.pool.ntp.org/, + }) + end + end + + describe "on osfamily SuSE" do + let(:facts) {{ :osfamily => 'SuSE' }} + + it 'uses the opensuse ntp servers by default' do + should contain_file('/etc/ntp.conf').with({ + 'content' => /server \d.opensuse.pool.ntp.org/, + }) + end + end + + describe "on osfamily FreeBSD" do + let(:facts) {{ :osfamily => 'FreeBSD' }} + + it 'uses the freebsd ntp servers by default' do + should contain_file('/etc/ntp.conf').with({ + 'content' => /server \d.freebsd.pool.ntp.org iburst maxpoll 9/, + }) + end + end + + describe "on osfamily ArchLinux" do + let(:facts) {{ :osfamily => 'ArchLinux' }} + + it 'uses the NTP pool servers by default' do + should contain_file('/etc/ntp.conf').with({ + 'content' => /server \d.pool.ntp.org/, + }) + 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(:facts) {{ :osfamily => 'Archlinux', + :is_virtual => 'true' }} + + it 'should not use local clock as a time source' do + should_not contain_file('/etc/ntp.conf').with({ + 'content' => /server.*127.127.1.0.*fudge.*127.127.1.0 stratum 10/, + }) + end + + it 'allows large clock skews' do + should contain_file('/etc/ntp.conf').with({ + 'content' => /tinker panic 0/, + }) + end + end + + describe 'for physical machines' do + let(:facts) {{ :osfamily => 'Archlinux', + :is_virtual => 'false' }} + + it 'disallows large clock skews' do + should_not contain_file('/etc/ntp.conf').with({ + 'content' => /tinker panic 0/, + }) + end + end + end end -- cgit v1.2.3