diff --git a/lib/puppet/functions/mysql_password.rb b/lib/puppet/functions/mysql_password.rb deleted file mode 100644 index dc2916261..000000000 --- a/lib/puppet/functions/mysql_password.rb +++ /dev/null @@ -1,21 +0,0 @@ -# @summary -# A wrapper for the 4.x function 'mysql::password' to bridge the gap between -# it and the 3.x function 'mysql_password'. -# -Puppet::Functions.create_function(:mysql_password) do - # @param password - # Plain text password. - # - # @return - # The mysql password hash from the 4.x function mysql::password. - # - dispatch :mysql_password do - required_param 'String', :password - return_type 'String' - end - - def mysql_password(password) - call_function('deprecation', 'mysql_password', "This method has been deprecated, please use the namespaced version 'mysql::password' instead.") - call_function('mysql::password', password) - end -end diff --git a/lib/puppet/functions/mysql_strip_hash.rb b/lib/puppet/functions/mysql_strip_hash.rb deleted file mode 100644 index 263a9b747..000000000 --- a/lib/puppet/functions/mysql_strip_hash.rb +++ /dev/null @@ -1,21 +0,0 @@ -# @summary -# A wrapper for the 4.x function 'mysql::strip_hash' to bridge the gap between -# it and the 3.x function 'mysql_strip_hash'. -# -Puppet::Functions.create_function(:mysql_strip_hash) do - # @param hash - # Hash to be stripped - # - # @return hash - # The given hash with all blank entries removed - # - dispatch :mysql_strip_hash do - required_param 'Hash', :hash - return_type 'Hash' - end - - def mysql_strip_hash(hash) - call_function('deprecation', 'mysql_strip_hash', "This method has been deprecated, please use the namespaced version 'mysql::strip_hash' instead.") - call_function('mysql::strip_hash', hash) - end -end diff --git a/lib/puppet/parser/functions/mysql_strip_hash.rb b/lib/puppet/parser/functions/mysql_strip_hash.rb deleted file mode 100644 index 327f9fd5d..000000000 --- a/lib/puppet/parser/functions/mysql_strip_hash.rb +++ /dev/null @@ -1,19 +0,0 @@ -# When given a hash this function strips out all blank entries. -module Puppet::Parser::Functions - newfunction(:mysql_strip_hash, type: :rvalue, arity: 1, doc: <<-EOS - TEMPORARY FUNCTION: EXPIRES 2014-03-10 - When given a hash this function strips out all blank entries. -EOS - ) do |args| - - hash = args[0] - unless hash.is_a?(Hash) - raise(Puppet::ParseError, _('mysql_strip_hash(): Requires a hash to work.')) - end - - # Filter out all the top level blanks. - hash.reject { |_k, v| v == '' }.each do |_k, v| - v.reject! { |_ki, vi| vi == '' } if v.is_a?(Hash) - end - end -end diff --git a/spec/acceptance/locales_spec.rb b/spec/acceptance/locales_spec.rb index 23bbcbcc6..67c9249d0 100644 --- a/spec/acceptance/locales_spec.rb +++ b/spec/acceptance/locales_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper_acceptance' require 'beaker/i18n_helper' -describe 'mysql localization', if: (fact('osfamily') == 'Debian' || fact('osfamily') == 'RedHat') && (Gem::Version.new(puppet_version) >= Gem::Version.new('4.10.5')) do +describe 'mysql localization', if: (os[:family] == 'debian' || os[:family] == 'redhat') && (Gem::Version.new(puppet_version) >= Gem::Version.new('4.10.5')) do before :all do hosts.each do |host| on(host, "sed -i \"96i FastGettext.locale='ja'\" /opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet.rb") @@ -12,7 +12,7 @@ context 'when triggering puppet simple string error' do # 'service_enabled' being set to false can cause random failures in Debian 9 let(:os_variant) do - if fact('operatingsystem') =~ %r{Debian} && fact('operatingsystemrelease') =~ %r{^9\.} + if os[:family] == 'debian' && os[:release].to_i == 9 'true' else 'false' diff --git a/spec/acceptance/mysql_backup_spec.rb b/spec/acceptance/mysql_backup_spec.rb index 18e1ade87..5454eb2f6 100644 --- a/spec/acceptance/mysql_backup_spec.rb +++ b/spec/acceptance/mysql_backup_spec.rb @@ -29,8 +29,7 @@ class { 'mysql::server::backup': } MANIFEST it 'when configuring mysql backups' do - execute_manifest(pp, catch_failures: true) - execute_manifest(pp, catch_failures: true) + idempotent_apply(default, pp, {}) end end @@ -104,8 +103,7 @@ class { 'mysql::server::backup': } MANIFEST it 'when configuring mysql backups' do - execute_manifest(pp, catch_failures: true) - execute_manifest(pp, catch_failures: true) + idempotent_apply(default, pp, {}) end end @@ -153,50 +151,4 @@ class { 'mysql::server::backup': # rubocop:enable RSpec/MultipleExpectations, RSpec/ExampleLength end end - - context 'with triggers and routines' do - pre_run - pp = <<-MANIFEST - class { 'mysql::server': root_password => 'password' } - mysql::db { [ - 'backup1', - 'backup2' - ]: - user => 'backup', - password => 'secret', - } - package { 'bzip2': - ensure => present, - } - class { 'mysql::server::backup': - backupuser => 'myuser', - backuppassword => 'mypassword', - backupdir => '/tmp/backups', - backupcompress => true, - file_per_database => true, - include_triggers => #{version_is_greater_than('5.1.5')}, - include_routines => true, - postscript => [ - 'rm -rf /var/tmp/mysqlbackups', - 'rm -f /var/tmp/mysqlbackups.done', - 'cp -r /tmp/backups /var/tmp/mysqlbackups', - 'touch /var/tmp/mysqlbackups.done', - ], - execpath => '/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin', - require => Package['bzip2'], - } - MANIFEST - it 'when configuring mysql backups with triggers and routines' do - execute_manifest(pp, catch_failures: true) - end - - it 'runs mysqlbackup.sh with no errors' do - pre_run - unless version_is_greater_than('5.7.0') - shell('/usr/local/sbin/mysqlbackup.sh') do |r| - expect(r.stderr).to eq('') - end - end - end - end end diff --git a/spec/acceptance/mysql_db_spec.rb b/spec/acceptance/mysql_db_spec.rb index dc0af4012..cf28fb541 100644 --- a/spec/acceptance/mysql_db_spec.rb +++ b/spec/acceptance/mysql_db_spec.rb @@ -1,25 +1,6 @@ require 'spec_helper_acceptance' describe 'mysql::db define' do - describe 'creating a database' do - let(:pp) do - <<-MANIFEST - class { 'mysql::server': root_password => 'password' } - mysql::db { 'spec1': - user => 'root1', - password => 'password', - } - MANIFEST - end - - it_behaves_like 'a idempotent resource' - - describe command("mysql -e 'show databases;'") do - its(:exit_status) { is_expected.to eq 0 } - its(:stdout) { is_expected.to match %r{^spec1$} } - end - end - describe 'creating a database with post-sql' do let(:pp) do <<-MANIFEST @@ -44,25 +25,4 @@ class { 'mysql::server': override_options => { 'root_password' => 'password' } } its(:stdout) { is_expected.to match %r{^table1$} } end end - - describe 'creating a database with dbname parameter' do - let(:check_command) { ' | grep realdb' } - let(:pp) do - <<-MANIFEST - class { 'mysql::server': override_options => { 'root_password' => 'password' } } - mysql::db { 'spec1': - user => 'root1', - password => 'password', - dbname => 'realdb', - } - MANIFEST - end - - it_behaves_like 'a idempotent resource' - - describe command("mysql -e 'show databases;'") do - its(:exit_status) { is_expected.to eq 0 } - its(:stdout) { is_expected.to match %r{^realdb$} } - end - end end diff --git a/spec/acceptance/mysql_server_spec.rb b/spec/acceptance/mysql_server_spec.rb index 6d4fdde24..15770420b 100644 --- a/spec/acceptance/mysql_server_spec.rb +++ b/spec/acceptance/mysql_server_spec.rb @@ -46,41 +46,6 @@ class { 'mysql::server': it_behaves_like 'a idempotent resource' end - describe 'minimal config' do - before(:all) do - @tmpdir = default.tmpdir('mysql') - end - # 'manage_config_file'/'service_enabled' being set to false can cause random failures in Debian 9 - let(:os_variant) do - if fact('operatingsystem') =~ %r{Debian} && fact('operatingsystemrelease') =~ %r{^9\.} - 'true' - else - 'false' - end - end - let(:pp) do - <<-MANIFEST - class { 'mysql::server': - manage_config_file => '#{os_variant}', - override_options => { 'mysqld' => { 'key_buffer_size' => '32M' }}, - package_ensure => 'present', - purge_conf_dir => 'false', - remove_default_accounts => 'false', - restart => 'false', - root_group => 'root', - root_password => 'test', - service_enabled => '#{os_variant}', - service_manage => 'false', - users => {}, - grants => {}, - databases => {}, - } - MANIFEST - end - - it_behaves_like 'a idempotent resource' - end - describe 'syslog configuration' do let(:pp) do <<-MANIFEST @@ -92,17 +57,4 @@ class { 'mysql::server': it_behaves_like 'a idempotent resource' end - - context 'when changing the password' do - let(:password) { 'THE NEW SECRET' } - let(:pp) { "class { 'mysql::server': root_password => '#{password}' }" } - - it 'does not display the password' do - result = execute_manifest(pp, catch_failures: true) - # this does not actually prove anything, as show_diff in the puppet config defaults to false. - expect(result.stdout).not_to match %r{#{password}} - end - - it_behaves_like 'a idempotent resource' - end end diff --git a/spec/acceptance/sql_task_spec.rb b/spec/acceptance/sql_task_spec.rb index 781b2e478..e0f78cd72 100644 --- a/spec/acceptance/sql_task_spec.rb +++ b/spec/acceptance/sql_task_spec.rb @@ -1,7 +1,7 @@ # run a test task require 'spec_helper_acceptance' -describe 'mysql tasks', if: puppet_version =~ %r{(5\.\d\.\d)} && fact('operatingsystem') != 'SLES' do +describe 'mysql tasks', if: puppet_version =~ %r{(5\.\d\.\d)} && os[:family] != 'sles' do describe 'execute some sql' do pp = <<-MANIFEST class { 'mysql::server': root_password => 'password' } diff --git a/spec/acceptance/types/mysql_database_spec.rb b/spec/acceptance/types/mysql_database_spec.rb index 1faad0995..67f77d86a 100644 --- a/spec/acceptance/types/mysql_database_spec.rb +++ b/spec/acceptance/types/mysql_database_spec.rb @@ -44,8 +44,7 @@ class { 'mysql::server': } } MANIFEST it 'creates two db of different types idempotently' do - execute_manifest(pp, catch_failures: true) - execute_manifest(pp, catch_changes: true) + idempotent_apply(default, pp, {}) end it 'finds latin1 db #stdout' do diff --git a/spec/acceptance/types/mysql_plugin_spec.rb b/spec/acceptance/types/mysql_plugin_spec.rb index 327e02299..2fab7df4c 100644 --- a/spec/acceptance/types/mysql_plugin_spec.rb +++ b/spec/acceptance/types/mysql_plugin_spec.rb @@ -4,19 +4,19 @@ # of mysql) have varying levels of support for plugins and have # different plugins available. Choose a plugin that works or don't try # to test plugins if not available. -if fact('osfamily') =~ %r{RedHat} - if fact('operatingsystemrelease') =~ %r{^5\.} +if fact('osfamily') == 'redhat' + if os[:release].to_i == 5 plugin = nil # Plugins not supported on mysql on RHEL 5 - elsif fact('operatingsystemrelease') =~ %r{^6\.} + elsif os[:release].to_i == 6 plugin = 'example' plugin_lib = 'ha_example.so' - elsif fact('operatingsystemrelease') =~ %r{^7\.} + elsif os[:release].to_i == 7 plugin = 'pam' plugin_lib = 'auth_pam.so' end -elsif fact('osfamily') =~ %r{Debian} - if fact('operatingsystem') =~ %r{Ubuntu} - if fact('operatingsystemrelease') =~ %r{^16\.04|^18\.04} +elsif fact('osfamily') == 'debian' + if os[:family] == 'ubuntu' + if os[:release] =~ %r{^16\.04|^18\.04} # On Xenial running 5.7.12, the example plugin does not appear to be available. plugin = 'validate_password' plugin_lib = 'validate_password.so' @@ -25,7 +25,7 @@ plugin_lib = 'ha_example.so' end end -elsif fact('osfamily') =~ %r{Suse} +elsif fact('osfamily') == 'suse' plugin = nil # Plugin library path is broken on Suse http://lists.opensuse.org/opensuse-bugs/2013-08/msg01123.html end diff --git a/spec/classes/mysql_client_spec.rb b/spec/classes/mysql_client_spec.rb index 83eec9d72..6c651dcae 100644 --- a/spec/classes/mysql_client_spec.rb +++ b/spec/classes/mysql_client_spec.rb @@ -2,7 +2,6 @@ describe 'mysql::client' do on_supported_os.each do |os, facts| - next if facts[:osfamily] == 'Archlinux' context "on #{os}" do let(:facts) do facts.merge(root_home: '/root') diff --git a/spec/defines/mysql_db_spec.rb b/spec/defines/mysql_db_spec.rb index 00b74d517..8c175bfd8 100644 --- a/spec/defines/mysql_db_spec.rb +++ b/spec/defines/mysql_db_spec.rb @@ -21,6 +21,7 @@ it 'subscribes to database if sql script is given' do params['sql'] = 'test_sql' + is_expected.to contain_mysql_database('test_db') is_expected.to contain_exec('test_db-import').with_subscribe('Mysql_database[test_db]') end @@ -29,21 +30,19 @@ is_expected.to contain_exec('test_db-import').with_refreshonly(true) end - it 'imports sql script on creation if enforcing #refreshonly' do + it 'imports sql script on creation' do params.merge!('sql' => 'test_sql', 'enforce_sql' => true) + # ' if enforcing #refreshonly' is_expected.to contain_exec('test_db-import').with_refreshonly(false) - end - it 'imports sql script on creation if enforcing #command' do - params.merge!('sql' => 'test_sql', 'enforce_sql' => true) + # 'if enforcing #command' is_expected.to contain_exec('test_db-import').with_command('cat test_sql | mysql test_db') end - it 'imports sql script with custom command on creation if enforcing #refreshonly' do + it 'imports sql script with custom command on creation ' do params.merge!('sql' => 'test_sql', 'enforce_sql' => true, 'import_cat_cmd' => 'zcat') + # if enforcing #refreshonly is_expected.to contain_exec('test_db-import').with_refreshonly(false) - end - it 'imports sql script with custom command on creation if enforcing #command' do - params.merge!('sql' => 'test_sql', 'enforce_sql' => true, 'import_cat_cmd' => 'zcat') + # if enforcing #command is_expected.to contain_exec('test_db-import').with_command('zcat test_sql | mysql test_db') end @@ -55,9 +54,6 @@ it 'does not create database' do params.merge!('ensure' => 'absent', 'host' => 'localhost') is_expected.to contain_mysql_database('test_db').with_ensure('absent') - end - it 'does not create database user' do - params.merge!('ensure' => 'absent', 'host' => 'localhost') is_expected.to contain_mysql_user('testuser@localhost').with_ensure('absent') end diff --git a/spec/functions/mysql_password_spec.rb b/spec/functions/mysql_password_spec.rb index 8ca4fb859..ef90399d4 100644 --- a/spec/functions/mysql_password_spec.rb +++ b/spec/functions/mysql_password_spec.rb @@ -17,6 +17,10 @@ is_expected.to run.with_params('password').and_return('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19') end + it 'password should be String' do + is_expected.to run.with_params(123).and_raise_error(ArgumentError) + end + it 'converts an empty password into a empty string' do is_expected.to run.with_params('').and_return('') end diff --git a/spec/functions/mysql_password_wrapper_spec.rb b/spec/functions/mysql_password_wrapper_spec.rb deleted file mode 100644 index 5b68dd972..000000000 --- a/spec/functions/mysql_password_wrapper_spec.rb +++ /dev/null @@ -1,28 +0,0 @@ -require 'spec_helper' - -describe 'mysql_password' do - it 'exists' do - is_expected.not_to eq(nil) - end - - it 'raises a ArgumentError if there is less than 1 arguments' do - is_expected.to run.with_params.and_raise_error(ArgumentError) - end - - it 'raises a ArgumentError if there is more than 1 arguments' do - is_expected.to run.with_params('foo', 'bar').and_raise_error(ArgumentError) - end - - # Please note that the below test's fail if run via rspec rather than rake - it 'converts password into a hash' do - is_expected.to run.with_params('password').and_return('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19') - end - - it 'converts an empty password into a empty string' do - is_expected.to run.with_params('').and_return('') - end - - it 'does not convert a password that is already a hash' do - is_expected.to run.with_params('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19').and_return('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19') - end -end diff --git a/spec/functions/mysql_strip_hash_wrapper_spec.rb b/spec/functions/mysql_strip_hash_wrapper_spec.rb deleted file mode 100644 index be11d835f..000000000 --- a/spec/functions/mysql_strip_hash_wrapper_spec.rb +++ /dev/null @@ -1,28 +0,0 @@ -require 'spec_helper' - -describe 'mysql_strip_hash' do - it 'exists' do - is_expected.not_to eq(nil) - end - - it 'raises a ArgumentError if there is less than 1 arguments' do - is_expected.to run.with_params.and_raise_error(ArgumentError) - end - - it 'raises a ArgumentError if there is more than 1 arguments' do - is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(ArgumentError) - end - - # Please note that the below test's fail if run via rspec rather than rake - it 'raises a ArgumentError if argument is not a hash' do - is_expected.to run.with_params('foo').and_raise_error(ArgumentError) - end - - it 'passes a hash without blanks through' do - is_expected.to run.with_params('one' => 1, 'two' => 2, 'three' => 3).and_return('one' => 1, 'two' => 2, 'three' => 3) - end - - it 'removes blank hash elements' do - is_expected.to run.with_params('one' => 1, 'two' => '', 'three' => nil, 'four' => 4).and_return('one' => 1, 'three' => nil, 'four' => 4) - end -end diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index d1fb56c7b..93e2df350 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -16,13 +16,27 @@ install_module_on(hosts) install_module_dependencies_on(hosts) +def idempotent_apply(hosts, manifest, opts = {}, &block) + block_on hosts, opts do |host| + file_path = host.tmpfile('apply_manifest.pp') + create_remote_file(host, file_path, manifest + "\n") + + puppet_apply_opts = { :verbose => nil, 'detailed-exitcodes' => nil } + on_options = { acceptable_exit_codes: [0, 2] } + on host, puppet('apply', file_path, puppet_apply_opts), on_options, &block + puppet_apply_opts2 = { :verbose => nil, 'detailed-exitcodes' => nil } + on_options2 = { acceptable_exit_codes: [0] } + on host, puppet('apply', file_path, puppet_apply_opts2), on_options2, &block + end +end + RSpec.configure do |c| # Readable test descriptions c.formatter = :documentation # detect the situation where PUP-5016 is triggered and skip the idempotency tests in that case # also note how fact('puppetversion') is not available because of PUP-4359 - if fact('osfamily') == 'Debian' && fact('operatingsystemmajrelease') == '8' && shell('puppet --version').stdout =~ %r{^4\.2} + if os[:family] == 'debian' && os[:release].to_i == 8 && shell('puppet --version').stdout =~ %r{^4\.2} c.filter_run_excluding skip_pup_5016: true end @@ -31,20 +45,15 @@ run_puppet_access_login(user: 'admin') if pe_install? && (Gem::Version.new(puppet_version) >= Gem::Version.new('5.0.0')) hosts.each do |host| # This will be removed, this is temporary to test localisation. - if (fact('osfamily') == 'Debian' || fact('osfamily') == 'RedHat') && - (Gem::Version.new(puppet_version) >= Gem::Version.new('4.10.5') && - Gem::Version.new(puppet_version) < Gem::Version.new('5.2.0')) - on(host, 'mkdir /opt/puppetlabs/puppet/share/locale/ja') - on(host, 'touch /opt/puppetlabs/puppet/share/locale/ja/puppet.po') - end - if fact('osfamily') == 'Debian' + + if os[:family] == 'debian' # install language on debian systems install_language_on(host, 'ja_JP.utf-8') if not_controller(host) # This will be removed, this is temporary to test localisation. end # Required for binding tests. - if fact('osfamily') == 'RedHat' - if fact('operatingsystemmajrelease') =~ %r{7} || fact('operatingsystem') =~ %r{Fedora} + if os[:family] == 'redhat' + if os[:release].to_i == 7 || os[:family] == 'fedora' shell('yum install -y bzip2') end end diff --git a/spec/spec_helper_local.rb b/spec/spec_helper_local.rb index 9ff7f23e5..0a0c2d772 100644 --- a/spec/spec_helper_local.rb +++ b/spec/spec_helper_local.rb @@ -1,2 +1,31 @@ require 'rspec-puppet-facts' include RspecPuppetFacts + +if ENV['COVERAGE'] == 'yes' + require 'simplecov' + require 'simplecov-console' + require 'codecov' + + SimpleCov.formatters = [ + SimpleCov::Formatter::HTMLFormatter, + SimpleCov::Formatter::Console, + SimpleCov::Formatter::Codecov, + ] + SimpleCov.start do + track_files 'lib/**/*.rb' + + add_filter '/spec' + + # do not track vendored files + add_filter '/vendor' + add_filter '/.vendor' + + # do not track gitignored files + # this adds about 4 seconds to the coverage check + # this could definitely be optimized + add_filter do |f| + # system returns true if exit status is 0, which with git-check-ignore means file is ignored + system("git check-ignore --quiet #{f.filename}") + end + end +end diff --git a/spec/unit/facter/mysql_server_id_spec.rb b/spec/unit/facter/mysql_server_id_spec.rb index 889dab0c9..74f7772ba 100644 --- a/spec/unit/facter/mysql_server_id_spec.rb +++ b/spec/unit/facter/mysql_server_id_spec.rb @@ -23,5 +23,14 @@ Facter.fact(:mysql_server_id).value.to_s.should == '0' end end + + context 'test nil case' do + before :each do + Facter.fact(:macaddress).stubs(:value).returns(nil) + end + it do + Facter.fact(:mysql_server_id).value.to_s.should == '' + end + end end end diff --git a/spec/unit/puppet/type/mysql_user_spec.rb b/spec/unit/puppet/type/mysql_user_spec.rb index c491d8cf9..cdc66b81b 100644 --- a/spec/unit/puppet/type/mysql_user_spec.rb +++ b/spec/unit/puppet/type/mysql_user_spec.rb @@ -125,4 +125,12 @@ }.to raise_error %r{Invalid database user "misquoted@localhost} end end + + context 'using invalid options' do + it 'fails with an invalid option' do + expect { + Puppet::Type.type(:mysql_user).new(name: 'misquoted@localhost', password_hash: 'pass', tls_options: ['SOMETHING_ELSE']) + }.to raise_error %r{Invalid tls option} + end + end end