From 6527a3aa22a79eb8a7fb554c9ddee7e53f4e534f Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Tue, 13 Oct 2015 11:29:42 +0100 Subject: [PATCH] (MAINT) switch to rspec-puppet-facts Switching out the puppet version, as puppet_facts does, does not work, as this will confuse code that wants to use version-dependent features. Instead this uses rspec-puppet-facts, which doesn't touch the puppet version. --- Gemfile | 2 +- spec/classes/graceful_failures_spec.rb | 20 +- spec/classes/mycnf_template_spec.rb | 132 ++-- spec/classes/mysql_bindings_spec.rb | 46 +- spec/classes/mysql_client_spec.rb | 58 +- .../mysql_server_account_security_spec.rb | 34 +- spec/classes/mysql_server_backup_spec.rb | 640 +++++++++--------- spec/classes/mysql_server_monitor_spec.rb | 59 +- spec/classes/mysql_server_mysqltuner_spec.rb | 66 +- spec/classes/mysql_server_spec.rb | 346 +++++----- spec/defines/mysql_db_spec.rb | 112 +-- spec/spec_helper.rb | 4 +- 12 files changed, 779 insertions(+), 740 deletions(-) diff --git a/Gemfile b/Gemfile index 1a88250..02ae1c3 100644 --- a/Gemfile +++ b/Gemfile @@ -14,9 +14,9 @@ group :development, :unit_tests do gem 'rspec-core', '3.1.7', :require => false gem 'puppetlabs_spec_helper', :require => false gem 'simplecov', :require => false - gem 'puppet_facts', :require => false gem 'json', :require => false gem 'metadata-json-lint', :require => false + gem 'rspec-puppet-facts', :require => false end group :system_tests do diff --git a/spec/classes/graceful_failures_spec.rb b/spec/classes/graceful_failures_spec.rb index 7f0781b..9fb8117 100644 --- a/spec/classes/graceful_failures_spec.rb +++ b/spec/classes/graceful_failures_spec.rb @@ -1,15 +1,19 @@ require 'spec_helper' describe 'mysql::server' do - on_pe_unsupported_platforms.each do |pe_version,pe_platforms| - pe_platforms.each do |pe_platform,facts| - describe "on #{pe_version} #{pe_platform}" do - let(:facts) { facts } + context "on an unsupported OS" do + # fetch any sets of facts to modify them + os, facts = on_supported_os.first - context 'should gracefully fail' do - it { expect { is_expected.to compile}.to raise_error(Puppet::Error, /Unsupported platform:/) } - end - end + let(:facts) { + facts.merge({ + :osfamily => 'UNSUPPORTED', + :operatingsystem => 'UNSUPPORTED', + }) + } + + it 'should gracefully fail' do + is_expected.to compile.and_raise_error(/Unsupported platform:/) end end end diff --git a/spec/classes/mycnf_template_spec.rb b/spec/classes/mycnf_template_spec.rb index e6f3c16..6446c90 100644 --- a/spec/classes/mycnf_template_spec.rb +++ b/spec/classes/mycnf_template_spec.rb @@ -2,76 +2,78 @@ require 'spec_helper' describe 'mysql::server' do context 'my.cnf template' do - on_pe_supported_platforms(PLATFORMS).each do |pe_version,pe_platforms| - pe_platforms.each do |pe_platform,facts| - describe "on #{pe_version} #{pe_platform}" do - let(:facts) { facts } + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) { + facts.merge({ + :root_home => '/root', + }) + } - context 'normal entry' do - let(:params) {{ :override_options => { 'mysqld' => { 'socket' => '/var/lib/mysql/mysql.sock' } } }} - it do - is_expected.to contain_file('mysql-config-file').with({ - :mode => '0644', - :selinux_ignore_defaults => true, - }).with_content(/socket = \/var\/lib\/mysql\/mysql.sock/) - end + context 'normal entry' do + let(:params) {{ :override_options => { 'mysqld' => { 'socket' => '/var/lib/mysql/mysql.sock' } } }} + it do + is_expected.to contain_file('mysql-config-file').with({ + :mode => '0644', + :selinux_ignore_defaults => true, + }).with_content(/socket = \/var\/lib\/mysql\/mysql.sock/) + end + end + + describe 'array entry' do + let(:params) {{ :override_options => { 'mysqld' => { 'replicate-do-db' => ['base1', 'base2'], } }}} + it do + is_expected.to contain_file('mysql-config-file').with_content( + /.*replicate-do-db = base1\nreplicate-do-db = base2.*/ + ) + end + end + + describe 'ssl set to true' do + let(:params) {{ :override_options => { 'mysqld' => { 'ssl' => true }}}} + it { is_expected.to contain_file('mysql-config-file').with_content(/ssl/) } + it { is_expected.to contain_file('mysql-config-file').without_content(/ssl = true/) } + end + + describe 'ssl set to false' do + let(:params) {{ :override_options => { 'mysqld' => { 'ssl' => false }}}} + it { is_expected.to contain_file('mysql-config-file').with_content(/ssl = false/) } + end + + # ssl-disable (and ssl) are special cased within mysql. + describe 'possibility of disabling ssl completely' do + let(:params) {{ :override_options => { 'mysqld' => { 'ssl' => true, 'ssl-disable' => true }}}} + it { is_expected.to contain_file('mysql-config-file').without_content(/ssl = true/) } + end + + describe 'a non ssl option set to true' do + let(:params) {{ :override_options => { 'mysqld' => { 'test' => true }}}} + it { is_expected.to contain_file('mysql-config-file').with_content(/^test$/) } + it { is_expected.to contain_file('mysql-config-file').without_content(/test = true/) } + end + + context 'with includedir' do + let(:params) {{ :includedir => '/etc/my.cnf.d' }} + it 'makes the directory' do + is_expected.to contain_file('/etc/my.cnf.d').with({ + :ensure => :directory, + :mode => '0755', + }) end - describe 'array entry' do - let(:params) {{ :override_options => { 'mysqld' => { 'replicate-do-db' => ['base1', 'base2'], } }}} - it do - is_expected.to contain_file('mysql-config-file').with_content( - /.*replicate-do-db = base1\nreplicate-do-db = base2.*/ - ) - end + it { is_expected.to contain_file('mysql-config-file').with_content(/!includedir/) } + end + + context 'without includedir' do + let(:params) {{ :includedir => '' }} + it 'shouldnt contain the directory' do + is_expected.not_to contain_file('mysql-config-file').with({ + :ensure => :directory, + :mode => '0755', + }) end - describe 'ssl set to true' do - let(:params) {{ :override_options => { 'mysqld' => { 'ssl' => true }}}} - it { is_expected.to contain_file('mysql-config-file').with_content(/ssl/) } - it { is_expected.to contain_file('mysql-config-file').without_content(/ssl = true/) } - end - - describe 'ssl set to false' do - let(:params) {{ :override_options => { 'mysqld' => { 'ssl' => false }}}} - it { is_expected.to contain_file('mysql-config-file').with_content(/ssl = false/) } - end - - # ssl-disable (and ssl) are special cased within mysql. - describe 'possibility of disabling ssl completely' do - let(:params) {{ :override_options => { 'mysqld' => { 'ssl' => true, 'ssl-disable' => true }}}} - it { is_expected.to contain_file('mysql-config-file').without_content(/ssl = true/) } - end - - describe 'a non ssl option set to true' do - let(:params) {{ :override_options => { 'mysqld' => { 'test' => true }}}} - it { is_expected.to contain_file('mysql-config-file').with_content(/^test$/) } - it { is_expected.to contain_file('mysql-config-file').without_content(/test = true/) } - end - - context 'with includedir' do - let(:params) {{ :includedir => '/etc/my.cnf.d' }} - it 'makes the directory' do - is_expected.to contain_file('/etc/my.cnf.d').with({ - :ensure => :directory, - :mode => '0755', - }) - end - - it { is_expected.to contain_file('mysql-config-file').with_content(/!includedir/) } - end - - context 'without includedir' do - let(:params) {{ :includedir => '' }} - it 'shouldnt contain the directory' do - is_expected.not_to contain_file('mysql-config-file').with({ - :ensure => :directory, - :mode => '0755', - }) - end - - it { is_expected.to contain_file('mysql-config-file').without_content(/!includedir/) } - end + it { is_expected.to contain_file('mysql-config-file').without_content(/!includedir/) } end end end diff --git a/spec/classes/mysql_bindings_spec.rb b/spec/classes/mysql_bindings_spec.rb index 8058179..066e876 100644 --- a/spec/classes/mysql_bindings_spec.rb +++ b/spec/classes/mysql_bindings_spec.rb @@ -1,30 +1,32 @@ require 'spec_helper' describe 'mysql::bindings' do - on_pe_supported_platforms(PLATFORMS).each do |pe_version,pe_platforms| - pe_platforms.each do |pe_platform,facts| - describe "on #{pe_version} #{pe_platform}" do - let(:facts) { facts } + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) { + facts.merge({ + :root_home => '/root', + }) + } - let(:params) {{ - 'java_enable' => true, - 'perl_enable' => true, - 'php_enable' => true, - 'python_enable' => true, - 'ruby_enable' => true, - 'client_dev' => true, - 'daemon_dev' => true, - 'client_dev_package_name' => 'libmysqlclient-devel', - 'daemon_dev_package_name' => 'mysql-devel', - }} + let(:params) {{ + 'java_enable' => true, + 'perl_enable' => true, + 'php_enable' => true, + 'python_enable' => true, + 'ruby_enable' => true, + 'client_dev' => true, + 'daemon_dev' => true, + 'client_dev_package_name' => 'libmysqlclient-devel', + 'daemon_dev_package_name' => 'mysql-devel', + }} - it { is_expected.to contain_package('mysql-connector-java') } - it { is_expected.to contain_package('perl_mysql') } - it { is_expected.to contain_package('python-mysqldb') } - it { is_expected.to contain_package('ruby_mysql') } - it { is_expected.to contain_package('mysql-client_dev') } - it { is_expected.to contain_package('mysql-daemon_dev') } - end + it { is_expected.to contain_package('mysql-connector-java') } + it { is_expected.to contain_package('perl_mysql') } + it { is_expected.to contain_package('python-mysqldb') } + it { is_expected.to contain_package('ruby_mysql') } + it { is_expected.to contain_package('mysql-client_dev') } + it { is_expected.to contain_package('mysql-daemon_dev') } end end end diff --git a/spec/classes/mysql_client_spec.rb b/spec/classes/mysql_client_spec.rb index b2689b5..fdbcc19 100644 --- a/spec/classes/mysql_client_spec.rb +++ b/spec/classes/mysql_client_spec.rb @@ -1,36 +1,38 @@ require 'spec_helper' describe 'mysql::client' do - on_pe_supported_platforms(PLATFORMS).each do |pe_version,pe_platforms| - pe_platforms.each do |pe_platform,facts| - describe "on #{pe_version} #{pe_platform}" do - let(:facts) { facts } - - context 'with defaults' do - it { is_expected.not_to contain_class('mysql::bindings') } - it { is_expected.to contain_package('mysql_client') } - end - - context 'with bindings enabled' do - let(:params) {{ :bindings_enable => true }} - - it { is_expected.to contain_class('mysql::bindings') } - it { is_expected.to contain_package('mysql_client') } - end - - context 'with package_manage set to true' do - let(:params) {{ :package_manage => true }} - - it { is_expected.to contain_package('mysql_client') } - end - - context 'with package_manage set to false' do - let(:params) {{ :package_manage => false }} - - it { is_expected.not_to contain_package('mysql_client') } - end + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) { + facts.merge({ + :root_home => '/root', + }) + } + context 'with defaults' do + it { is_expected.not_to contain_class('mysql::bindings') } + it { is_expected.to contain_package('mysql_client') } end + + context 'with bindings enabled' do + let(:params) {{ :bindings_enable => true }} + + it { is_expected.to contain_class('mysql::bindings') } + it { is_expected.to contain_package('mysql_client') } + end + + context 'with package_manage set to true' do + let(:params) {{ :package_manage => true }} + + it { is_expected.to contain_package('mysql_client') } + end + + context 'with package_manage set to false' do + let(:params) {{ :package_manage => false }} + + it { is_expected.not_to contain_package('mysql_client') } + end + end end end diff --git a/spec/classes/mysql_server_account_security_spec.rb b/spec/classes/mysql_server_account_security_spec.rb index 6f6e73d..d45c46e 100644 --- a/spec/classes/mysql_server_account_security_spec.rb +++ b/spec/classes/mysql_server_account_security_spec.rb @@ -1,10 +1,16 @@ require 'spec_helper' describe 'mysql::server::account_security' do - on_pe_supported_platforms(PLATFORMS).each do |pe_version,pe_platforms| - pe_platforms.each do |pe_platform,facts| - describe "on #{pe_version} #{pe_platform}" do - let(:facts) { facts.merge({:fqdn => 'myhost.mydomain', :hostname => 'myhost'}) } + on_supported_os.each do |os, facts| + context "on #{os}" do + context "with fqdn==myhost.mydomain" do + let(:facts) { + facts.merge({ + :root_home => '/root', + :fqdn => 'myhost.mydomain', + :hostname => 'myhost', + }) + } [ 'root@myhost.mydomain', 'root@127.0.0.1', @@ -32,8 +38,14 @@ describe 'mysql::server::account_security' do end end - describe "on #{pe_version} #{pe_platform} with fqdn==localhost" do - let(:facts) { facts.merge({:fqdn => 'localhost', :hostname => 'localhost'}) } + context "with fqdn==localhost" do + let(:facts) { + facts.merge({ + :root_home => '/root', + :fqdn => 'localhost', + :hostname => 'localhost', + }) + } [ 'root@127.0.0.1', 'root@::1', @@ -48,8 +60,14 @@ describe 'mysql::server::account_security' do end end - describe "on #{pe_version} #{pe_platform} with fqdn==localhost.localdomain" do - let(:facts) { facts.merge({:fqdn => 'localhost.localdomain', :hostname => 'localhost'}) } + context "with fqdn==localhost.localdomain" do + let(:facts) { + facts.merge({ + :root_home => '/root', + :fqdn => 'localhost.localdomain', + :hostname => 'localhost', + }) + } [ 'root@127.0.0.1', 'root@::1', diff --git a/spec/classes/mysql_server_backup_spec.rb b/spec/classes/mysql_server_backup_spec.rb index 8d19943..c1bc374 100644 --- a/spec/classes/mysql_server_backup_spec.rb +++ b/spec/classes/mysql_server_backup_spec.rb @@ -1,401 +1,403 @@ require 'spec_helper' describe 'mysql::server::backup' do - on_pe_supported_platforms(PLATFORMS).each do |pe_version,pe_platforms| - pe_platforms.each do |pe_platform,facts| - describe "on #{pe_version} #{pe_platform}" do - let(:facts) { facts } + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) { + facts.merge({ + :root_home => '/root', + }) + } - let(:default_params) { - { 'backupuser' => 'testuser', - 'backuppassword' => 'testpass', - 'backupdir' => '/tmp', - 'backuprotate' => '25', - 'delete_before_dump' => true, - 'execpath' => '/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin', - } + let(:default_params) { + { 'backupuser' => 'testuser', + 'backuppassword' => 'testpass', + 'backupdir' => '/tmp', + 'backuprotate' => '25', + 'delete_before_dump' => true, + 'execpath' => '/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin', + } + } + + context 'standard conditions' do + let(:params) { default_params } + + # Cannot use that_requires here, doesn't work on classes. + it { is_expected.to contain_mysql_user('testuser@localhost').with( + :require => 'Class[Mysql::Server::Root_password]') } + + it { is_expected.to contain_mysql_grant('testuser@localhost/*.*').with( + :privileges => ['SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS'] + ).that_requires('Mysql_user[testuser@localhost]') } + + context 'with triggers included' do + let(:params) do + { :include_triggers => true }.merge(default_params) + end + it { is_expected.to contain_mysql_grant('testuser@localhost/*.*').with( + :privileges => ['SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS', 'TRIGGER'] + ).that_requires('Mysql_user[testuser@localhost]') } + end + + it { is_expected.to contain_cron('mysql-backup').with( + :command => '/usr/local/sbin/mysqlbackup.sh', + :ensure => 'present' + )} + + it { is_expected.to contain_file('mysqlbackup.sh').with( + :path => '/usr/local/sbin/mysqlbackup.sh', + :ensure => 'present' + ) } + + it { is_expected.to contain_file('mysqlbackupdir').with( + :path => '/tmp', + :ensure => 'directory' + )} + + it 'should have compression by default' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /bzcat -zc/ + ) + end + + it 'should skip backing up events table by default' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /ADDITIONAL_OPTIONS="--ignore-table=mysql.event"/ + ) + end + + it 'should not mention triggers by default because file_per_database is false' do + is_expected.to contain_file('mysqlbackup.sh').without_content( + /.*triggers.*/ + ) + end + + it 'should not mention routines by default because file_per_database is false' do + is_expected.to contain_file('mysqlbackup.sh').without_content( + /.*routines.*/ + ) + end + + it 'should have 25 days of rotation' do + # MySQL counts from 0 + is_expected.to contain_file('mysqlbackup.sh').with_content(/.*ROTATE=24.*/) + end + + it 'should have a standard PATH' do + is_expected.to contain_file('mysqlbackup.sh').with_content(%r{PATH=/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin}) + end + end + + context 'custom ownership and mode for backupdir' do + let(:params) do + { :backupdirmode => '0750', + :backupdirowner => 'testuser', + :backupdirgroup => 'testgrp', + }.merge(default_params) + end + + it { is_expected.to contain_file('mysqlbackupdir').with( + :path => '/tmp', + :ensure => 'directory', + :mode => '0750', + :owner => 'testuser', + :group => 'testgrp' + ) } + end + + context 'with compression disabled' do + let(:params) do + { :backupcompress => false }.merge(default_params) + end + + it { is_expected.to contain_file('mysqlbackup.sh').with( + :path => '/usr/local/sbin/mysqlbackup.sh', + :ensure => 'present' + ) } + + it 'should be able to disable compression' do + is_expected.to contain_file('mysqlbackup.sh').without_content( + /.*bzcat -zc.*/ + ) + end + end + + context 'with mysql.events backedup' do + let(:params) do + { :ignore_events => false }.merge(default_params) + end + + it { is_expected.to contain_file('mysqlbackup.sh').with( + :path => '/usr/local/sbin/mysqlbackup.sh', + :ensure => 'present' + ) } + + it 'should be able to backup events table' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /ADDITIONAL_OPTIONS="--events"/ + ) + end + end + + context 'with database list specified' do + let(:params) do + { :backupdatabases => ['mysql'] }.merge(default_params) + end + + it { is_expected.to contain_file('mysqlbackup.sh').with( + :path => '/usr/local/sbin/mysqlbackup.sh', + :ensure => 'present' + ) } - context 'standard conditions' do - let(:params) { default_params } + it 'should have a backup file for each database' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /mysql | bzcat -zc \${DIR}\\\${PREFIX}mysql_`date'/ + ) + end - # Cannot use that_requires here, doesn't work on classes. - it { is_expected.to contain_mysql_user('testuser@localhost').with( - :require => 'Class[Mysql::Server::Root_password]') } + it 'should skip backup triggers by default' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/ + ) + end - it { is_expected.to contain_mysql_grant('testuser@localhost/*.*').with( - :privileges => ['SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS'] - ).that_requires('Mysql_user[testuser@localhost]') } + it 'should skip backing up routines by default' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/ + ) + end - context 'with triggers included' do - let(:params) do - { :include_triggers => true }.merge(default_params) - end - it { is_expected.to contain_mysql_grant('testuser@localhost/*.*').with( - :privileges => ['SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS', 'TRIGGER'] - ).that_requires('Mysql_user[testuser@localhost]') } + context 'with include_triggers set to true' do + let(:params) do + default_params.merge({ + :backupdatabases => ['mysql'], + :include_triggers => true + }) end - it { is_expected.to contain_cron('mysql-backup').with( - :command => '/usr/local/sbin/mysqlbackup.sh', - :ensure => 'present' - )} - - it { is_expected.to contain_file('mysqlbackup.sh').with( - :path => '/usr/local/sbin/mysqlbackup.sh', - :ensure => 'present' - ) } - - it { is_expected.to contain_file('mysqlbackupdir').with( - :path => '/tmp', - :ensure => 'directory' - )} - - it 'should have compression by default' do + it 'should backup triggers when asked' do is_expected.to contain_file('mysqlbackup.sh').with_content( - /bzcat -zc/ + /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"/ ) end - - it 'should skip backing up events table by default' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="--ignore-table=mysql.event"/ - ) - end - - it 'should not mention triggers by default because file_per_database is false' do - is_expected.to contain_file('mysqlbackup.sh').without_content( - /.*triggers.*/ - ) - end - - it 'should not mention routines by default because file_per_database is false' do - is_expected.to contain_file('mysqlbackup.sh').without_content( - /.*routines.*/ - ) - end - - it 'should have 25 days of rotation' do - # MySQL counts from 0 - is_expected.to contain_file('mysqlbackup.sh').with_content(/.*ROTATE=24.*/) - end - - it 'should have a standard PATH' do - is_expected.to contain_file('mysqlbackup.sh').with_content(%r{PATH=/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin}) - end end - context 'custom ownership and mode for backupdir' do + context 'with include_triggers set to false' do let(:params) do - { :backupdirmode => '0750', - :backupdirowner => 'testuser', - :backupdirgroup => 'testgrp', - }.merge(default_params) + default_params.merge({ + :backupdatabases => ['mysql'], + :include_triggers => false + }) end - it { is_expected.to contain_file('mysqlbackupdir').with( - :path => '/tmp', - :ensure => 'directory', - :mode => '0750', - :owner => 'testuser', - :group => 'testgrp' - ) } + it 'should skip backing up triggers when asked to skip' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/ + ) + end + end + + context 'with include_routines set to true' do + let(:params) do + default_params.merge({ + :backupdatabases => ['mysql'], + :include_routines => true + }) + end + + it 'should backup routines when asked' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --routines"/ + ) + end + end + + context 'with include_routines set to false' do + let(:params) do + default_params.merge({ + :backupdatabases => ['mysql'], + :include_triggers => true + }) + end + + it 'should skip backing up routines when asked to skip' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/ + ) + end + end + end + + context 'with file per database' do + let(:params) do + default_params.merge({ :file_per_database => true }) + end + + it 'should loop through backup all databases' do + is_expected.to contain_file('mysqlbackup.sh').with_content(/.*SHOW DATABASES.*/) end context 'with compression disabled' do let(:params) do - { :backupcompress => false }.merge(default_params) + default_params.merge({ :file_per_database => true, :backupcompress => false }) end - it { is_expected.to contain_file('mysqlbackup.sh').with( - :path => '/usr/local/sbin/mysqlbackup.sh', - :ensure => 'present' - ) } - - it 'should be able to disable compression' do + it 'should loop through backup all databases without compression' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /.*SHOW DATABASES.*/ + ) is_expected.to contain_file('mysqlbackup.sh').without_content( /.*bzcat -zc.*/ ) end end - context 'with mysql.events backedup' do + it 'should skip backup triggers by default' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/ + ) + end + + it 'should skip backing up routines by default' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/ + ) + end + + context 'with include_triggers set to true' do let(:params) do - { :ignore_events => false }.merge(default_params) + default_params.merge({ + :file_per_database => true, + :include_triggers => true + }) end - it { is_expected.to contain_file('mysqlbackup.sh').with( - :path => '/usr/local/sbin/mysqlbackup.sh', - :ensure => 'present' - ) } - - it 'should be able to backup events table' do + it 'should backup triggers when asked' do is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="--events"/ + /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"/ ) end end - context 'with database list specified' do + context 'with include_triggers set to false' do let(:params) do - { :backupdatabases => ['mysql'] }.merge(default_params) + default_params.merge({ + :file_per_database => true, + :include_triggers => false + }) end - it { is_expected.to contain_file('mysqlbackup.sh').with( - :path => '/usr/local/sbin/mysqlbackup.sh', - :ensure => 'present' - ) - } - - it 'should have a backup file for each database' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /mysql | bzcat -zc \${DIR}\\\${PREFIX}mysql_`date'/ - ) - end - - it 'should skip backup triggers by default' do + it 'should skip backing up triggers when asked to skip' do is_expected.to contain_file('mysqlbackup.sh').with_content( /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/ ) end - - it 'should skip backing up routines by default' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/ - ) - end - - context 'with include_triggers set to true' do - let(:params) do - default_params.merge({ - :backupdatabases => ['mysql'], - :include_triggers => true - }) - end - - it 'should backup triggers when asked' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"/ - ) - end - end - - context 'with include_triggers set to false' do - let(:params) do - default_params.merge({ - :backupdatabases => ['mysql'], - :include_triggers => false - }) - end - - it 'should skip backing up triggers when asked to skip' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/ - ) - end - end - - context 'with include_routines set to true' do - let(:params) do - default_params.merge({ - :backupdatabases => ['mysql'], - :include_routines => true - }) - end - - it 'should backup routines when asked' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --routines"/ - ) - end - end - - context 'with include_routines set to false' do - let(:params) do - default_params.merge({ - :backupdatabases => ['mysql'], - :include_triggers => true - }) - end - - it 'should skip backing up routines when asked to skip' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/ - ) - end - end end - context 'with file per database' do + context 'with include_routines set to true' do let(:params) do - default_params.merge({ :file_per_database => true }) + default_params.merge({ + :file_per_database => true, + :include_routines => true + }) end - it 'should loop through backup all databases' do - is_expected.to contain_file('mysqlbackup.sh').with_content(/.*SHOW DATABASES.*/) - end - - context 'with compression disabled' do - let(:params) do - default_params.merge({ :file_per_database => true, :backupcompress => false }) - end - - it 'should loop through backup all databases without compression' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /.*SHOW DATABASES.*/ - ) - is_expected.to contain_file('mysqlbackup.sh').without_content( - /.*bzcat -zc.*/ - ) - end - end - - it 'should skip backup triggers by default' do + it 'should backup routines when asked' do is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/ + /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --routines"/ ) end + end - it 'should skip backing up routines by default' do + context 'with include_routines set to false' do + let(:params) do + default_params.merge({ + :file_per_database => true, + :include_triggers => true + }) + end + + it 'should skip backing up routines when asked to skip' do is_expected.to contain_file('mysqlbackup.sh').with_content( /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/ ) end + end + end - context 'with include_triggers set to true' do - let(:params) do - default_params.merge({ - :file_per_database => true, - :include_triggers => true - }) - end - - it 'should backup triggers when asked' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --triggers"/ - ) - end - end - - context 'with include_triggers set to false' do - let(:params) do - default_params.merge({ - :file_per_database => true, - :include_triggers => false - }) - end - - it 'should skip backing up triggers when asked to skip' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-triggers"/ - ) - end - end - - context 'with include_routines set to true' do - let(:params) do - default_params.merge({ - :file_per_database => true, - :include_routines => true - }) - end - - it 'should backup routines when asked' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --routines"/ - ) - end - end - - context 'with include_routines set to false' do - let(:params) do - default_params.merge({ - :file_per_database => true, - :include_triggers => true - }) - end - - it 'should skip backing up routines when asked to skip' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /ADDITIONAL_OPTIONS="\$ADDITIONAL_OPTIONS --skip-routines"/ - ) - end - end + context 'with postscript' do + let(:params) do + default_params.merge({ :postscript => 'rsync -a /tmp backup01.local-lan:' }) end - context 'with postscript' do - let(:params) do - default_params.merge({ :postscript => 'rsync -a /tmp backup01.local-lan:' }) - end + it 'should be add postscript' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /rsync -a \/tmp backup01.local-lan:/ + ) + end + end - it 'should be add postscript' do - is_expected.to contain_file('mysqlbackup.sh').with_content( - /rsync -a \/tmp backup01.local-lan:/ - ) - end + context 'with postscripts' do + let(:params) do + default_params.merge({ :postscript => [ + 'rsync -a /tmp backup01.local-lan:', + 'rsync -a /tmp backup02.local-lan:', + ]}) end - context 'with postscripts' do + it 'should be add postscript' do + is_expected.to contain_file('mysqlbackup.sh').with_content( + /.*rsync -a \/tmp backup01.local-lan:\n\nrsync -a \/tmp backup02.local-lan:.*/ + ) + end + end + + context 'with the xtrabackup provider' do + let(:params) do + default_params.merge({:provider => 'xtrabackup'}) + end + + it 'should contain the wrapper script' do + is_expected.to contain_file('xtrabackup.sh').with_content( + /^innobackupex\s+"\$@"/ + ) + end + + context 'with prescript defined' do let(:params) do - default_params.merge({ :postscript => [ - 'rsync -a /tmp backup01.local-lan:', - 'rsync -a /tmp backup02.local-lan:', - ]}) + default_params.merge({ + :provider => 'xtrabackup', + :prescript => [ + 'rsync -a /tmp backup01.local-lan:', + 'rsync -a /tmp backup02.local-lan:', + ] + }) end - it 'should be add postscript' do - is_expected.to contain_file('mysqlbackup.sh').with_content( + it 'should contain the prescript' do + is_expected.to contain_file('xtrabackup.sh').with_content( /.*rsync -a \/tmp backup01.local-lan:\n\nrsync -a \/tmp backup02.local-lan:.*/ ) end end - context 'with the xtrabackup provider' do + context 'with postscript defined' do let(:params) do - default_params.merge({:provider => 'xtrabackup'}) + default_params.merge({ + :provider => 'xtrabackup', + :postscript => [ + 'rsync -a /tmp backup01.local-lan:', + 'rsync -a /tmp backup02.local-lan:', + ] + }) end - it 'should contain the wrapper script' do + it 'should contain the prostscript' do is_expected.to contain_file('xtrabackup.sh').with_content( - /^innobackupex\s+"\$@"/ + /.*rsync -a \/tmp backup01.local-lan:\n\nrsync -a \/tmp backup02.local-lan:.*/ ) end - - context 'with prescript defined' do - let(:params) do - default_params.merge({ - :provider => 'xtrabackup', - :prescript => [ - 'rsync -a /tmp backup01.local-lan:', - 'rsync -a /tmp backup02.local-lan:', - ] - }) - end - - it 'should contain the prescript' do - is_expected.to contain_file('xtrabackup.sh').with_content( - /.*rsync -a \/tmp backup01.local-lan:\n\nrsync -a \/tmp backup02.local-lan:.*/ - ) - end - end - - context 'with postscript defined' do - let(:params) do - default_params.merge({ - :provider => 'xtrabackup', - :postscript => [ - 'rsync -a /tmp backup01.local-lan:', - 'rsync -a /tmp backup02.local-lan:', - ] - }) - end - - it 'should contain the prostscript' do - is_expected.to contain_file('xtrabackup.sh').with_content( - /.*rsync -a \/tmp backup01.local-lan:\n\nrsync -a \/tmp backup02.local-lan:.*/ - ) - end - end end end end diff --git a/spec/classes/mysql_server_monitor_spec.rb b/spec/classes/mysql_server_monitor_spec.rb index d9eea00..27fab17 100644 --- a/spec/classes/mysql_server_monitor_spec.rb +++ b/spec/classes/mysql_server_monitor_spec.rb @@ -1,35 +1,38 @@ require 'spec_helper' describe 'mysql::server::monitor' do - on_pe_supported_platforms(PLATFORMS).each do |pe_version,pe_platforms| - pe_platforms.each do |pe_platform,facts| - describe "on #{pe_version} #{pe_platform}" do - let(:facts) { facts } - let :pre_condition do - "include 'mysql::server'" - end + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) { + facts.merge({ + :root_home => '/root', + }) + } - let :default_params do - { - :mysql_monitor_username => 'monitoruser', - :mysql_monitor_password => 'monitorpass', - :mysql_monitor_hostname => 'monitorhost', - } - end - - let :params do - default_params - end - - it { is_expected.to contain_mysql_user('monitoruser@monitorhost')} - - it { is_expected.to contain_mysql_grant('monitoruser@monitorhost/*.*').with( - :ensure => 'present', - :user => 'monitoruser@monitorhost', - :table => '*.*', - :privileges => ["PROCESS", "SUPER"], - :require => 'Mysql_user[monitoruser@monitorhost]' - )} + let :pre_condition do + "include 'mysql::server'" end + + let :default_params do + { + :mysql_monitor_username => 'monitoruser', + :mysql_monitor_password => 'monitorpass', + :mysql_monitor_hostname => 'monitorhost', + } + end + + let :params do + default_params + end + + it { is_expected.to contain_mysql_user('monitoruser@monitorhost')} + + it { is_expected.to contain_mysql_grant('monitoruser@monitorhost/*.*').with( + :ensure => 'present', + :user => 'monitoruser@monitorhost', + :table => '*.*', + :privileges => ["PROCESS", "SUPER"], + :require => 'Mysql_user[monitoruser@monitorhost]' + )} end end end diff --git a/spec/classes/mysql_server_mysqltuner_spec.rb b/spec/classes/mysql_server_mysqltuner_spec.rb index e8d0c73..207246a 100644 --- a/spec/classes/mysql_server_mysqltuner_spec.rb +++ b/spec/classes/mysql_server_mysqltuner_spec.rb @@ -1,42 +1,44 @@ require 'spec_helper' describe 'mysql::server::mysqltuner' do - on_pe_supported_platforms(PLATFORMS).each do |pe_version,pe_platforms| - pe_platforms.each do |pe_platform,facts| - describe "on #{pe_version} #{pe_platform}" do - let(:facts) { facts } + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) { + facts.merge({ + :root_home => '/root', + }) + } - context 'ensure => present' do - it { is_expected.to compile } - it { is_expected.to contain_staging__file('mysqltuner-v1.3.0').with({ - :source => 'https://github.com/major/MySQLTuner-perl/raw/v1.3.0/mysqltuner.pl', - }) - } - end + context 'ensure => present' do + it { is_expected.to compile } + it { is_expected.to contain_staging__file('mysqltuner-v1.3.0').with({ + :source => 'https://github.com/major/MySQLTuner-perl/raw/v1.3.0/mysqltuner.pl', + }) + } + end - context 'ensure => absent' do - let(:params) {{ :ensure => 'absent' }} - it { is_expected.to compile } - it { is_expected.to contain_file('/usr/local/bin/mysqltuner').with(:ensure => 'absent') } - end + context 'ensure => absent' do + let(:params) {{ :ensure => 'absent' }} + it { is_expected.to compile } + it { is_expected.to contain_file('/usr/local/bin/mysqltuner').with(:ensure => 'absent') } + end - context 'custom version' do - let(:params) {{ :version => 'v1.2.0' }} - it { is_expected.to compile } - it { is_expected.to contain_staging__file('mysqltuner-v1.2.0').with({ - :source => 'https://github.com/major/MySQLTuner-perl/raw/v1.2.0/mysqltuner.pl', - }) - } - end + context 'custom version' do + let(:params) {{ :version => 'v1.2.0' }} + it { is_expected.to compile } + it { is_expected.to contain_staging__file('mysqltuner-v1.2.0').with({ + :source => 'https://github.com/major/MySQLTuner-perl/raw/v1.2.0/mysqltuner.pl', + }) + } + end - context 'custom source' do - let(:params) {{ :source => '/tmp/foo' }} - it { is_expected.to compile } - it { is_expected.to contain_staging__file('mysqltuner-/tmp/foo').with({ - :source => '/tmp/foo', - }) - } - end + context 'custom source' do + let(:params) {{ :source => '/tmp/foo' }} + it { is_expected.to compile } + it { is_expected.to contain_staging__file('mysqltuner-/tmp/foo').with({ + :source => '/tmp/foo', + }) + } end end end diff --git a/spec/classes/mysql_server_spec.rb b/spec/classes/mysql_server_spec.rb index e5909f4..727f10d 100644 --- a/spec/classes/mysql_server_spec.rb +++ b/spec/classes/mysql_server_spec.rb @@ -1,190 +1,192 @@ require 'spec_helper' describe 'mysql::server' do - on_pe_supported_platforms(PLATFORMS).each do |pe_version,pe_platforms| - pe_platforms.each do |pe_platform,facts| - describe "on #{pe_version} #{pe_platform}" do - let(:facts) { facts } + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) { + facts.merge({ + :root_home => '/root', + }) + } + context 'with defaults' do + it { is_expected.to contain_class('mysql::server::install') } + it { is_expected.to contain_class('mysql::server::config') } + it { is_expected.to contain_class('mysql::server::service') } + it { is_expected.to contain_class('mysql::server::root_password') } + it { is_expected.to contain_class('mysql::server::providers') } + end + + context 'with remove_default_accounts set' do + let(:params) {{ :remove_default_accounts => true }} + it { is_expected.to contain_class('mysql::server::account_security') } + end + + context 'when not managing config file' do + let(:params) {{ :manage_config_file => false }} + it { is_expected.to compile.with_all_deps } + end + + context 'mysql::server::install' do + it 'contains the package by default' do + is_expected.to contain_package('mysql-server').with({ + :ensure => :present, + }) + end + context 'with package_manage set to true' do + let(:params) {{ :package_manage => true }} + it { is_expected.to contain_package('mysql-server') } + end + context 'with package_manage set to false' do + let(:params) {{ :package_manage => false }} + it { is_expected.not_to contain_package('mysql-server') } + end + context 'with datadir overridden' do + let(:params) {{ :override_options => { 'mysqld' => { 'datadir' => '/tmp' }} }} + it { is_expected.to contain_exec('mysql_install_db') } + end + end + + context 'mysql::server::service' do context 'with defaults' do - it { is_expected.to contain_class('mysql::server::install') } - it { is_expected.to contain_class('mysql::server::config') } - it { is_expected.to contain_class('mysql::server::service') } - it { is_expected.to contain_class('mysql::server::root_password') } - it { is_expected.to contain_class('mysql::server::providers') } + it { is_expected.to contain_service('mysqld') } end - - context 'with remove_default_accounts set' do - let(:params) {{ :remove_default_accounts => true }} - it { is_expected.to contain_class('mysql::server::account_security') } + context 'with package_manage set to true' do + let(:params) {{ :package_manage => true }} + it { is_expected.to contain_service('mysqld').that_requires('Package[mysql-server]') } end - - context 'when not managing config file' do - let(:params) {{ :manage_config_file => false }} - it { is_expected.to compile.with_all_deps } + context 'with package_manage set to false' do + let(:params) {{ :package_manage => false }} + it { is_expected.to contain_service('mysqld') } + it { is_expected.not_to contain_service('mysqld').that_requires('Package[mysql-server]') } end + context 'service_enabled set to false' do + let(:params) {{ :service_enabled => false }} - context 'mysql::server::install' do - it 'contains the package by default' do - is_expected.to contain_package('mysql-server').with({ - :ensure => :present, + it do + is_expected.to contain_service('mysqld').with({ + :ensure => :stopped }) end - context 'with package_manage set to true' do - let(:params) {{ :package_manage => true }} - it { is_expected.to contain_package('mysql-server') } - end - context 'with package_manage set to false' do - let(:params) {{ :package_manage => false }} - it { is_expected.not_to contain_package('mysql-server') } - end - context 'with datadir overridden' do - let(:params) {{ :override_options => { 'mysqld' => { 'datadir' => '/tmp' }} }} - it { is_expected.to contain_exec('mysql_install_db') } - end + end + context 'with log-error overridden' do + let(:params) {{ :override_options => { 'mysqld' => { 'log-error' => '/tmp/error.log' }} }} + it { is_expected.to contain_file('/tmp/error.log') } + end + end + + context 'mysql::server::root_password' do + describe 'when defaults' do + it { + is_expected.to contain_exec('remove install pass').with( + :command => 'mysqladmin -u root --password=$(grep -o \'[^ ]\\+$\' /.mysql_secret) password \'\' && rm -f /.mysql_secret', + :onlyif => 'test -f /.mysql_secret', + :path => '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin' + ) + } + it { is_expected.not_to contain_mysql_user('root@localhost') } + it { is_expected.not_to contain_file('/root/.my.cnf') } + end + describe 'when root_password set' do + let(:params) {{:root_password => 'SET' }} + it { is_expected.to contain_mysql_user('root@localhost') } + it { is_expected.to contain_file('/root/.my.cnf').that_requires('Mysql_user[root@localhost]') } + end + describe 'when root_password set, create_root_user set to false' do + let(:params) {{ :root_password => 'SET', :create_root_user => false }} + it { is_expected.not_to contain_mysql_user('root@localhost') } + it { is_expected.to contain_file('/root/.my.cnf') } + end + describe 'when root_password set, create_root_my_cnf set to false' do + let(:params) {{ :root_password => 'SET', :create_root_my_cnf => false }} + it { is_expected.to contain_mysql_user('root@localhost') } + it { is_expected.not_to contain_file('/root/.my.cnf') } + end + describe 'when root_password set, create_root_user and create_root_my_cnf set to false' do + let(:params) {{ :root_password => 'SET', :create_root_user => false, :create_root_my_cnf => false }} + it { is_expected.not_to contain_mysql_user('root@localhost') } + it { is_expected.not_to contain_file('/root/.my.cnf') } + end + describe 'when install_secret_file set to /root/.mysql_secret' do + let(:params) {{ :install_secret_file => '/root/.mysql_secret' }} + it { + is_expected.to contain_exec('remove install pass').with( + :command => 'mysqladmin -u root --password=$(grep -o \'[^ ]\\+$\' /root/.mysql_secret) password \'\' && rm -f /root/.mysql_secret', + :onlyif => 'test -f /root/.mysql_secret' + ) + } + end + end + + context 'mysql::server::providers' do + describe 'with users' do + let(:params) {{:users => { + 'foo@localhost' => { + 'max_connections_per_hour' => '1', + 'max_queries_per_hour' => '2', + 'max_updates_per_hour' => '3', + 'max_user_connections' => '4', + 'password_hash' => '*F3A2A51A9B0F2BE2468926B4132313728C250DBF' + }, + 'foo2@localhost' => {} + }}} + it { is_expected.to contain_mysql_user('foo@localhost').with( + :max_connections_per_hour => '1', + :max_queries_per_hour => '2', + :max_updates_per_hour => '3', + :max_user_connections => '4', + :password_hash => '*F3A2A51A9B0F2BE2468926B4132313728C250DBF' + )} + it { is_expected.to contain_mysql_user('foo2@localhost').with( + :max_connections_per_hour => nil, + :max_queries_per_hour => nil, + :max_updates_per_hour => nil, + :max_user_connections => nil, + :password_hash => nil + )} end - context 'mysql::server::service' do - context 'with defaults' do - it { is_expected.to contain_service('mysqld') } - end - context 'with package_manage set to true' do - let(:params) {{ :package_manage => true }} - it { is_expected.to contain_service('mysqld').that_requires('Package[mysql-server]') } - end - context 'with package_manage set to false' do - let(:params) {{ :package_manage => false }} - it { is_expected.to contain_service('mysqld') } - it { is_expected.not_to contain_service('mysqld').that_requires('Package[mysql-server]') } - end - context 'service_enabled set to false' do - let(:params) {{ :service_enabled => false }} - - it do - is_expected.to contain_service('mysqld').with({ - :ensure => :stopped - }) - end - end - context 'with log-error overridden' do - let(:params) {{ :override_options => { 'mysqld' => { 'log-error' => '/tmp/error.log' }} }} - it { is_expected.to contain_file('/tmp/error.log') } - end + describe 'with grants' do + let(:params) {{:grants => { + 'foo@localhost/somedb.*' => { + 'user' => 'foo@localhost', + 'table' => 'somedb.*', + 'privileges' => ["SELECT", "UPDATE"], + 'options' => ["GRANT"], + }, + 'foo2@localhost/*.*' => { + 'user' => 'foo2@localhost', + 'table' => '*.*', + 'privileges' => ["SELECT"], + }, + }}} + it { is_expected.to contain_mysql_grant('foo@localhost/somedb.*').with( + :user => 'foo@localhost', + :table => 'somedb.*', + :privileges => ["SELECT", "UPDATE"], + :options => ["GRANT"] + )} + it { is_expected.to contain_mysql_grant('foo2@localhost/*.*').with( + :user => 'foo2@localhost', + :table => '*.*', + :privileges => ["SELECT"], + :options => nil + )} end - context 'mysql::server::root_password' do - describe 'when defaults' do - it { - is_expected.to contain_exec('remove install pass').with( - :command => 'mysqladmin -u root --password=$(grep -o \'[^ ]\\+$\' /.mysql_secret) password \'\' && rm -f /.mysql_secret', - :onlyif => 'test -f /.mysql_secret', - :path => '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin' - ) - } - it { is_expected.not_to contain_mysql_user('root@localhost') } - it { is_expected.not_to contain_file('/root/.my.cnf') } - end - describe 'when root_password set' do - let(:params) {{:root_password => 'SET' }} - it { is_expected.to contain_mysql_user('root@localhost') } - it { is_expected.to contain_file('/root/.my.cnf').that_requires('Mysql_user[root@localhost]') } - end - describe 'when root_password set, create_root_user set to false' do - let(:params) {{ :root_password => 'SET', :create_root_user => false }} - it { is_expected.not_to contain_mysql_user('root@localhost') } - it { is_expected.to contain_file('/root/.my.cnf') } - end - describe 'when root_password set, create_root_my_cnf set to false' do - let(:params) {{ :root_password => 'SET', :create_root_my_cnf => false }} - it { is_expected.to contain_mysql_user('root@localhost') } - it { is_expected.not_to contain_file('/root/.my.cnf') } - end - describe 'when root_password set, create_root_user and create_root_my_cnf set to false' do - let(:params) {{ :root_password => 'SET', :create_root_user => false, :create_root_my_cnf => false }} - it { is_expected.not_to contain_mysql_user('root@localhost') } - it { is_expected.not_to contain_file('/root/.my.cnf') } - end - describe 'when install_secret_file set to /root/.mysql_secret' do - let(:params) {{ :install_secret_file => '/root/.mysql_secret' }} - it { - is_expected.to contain_exec('remove install pass').with( - :command => 'mysqladmin -u root --password=$(grep -o \'[^ ]\\+$\' /root/.mysql_secret) password \'\' && rm -f /root/.mysql_secret', - :onlyif => 'test -f /root/.mysql_secret' - ) - } - end - end - - context 'mysql::server::providers' do - describe 'with users' do - let(:params) {{:users => { - 'foo@localhost' => { - 'max_connections_per_hour' => '1', - 'max_queries_per_hour' => '2', - 'max_updates_per_hour' => '3', - 'max_user_connections' => '4', - 'password_hash' => '*F3A2A51A9B0F2BE2468926B4132313728C250DBF' - }, - 'foo2@localhost' => {} - }}} - it { is_expected.to contain_mysql_user('foo@localhost').with( - :max_connections_per_hour => '1', - :max_queries_per_hour => '2', - :max_updates_per_hour => '3', - :max_user_connections => '4', - :password_hash => '*F3A2A51A9B0F2BE2468926B4132313728C250DBF' - )} - it { is_expected.to contain_mysql_user('foo2@localhost').with( - :max_connections_per_hour => nil, - :max_queries_per_hour => nil, - :max_updates_per_hour => nil, - :max_user_connections => nil, - :password_hash => nil - )} - end - - describe 'with grants' do - let(:params) {{:grants => { - 'foo@localhost/somedb.*' => { - 'user' => 'foo@localhost', - 'table' => 'somedb.*', - 'privileges' => ["SELECT", "UPDATE"], - 'options' => ["GRANT"], - }, - 'foo2@localhost/*.*' => { - 'user' => 'foo2@localhost', - 'table' => '*.*', - 'privileges' => ["SELECT"], - }, - }}} - it { is_expected.to contain_mysql_grant('foo@localhost/somedb.*').with( - :user => 'foo@localhost', - :table => 'somedb.*', - :privileges => ["SELECT", "UPDATE"], - :options => ["GRANT"] - )} - it { is_expected.to contain_mysql_grant('foo2@localhost/*.*').with( - :user => 'foo2@localhost', - :table => '*.*', - :privileges => ["SELECT"], - :options => nil - )} - end - - describe 'with databases' do - let(:params) {{:databases => { - 'somedb' => { - 'charset' => 'latin1', - 'collate' => 'latin1', - }, - 'somedb2' => {} - }}} - it { is_expected.to contain_mysql_database('somedb').with( - :charset => 'latin1', - :collate => 'latin1' - )} - it { is_expected.to contain_mysql_database('somedb2')} - end + describe 'with databases' do + let(:params) {{:databases => { + 'somedb' => { + 'charset' => 'latin1', + 'collate' => 'latin1', + }, + 'somedb2' => {} + }}} + it { is_expected.to contain_mysql_database('somedb').with( + :charset => 'latin1', + :collate => 'latin1' + )} + it { is_expected.to contain_mysql_database('somedb2')} end end end diff --git a/spec/defines/mysql_db_spec.rb b/spec/defines/mysql_db_spec.rb index 7ff3b9c..15c00f6 100644 --- a/spec/defines/mysql_db_spec.rb +++ b/spec/defines/mysql_db_spec.rb @@ -1,74 +1,76 @@ require 'spec_helper' describe 'mysql::db', :type => :define do - on_pe_supported_platforms(PLATFORMS).each do |pe_version,pe_platforms| - pe_platforms.each do |pe_platform,facts| - describe "on #{pe_version} #{pe_platform}" do - let(:facts) { facts } + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) { + facts.merge({ + :root_home => '/root', + }) + } - let(:title) { 'test_db' } + let(:title) { 'test_db' } - let(:params) { - { 'user' => 'testuser', - 'password' => 'testpass', - } + let(:params) { + { 'user' => 'testuser', + 'password' => 'testpass', } + } - it 'should report an error when ensure is not present or absent' do - params.merge!({'ensure' => 'invalid_val'}) - expect { catalogue }.to raise_error(Puppet::Error, - /invalid_val is not supported for ensure\. Allowed values are 'present' and 'absent'\./) - end + it 'should report an error when ensure is not present or absent' do + params.merge!({'ensure' => 'invalid_val'}) + expect { catalogue }.to raise_error(Puppet::Error, + /invalid_val is not supported for ensure\. Allowed values are 'present' and 'absent'\./) + end - it 'should not notify the import sql exec if no sql script was provided' do - is_expected.to contain_mysql_database('test_db').without_notify - end + it 'should not notify the import sql exec if no sql script was provided' do + is_expected.to contain_mysql_database('test_db').without_notify + end - it 'should subscribe to database if sql script is given' do - params.merge!({'sql' => 'test_sql'}) - is_expected.to contain_exec('test_db-import').with_subscribe('Mysql_database[test_db]') - end + it 'should subscribe to database if sql script is given' do + params.merge!({'sql' => 'test_sql'}) + is_expected.to contain_exec('test_db-import').with_subscribe('Mysql_database[test_db]') + end - it 'should only import sql script on creation if not enforcing' do - params.merge!({'sql' => 'test_sql', 'enforce_sql' => false}) - is_expected.to contain_exec('test_db-import').with_refreshonly(true) - end + it 'should only import sql script on creation if not enforcing' do + params.merge!({'sql' => 'test_sql', 'enforce_sql' => false}) + is_expected.to contain_exec('test_db-import').with_refreshonly(true) + end - it 'should import sql script on creation if enforcing' do - params.merge!({'sql' => 'test_sql', 'enforce_sql' => true}) - is_expected.to contain_exec('test_db-import').with_refreshonly(false) - is_expected.to contain_exec('test_db-import').with_command("cat test_sql | mysql test_db") - end + it 'should import sql script on creation if enforcing' do + params.merge!({'sql' => 'test_sql', 'enforce_sql' => true}) + is_expected.to contain_exec('test_db-import').with_refreshonly(false) + is_expected.to contain_exec('test_db-import').with_command("cat test_sql | mysql test_db") + end - it 'should import sql scripts when more than one is specified' do - params.merge!({'sql' => ['test_sql', 'test_2_sql']}) - is_expected.to contain_exec('test_db-import').with_command('cat test_sql test_2_sql | mysql test_db') - end + it 'should import sql scripts when more than one is specified' do + params.merge!({'sql' => ['test_sql', 'test_2_sql']}) + is_expected.to contain_exec('test_db-import').with_command('cat test_sql test_2_sql | mysql test_db') + end - it 'should report an error if sql isn\'t a string or an array' do - params.merge!({'sql' => {'foo' => 'test_sql', 'bar' => 'test_2_sql'}}) - expect { catalogue }.to raise_error(Puppet::Error, - /\$sql must be either a string or an array\./) - end + it 'should report an error if sql isn\'t a string or an array' do + params.merge!({'sql' => {'foo' => 'test_sql', 'bar' => 'test_2_sql'}}) + expect { catalogue }.to raise_error(Puppet::Error, + /\$sql must be either a string or an array\./) + end - it 'should not create database and database user' do - params.merge!({'ensure' => 'absent', 'host' => 'localhost'}) - is_expected.to contain_mysql_database('test_db').with_ensure('absent') - is_expected.to contain_mysql_user('testuser@localhost').with_ensure('absent') - end + it 'should not create database and database user' do + params.merge!({'ensure' => 'absent', 'host' => 'localhost'}) + is_expected.to contain_mysql_database('test_db').with_ensure('absent') + is_expected.to contain_mysql_user('testuser@localhost').with_ensure('absent') + end - it 'should create with an appropriate collate and charset' do - params.merge!({'charset' => 'utf8', 'collate' => 'utf8_danish_ci'}) - is_expected.to contain_mysql_database('test_db').with({ - 'charset' => 'utf8', - 'collate' => 'utf8_danish_ci', - }) - end + it 'should create with an appropriate collate and charset' do + params.merge!({'charset' => 'utf8', 'collate' => 'utf8_danish_ci'}) + is_expected.to contain_mysql_database('test_db').with({ + 'charset' => 'utf8', + 'collate' => 'utf8_danish_ci', + }) + end - it 'should use dbname parameter as database name instead of name' do - params.merge!({'dbname' => 'real_db'}) - is_expected.to contain_mysql_database('real_db') - end + it 'should use dbname parameter as database name instead of name' do + params.merge!({'dbname' => 'real_db'}) + is_expected.to contain_mysql_database('real_db') end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index be79afe..6277b13 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,6 @@ require 'puppetlabs_spec_helper/module_spec_helper' -require 'puppet_facts' -include PuppetFacts +require 'rspec-puppet-facts' +include RspecPuppetFacts # The default set of platforms to test again. ENV['UNIT_TEST_PLATFORMS'] = 'centos-6-x86_64 ubuntu-1404-x86_64'