diff --git a/spec/classes/timezone_spec.rb b/spec/classes/timezone_spec.rb index 50a19fd..34d116c 100644 --- a/spec/classes/timezone_spec.rb +++ b/spec/classes/timezone_spec.rb @@ -1,11 +1,95 @@ require 'spec_helper' -describe 'timezone' do - %w[Debian RedHat Gentoo FreeBSD].each do |osfamily| - describe "on supported osfamily: #{osfamily}" do - let(:title) { 'EST' } +describe 'timezone', type: :class do + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:params) { {} } + let(:facts) do + facts + end - include_examples osfamily + it { is_expected.to compile.with_all_deps } + + it { is_expected.to create_class('timezone') } + + case facts[:os]['family'] + when 'Debian' + + context 'when using default class parameters' do + it { is_expected.to contain_file('/etc/timezone') } + it { is_expected.to contain_file('/etc/timezone').with_ensure('file') } + it { is_expected.to contain_file('/etc/timezone').with_content(%r{Etc/UTC}) } + it { is_expected.to contain_exec('update_timezone').with_command(%r{^dpkg-reconfigure -f noninteractive tzdata$}) } + it { is_expected.to contain_package('tzdata').with(ensure: 'present', before: 'File[/etc/localtime]') } + it { is_expected.to contain_file('/etc/localtime').with(ensure: 'link', target: '/usr/share/zoneinfo/Etc/UTC') } + end + + context 'when timezone => "Europe/Berlin"' do + let(:params) { { timezone: 'Europe/Berlin' } } + + it { is_expected.to contain_file('/etc/timezone').with_content(%r{^Europe/Berlin$}) } + it { is_expected.to contain_file('/etc/localtime').with_target('/usr/share/zoneinfo/Europe/Berlin') } + end + + context 'when autoupgrade => true' do + let(:params) { { autoupgrade: true } } + + it { is_expected.to contain_package('tzdata').with_ensure('latest') } + end + + context 'when ensure => absent' do + let(:params) { { ensure: 'absent' } } + + it { is_expected.to contain_package('tzdata').with_ensure('present') } + it { is_expected.to contain_file('/etc/timezone').with_ensure('absent') } + it { is_expected.to contain_file('/etc/localtime').with_ensure('absent') } + end + + when 'RedHat' + case facts[:os]['release']['major'] + when '6' + context 'redhat/centos 6' do + context 'when using default class parameters' do + it { is_expected.to contain_package('tzdata').with(ensure: 'present', before: 'File[/etc/localtime]') } + + it { is_expected.to contain_file('/etc/sysconfig/clock').with_ensure('file') } + it { is_expected.to contain_file('/etc/sysconfig/clock').with_content(%r{^ZONE="Etc/UTC"$}) } + it { is_expected.not_to contain_exec('update_timezone') } + + it { is_expected.to contain_file('/etc/localtime').with(ensure: 'link', target: '/usr/share/zoneinfo/Etc/UTC') } + end + + context 'when timezone => "Europe/Berlin"' do + let(:params) { { timezone: 'Europe/Berlin' } } + + it { is_expected.to contain_file('/etc/sysconfig/clock').with_content(%r{^ZONE="Europe/Berlin"$}) } + it { is_expected.to contain_file('/etc/localtime').with_target('/usr/share/zoneinfo/Europe/Berlin') } + end + + context 'when autoupgrade => true' do + let(:params) { { autoupgrade: true } } + + it { is_expected.to contain_package('tzdata').with_ensure('latest') } + end + + context 'when ensure => absent' do + let(:params) { { ensure: 'absent' } } + + it { is_expected.to contain_package('tzdata').with_ensure('present') } + it { is_expected.to contain_file('/etc/sysconfig/clock').with_ensure('absent') } + it { is_expected.to contain_file('/etc/localtime').with_ensure('absent') } + end + end + when '7', '8' + context 'redhat/centos 7, 8' do + it { is_expected.not_to contain_file('/etc/sysconfig/clock') } + it { is_expected.to contain_file('/etc/localtime').with_ensure('link') } + it { is_expected.to contain_exec('update_timezone').with_command('timedatectl set-timezone Etc/UTC').with_unless('timedatectl status | grep "Timezone:\|Time zone:" | grep -q Etc/UTC') } + end + end # end RedHat version + else + pending "There are no tests for #{os}" + end # end OS Family end end end diff --git a/spec/fixtures/hiera/hiera.yaml b/spec/fixtures/hiera/hiera.yaml new file mode 100644 index 0000000..58f410e --- /dev/null +++ b/spec/fixtures/hiera/hiera.yaml @@ -0,0 +1,7 @@ +--- +:backends: + - yaml +:hierarchy: + - test +:yaml: + :datadir: 'spec/fixtures/hiera' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c8f3151..661a512 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,66 @@ +# This file is based on Vox Pupuli's modulesync +# https://github.com/voxpupuli/modulesync +# https://github.com/voxpupuli/modulesync_config +RSpec.configure do |c| + c.mock_with :rspec +end + require 'puppetlabs_spec_helper/module_spec_helper' -require 'pathname' +require 'rspec-puppet-facts' +require 'bundler' +include RspecPuppetFacts + +if ENV['DEBUG'] + Puppet::Util::Log.level = :debug + Puppet::Util::Log.newdestination(:console) +end + +if File.exist?(File.join(__dir__, 'default_module_facts.yml')) + facts = YAML.load(File.read(File.join(__dir__, 'default_module_facts.yml'))) + if facts + facts.each do |name, value| + add_custom_fact name.to_sym, value + end + end +end + +if Dir.exist?(File.expand_path('../../lib', __FILE__)) + require 'coveralls' + require 'simplecov' + require 'simplecov-console' + SimpleCov.formatters = [ + SimpleCov::Formatter::HTMLFormatter, + SimpleCov::Formatter::Console + ] + SimpleCov.start do + track_files 'lib/**/*.rb' + add_filter '/spec' + add_filter '/vendor' + add_filter '/.vendor' + add_filter Bundler.configured_bundle_path.path + end +end -dir = Pathname.new(__FILE__).parent -# Load all shared contexts and shared examples -Dir["#{dir}/support/**/*.rb"].sort.each { |f| require f } +RSpec.configure do |c| + c.hiera_config = 'spec/fixtures/hiera/hiera.yaml' + # getting the correct facter version is tricky. We use facterdb as a source to mock facts + # see https://github.com/camptocamp/facterdb + # people might provide a specific facter version. In that case we use it. + # Otherwise we need to match the correct facter version to the used puppet version. + # as of 2019-10-31, puppet 5 ships facter 3.11 and puppet 6 ships facter 3.14 + # https://puppet.com/docs/puppet/5.5/about_agent.html + # + # The environment variable `PUPPET_VERSION` is available in our travis environment, but we cannot rely on it + # if somebody runs the tests locally. For that case we should fallback the the puppet gem version. + c.default_facter_version = if ENV['FACTERDB_FACTS_VERSION'] + ENV['FACTERDB_FACTS_VERSION'] + else + puppet_version = ENV['PUPPET_VERSION'] ? ENV['PUPPET_VERSION'] : Gem.loaded_specs['puppet'].version.to_s + Gem::Dependency.new('', puppet_version).match?('', '5') ? '3.11.0' : '3.14.0' + end -at_exit { RSpec::Puppet::Coverage.report! } + # Coverage generation + c.after(:suite) do + RSpec::Puppet::Coverage.report! + end +end diff --git a/spec/support/debian.rb b/spec/support/debian.rb deleted file mode 100644 index 4ec9dde..0000000 --- a/spec/support/debian.rb +++ /dev/null @@ -1,49 +0,0 @@ -shared_examples 'Debian' do - let(:facts) do - { - os: { - name: 'Debian', - family: 'Debian', - release: { major: '8' } - } - } - end - - describe 'when using default class parameters' do - let(:params) { {} } - - it { is_expected.to create_class('timezone') } - it { is_expected.to contain_file('/etc/timezone') } - it { is_expected.to contain_file('/etc/timezone').with_ensure('file') } - it { is_expected.to contain_file('/etc/timezone').with_content(%r{Etc/UTC}) } - it { is_expected.to contain_exec('update_timezone').with_command(%r{^dpkg-reconfigure -f noninteractive tzdata$}) } - - it do - is_expected.to contain_package('tzdata').with(ensure: 'present', before: 'File[/etc/localtime]') - end - it do - is_expected.to contain_file('/etc/localtime').with(ensure: 'link', target: '/usr/share/zoneinfo/Etc/UTC') - end - - context 'when timezone => "Europe/Berlin"' do - let(:params) { { timezone: 'Europe/Berlin' } } - - it { is_expected.to contain_file('/etc/timezone').with_content(%r{^Europe/Berlin$}) } - it { is_expected.to contain_file('/etc/localtime').with_target('/usr/share/zoneinfo/Europe/Berlin') } - end - - context 'when autoupgrade => true' do - let(:params) { { autoupgrade: true } } - - it { is_expected.to contain_package('tzdata').with_ensure('latest') } - end - - context 'when ensure => absent' do - let(:params) { { ensure: 'absent' } } - - it { is_expected.to contain_package('tzdata').with_ensure('present') } - it { is_expected.to contain_file('/etc/timezone').with_ensure('absent') } - it { is_expected.to contain_file('/etc/localtime').with_ensure('absent') } - end - end -end diff --git a/spec/support/redhat.rb b/spec/support/redhat.rb deleted file mode 100644 index 9d0f5bc..0000000 --- a/spec/support/redhat.rb +++ /dev/null @@ -1,87 +0,0 @@ -shared_examples 'RedHat' do - describe 'when using default class parameters with osfamily => RedHat and major release => 6' do - let(:params) { {} } - let(:facts) do - { - os: { - name: 'CentOS', - family: 'RedHat', - release: { major: '6' } - } - } - end - - it { is_expected.to create_class('timezone') } - - it do - is_expected.to contain_package('tzdata').with(ensure: 'present', - before: 'File[/etc/localtime]') - end - - it { is_expected.to contain_file('/etc/sysconfig/clock').with_ensure('file') } - it { is_expected.to contain_file('/etc/sysconfig/clock').with_content(%r{^ZONE="Etc/UTC"$}) } - it { is_expected.not_to contain_exec('update_timezone') } - - it do - is_expected.to contain_file('/etc/localtime').with(ensure: 'link', - target: '/usr/share/zoneinfo/Etc/UTC') - end - - context 'when timezone => "Europe/Berlin"' do - let(:params) { { timezone: 'Europe/Berlin' } } - - it { is_expected.to contain_file('/etc/sysconfig/clock').with_content(%r{^ZONE="Europe/Berlin"$}) } - it { is_expected.to contain_file('/etc/localtime').with_target('/usr/share/zoneinfo/Europe/Berlin') } - end - - context 'when autoupgrade => true' do - let(:params) { { autoupgrade: true } } - - it { is_expected.to contain_package('tzdata').with_ensure('latest') } - end - - context 'when ensure => absent' do - let(:params) { { ensure: 'absent' } } - - it { is_expected.to contain_package('tzdata').with_ensure('present') } - it { is_expected.to contain_file('/etc/sysconfig/clock').with_ensure('absent') } - it { is_expected.to contain_file('/etc/localtime').with_ensure('absent') } - end - end - - describe 'when using default class parameters with osfamily => RedHat and major release => 7' do - let(:params) { {} } - let(:facts) do - { - os: { - name: 'CentOS', - family: 'RedHat', - release: { major: '7' } - } - } - end - - it { is_expected.to create_class('timezone') } - it { is_expected.not_to contain_file('/etc/sysconfig/clock') } - it { is_expected.to contain_file('/etc/localtime').with_ensure('link') } - it { is_expected.to contain_exec('update_timezone').with_command('timedatectl set-timezone Etc/UTC').with_unless('timedatectl status | grep "Timezone:\|Time zone:" | grep -q Etc/UTC') } - end - - describe 'when using default class parameters with osfamily => RedHat and major release => 8' do - let(:params) { {} } - let(:facts) do - { - os: { - name: 'CentOS', - family: 'RedHat', - release: { major: '8' } - } - } - end - - it { is_expected.to create_class('timezone') } - it { is_expected.not_to contain_file('/etc/sysconfig/clock') } - it { is_expected.to contain_file('/etc/localtime').with_ensure('link') } - it { is_expected.to contain_exec('update_timezone').with_command('timedatectl set-timezone Etc/UTC').with_unless('timedatectl status | grep "Timezone:\|Time zone:" | grep -q Etc/UTC') } - end -end