diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 7c0a436..a899725 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -3,24 +3,20 @@ require_relative 'spec_helper' describe 'openstack-ops-database::client' do - before { ops_database_stubs } describe 'ubuntu' do + include_context 'database-stubs' + let(:runner) { ChefSpec::Runner.new(UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) { runner.converge(described_recipe) } it 'uses mysql database client recipe by default' do - chef_run = ::ChefSpec::Runner.new ::UBUNTU_OPTS - chef_run.converge 'openstack-ops-database::client' - expect(chef_run).to include_recipe 'openstack-ops-database::mysql-client' end it 'uses postgresql database client recipe when configured' do - chef_run = ::ChefSpec::Runner.new ::UBUNTU_OPTS - node = chef_run.node node.set['openstack']['db']['service_type'] = 'postgresql' node.set['postgresql']['password']['postgres'] = 'secret' - chef_run.converge 'openstack-ops-database::client' - expect(chef_run).to include_recipe 'openstack-ops-database::postgresql-client' end end diff --git a/spec/mysql-client-suse_spec.rb b/spec/mysql-client-suse_spec.rb index 803edb4..89f3983 100644 --- a/spec/mysql-client-suse_spec.rb +++ b/spec/mysql-client-suse_spec.rb @@ -2,16 +2,14 @@ require_relative 'spec_helper' -describe 'openstack-ops-database::postgresql-client' do - before { ops_database_stubs } +describe 'openstack-ops-database::mysql-client' do describe 'suse' do - before do - @chef_run = ::ChefSpec::Runner.new ::SUSE_OPTS - @chef_run.converge 'openstack-ops-database::mysql-client' - end + let(:runner) { ChefSpec::Runner.new(SUSE_OPTS) } + let(:node) { runner.node } + let(:chef_run) { runner.converge(described_recipe) } it 'installs mysql packages' do - expect(@chef_run).to install_package 'python-mysql' + expect(chef_run).to install_package('python-mysql') end end end diff --git a/spec/mysql-client_spec.rb b/spec/mysql-client_spec.rb index 09ff29b..53795d0 100644 --- a/spec/mysql-client_spec.rb +++ b/spec/mysql-client_spec.rb @@ -3,20 +3,19 @@ require_relative 'spec_helper' describe 'openstack-ops-database::mysql-client' do - before { ops_database_stubs } + include_context 'database-stubs' describe 'ubuntu' do - before do - @chef_run = ::ChefSpec::Runner.new ::UBUNTU_OPTS - @chef_run.converge 'openstack-ops-database::mysql-client' - end + let(:runner) { ChefSpec::Runner.new(UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) { runner.converge(described_recipe) } it 'includes mysql recipes' do - expect(@chef_run).to include_recipe 'mysql::ruby' - expect(@chef_run).to include_recipe 'mysql::client' + expect(chef_run).to include_recipe 'mysql::ruby' + expect(chef_run).to include_recipe 'mysql::client' end it 'installs mysql packages' do - expect(@chef_run).to install_package 'python-mysqldb' + expect(chef_run).to install_package 'python-mysqldb' end end end diff --git a/spec/mysql-server-redhat_spec.rb b/spec/mysql-server-redhat_spec.rb index 2c1aee9..51406cb 100644 --- a/spec/mysql-server-redhat_spec.rb +++ b/spec/mysql-server-redhat_spec.rb @@ -3,21 +3,21 @@ require_relative 'spec_helper' describe 'openstack-ops-database::mysql-server' do - before { ops_database_stubs } describe 'redhat' do - before do - @chef_run = ::ChefSpec::Runner.new(::REDHAT_OPTS) do |n| - n.set['mysql'] = { - 'server_debian_password' => 'server-debian-password', - 'server_root_password' => 'server-root-password', - 'server_repl_password' => 'server-repl-password' - } - end - @chef_run.converge described_recipe + include_context 'database-stubs' + let(:runner) { ChefSpec::Runner.new(REDHAT_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + node.set_unless['mysql'] = { + 'server_debian_password' => 'server-debian-password', + 'server_root_password' => 'server-root-password', + 'server_repl_password' => 'server-repl-password' + } + runner.converge(described_recipe) end it 'modifies my.cnf template to notify mysql restart' do - file = @chef_run.template 'final-my.cnf' + file = chef_run.template('final-my.cnf') expect(file).to notify('service[mysql]').to(:restart) end end diff --git a/spec/mysql-server_spec.rb b/spec/mysql-server_spec.rb index cdba344..e236f7f 100644 --- a/spec/mysql-server_spec.rb +++ b/spec/mysql-server_spec.rb @@ -3,80 +3,78 @@ require_relative 'spec_helper' describe 'openstack-ops-database::mysql-server' do - before { ops_database_stubs } describe 'ubuntu' do - before do - @chef_run = ::ChefSpec::Runner.new(::UBUNTU_OPTS) do |n| - n.set['mysql'] = { + include_context 'database-stubs' + let(:runner) { ChefSpec::Runner.new(UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + node.set_unless['mysql'] = { 'server_debian_password' => 'server-debian-password', 'server_root_password' => 'server-root-password', 'server_repl_password' => 'server-repl-password' - } - end - @chef_run.converge 'openstack-ops-database::mysql-server' + } + runner.converge(described_recipe) end it 'overrides default mysql attributes' do - expect(@chef_run.node['mysql']['bind_address']).to eql '127.0.0.1' - expect(@chef_run.node['mysql']['tunable']['innodb_thread_concurrency']).to eql '0' - expect(@chef_run.node['mysql']['tunable']['innodb_commit_concurrency']).to eql '0' - expect(@chef_run.node['mysql']['tunable']['innodb_read_io_threads']).to eql '4' - expect(@chef_run.node['mysql']['tunable']['innodb_flush_log_at_trx_commit']).to eql '2' - expect(@chef_run.node['mysql']['tunable']['skip-name-resolve']).to eql true + expect(chef_run.node['mysql']['bind_address']).to eql '127.0.0.1' + expect(chef_run.node['mysql']['tunable']['innodb_thread_concurrency']).to eql '0' + expect(chef_run.node['mysql']['tunable']['innodb_commit_concurrency']).to eql '0' + expect(chef_run.node['mysql']['tunable']['innodb_read_io_threads']).to eql '4' + expect(chef_run.node['mysql']['tunable']['innodb_flush_log_at_trx_commit']).to eql '2' + expect(chef_run.node['mysql']['tunable']['skip-name-resolve']).to eql true end it 'includes mysql recipes' do - expect(@chef_run).to include_recipe 'openstack-ops-database::mysql-client' - expect(@chef_run).to include_recipe 'mysql::server' + expect(chef_run).to include_recipe 'openstack-ops-database::mysql-client' + expect(chef_run).to include_recipe 'mysql::server' end it 'modifies my.cnf template to notify mysql restart' do - file = @chef_run.template '/etc/mysql/my.cnf' + file = chef_run.template '/etc/mysql/my.cnf' expect(file).to notify('service[mysql]').to(:restart) end describe 'lwrps' do - before do - @connection = { - host: 'localhost', - username: 'root', - password: 'server-root-password' - } - end + connection = { + host: 'localhost', + username: 'root', + password: 'server-root-password' + } it 'removes insecure default localhost mysql users' do - resource = @chef_run.find_resource( + resource = chef_run.find_resource( 'mysql_database', 'drop empty localhost user' ).to_hash expect(resource).to include( sql: "DELETE FROM mysql.user WHERE User = '' OR Password = ''", - connection: @connection, + connection: connection, action: [:query] ) end it 'drops the test database' do - resource = @chef_run.find_resource( + resource = chef_run.find_resource( 'mysql_database', 'test' ).to_hash expect(resource).to include( - connection: @connection, + connection: connection, action: [:drop] ) end it 'flushes privileges' do - resource = @chef_run.find_resource( + resource = chef_run.find_resource( 'mysql_database', 'FLUSH PRIVILEGES' ).to_hash expect(resource).to include( - connection: @connection, + connection: connection, sql: 'FLUSH PRIVILEGES', action: [:query] ) diff --git a/spec/postgresql-server_spec.rb b/spec/postgresql-server_spec.rb index 017aca6..048dba2 100644 --- a/spec/postgresql-server_spec.rb +++ b/spec/postgresql-server_spec.rb @@ -3,21 +3,24 @@ require_relative 'spec_helper' describe 'openstack-ops-database::postgresql-server' do - before { ops_database_stubs } describe 'ubuntu' do - before do - @chef_run = ::ChefSpec::Runner.new ::UBUNTU_OPTS + include_context 'database-stubs' + + let(:runner) { ChefSpec::Runner.new(UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do # The postgresql cookbook will raise an 'uninitialized constant # Chef::Application' error without this attribute when running # the tests - @chef_run.node.set['postgresql']['password']['postgres'] = String.new - @chef_run.converge 'openstack-ops-database::postgresql-server' + node.set_unless['postgresql']['password']['postgres'] = String.new + + runner.converge(described_recipe) end it 'includes postgresql recipes' do - expect(@chef_run).to include_recipe( + expect(chef_run).to include_recipe( 'openstack-ops-database::postgresql-client') - expect(@chef_run).to include_recipe 'postgresql::server' + expect(chef_run).to include_recipe('postgresql::server') end end end diff --git a/spec/server_spec.rb b/spec/server_spec.rb index 5d04301..33c84cb 100644 --- a/spec/server_spec.rb +++ b/spec/server_spec.rb @@ -3,34 +3,32 @@ require_relative 'spec_helper' describe 'openstack-ops-database::server' do - before { ops_database_stubs } describe 'ubuntu' do + include_context 'database-stubs' + let(:runner) { ChefSpec::Runner.new(UBUNTU_OPTS) } + let(:node) { runner.node } + let(:chef_run) do + node.set_unless['mysql'] = { + 'server_debian_password' => 'server-debian-password', + 'server_root_password' => 'server-root-password', + 'server_repl_password' => 'server-repl-password' + } + runner.converge(described_recipe) + end it 'uses mysql database server recipe by default' do - chef_run = ::ChefSpec::Runner.new(::UBUNTU_OPTS) do |n| - n.set['mysql'] = { - 'server_debian_password' => 'server-debian-password', - 'server_root_password' => 'server-root-password', - 'server_repl_password' => 'server-repl-password' - } - end - chef_run.converge 'openstack-ops-database::server' - - expect(chef_run).to include_recipe 'openstack-ops-database::mysql-server' + expect(chef_run).to include_recipe('openstack-ops-database::mysql-server') end it 'uses postgresql database server recipe when configured' do - chef_run = ::ChefSpec::Runner.new ::UBUNTU_OPTS do |n| - n.set['openstack']['db']['service_type'] = 'postgresql' - # The postgresql cookbook will raise an 'uninitialized constant - # Chef::Application' error without this attribute when running - # the tests - n.set['postgresql']['password']['postgres'] = String.new - end + node.set['openstack']['db']['service_type'] = 'postgresql' + # The postgresql cookbook will raise an 'uninitialized constant + # Chef::Application' error without this attribute when running + # the tests + node.set_unless['postgresql']['password']['postgres'] = String.new - chef_run.converge 'openstack-ops-database::server' - - expect(chef_run).to include_recipe 'openstack-ops-database::postgresql-server' + expect(chef_run).to include_recipe( + 'openstack-ops-database::postgresql-server') end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5fecc0a..2b8ce0f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,15 +20,17 @@ require 'chefspec/berkshelf' log_level: ::LOG_LEVEL } -def ops_database_stubs - # for redhat - stub_command("/usr/bin/mysql -u root -e 'show databases;'") - # for debian - stub_command("\"/usr/bin/mysql\" -u root -e 'show databases;'") +shared_context 'database-stubs' do + before do + # for redhat + stub_command("/usr/bin/mysql -u root -e 'show databases;'") + # for debian + stub_command("\"/usr/bin/mysql\" -u root -e 'show databases;'") - ::Chef::Recipe.any_instance.stub(:address_for) - .with('lo') - .and_return '127.0.0.1' + ::Chef::Recipe.any_instance.stub(:address_for) + .with('lo') + .and_return('127.0.0.1') + end end # README(galstrom21): This will remove any coverage warnings from