refactor chefspec tests to be cleaner and faster

Implements: blueprint refactor-spec-files
Change-Id: Iaf86f8a53816c8100ef6262fd976fee417e8eb8b
This commit is contained in:
Ionuț Arțăriși 2014-03-06 15:04:37 +01:00
parent 46399a40ef
commit 9e3fa4adb3
8 changed files with 93 additions and 99 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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]
)

View File

@ -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

View File

@ -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

View File

@ -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