(maint) refactor main acceptance suite

This reduces duplication of testing code and makes the intent of the tests
clearer.
This commit is contained in:
David Schmitt 2016-01-18 14:21:51 +00:00
parent a261a3d493
commit 2de4597ecd
3 changed files with 45 additions and 49 deletions

View file

@ -2,28 +2,26 @@ require 'spec_helper_acceptance'
describe 'mysql::db define' do describe 'mysql::db define' do
describe 'creating a database' do describe 'creating a database' do
# Using puppet_apply as a helper let(:pp) do
it 'should work with no errors' do <<-EOS
pp = <<-EOS
class { 'mysql::server': root_password => 'password' } class { 'mysql::server': root_password => 'password' }
mysql::db { 'spec1': mysql::db { 'spec1':
user => 'root1', user => 'root1',
password => 'password', password => 'password',
} }
EOS EOS
end
it_behaves_like "a idempotent resource"
# Run it twice and test for idempotency describe command("mysql -e 'show databases;'") do
apply_manifest(pp, :catch_failures => true) its(:exit_status) { is_expected.to eq 0 }
apply_manifest(pp, :catch_changes => true) its(:stdout) { is_expected.to match /^spec1$/ }
expect(shell("mysql -e 'show databases;'|grep spec1").exit_code).to be_zero
end end
end end
describe 'creating a database with post-sql' do describe 'creating a database with post-sql' do
# Using puppet_apply as a helper let(:pp) do
it 'should work with no errors' do <<-EOS
pp = <<-EOS
class { 'mysql::server': override_options => { 'root_password' => 'password' } } class { 'mysql::server': override_options => { 'root_password' => 'password' } }
file { '/tmp/spec.sql': file { '/tmp/spec.sql':
ensure => file, ensure => file,
@ -36,21 +34,19 @@ describe 'mysql::db define' do
sql => '/tmp/spec.sql', sql => '/tmp/spec.sql',
} }
EOS EOS
# Run it twice and test for idempotency
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end end
it_behaves_like "a idempotent resource"
it 'should have the table' do describe command("mysql -e 'show tables;' spec2") do
expect(shell("mysql -e 'show tables;' spec2|grep table1").exit_code).to be_zero its(:exit_status) { is_expected.to eq 0 }
its(:stdout) { is_expected.to match /^table1$/ }
end end
end end
describe 'creating a database with dbname parameter' do describe 'creating a database with dbname parameter' do
# Using puppet_apply as a helper let(:check_command) { " | grep realdb" }
it 'should work with no errors' do let(:pp) do
pp = <<-EOS <<-EOS
class { 'mysql::server': override_options => { 'root_password' => 'password' } } class { 'mysql::server': override_options => { 'root_password' => 'password' } }
mysql::db { 'spec1': mysql::db { 'spec1':
user => 'root1', user => 'root1',
@ -58,14 +54,12 @@ describe 'mysql::db define' do
dbname => 'realdb', dbname => 'realdb',
} }
EOS EOS
end
it_behaves_like "a idempotent resource"
# Run it twice and test for idempotency describe command("mysql -e 'show databases;'") do
apply_manifest(pp, :catch_failures => true) its(:exit_status) { is_expected.to eq 0 }
apply_manifest(pp, :catch_changes => true) its(:stdout) { is_expected.to match /^realdb$/ }
end
it 'should have the database named realdb' do
expect(shell("mysql -e 'show databases;'|grep realdb").exit_code).to be_zero
end end
end end
end end

View file

@ -1,12 +1,10 @@
require 'spec_helper_acceptance' require 'spec_helper_acceptance'
describe 'mysql class' do describe 'mysql class' do
describe 'advanced config' do
describe 'running puppet code' do let(:tmpdir) { default.tmpdir('mysql') }
# Using puppet_apply as a helper let(:pp) do
it 'should work with no errors' do <<-EOS
tmpdir = default.tmpdir('mysql')
pp = <<-EOS
class { 'mysql::server': class { 'mysql::server':
config_file => '#{tmpdir}/my.cnf', config_file => '#{tmpdir}/my.cnf',
includedir => '#{tmpdir}/include', includedir => '#{tmpdir}/include',
@ -46,39 +44,33 @@ describe 'mysql class' do
} }
} }
EOS EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
end end
describe 'configuration needed for syslog' do it_behaves_like "a idempotent resource"
it 'should work with no errors' do end
pp = <<-EOS
describe 'syslog configuration' do
let(:pp) do
<<-EOS
class { 'mysql::server': class { 'mysql::server':
override_options => { 'mysqld' => { 'log-error' => undef }, 'mysqld_safe' => { 'log-error' => false, 'syslog' => true }}, override_options => { 'mysqld' => { 'log-error' => undef }, 'mysqld_safe' => { 'log-error' => false, 'syslog' => true }},
} }
EOS EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
end end
describe 'when changing the password' do it_behaves_like "a idempotent resource"
end
context 'when changing the password' do
let(:password) { 'THE NEW SECRET' } let(:password) { 'THE NEW SECRET' }
let(:manifest) { "class { 'mysql::server': root_password => '#{password}' }" } let(:pp) { "class { 'mysql::server': root_password => '#{password}' }" }
it 'should not display the password' do it 'should not display the password' do
result = apply_manifest(manifest, :expect_changes => true) result = apply_manifest(pp, :catch_failures => true)
# this does not actually prove anything, as show_diff in the puppet config defaults to false. # this does not actually prove anything, as show_diff in the puppet config defaults to false.
expect(result.stdout).not_to match /#{password}/ expect(result.stdout).not_to match /#{password}/
end end
it 'should be idempotent' do it_behaves_like "a idempotent resource"
result = apply_manifest(manifest, :catch_changes => true)
end end
end
end end

View file

@ -47,3 +47,13 @@ RSpec.configure do |c|
end end
end end
end end
shared_examples "a idempotent resource" do
it 'should apply with no errors' do
apply_manifest(pp, :catch_failures => true)
end
it 'should apply a second time without changes' do
apply_manifest(pp, :catch_changes => true)
end
end