Improve ChefSpec test speed by enabling caching

This updates all references of let(:chef_run) to cached(:chef_run) to
speed up tests. By doing this, we have to create a new cached(:chef_run)
block whenever we need to adjust node attributes for testing.

- Remove unused default recipe ChefSpec
- Formatting cleanup

Speed was improved from 3 minutes 9.1 seconds to 38.21 seconds

Change-Id: I470991c600bca0ad2b6a389923a2fe8c954b7008
This commit is contained in:
Lance Albertson
2020-01-08 11:10:41 -08:00
parent a95549bae0
commit b81d56bdc7
13 changed files with 76 additions and 79 deletions

View File

@@ -8,7 +8,7 @@ describe 'openstack-block-storage::api' do
describe 'redhat' do describe 'redhat' do
let(:runner) { ChefSpec::SoloRunner.new(REDHAT_OPTS) } let(:runner) { ChefSpec::SoloRunner.new(REDHAT_OPTS) }
let(:node) { runner.node } let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) } cached(:chef_run) { runner.converge(described_recipe) }
include_context 'block-storage-stubs' include_context 'block-storage-stubs'

View File

@@ -8,7 +8,7 @@ describe 'openstack-block-storage::api' do
describe 'ubuntu' do describe 'ubuntu' do
let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) } let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) }
let(:node) { runner.node } let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) } cached(:chef_run) { runner.converge(described_recipe) }
include_context 'block-storage-stubs' include_context 'block-storage-stubs'
include_examples 'common-logging' include_examples 'common-logging'
@@ -60,8 +60,11 @@ describe 'openstack-block-storage::api' do
it 'does not manage policy file unless specified' do it 'does not manage policy file unless specified' do
expect(chef_run).not_to create_remote_file('/etc/cinder/policy.json') expect(chef_run).not_to create_remote_file('/etc/cinder/policy.json')
end end
describe 'policy file specified' do context 'policy file specified' do
before { node.override['openstack']['block-storage']['policyfile_url'] = 'http://server/mypolicy.json' } cached(:chef_run) do
node.override['openstack']['block-storage']['policyfile_url'] = 'http://server/mypolicy.json'
runner.converge(described_recipe)
end
let(:remote_policy) { chef_run.remote_file('/etc/cinder/policy.json') } let(:remote_policy) { chef_run.remote_file('/etc/cinder/policy.json') }
it 'manages policy file when remote file is specified' do it 'manages policy file when remote file is specified' do

View File

@@ -8,7 +8,7 @@ describe 'openstack-block-storage::backup' do
describe 'redhat' do describe 'redhat' do
let(:runner) { ChefSpec::SoloRunner.new(REDHAT_OPTS) } let(:runner) { ChefSpec::SoloRunner.new(REDHAT_OPTS) }
let(:node) { runner.node } let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) } cached(:chef_run) { runner.converge(described_recipe) }
include_context 'block-storage-stubs' include_context 'block-storage-stubs'

View File

@@ -8,7 +8,7 @@ describe 'openstack-block-storage::backup' do
describe 'ubuntu' do describe 'ubuntu' do
let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) } let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) }
let(:node) { runner.node } let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) } cached(:chef_run) { runner.converge(described_recipe) }
include_context 'block-storage-stubs' include_context 'block-storage-stubs'

View File

@@ -8,7 +8,7 @@ describe 'openstack-block-storage::cinder-common' do
describe 'rhel' do describe 'rhel' do
let(:runner) { ChefSpec::SoloRunner.new(REDHAT_OPTS) } let(:runner) { ChefSpec::SoloRunner.new(REDHAT_OPTS) }
let(:node) { runner.node } let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) } cached(:chef_run) { runner.converge(described_recipe) }
include_context 'block-storage-stubs' include_context 'block-storage-stubs'

View File

@@ -8,13 +8,21 @@ describe 'openstack-block-storage::cinder-common' do
describe 'ubuntu' do describe 'ubuntu' do
let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) } let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) }
let(:node) { runner.node } let(:node) { runner.node }
let(:chef_run) do let(:test_pass) { 'test_pass' }
cached(:chef_run) do
node.override['openstack']['mq']['host'] = '127.0.0.1' node.override['openstack']['mq']['host'] = '127.0.0.1'
node.override['openstack']['mq']['block-storage']['rabbit']['notification_topic'] = 'rabbit_topic' node.override['openstack']['mq']['block-storage']['rabbit']['notification_topic'] = 'rabbit_topic'
runner.converge(described_recipe) runner.converge(described_recipe)
end end
before do
allow_any_instance_of(Chef::Recipe).to receive(:get_password)
.with('user', anything)
.and_return(test_pass)
allow_any_instance_of(Chef::Recipe).to receive(:db_uri)
.and_return('sql_connection_value')
end
include_context 'block-storage-stubs' include_context 'block-storage-stubs'
it 'upgrades the cinder-common package' do it 'upgrades the cinder-common package' do
@@ -35,12 +43,6 @@ describe 'openstack-block-storage::cinder-common' do
describe 'cinder.conf' do describe 'cinder.conf' do
let(:file) { chef_run.template('/etc/cinder/cinder.conf') } let(:file) { chef_run.template('/etc/cinder/cinder.conf') }
let(:test_pass) { 'test_pass' }
before do
allow_any_instance_of(Chef::Recipe).to receive(:get_password)
.with('user', anything)
.and_return(test_pass)
end
it 'should create the cinder.conf template' do it 'should create the cinder.conf template' do
expect(chef_run).to create_template(file.name).with( expect(chef_run).to create_template(file.name).with(
@@ -50,7 +52,7 @@ describe 'openstack-block-storage::cinder-common' do
) )
end end
context 'keystone authtoken attributes with default values' do describe 'keystone authtoken attributes with default values' do
it 'does not set memcached server(s)' do it 'does not set memcached server(s)' do
expect(chef_run).not_to render_file(file.name).with_content(/^memcached_servers = $/) expect(chef_run).not_to render_file(file.name).with_content(/^memcached_servers = $/)
end end
@@ -68,20 +70,22 @@ describe 'openstack-block-storage::cinder-common' do
end end
end end
context 'keystone authtoken attributes' do describe 'keystone authtoken attributes' do
it do it do
expect(chef_run).not_to render_file(file.name).with_content(/^auth_version = v2.0$/) expect(chef_run).not_to render_file(file.name).with_content(/^auth_version = v2.0$/)
end end
it 'has an admin password' do it 'has an admin password' do
# (fgimenez) the get_password mocking is set in spec/spec_helper.rb # (fgimenez) the get_password mocking is set in spec/spec_helper.rb
expect(chef_run).to render_config_file(file.name).with_section_content('keystone_authtoken', /^password = cinder-pass$/) expect(chef_run).to render_config_file(file.name)
.with_section_content('keystone_authtoken', /^password = cinder-pass$/)
end end
end end
context 'template contents' do describe 'template contents' do
it 'has a lock_path attribute' do it 'has a lock_path attribute' do
expect(chef_run).to render_config_file(file.name).with_section_content('oslo_concurrency', %r{^lock_path = /var/lib/cinder/tmp}) expect(chef_run).to render_config_file(file.name)
.with_section_content('oslo_concurrency', %r{^lock_path = /var/lib/cinder/tmp})
end end
it 'does not have unique host id by default' do it 'does not have unique host id by default' do
@@ -93,73 +97,67 @@ describe 'openstack-block-storage::cinder-common' do
end end
context 'syslog use' do context 'syslog use' do
it 'sets the log_config value when syslog is in use' do cached(:chef_run) do
node.override['openstack']['block-storage']['syslog']['use'] = true node.override['openstack']['block-storage']['syslog']['use'] = true
runner.converge(described_recipe)
expect(chef_run).to render_file(file.name) end
.with_content(%r{^log_config = /etc/openstack/logging.conf$}) it 'sets the log_config value when syslog is in use' do
expect(chef_run).to render_file(file.name).with_content(%r{^log_config = /etc/openstack/logging.conf$})
end end
end end
it 'has a db connection attribute' do it 'has a db connection attribute' do
allow_any_instance_of(Chef::Recipe).to receive(:db_uri)
.and_return('sql_connection_value')
expect(chef_run).to render_config_file(file.name) expect(chef_run).to render_config_file(file.name)
.with_section_content('database', /^connection = sql_connection_value$/) .with_section_content('database', /^connection = sql_connection_value$/)
end end
it 'has a glance_api_servers attribute' do it 'has a glance_api_servers attribute' do
expect(chef_run).to render_config_file(file.name).with_section_content('DEFAULT', %r{^glance_api_servers = http://127.0.0.1:9292$}) expect(chef_run).to render_config_file(file.name)
.with_section_content('DEFAULT', %r{^glance_api_servers = http://127.0.0.1:9292$})
end end
context 'cinder endpoint' do describe 'cinder endpoint' do
it 'has osapi_volume_listen set' do it 'has osapi_volume_listen set' do
expect(chef_run).to render_config_file(file.name).with_section_content('DEFAULT', /^osapi_volume_listen = 127.0.0.1$/) expect(chef_run).to render_config_file(file.name)
.with_section_content('DEFAULT', /^osapi_volume_listen = 127.0.0.1$/)
end end
it 'has osapi_volume_listen_port set' do it 'has osapi_volume_listen_port set' do
expect(chef_run).to render_config_file(file.name).with_section_content('DEFAULT', /^osapi_volume_listen_port = 8776$/) expect(chef_run).to render_config_file(file.name)
.with_section_content('DEFAULT', /^osapi_volume_listen_port = 8776$/)
end end
end end
it 'has default transport_url/AMQP options set' do it 'has default transport_url/AMQP options set' do
[%r{^transport_url = rabbit://guest:mypass@127.0.0.1:5672$}].each do |line| [
%r{^transport_url = rabbit://guest:mypass@127.0.0.1:5672$},
].each do |line|
expect(chef_run).to render_config_file(file.name).with_section_content('DEFAULT', line) expect(chef_run).to render_config_file(file.name).with_section_content('DEFAULT', line)
end end
end end
context 'rabbitmq as mq service' do describe 'rabbitmq as mq service' do
context 'non ha attributes' do describe 'non ha attributes' do
before do
node.override['openstack']['mq']['block-storage']['rabbit']['ha'] = false
end
it 'does not have a rabbit_hosts attribute' do it 'does not have a rabbit_hosts attribute' do
expect(chef_run).not_to render_config_file(file.name).with_section_content('oslo_messaging_rabbit', /^rabbit_hosts = /) expect(chef_run).not_to render_config_file(file.name)
.with_section_content('oslo_messaging_rabbit', /^rabbit_hosts = /)
end end
end end
end end
context 'lvm settings' do
before do
node.override['openstack']['block-storage']['volume']['driver'] = 'cinder.volume.drivers.lvm.LVMVolumeDriver'
end
end
context 'commonly named volume attributes' do context 'commonly named volume attributes' do
%w(iscsi_ip_address iscsi_port iscsi_helper volumes_dir).each do |attr| vol_attrs = %w(iscsi_ip_address iscsi_port iscsi_helper volumes_dir)
it "has volume related #{attr} attribute" do cached(:chef_run) do
vol_attrs.each do |attr|
node.override['openstack']['block-storage']['conf']['DEFAULT'][attr] = "common_volume_#{attr}_value" node.override['openstack']['block-storage']['conf']['DEFAULT'][attr] = "common_volume_#{attr}_value"
end
runner.converge(described_recipe)
end
vol_attrs.each do |attr|
it "has volume related #{attr} attribute" do
expect(chef_run).to render_file(file.name).with_content(/^#{attr} = common_volume_#{attr}_value$/) expect(chef_run).to render_file(file.name).with_content(/^#{attr} = common_volume_#{attr}_value$/)
end end
end end
end end
context 'netapp ISCSI settings' do
before do
node.override['openstack']['block-storage']['conf']['DEFAULT']['volume_driver'] = 'cinder.volume.drivers.netapp.NetAppISCSIDriver'
end
end
end end
end end

View File

@@ -1,8 +0,0 @@
# encoding: UTF-8
#
# Cookbook Name:: openstack-block-storage
require_relative 'spec_helper'
describe 'openstack-block-storage::default' do
end

View File

@@ -8,7 +8,7 @@ describe 'openstack-block-storage::identity_registration' do
describe 'ubuntu' do describe 'ubuntu' do
let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) } let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) }
let(:node) { runner.node } let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) } cached(:chef_run) { runner.converge(described_recipe) }
include_context 'block-storage-stubs' include_context 'block-storage-stubs'
@@ -83,11 +83,16 @@ describe 'openstack-block-storage::identity_registration' do
end end
end end
it 'with custom region override' do context 'with custom region override' do
node.override['openstack']['block-storage']['region'] = 'volumeRegion' cached(:chef_run) do
expect(chef_run).to create_openstack_endpoint( node.override['openstack']['block-storage']['region'] = 'volumeRegion'
service_type runner.converge(described_recipe)
).with(region: 'volumeRegion') end
it do
expect(chef_run).to create_openstack_endpoint(
service_type
).with(region: 'volumeRegion')
end
end end
end end

View File

@@ -8,7 +8,7 @@ describe 'openstack-block-storage::scheduler' do
describe 'redhat' do describe 'redhat' do
let(:runner) { ChefSpec::SoloRunner.new(REDHAT_OPTS) } let(:runner) { ChefSpec::SoloRunner.new(REDHAT_OPTS) }
let(:node) { runner.node } let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) } cached(:chef_run) { runner.converge(described_recipe) }
include_context 'block-storage-stubs' include_context 'block-storage-stubs'

View File

@@ -8,7 +8,7 @@ describe 'openstack-block-storage::scheduler' do
describe 'ubuntu' do describe 'ubuntu' do
let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) } let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) }
let(:node) { runner.node } let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) } cached(:chef_run) { runner.converge(described_recipe) }
include_context 'block-storage-stubs' include_context 'block-storage-stubs'
include_examples 'common-logging' include_examples 'common-logging'

View File

@@ -4,25 +4,22 @@
require 'chefspec' require 'chefspec'
require 'chefspec/berkshelf' require 'chefspec/berkshelf'
ChefSpec::Coverage.start! { add_filter 'openstack-block-storage' }
require 'chef/application' require 'chef/application'
RSpec.configure do |config| RSpec.configure do |config|
config.color = true config.color = true
config.formatter = :documentation config.formatter = :documentation
config.log_level = :fatal config.log_level = :warn
config.file_cache_path = '/var/chef/cache' config.file_cache_path = '/var/chef/cache'
end end
REDHAT_OPTS = { REDHAT_OPTS = {
platform: 'redhat', platform: 'redhat',
version: '7.4', version: '7',
}.freeze }.freeze
UBUNTU_OPTS = { UBUNTU_OPTS = {
platform: 'ubuntu', platform: 'ubuntu',
version: '16.04', version: '18.04',
}.freeze }.freeze
shared_context 'block-storage-stubs' do shared_context 'block-storage-stubs' do
@@ -64,8 +61,9 @@ end
shared_examples 'common-logging' do shared_examples 'common-logging' do
context 'when syslog.use is true' do context 'when syslog.use is true' do
before do cached(:chef_run) do
node.override['openstack']['block-storage']['syslog']['use'] = true node.override['openstack']['block-storage']['syslog']['use'] = true
runner.converge(described_recipe)
end end
it 'runs logging recipe if node attributes say to' do it 'runs logging recipe if node attributes say to' do
@@ -74,8 +72,9 @@ shared_examples 'common-logging' do
end end
context 'when syslog.use is false' do context 'when syslog.use is false' do
before do cached(:chef_run) do
node.override['openstack']['block-storage']['syslog']['use'] = false node.override['openstack']['block-storage']['syslog']['use'] = false
runner.converge(described_recipe)
end end
it 'runs logging recipe if node attributes say to' do it 'runs logging recipe if node attributes say to' do

View File

@@ -8,7 +8,7 @@ describe 'openstack-block-storage::volume' do
describe 'redhat' do describe 'redhat' do
let(:runner) { ChefSpec::SoloRunner.new(REDHAT_OPTS) } let(:runner) { ChefSpec::SoloRunner.new(REDHAT_OPTS) }
let(:node) { runner.node } let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) } cached(:chef_run) { runner.converge(described_recipe) }
include_context 'block-storage-stubs' include_context 'block-storage-stubs'

View File

@@ -8,7 +8,7 @@ describe 'openstack-block-storage::volume' do
describe 'ubuntu' do describe 'ubuntu' do
let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) } let(:runner) { ChefSpec::SoloRunner.new(UBUNTU_OPTS) }
let(:node) { runner.node } let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) } cached(:chef_run) { runner.converge(described_recipe) }
include_context 'block-storage-stubs' include_context 'block-storage-stubs'
include_examples 'common-logging' include_examples 'common-logging'