Skip to content

Commit

Permalink
Use shared example for indempotent resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Valantin committed Mar 12, 2024
1 parent 934afa2 commit 3936467
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 107 deletions.
67 changes: 26 additions & 41 deletions spec/acceptance/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
next if version < '6.0' && default[:platform] =~ %r{debian-12}

describe "zabbix::agent class with zabbix_version #{version}" do
before(:all) do
prepare_host
end

context 'With minimal parameter' do
it 'works idempotently with no errors' do
pp = <<-EOS
class { 'zabbix::agent':
server => '192.168.20.11',
zabbix_package_state => 'latest',
zabbix_version => '#{version}',
}
EOS

prepare_host

# Run it twice and test for idempotency
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-PUPPET
class { 'zabbix::agent':
server => '192.168.20.11',
zabbix_package_state => 'latest',
zabbix_version => '#{version}',
}
PUPPET
end
end

# do some basic checks
Expand All @@ -40,17 +40,17 @@ class { 'zabbix::agent':
end

context 'With ListenIP set to an IP-Address' do
it 'works idempotently with no errors' do
pp = <<-EOS
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-PUPPET
class { 'zabbix::agent':
server => '192.168.20.11',
zabbix_package_state => 'latest',
listenip => '127.0.0.1',
zabbix_version => '#{version}',
}
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
PUPPET
end
end

describe file('/etc/zabbix/zabbix_agentd.conf') do
Expand All @@ -59,36 +59,21 @@ class { 'zabbix::agent':
end

context 'With ListenIP set to lo' do
it 'works idempotently with no errors' do
pp = <<-EOS
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-PUPPET
class { 'zabbix::agent':
server => '192.168.20.11',
zabbix_package_state => 'latest',
listenip => 'lo',
zabbix_version => '#{version}',
}
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

context 'With ListenIP set to an IP-Address' do
it 'works idempotently with no errors' do
pp = <<-EOS
class { 'zabbix::agent':
server => '192.168.20.11',
zabbix_package_state => 'latest',
listenip => '127.0.0.1',
zabbix_version => '#{version}',
}
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
PUPPET
end
end

describe file('/etc/zabbix/zabbix_agentd.conf') do
its(:content) { is_expected.to match %r{ListenIP=127.0.0.1} }
end
describe file('/etc/zabbix/zabbix_agentd.conf') do
its(:content) { is_expected.to match %r{ListenIP=127.0.0.1} }
end
end
end
Expand Down
35 changes: 14 additions & 21 deletions spec/acceptance/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

require 'spec_helper_acceptance'
describe 'zabbix::server class', unless: default[:platform] =~ %r{archlinux} do
before(:all) do
prepare_host
end

context 'default parameters' do
# Using puppet_apply as a helper
it 'works idempotently with no errors' do
it_behaves_like 'an idempotent resource' do
# this is a minimal working example if you've a postgres server
# running on another node. multinode testing with beaker is pain,
# so we will deploy multiple services into one box
Expand All @@ -15,16 +18,12 @@
# EOS

# this will actually deploy apache + postgres + zabbix-server + zabbix-web
pp = <<-EOS
let(:manifest) do
<<-PUPPET
class { 'zabbix::database': }
-> class { 'zabbix::server': }
EOS

prepare_host

# Run it twice and test for idempotency
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
PUPPET
end
end

# do some basic checks
Expand All @@ -49,8 +48,7 @@ class { 'zabbix::database': }
next if zabbix_version < '6.0' && default[:platform] =~ %r{debian-12}

context "deploys a zabbix #{zabbix_version} server" do
# Using puppet_apply as a helper
it 'works idempotently with no errors' do
it_behaves_like 'an idempotent resource' do
# this is a minimal working example if you've a postgres server
# running on another node. multinode testing with beaker is pain,
# so we will deploy multiple services into one box
Expand All @@ -59,20 +57,15 @@ class { 'zabbix::database': }
# manage_database => false,
# }
# EOS

# this will actually deploy apache + postgres + zabbix-server + zabbix-web
pp = <<-EOS
let(:manifest) do
<<-PUPPET
class { 'zabbix::database': }
-> class { 'zabbix::server':
zabbix_version => "#{zabbix_version}"
}
EOS

prepare_host

# Run it twice and test for idempotency
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
PUPPET
end
end

# do some basic checks
Expand Down
84 changes: 39 additions & 45 deletions spec/acceptance/zabbix_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,20 @@ class { 'zabbix':
end

# setup proxies within zabbix
pp2 = <<-EOS
zabbix_proxy { 'ZabbixProxy1':
mode => 0,
}
zabbix_proxy { 'ZabbixProxy2':
ipaddress => '127.0.0.3',
use_ip => false,
mode => 1,
port => 10055,
}
EOS

it 'works idempotently with no errors' do
# Run it twice and test for idempotency
apply_manifest(pp2, catch_failures: true)
apply_manifest(pp2, catch_changes: true)
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-PUPPET
zabbix_proxy { 'ZabbixProxy1':
mode => 0,
}
zabbix_proxy { 'ZabbixProxy2':
ipaddress => '127.0.0.3',
use_ip => false,
mode => 1,
port => 10055,
}
PUPPET
end
end

let(:result_proxies) do
Expand Down Expand Up @@ -104,22 +102,20 @@ class { 'zabbix':

context 'update zabbix_proxy resources' do
# This will update the Zabbix proxies created above by switching their configuration
pp_update = <<-EOS
zabbix_proxy { 'ZabbixProxy1':
ipaddress => '127.0.0.3',
use_ip => false,
mode => 1,
port => 10055,
}
zabbix_proxy { 'ZabbixProxy2':
mode => 0,
}
EOS

it 'works idempotently with no errors' do
# Run it twice and test for idempotency
apply_manifest(pp_update, catch_failures: true)
apply_manifest(pp_update, catch_changes: true)
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-PUPPET
zabbix_proxy { 'ZabbixProxy1':
ipaddress => '127.0.0.3',
use_ip => false,
mode => 1,
port => 10055,
}
zabbix_proxy { 'ZabbixProxy2':
mode => 0,
}
PUPPET
end
end

let(:result_proxies) do
Expand Down Expand Up @@ -166,19 +162,17 @@ class { 'zabbix':

context 'delete zabbix_proxy resources' do
# This will delete the Zabbix proxies create above
pp_delete = <<-EOS
zabbix_proxy { 'ZabbixProxy1':
ensure => absent,
}
zabbix_proxy { 'ZabbixProxy2':
ensure => absent,
}
EOS

it 'works idempotently with no errors' do
# Run it twice and test for idempotency
apply_manifest(pp_delete, catch_failures: true)
apply_manifest(pp_delete, catch_changes: true)
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-PUPPET
zabbix_proxy { 'ZabbixProxy1':
ensure => absent,
}
zabbix_proxy { 'ZabbixProxy2':
ensure => absent,
}
PUPPET
end
end

let(:result_proxies) do
Expand Down

0 comments on commit 3936467

Please sign in to comment.