From fc2e8816c94ff7652f1ad5015adce0c098a685ce Mon Sep 17 00:00:00 2001 From: Samuel Cassiba Date: Fri, 1 Jul 2016 16:08:05 -0700 Subject: [PATCH] Style and lint fixes to support newer ChefDK - bumps ChefDK release to 0.17.17 in bootstrap Change-Id: I6ce4587caa3ae68ddbd3ef1a521aaf46f4840b2c (cherry picked from commit 51ac5f8b9f4f2afca353083c3eec826058e4e591) --- README.md | 2 +- bootstrap.sh | 13 +++---- libraries/cli.rb | 2 +- libraries/config_helpers.rb | 10 +++--- libraries/matchers.rb | 10 +----- libraries/network.rb | 21 ++++++------ libraries/search.rb | 10 +++--- libraries/uri.rb | 2 +- metadata.rb | 2 ++ providers/database.rb | 2 +- recipes/logging.rb | 5 +++ spec/endpoints_spec.rb | 2 +- spec/parse_spec.rb | 54 ++++++++++++++---------------- spec/password_spec.rb | 4 +-- spec/spec_helper.rb | 4 +-- templates/default/logging.conf.erb | 20 +++++------ 16 files changed, 78 insertions(+), 85 deletions(-) diff --git a/README.md b/README.md index c31a5219..a197ab2a 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Requirements ============ - Chef 12 or higher -- chefdk 0.9.0 for testing (also includes berkshelf for cookbook dependency +- chefdk 0.15.15 for testing (also includes berkshelf for cookbook dependency resolution) Platform diff --git a/bootstrap.sh b/bootstrap.sh index 882b7f1d..d2aa778f 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,5 +1,5 @@ #!/bin/bash -x -## This script is for installing all the needed packages on centos 7 and trusty to run the chef tests with 'chef exec rake' +## This script is for installing all the needed packages on CentOS 7 and Ubuntu Trusty to run the chef tests with 'chef exec rake' if [ -f /usr/bin/yum ] ; then # install needed packages @@ -11,8 +11,8 @@ if [ -f /usr/bin/yum ] ; then sudo pip uninstall requests -y || true # install chefdk - chefdk=chefdk-0.9.0-1.el7.x86_64.rpm - wget -nv -t 3 https://opscode-omnibus-packages.s3.amazonaws.com/el/7/x86_64/$chefdk + chefdk=chefdk-0.17.17-1.el7.x86_64.rpm + wget -nv -t 3 https://packages.chef.io/stable/el/7/$chefdk sudo yum -y install $chefdk rm $chefdk @@ -26,16 +26,13 @@ elif [ -f /usr/bin/apt-get ]; then sudo apt-get -y install build-essential liblzma-dev zlib1g-dev # install chefdk - chefdk=chefdk_0.9.0-1_amd64.deb - wget -nv -t 3 https://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/12.04/x86_64/$chefdk + chefdk=chefdk_0.17.17-1_amd64.deb + wget -nv -t 3 https://packages.chef.io/stable/ubuntu/12.04/$chefdk sudo dpkg -i $chefdk rm $chefdk fi -## workaround to fix redhat fauxhai permission issue (can be removed with fauxhai > 2.3 in chefdk) -sudo chef exec ruby -e "require 'fauxhai'; Fauxhai.mock(platform:'redhat', version:'7.1')" - # The following will handle cross cookbook patch dependencies via the Depends-On in commit message # ZUUL_CHANGES has a ^ separated list of patches, the last being the current patch. diff --git a/libraries/cli.rb b/libraries/cli.rb index 19852a8d..c61234aa 100644 --- a/libraries/cli.rb +++ b/libraries/cli.rb @@ -64,7 +64,7 @@ module ::Openstack Chef::Log.debug("Running openstack command: #{openstackcmd} with environment: #{env}") result = shell_out(openstackcmd, env: env) Chef::Log.debug("Output for command: #{cmd}:\n#{result.stdout}\n#{result.stderr}") - fail "#{result.stderr} (#{result.exitstatus})" if result.exitstatus != 0 + raise "#{result.stderr} (#{result.exitstatus})" if result.exitstatus != 0 result.stdout end diff --git a/libraries/config_helpers.rb b/libraries/config_helpers.rb index 8fa9543d..02c2b361 100644 --- a/libraries/config_helpers.rb +++ b/libraries/config_helpers.rb @@ -26,11 +26,11 @@ module ::Openstack # @param [String] service def merge_config_options(service) conf = deep_dup(node['openstack'][service]['conf']) - if node['openstack'][service]['conf_secrets'] - conf_secrets = deep_dup(node['openstack'][service]['conf_secrets']) - else - conf_secrets = {} - end + conf_secrets = if node['openstack'][service]['conf_secrets'] + deep_dup(node['openstack'][service]['conf_secrets']) + else + {} + end Chef::Mixin::DeepMerge.merge(conf, conf_secrets) end diff --git a/libraries/matchers.rb b/libraries/matchers.rb index aeacae74..e2a2450d 100644 --- a/libraries/matchers.rb +++ b/libraries/matchers.rb @@ -49,11 +49,7 @@ if defined?(ChefSpec) # rubocop:disable MethodLength, CyclomaticComplexity def matches_content? def section?(line, section = '.*') - if line =~ /^[ \t]*\[#{section}\]/ - return true - else - return false - end + return true if line =~ /^[ \t]*\[#{section}\]/ end def get_section_content(content, section) @@ -86,10 +82,6 @@ if defined?(ChefSpec) if @expected_content.is_a?(Regexp) - # MRV Hack to allow windows env to work with rspec - # when regex end with $ - @actual_content = @actual_content.gsub(/\r/, '') - @actual_content =~ @expected_content elsif RSpec::Matchers.is_a_matcher?(@expected_content) @expected_content.matches?(@actual_content) diff --git a/libraries/network.rb b/libraries/network.rb index 3c0b0b13..8ec3a42f 100644 --- a/libraries/network.rb +++ b/libraries/network.rb @@ -31,15 +31,16 @@ module ::Openstack def address_for(interface, family = node['openstack']['endpoints']['family'], nodeish = node, drop_vips = true) Chef::Log.debug("address_for(#{interface}, #{family}, #{nodeish})") if interface == 'all' - if family == 'inet6' + case family + when 'inet6' return '::' - else + when 'inet' return '0.0.0.0' end end - fail "Interface #{interface} does not exist" unless nodeish['network']['interfaces'][interface] + raise "Interface #{interface} does not exist" unless nodeish['network']['interfaces'][interface] addresses = nodeish['network']['interfaces'][interface]['addresses'] - fail "Interface #{interface} has no addresses assigned" if addresses.to_a.empty? + raise "Interface #{interface} has no addresses assigned" if addresses.to_a.empty? get_address addresses, family, drop_vips end @@ -49,11 +50,11 @@ module ::Openstack # @param [Hash] service_config pointed to the set Hash def bind_address(service_config) iface = service_config['interface'] - if iface - address = address_for(iface) - else - address = service_config['host'] - end + address = if iface + address_for(iface) + else + service_config['host'] + end address end @@ -69,6 +70,6 @@ module ::Openstack addresses.each do |addr, data| return addr if data['family'] == family && (data['prefixlen'] != vip_prefixlen || !drop_vips) end - fail "No address for family #{family} found" + raise "No address for family #{family} found" end end diff --git a/libraries/search.rb b/libraries/search.rb index 3844dc1d..ab3ee57b 100644 --- a/libraries/search.rb +++ b/libraries/search.rb @@ -46,19 +46,17 @@ module ::Openstack # @return [Array] A list of memcached servers in format # ':'. def memcached_servers(role = 'infra-caching') - if !node['openstack']['memcached_servers'] + if node['openstack']['memcached_servers'].nil? search_for(role).map do |n| listen = n['memcached']['listen'] port = n['memcached']['port'] || '11211' "#{listen}:#{port}" end.sort + elsif node['openstack']['memcached_servers'] + node['openstack']['memcached_servers'] else - if node['openstack']['memcached_servers'].length != 0 - node['openstack']['memcached_servers'] - else - [] - end + [] end end diff --git a/libraries/uri.rb b/libraries/uri.rb index 47d7cf7f..6459e622 100644 --- a/libraries/uri.rb +++ b/libraries/uri.rb @@ -43,7 +43,7 @@ module ::Openstack # intended for joining URI relative path segments. This function merely # helps to accurately join supplied paths. def uri_join_paths(*paths) - return nil if paths.length == 0 + return nil if paths.empty? leadingslash = paths[0][0] == '/' ? '/' : '' trailingslash = paths[-1][-1] == '/' ? '/' : '' paths.map! { |path| path.sub(%r{^\/+}, '').sub(%r{\/+$}, '') } diff --git a/metadata.rb b/metadata.rb index 80bf0324..ccd31082 100644 --- a/metadata.rb +++ b/metadata.rb @@ -1,6 +1,8 @@ name 'openstack-common' maintainer 'openstack-chef' maintainer_email 'openstack-dev@lists.openstack.org' +issues_url 'https://launchpad.net/openstack-chef' if respond_to?(:issues_url) +source_url 'https://github.com/openstack/cookbook-openstack-common' if respond_to?(:source_url) license 'Apache 2.0' description 'Common OpenStack attributes, libraries and recipes.' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) diff --git a/providers/database.rb b/providers/database.rb index 4641baf6..179226d1 100644 --- a/providers/database.rb +++ b/providers/database.rb @@ -46,7 +46,7 @@ def db_types @user_prov = ::Chef::Provider::Database::MysqlUser @super_user = 'root' else - fail "Unsupported database type #{@db_type}" + raise "Unsupported database type #{@db_type}" end end diff --git a/recipes/logging.rb b/recipes/logging.rb index 760e1313..68b76186 100644 --- a/recipes/logging.rb +++ b/recipes/logging.rb @@ -30,4 +30,9 @@ template '/etc/openstack/logging.conf' do owner 'root' group 'root' mode 00644 + variables( + loggers: node['openstack']['logging']['loggers'], + formatters: node['openstack']['logging']['formatters'], + handlers: node['openstack']['logging']['handlers'] + ) end diff --git a/spec/endpoints_spec.rb b/spec/endpoints_spec.rb index 309ce7e2..41986979 100644 --- a/spec/endpoints_spec.rb +++ b/spec/endpoints_spec.rb @@ -80,7 +80,7 @@ describe 'openstack-common::default' do end it 'returns endpoint URI object when uri key not in endpoint hash but host is in hash' do - subject.should_receive(:uri_from_hash).with('host' => 'localhost', 'port' => '1234') + expect(subject).to receive(:uri_from_hash).with('host' => 'localhost', 'port' => '1234') uri_hash = { 'openstack' => { 'endpoints' => { diff --git a/spec/parse_spec.rb b/spec/parse_spec.rb index ce2d4cbd..76d7a2ee 100644 --- a/spec/parse_spec.rb +++ b/spec/parse_spec.rb @@ -19,11 +19,11 @@ describe 'Openstack parse' do end it 'returns proper array of hashes when proper table provided' do table = -'+---------+----------------------------------+----------------------------------+ -| tenant | access | secret | -+---------+----------------------------------+----------------------------------+ -| service | 91af731b3be244beb8f30fc59b7bc96d | ce811442cfb549c39390a203778a4bf5 | -+---------+----------------------------------+----------------------------------+' + '+---------+----------------------------------+----------------------------------+ + | tenant | access | secret | + +---------+----------------------------------+----------------------------------+ + | service | 91af731b3be244beb8f30fc59b7bc96d | ce811442cfb549c39390a203778a4bf5 | + +---------+----------------------------------+----------------------------------+' expect( subject.prettytable_to_array(table) ).to eq( @@ -33,11 +33,11 @@ describe 'Openstack parse' do end it 'returns proper array of hashes when proper table provided including whitespace' do table = -'+---------+----------------------------------+----------------------------------+ -| tenant | access | secret | -+---------+----------------------------------+----------------------------------+ -| service | 91af731b3be244beb8f30fc59b7bc96d | ce811442cfb549c39390a203778a4bf5 | -+---------+----------------------------------+----------------------------------+ + '+---------+----------------------------------+----------------------------------+ + | tenant | access | secret | + +---------+----------------------------------+----------------------------------+ + | service | 91af731b3be244beb8f30fc59b7bc96d | ce811442cfb549c39390a203778a4bf5 | + +---------+----------------------------------+----------------------------------+ ' @@ -50,14 +50,14 @@ describe 'Openstack parse' do end it 'returns a flatten hash when provided a Property/Value table' do table = -'+-----------+----------------------------------+ -| Property | Value | -+-----------+----------------------------------+ -| access | 91af731b3be244beb8f30fc59b7bc96d | -| secret | ce811442cfb549c39390a203778a4bf5 | -| tenant_id | 429271dd1cf54b7ca921a0017524d8ea | -| user_id | 1c4fc229560f40689c490c5d0838fd84 | -+-----------+----------------------------------+' + '+-----------+----------------------------------+ + | Property | Value | + +-----------+----------------------------------+ + | access | 91af731b3be244beb8f30fc59b7bc96d | + | secret | ce811442cfb549c39390a203778a4bf5 | + | tenant_id | 429271dd1cf54b7ca921a0017524d8ea | + | user_id | 1c4fc229560f40689c490c5d0838fd84 | + +-----------+----------------------------------+' expect( subject.prettytable_to_array(table) ).to eq( @@ -68,16 +68,14 @@ describe 'Openstack parse' do end it 'returns a flatten hash when provided a Property/Value table including whitespace' do table = -' - -+-----------+----------------------------------+ -| Property | Value | -+-----------+----------------------------------+ -| access | 91af731b3be244beb8f30fc59b7bc96d | -| secret | ce811442cfb549c39390a203778a4bf5 | -| tenant_id | 429271dd1cf54b7ca921a0017524d8ea | -| user_id | 1c4fc229560f40689c490c5d0838fd84 | -+-----------+----------------------------------+' + '+-----------+----------------------------------+ + | Property | Value | + +-----------+----------------------------------+ + | access | 91af731b3be244beb8f30fc59b7bc96d | + | secret | ce811442cfb549c39390a203778a4bf5 | + | tenant_id | 429271dd1cf54b7ca921a0017524d8ea | + | user_id | 1c4fc229560f40689c490c5d0838fd84 | + +-----------+----------------------------------+' expect( subject.prettytable_to_array(table) ).to eq( diff --git a/spec/password_spec.rb b/spec/password_spec.rb index f89df203..f605365c 100644 --- a/spec/password_spec.rb +++ b/spec/password_spec.rb @@ -34,7 +34,7 @@ describe 'openstack-common::default' do end describe '#get_password' do - ['service', 'db', 'user'].each do |type| + %w(service db user).each do |type| it "returns databag value for #{type}" do value = { 'nova' => 'this' } allow(Chef::EncryptedDataBagItem).to receive(:load_secret).with('/etc/chef/openstack_data_bag_secret').and_return('secret') @@ -69,7 +69,7 @@ describe 'openstack-common::default' do end describe '#get_password' do - ['service', 'db', 'user'].each do |type| + %w(service db user).each do |type| it "returns databag value for #{type}" do value = { 'nova' => 'this' } allow(Chef::DataBagItem).to receive(:load).with("#{type}_passwords", 'nova').and_return(value) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a12b1373..8e53b136 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,12 +9,12 @@ UBUNTU_OPTS = { platform: 'ubuntu', version: '14.04', log_level: LOG_LEVEL -} +}.freeze REDHAT_OPTS = { platform: 'redhat', version: '7.1', log_level: LOG_LEVEL -} +}.freeze # We set a default platform for non-platform specific test cases CHEFSPEC_OPTS = UBUNTU_OPTS diff --git a/templates/default/logging.conf.erb b/templates/default/logging.conf.erb index 145bb818..5940d38b 100644 --- a/templates/default/logging.conf.erb +++ b/templates/default/logging.conf.erb @@ -1,26 +1,26 @@ [loggers] -keys=<%= (node["openstack"]["logging"]['loggers'].keys).join(',') %> +keys=<%= (@loggers.keys).join(',') %> [formatters] -keys=<%= node["openstack"]["logging"]['formatters'].keys.join(',') %> +keys=<%= @formatters.keys.join(',') %> [handlers] -keys=<%= node["openstack"]["logging"]['handlers'].keys.join(',') %> +keys=<%= @handlers.keys.join(',') %> -## FORMATTERS ## +## LOGGERS ## -<% node["openstack"]["logging"]['formatters'].each do |section, options| %> -[formatter_<%= section %>] +<% @loggers.each do |section, options| %> +[logger_<%= section %>] <% options.each do |key, value| %> <%= key %>=<%= value %> <% end %> <% end %> -## LOGGERS ## +## FORMATTERS ## -<% node["openstack"]["logging"]['loggers'].each do |section, options| %> -[logger_<%= section %>] +<% @formatters.each do |section, options| %> +[formatter_<%= section %>] <% options.each do |key, value| %> <%= key %>=<%= value %> <% end %> @@ -29,7 +29,7 @@ keys=<%= node["openstack"]["logging"]['handlers'].keys.join(',') %> ## HANDLERS ## -<% node["openstack"]["logging"]['handlers'].each do |section, options| %> +<% @handlers.each do |section, options| %> [handler_<%= section %>] <% options.each do |key, value| %> <%= key %>=<%= value %>