Skip to content

Commit

Permalink
Add unit tests to reach 100% resource coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Friderici committed Mar 30, 2023
1 parent 52576bb commit 0f315bf
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 2 deletions.
18 changes: 17 additions & 1 deletion spec/classes/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
# service = facts[:osfamily] == 'Gentoo' ? 'zabbix-agentd' : 'zabbix-agent'

context 'with all defaults' do
it { is_expected.to contain_selinux__module('zabbix-agent') } if facts[:os]['name'] == 'CentOS'
it { is_expected.to contain_yumrepo('zabbix-frontend') } if facts[:os]['name'] == 'CentOS' && facts[:os]['release']['major'] == '7'
it { is_expected.to contain_package('zabbix-required-scl-repo') } if facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'] == '7'
it { is_expected.to contain_apt__key('zabbix-A1848F5') } if facts[:os]['family'] == 'Debian'
it { is_expected.to contain_apt__key('zabbix-FBABD5F') } if facts[:os]['family'] == 'Debian'
it { is_expected.to contain_file(include_dir).with_ensure('directory') }

# Make sure package will be installed, service running and ensure of directory.
if facts[:kernel] == 'windows'
it do
Expand All @@ -81,7 +88,6 @@
that_requires(["Package[#{package_name}]", "Zabbix::Startup[#{service_name}]"])
end

it { is_expected.to contain_file(include_dir).with_ensure('directory') }
it { is_expected.to contain_zabbix__startup(service_name).that_requires("Package[#{package_name}]") }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_class('zabbix::params') }
Expand Down Expand Up @@ -427,6 +433,16 @@
end
end
end

describe 'with systemd active' do
if facts[:kernel] == 'Linux'
let :facts do
super().merge(systemd: true)
end

it { is_expected.to contain_systemd__unit_file('zabbix-agent.service') }
end
end
end
end
end
4 changes: 4 additions & 0 deletions spec/classes/database_mysql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
}
end

it { is_expected.to contain_class('zabbix::params') }
it { is_expected.to contain_class('zabbix::database::mysql') }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('zabbix_server_create.sql').with_command("cd #{path} && if [ -f #{sql_server}.gz ]; then gunzip -f #{sql_server}.gz ; fi && mysql -h 'rspec.puppet.com' -u 'zabbix-server' -p'zabbix-server' -P 3306 -D 'zabbix-server' < #{sql_server} && touch /etc/zabbix/.schema.done") }
Expand All @@ -75,6 +76,7 @@
}
end

it { is_expected.to contain_class('zabbix::params') }
it { is_expected.to contain_class('zabbix::database::mysql') }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_exec('zabbix_server_create.sql').with_command("cd #{path} && if [ -f #{sql_server}.gz ]; then gunzip -f #{sql_server}.gz ; fi && mysql -h 'rspec.puppet.com' -u 'zabbix-server' -p'zabbix-server' -D 'zabbix-server' < #{sql_server} && touch /etc/zabbix/.schema.done") }
Expand All @@ -97,6 +99,7 @@
}
end

it { is_expected.to contain_class('zabbix::params') }
it { is_expected.to contain_class('zabbix::database::mysql') }
it { is_expected.to compile.with_all_deps }

Expand All @@ -119,6 +122,7 @@
}
end

it { is_expected.to contain_class('zabbix::params') }
it { is_expected.to contain_class('zabbix::database::mysql') }
it { is_expected.to compile.with_all_deps }

Expand Down
5 changes: 5 additions & 0 deletions spec/classes/javagateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
it { is_expected.to contain_service('zabbix-java-gateway').with_ensure('running') }
it { is_expected.to contain_service('zabbix-java-gateway').with_enable('true') }
it { is_expected.to contain_service('zabbix-java-gateway').with_require(['Package[zabbix-java-gateway]', 'File[/etc/zabbix/zabbix_java_gateway.conf]']) }

it { is_expected.to contain_yumrepo('zabbix-frontend') } if facts[:os]['name'] == 'CentOS' && facts[:os]['release']['major'] == '7'
it { is_expected.to contain_package('zabbix-required-scl-repo') } if facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'] == '7'
it { is_expected.to contain_apt__key('zabbix-A1848F5') } if facts[:os]['family'] == 'Debian'
it { is_expected.to contain_apt__key('zabbix-FBABD5F') } if facts[:os]['family'] == 'Debian'
end

context 'when declaring manage_repo is true' do
Expand Down
5 changes: 4 additions & 1 deletion spec/classes/repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
it { is_expected.to contain_class('zabbix::params') }
it { is_expected.to contain_class('zabbix::repo') }

it { is_expected.to contain_apt__key('zabbix-A1848F5') } if facts[:os]['family'] == 'Debian'
it { is_expected.to contain_apt__key('zabbix-FBABD5F') } if facts[:os]['family'] == 'Debian'

context 'when repo_location is "https://example.com/foo"' do
let :params do
{
Expand Down Expand Up @@ -109,7 +112,7 @@
it { is_expected.to contain_yumrepo('zabbix').with_gpgkey('https://repo.zabbix.com/RPM-GPG-KEY-ZABBIX-A14FE591') }
it { is_expected.to contain_yumrepo('zabbix-nonsupported').with_baseurl('https://repo.zabbix.com/non-supported/rhel/7/$basearch/') }
it { is_expected.to contain_yumrepo('zabbix-nonsupported').with_gpgkey('https://repo.zabbix.com/RPM-GPG-KEY-ZABBIX-79EA5ED4') }

it { is_expected.to contain_yumrepo('zabbix-frontend') }
it { is_expected.to contain_package('zabbix-required-scl-repo').with_ensure('latest').with_name('centos-release-scl') }
end
when '9'
Expand Down
3 changes: 3 additions & 0 deletions spec/classes/sender_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
when 'RedHat'
it { is_expected.to contain_yumrepo('zabbix-nonsupported') }
it { is_expected.to contain_yumrepo('zabbix') }

it { is_expected.to contain_yumrepo('zabbix-frontend') } if facts[:os]['release']['major'] == '7'
it { is_expected.to contain_package('zabbix-required-scl-repo') } if facts[:os]['release']['major'] == '7'
when 'Debian'
it { is_expected.to contain_apt__source('zabbix') }
it { is_expected.to contain_apt__key('zabbix-A1848F5') }
Expand Down
29 changes: 29 additions & 0 deletions spec/classes/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
it { is_expected.to contain_class('zabbix::params') }
it { is_expected.to contain_service('zabbix-server').with_ensure('running') }
it { is_expected.to contain_zabbix__startup('zabbix-server') }

it { is_expected.to contain_apt__source('zabbix') } if facts[:os]['family'] == 'Debian'
it { is_expected.to contain_apt__key('zabbix-A1848F5') } if facts[:os]['family'] == 'Debian'
it { is_expected.to contain_apt__key('zabbix-FBABD5F') } if facts[:os]['family'] == 'Debian'
end

if facts[:osfamily] == 'RedHat'
Expand All @@ -37,11 +41,16 @@
end

it { is_expected.to contain_selboolean('zabbix_can_network').with('value' => 'on', 'persistent' => true) }
it { is_expected.to contain_selinux__module('zabbix-server').with_ensure('present') }
it { is_expected.to contain_selinux__module('zabbix-server-ipc').with_ensure('present') }
end

describe 'with defaults' do
it { is_expected.to contain_yumrepo('zabbix-nonsupported') }
it { is_expected.to contain_yumrepo('zabbix') }

it { is_expected.to contain_yumrepo('zabbix-frontend') } if facts[:os]['release']['major'] == '7'
it { is_expected.to contain_package('zabbix-required-scl-repo') } if facts[:os]['release']['major'] == '7'
end
end

Expand All @@ -67,6 +76,8 @@
it { is_expected.to contain_package('zabbix-server-pgsql').with_ensure('present') }
it { is_expected.to contain_package('zabbix-server-pgsql').with_name('zabbix-server-pgsql') }
it { is_expected.to contain_file('/etc/zabbix/zabbix_server.conf').with_require('Package[zabbix-server-pgsql]') }
it { is_expected.to contain_exec('update_pgpass') }
it { is_expected.to contain_file('/root/.pgpass') }
end

describe 'with database_type as mysql' do
Expand All @@ -79,6 +90,9 @@
it { is_expected.to contain_package('zabbix-server-mysql').with_ensure('present') }
it { is_expected.to contain_package('zabbix-server-mysql').with_name('zabbix-server-mysql') }
it { is_expected.to contain_file('/etc/zabbix/zabbix_server.conf').with_require('Package[zabbix-server-mysql]') }
it { is_expected.to contain_exec('zabbix_server_create.sql') }
it { is_expected.to contain_exec('zabbix_server_data.sql') }
it { is_expected.to contain_exec('zabbix_server_images.sql') }
end

# Include directory should be available.
Expand Down Expand Up @@ -150,6 +164,7 @@
when 'Archlinux', 'Debian', 'Gentoo', 'RedHat'
it { is_expected.to contain_file('/etc/init.d/zabbix-server').with_ensure('absent') }
it { is_expected.to contain_file('/etc/systemd/system/zabbix-server.service').with_ensure('file') }
it { is_expected.to contain_systemd__unit_file('zabbix-server.service') }
else
it { is_expected.to contain_file('/etc/init.d/zabbix-server').with_ensure('file') }
it { is_expected.not_to contain_file('/etc/systemd/system/zabbix-server.service') }
Expand Down Expand Up @@ -410,6 +425,20 @@
it { is_expected.to contain_file('/etc/zabbix/zabbix_server.conf').with_content %r{^VaultURL=https://127.0.0.1:8200$} }
end

describe 'with zabbix_version 5.4' do
let(:params) { { zabbix_version: '5.4' } }

it do
is_expected.to contain_package('zabbix-sql-scripts').only_with(
{
'ensure' => 'present',
'require' => 'Class[Zabbix::Repo]',
'tag' => 'zabbix',
}
)
end
end

describe 'with zabbix_version 5.4 and report parameters defined' do
let :params do
{
Expand Down
17 changes: 17 additions & 0 deletions spec/classes/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,22 @@

context 'with all defaults' do
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_class('Zabbix::Params') }
it { is_expected.to contain_class('Zabbix::Repo') }
it { is_expected.to contain_file('/etc/zabbix/web').with_ensure('directory') }

it { is_expected.to contain_apt__key('zabbix-A1848F5') } if facts[:os]['family'] == 'Debian'
it { is_expected.to contain_apt__key('zabbix-FBABD5F') } if facts[:os]['family'] == 'Debian'
it { is_expected.to contain_apt__source('zabbix') } if facts[:os]['family'] == 'Debian'
it { is_expected.to contain_yumrepo('zabbix') } if facts[:os]['family'] == 'RedHat'
it { is_expected.to contain_yumrepo('zabbix-nonsupported') } if facts[:os]['family'] == 'RedHat'
it { is_expected.to contain_yumrepo('zabbix-frontend') } if facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'] == '7'
it { is_expected.to contain_package('zabbix-required-scl-repo') } if facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'] == '7'
it { is_expected.to contain_service('rh-php72-php-fpm') } if facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'] == '7'
it { is_expected.to contain_file('/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf') } if facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'] == '7'
it { is_expected.to contain_file('/etc/zabbix/zabbix.conf.php') } if facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'] == '7' && Puppet::Util::Package.versioncmp(zabbix_version, '5.0') >= 0
it { is_expected.to contain_service('php-fpm') } if facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'] >= '8'
it { is_expected.to contain_file('/etc/php-fpm.d/zabbix.conf') } if facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'] >= '8'
end

describe 'with enforcing selinux' do
Expand All @@ -44,6 +59,8 @@
end

it { is_expected.to contain_selboolean('httpd_can_connect_zabbix').with('value' => 'on', 'persistent' => true) }
it { is_expected.to contain_selboolean('httpd_can_network_connect_db').with('value' => 'on', 'persistent' => true) }
it { is_expected.to contain_apache__vhost('localhost') }
end

describe 'with false selinux' do
Expand Down

0 comments on commit 0f315bf

Please sign in to comment.