Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor acceptance test #325

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 89 additions & 96 deletions spec/acceptance/keepalived_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,35 @@
require 'spec_helper_acceptance'

describe 'keepalived class' do
context 'with default parameters' do
pp = <<-EOS
class { 'keepalived':
sysconf_options => '-D --vrrp',
}

notify { "Keepalived version was: ${facts['keepalived_version']}":
require => Class['keepalived'],
}
EOS

it 'works with no error' do
apply_manifest(pp, catch_failures: true)
end

it 'works idempotently' do
pp2 = <<-EOS
class { 'keepalived':
sysconf_options => '-D --vrrp',
}
EOS
apply_manifest(pp2, catch_changes: true)
end

it 'creates fact keepalived_version' do
service_fact = apply_manifest(pp, catch_failures: true)
expect(service_fact.output).to match %r{.*Keepalived version was: (\d.\d.\d).*}
context 'on master with vrrp instance' do
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-PUPPET
class { 'keepalived':
sysconf_options => '-D --vrrp',
}

keepalived::vrrp::instance { 'VI_50':
interface => $facts['networking']['primary'],
state => 'MASTER',
virtual_router_id => 50,
priority => 101,
auth_type => 'PASS',
auth_pass => 'secret',
virtual_ipaddress => [ '10.0.0.1/16' ],
}
PUPPET
end
end

describe package('keepalived') do
it { is_expected.to be_installed }
end

describe file('/etc/keepalived/keepalived.conf') do
it { is_expected.to be_file }
its(:content) { is_expected.not_to contain('include ') }
end
end

context 'on master with vrrp instance' do
pp = <<-EOS
class { 'keepalived':
sysconf_options => '-D --vrrp',
}

keepalived::vrrp::instance { 'VI_50':
interface => $facts['networking']['primary'],
state => 'MASTER',
virtual_router_id => 50,
priority => 101,
auth_type => 'PASS',
auth_pass => 'secret',
virtual_ipaddress => [ '10.0.0.1/16' ],
}
EOS

it 'works with no error' do
apply_manifest(pp, catch_failures: true)
end

it 'works idempotently' do
apply_manifest(pp, catch_changes: true)
end

describe file('/etc/keepalived/keepalived.conf') do
it { is_expected.to be_file }
its(:content) { is_expected.to match %r{.*MASTER.*} }
its(:content) { is_expected.not_to contain('include ') }
end

describe service('keepalived') do
Expand All @@ -78,63 +40,94 @@ class { 'keepalived':
end

# Works around any timing issues
it 'has acquired the ip' do
ip_result = shell('sleep 10; ip addr')
expect(ip_result.stdout).to match %r{.*inet 10\.0\.0\.1/16 .*}
describe 'when service running' do
it 'has acquired the ip' do
ip_result = shell('sleep 10; ip addr')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it should be like (just guessing from the file case above, never tried to execute):

describe shell('sleep 10; ip addr') do
  its(:stdout) { is_expected.to match %r{.*inet 10\.0\.0\.1/16 .*}
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for suggestion but I'm try now and in this way it fails, don't know why

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind then, it doesn't really matter :)

expect(ip_result.stdout).to match %r{.*inet 10\.0\.0\.1/16 .*}
end
end
end

context 'on master with globalconf' do
pp = <<-EOS
class { 'keepalived':
sysconf_options => '-D --vrrp',
}
class { 'keepalived::global_defs':
notification_email => '[email protected]',
}
EOS
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-PUPPET
class { 'keepalived':
sysconf_options => '-D --vrrp',
}

keepalived::vrrp::instance { 'VI_50':
interface => $facts['networking']['primary'],
state => 'MASTER',
virtual_router_id => 50,
priority => 101,
auth_type => 'PASS',
auth_pass => 'secret',
virtual_ipaddress => [ '10.0.0.1/16' ],
}
class { 'keepalived::global_defs':
notification_email => '[email protected]',
}
PUPPET
end
end

it 'works with no error' do
apply_manifest(pp, catch_failures: true)
describe file('/etc/keepalived/keepalived.conf') do
it { is_expected.to be_file }
its(:content) { is_expected.to contain('notification_email').from('global_defs').to('[email protected]') }
end
end

it 'works idempotently' do
apply_manifest(pp, catch_changes: true)
context 'with unmanaged external config' do
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-PUPPET
file { '/etc/keepalived/myconfig.conf':
owner => 'root',
group => 'root',
mode => '0644',
content => "vrrp_instance VI_50 { interface ${facts['networking']['primary']}
virtual_router_id 50 }",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the newline in the string looks a bit odd. Is that on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what separator keepalive use for different configuration, I've tried comma and semicolon but not work, with newline it pass the check during service start

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"...\n..." should be able to do the trick w/o wrapping the line then (I guess)

notify => Class['keepalived::service']
}

class { 'keepalived':
include_external_conf_files => ['/etc/keepalived/myconfig.conf'],
sysconf_options => '-D --vrrp',
}
PUPPET
end
end

describe file('/etc/keepalived/keepalived.conf') do
it { is_expected.to be_file }
its(:content) { is_expected.to contain('notification_email').from('global_defs').to('[email protected]') }
its(:content) { is_expected.to contain('include /etc/keepalived/myconfig.conf') }
end
end

context 'with unmanaged external config' do
context 'with minimal parameters' do
pp = <<-EOS
file { '/etc/keepalived/myconfig.conf':
owner => 'root',
group => 'root',
mode => '0644',
content => '',
notify => Class['keepalived::service']
}

class { 'keepalived':
include_external_conf_files => ['/etc/keepalived/myconfig.conf'],
sysconf_options => '-D --vrrp',
sysconf_options => '-D --vrrp',
}
EOS

it 'works with no error' do
apply_manifest(pp, catch_failures: true)
end

it 'works idempotently' do
apply_manifest(pp, catch_changes: true)
end
keepalived::vrrp::instance { 'VI_50':
interface => $facts['networking']['primary'],
state => 'MASTER',
virtual_router_id => 50,
priority => 101,
auth_type => 'PASS',
auth_pass => 'secret',
virtual_ipaddress => [ '10.0.0.1/16' ],
}

describe file('/etc/keepalived/keepalived.conf') do
it { is_expected.to be_file }
its(:content) { is_expected.to contain('include /etc/keepalived/myconfig.conf') }
notify { "Keepalived version was: ${facts['keepalived_version']}":
require => Class['keepalived'],
}
EOS
it 'creates fact keepalived_version' do
service_fact = apply_manifest(pp, catch_failures: true)
expect(service_fact.output).to match %r{.*Keepalived version was: (\d.\d.\d).*}
end
end
end