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 authored and evgeni committed Oct 5, 2023
1 parent 3ac0681 commit 276b0e0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 1 deletion.
18 changes: 17 additions & 1 deletion spec/classes/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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 @@ -85,7 +92,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 @@ -431,6 +437,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 @@ -54,6 +54,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 @@ -73,6 +74,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 @@ -95,6 +97,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 @@ -117,6 +120,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
4 changes: 4 additions & 0 deletions 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 @@ -83,6 +86,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') } if facts[:os]['name'] == 'CentOS'
it { is_expected.to contain_package('zabbix-required-scl-repo').with_ensure('latest').with_name('oracle-softwarecollection-release-el7') } if facts[:os]['name'] == 'OracleLinux'
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 @@ -48,6 +48,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
15 changes: 15 additions & 0 deletions spec/classes/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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 @@ -41,11 +45,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 @@ -71,6 +80,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 @@ -83,6 +94,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 @@ -154,6 +168,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
17 changes: 17 additions & 0 deletions spec/classes/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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 @@ -42,6 +57,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 276b0e0

Please sign in to comment.