From 01450eca641c8c25b81ed03305d03cf5fbe7e62c Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Wed, 9 Mar 2022 22:01:43 +0100 Subject: [PATCH] rubocop: autofix --- .rubocop.yml | 2 + .rubocop_todo.yml | 47 +++++++++++++++++++ lib/puppet/feature/zabbixapi.rb | 2 + lib/puppet/provider/zabbix.rb | 10 ++-- .../provider/zabbix_application/ruby.rb | 6 ++- lib/puppet/provider/zabbix_host/ruby.rb | 6 ++- lib/puppet/provider/zabbix_hostgroup/ruby.rb | 5 +- lib/puppet/provider/zabbix_proxy/ruby.rb | 14 ++++-- lib/puppet/provider/zabbix_template/ruby.rb | 2 + .../provider/zabbix_template_host/ruby.rb | 2 + .../provider/zabbix_userparameters/ruby.rb | 2 + lib/puppet/type/zabbix_application.rb | 2 + lib/puppet/type/zabbix_host.rb | 3 ++ lib/puppet/type/zabbix_hostgroup.rb | 2 + lib/puppet/type/zabbix_proxy.rb | 6 ++- lib/puppet/type/zabbix_template.rb | 6 ++- lib/puppet/type/zabbix_template_host.rb | 4 +- lib/puppet/type/zabbix_userparameters.rb | 2 + spec/acceptance/agent_spec.rb | 12 +++-- spec/acceptance/server_spec.rb | 2 + spec/acceptance/zabbix_application_spec.rb | 8 +++- spec/acceptance/zabbix_host_spec.rb | 46 ++++++++++++++++-- spec/acceptance/zabbix_hostgroup_spec.rb | 3 ++ spec/acceptance/zabbix_proxy_spec.rb | 10 ++++ spec/acceptance/zabbix_template_host_spec.rb | 3 ++ spec/acceptance/zabbix_template_spec.rb | 3 ++ spec/classes/agent_spec.rb | 14 ++++-- spec/classes/database_mysql_spec.rb | 5 +- spec/classes/database_postgresql_spec.rb | 21 +++++---- spec/classes/database_spec.rb | 5 +- spec/classes/javagateway_spec.rb | 5 +- spec/classes/proxy_spec.rb | 5 +- spec/classes/repo_spec.rb | 5 +- spec/classes/sender_spec.rb | 6 ++- spec/classes/server_spec.rb | 5 +- spec/classes/userparameter_spec.rb | 4 +- spec/classes/web_spec.rb | 12 +++-- spec/defines/startup_spec.rb | 17 ++++++- spec/defines/userparameters_spec.rb | 5 +- spec/serverspec_type_zabbixapi.rb | 3 ++ spec/spec_helper_methods.rb | 2 + spec/support/acceptance/prepare_host.rb | 10 ++-- spec/support/acceptance/supported_versions.rb | 2 + spec/types/zabbix_host_spec.rb | 2 + spec/unit/puppet/provider/zabbix_host/ruby.rb | 10 ++-- .../puppet/provider/zabbix_hostgroup/ruby.rb | 10 ++-- .../unit/puppet/provider/zabbix_proxy/ruby.rb | 10 ++-- spec/unit/puppet/type/zabbix_host_spec.rb | 44 ++++++++--------- .../unit/puppet/type/zabbix_hostgroup_spec.rb | 6 ++- spec/unit/puppet/type/zabbix_proxy_spec.rb | 24 ++++++---- spec/unit/puppet/type/zabbix_template_spec.rb | 21 ++++++--- spec/unit/puppet/x/spec_zabbix_types.rb | 20 ++++---- 52 files changed, 362 insertions(+), 121 deletions(-) create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml index 53ac18982..ea22bff86 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,4 +1,6 @@ --- +inherit_from: .rubocop_todo.yml + # Managed by modulesync - DO NOT EDIT # https://voxpupuli.org/docs/updating-files-managed-with-modulesync/ diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 000000000..a28f73a01 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,47 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2022-03-09 21:00:49 UTC using RuboCop version 1.22.3. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 3 +# Configuration parameters: MaximumRangeSize. +Lint/MissingCopEnableDirective: + Exclude: + - 'spec/acceptance/zabbix_host_spec.rb' + - 'spec/acceptance/zabbix_proxy_spec.rb' + - 'spec/unit/puppet/type/zabbix_host_spec.rb' + +# Offense count: 1 +Lint/MissingSuper: + Exclude: + - 'spec/serverspec_type_zabbixapi.rb' + +# Offense count: 7 +# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. +# AllowedNames: at, by, db, id, in, io, ip, of, on, os, pp, to +Naming/MethodParameterName: + Exclude: + - 'lib/puppet/provider/zabbix_proxy/ruby.rb' + - 'lib/puppet/type/zabbix_host.rb' + - 'lib/puppet/type/zabbix_template.rb' + +# Offense count: 4 +RSpec/EmptyExampleGroup: + Exclude: + - 'spec/classes/agent_spec.rb' + - 'spec/classes/javagateway_spec.rb' + - 'spec/classes/server_spec.rb' + +# Offense count: 4 +RSpec/RepeatedExampleGroupDescription: + Exclude: + - 'spec/classes/database_mysql_spec.rb' + - 'spec/defines/startup_spec.rb' + +# Offense count: 1 +Style/MixinUsage: + Exclude: + - 'spec/serverspec_type_zabbixapi.rb' diff --git a/lib/puppet/feature/zabbixapi.rb b/lib/puppet/feature/zabbixapi.rb index a9ea5666d..f6fcc09e2 100644 --- a/lib/puppet/feature/zabbixapi.rb +++ b/lib/puppet/feature/zabbixapi.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puppet/util/feature' Puppet.features.add(:zabbixapi, libs: 'zabbixapi') diff --git a/lib/puppet/provider/zabbix.rb b/lib/puppet/provider/zabbix.rb index d838f5d00..cd6eb00e9 100644 --- a/lib/puppet/provider/zabbix.rb +++ b/lib/puppet/provider/zabbix.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # zabbix provider type for puppet class Puppet::Provider::Zabbix < Puppet::Provider # This method is vendored from the AWS SDK, rather than including an @@ -46,7 +48,7 @@ def zbx # Create the api connection def self.create_connection protocol = api_config['default']['apache_use_ssl'] == 'true' ? 'https' : 'http' - zbx = ZabbixApi.connect( + ZabbixApi.connect( url: "#{protocol}://#{api_config['default']['zabbix_url']}/api_jsonrpc.php", user: api_config['default']['zabbix_user'], password: api_config['default']['zabbix_pass'], @@ -54,7 +56,6 @@ def self.create_connection http_password: api_config['default']['zabbix_pass'], ignore_version: true ) - zbx end def create_connection @@ -80,6 +81,7 @@ def check_host(host) # Get the template id from the name. def get_template_id(zbx, template) return template if a_number?(template) + zbx.templates.get_id(host: template) end @@ -95,7 +97,7 @@ def transform_to_array_hash(key, value_array) end # Is it a number? - def a_number?(s) - s.to_s.match(%r{\A[+-]?\d+?(\.\d+)?\Z}).nil? ? false : true + def a_number?(value) + !value.to_s.match(%r{\A[+-]?\d+?(\.\d+)?\Z}).nil? end end diff --git a/lib/puppet/provider/zabbix_application/ruby.rb b/lib/puppet/provider/zabbix_application/ruby.rb index 4b777764b..1c8dd8d8c 100644 --- a/lib/puppet/provider/zabbix_application/ruby.rb +++ b/lib/puppet/provider/zabbix_application/ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative '../zabbix' Puppet::Type.type(:zabbix_application).provide(:ruby, parent: Puppet::Provider::Zabbix) do confine feature: :zabbixapi @@ -23,7 +25,7 @@ def exists? def destroy zbx.applications.delete(application_id) - rescue => error - raise(Puppet::Error, "Zabbix Application Delete Failed\n#{error.message}") + rescue StandardError => e + raise(Puppet::Error, "Zabbix Application Delete Failed\n#{e.message}") end end diff --git a/lib/puppet/provider/zabbix_host/ruby.rb b/lib/puppet/provider/zabbix_host/ruby.rb index cf34110d3..ec32431a1 100644 --- a/lib/puppet/provider/zabbix_host/ruby.rb +++ b/lib/puppet/provider/zabbix_host/ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative '../zabbix' Puppet::Type.type(:zabbix_host).provide(:ruby, parent: Puppet::Provider::Zabbix) do confine feature: :zabbixapi @@ -94,7 +96,8 @@ def get_groupids(group_array, create) group_array.each do |g| id = zbx.hostgroups.get_id(name: g) if id.nil? - raise Puppet::Error, 'The hostgroup (' + g + ') does not exist in zabbix. Please use the correct one or set group_create => true.' unless create + raise Puppet::Error, "The hostgroup (#{g}) does not exist in zabbix. Please use the correct one or set group_create => true." unless create + groupids << zbx.hostgroups.create(name: g) else groupids << id @@ -108,6 +111,7 @@ def get_templateids(template_array) template_array.each do |t| template_id = zbx.templates.get_id(host: t) raise Puppet::Error, "The template #{t} does not exist in Zabbix. Please use a correct one." if template_id.nil? + templateids << template_id end templateids diff --git a/lib/puppet/provider/zabbix_hostgroup/ruby.rb b/lib/puppet/provider/zabbix_hostgroup/ruby.rb index fa245f2fa..3e3d9924f 100644 --- a/lib/puppet/provider/zabbix_hostgroup/ruby.rb +++ b/lib/puppet/provider/zabbix_hostgroup/ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative '../zabbix' Puppet::Type.type(:zabbix_hostgroup).provide(:ruby, parent: Puppet::Provider::Zabbix) do confine feature: :zabbixapi @@ -22,8 +24,7 @@ def self.prefetch(resources) def create # Connect to zabbix api - hgid = zbx.hostgroups.create(name: @resource[:name]) - hgid + zbx.hostgroups.create(name: @resource[:name]) end def exists? diff --git a/lib/puppet/provider/zabbix_proxy/ruby.rb b/lib/puppet/provider/zabbix_proxy/ruby.rb index 12acb5df1..9df114a93 100644 --- a/lib/puppet/provider/zabbix_proxy/ruby.rb +++ b/lib/puppet/provider/zabbix_proxy/ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative '../zabbix' Puppet::Type.type(:zabbix_proxy).provide(:ruby, parent: Puppet::Provider::Zabbix) do confine feature: :zabbixapi @@ -40,13 +42,13 @@ def self.proxy_properties_hash(p) ensure: :present, # Proxy object properties proxyid: p['proxyid'].to_i, - name: p['host'], - mode: status_to_mode(p['status']), + name: p['host'], + mode: status_to_mode(p['status']), # Proxy Interface object properties interfaceid: p['interface'].is_a?(Hash) ? p['interface']['interfaceid'] : nil, - ipaddress: p['interface'].is_a?(Hash) ? p['interface']['ip'] : nil, - use_ip: if p['interface'].is_a?(Hash) then p['interface']['useip'] == '1' ? :true : :false end, - port: p['interface'].is_a?(Hash) ? p['interface']['port'].to_i : nil + ipaddress: p['interface'].is_a?(Hash) ? p['interface']['ip'] : nil, + use_ip: if p['interface'].is_a?(Hash) then p['interface']['useip'] == '1' ? :true : :false end, + port: p['interface'].is_a?(Hash) ? p['interface']['port'].to_i : nil } end @@ -179,6 +181,7 @@ def update # At present, the only properties other than mode that can be updated are the interface properties applicable to passive proxies only. raise Puppet::Error, "Can't update proxy interface properties for an active proxy" unless @property_hash[:mode] == :passive + update_interface_properties end @@ -195,6 +198,7 @@ def self.status_to_mode(status) def self.mode_to_status(mode) return 5 if mode == :active + 6 end end diff --git a/lib/puppet/provider/zabbix_template/ruby.rb b/lib/puppet/provider/zabbix_template/ruby.rb index 8e214a4fd..c38a5a091 100644 --- a/lib/puppet/provider/zabbix_template/ruby.rb +++ b/lib/puppet/provider/zabbix_template/ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative '../zabbix' Puppet::Type.type(:zabbix_template).provide(:ruby, parent: Puppet::Provider::Zabbix) do confine feature: :zabbixapi diff --git a/lib/puppet/provider/zabbix_template_host/ruby.rb b/lib/puppet/provider/zabbix_template_host/ruby.rb index 423894b30..32a32d4b8 100644 --- a/lib/puppet/provider/zabbix_template_host/ruby.rb +++ b/lib/puppet/provider/zabbix_template_host/ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative '../zabbix' Puppet::Type.type(:zabbix_template_host).provide(:ruby, parent: Puppet::Provider::Zabbix) do confine feature: :zabbixapi diff --git a/lib/puppet/provider/zabbix_userparameters/ruby.rb b/lib/puppet/provider/zabbix_userparameters/ruby.rb index 38dfb060f..b293678ad 100644 --- a/lib/puppet/provider/zabbix_userparameters/ruby.rb +++ b/lib/puppet/provider/zabbix_userparameters/ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative '../zabbix' Puppet::Type.type(:zabbix_userparameters).provide(:ruby, parent: Puppet::Provider::Zabbix) do confine feature: :zabbixapi diff --git a/lib/puppet/type/zabbix_application.rb b/lib/puppet/type/zabbix_application.rb index e760a4464..065ef84ac 100644 --- a/lib/puppet/type/zabbix_application.rb +++ b/lib/puppet/type/zabbix_application.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Puppet::Type.newtype(:zabbix_application) do @doc = %q(Manage zabbix applications diff --git a/lib/puppet/type/zabbix_host.rb b/lib/puppet/type/zabbix_host.rb index c0f7584bd..1e900df84 100644 --- a/lib/puppet/type/zabbix_host.rb +++ b/lib/puppet/type/zabbix_host.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Puppet::Type.newtype(:zabbix_host) do ensurable do defaultvalues @@ -9,6 +11,7 @@ def initialize(*args) # Migrate group to groups return if self[:group].nil? + self[:groups] = self[:group] delete(:group) end diff --git a/lib/puppet/type/zabbix_hostgroup.rb b/lib/puppet/type/zabbix_hostgroup.rb index 535527c00..29c141ee2 100644 --- a/lib/puppet/type/zabbix_hostgroup.rb +++ b/lib/puppet/type/zabbix_hostgroup.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Puppet::Type.newtype(:zabbix_hostgroup) do @doc = 'Manage zabbix hostgroups' diff --git a/lib/puppet/type/zabbix_proxy.rb b/lib/puppet/type/zabbix_proxy.rb index 24c395fcd..bad3b7b1b 100644 --- a/lib/puppet/type/zabbix_proxy.rb +++ b/lib/puppet/type/zabbix_proxy.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Puppet::Type.newtype(:zabbix_proxy) do ensurable do defaultvalues @@ -55,7 +57,7 @@ def munge_boolean_to_symbol(value) begin IPAddr.new(value) - rescue => e + rescue StandardError => e raise Puppet::Error, e.to_s end end @@ -74,10 +76,12 @@ def munge_boolean_to_symbol(value) if value.is_a?(String) raise Puppet::Error, 'invalid port' unless value =~ %r{^\d+$} raise Puppet::Error, 'invalid port' unless value.to_i.between?(1, 65_535) + return end if value.is_a?(Integer) raise Puppet::Error, 'invalid port' unless value.between?(1, 65_535) + return end raise Puppet::Error, 'invalid port' diff --git a/lib/puppet/type/zabbix_template.rb b/lib/puppet/type/zabbix_template.rb index 791bbfd0a..00e552a18 100644 --- a/lib/puppet/type/zabbix_template.rb +++ b/lib/puppet/type/zabbix_template.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Puppet::Type.newtype(:zabbix_template) do ensurable do defaultvalues @@ -5,6 +7,7 @@ def insync?(is) return false if is == :present && !template_xmls_match? + super end @@ -18,11 +21,12 @@ def source_xml end def clean_xml(dirty) - dirty.gsub(%r{>\s*}, '>').gsub(%r{\s*<}, '<').gsub(%r{.*<\/date>}, 'DATEWASHERE') + dirty.gsub(%r{>\s*}, '>').gsub(%r{\s*<}, '<').gsub(%r{.*}, 'DATEWASHERE') end def change_to_s(currentvalue, newvalue) return 'Template updated' if currentvalue == :present && newvalue == :present + super end end diff --git a/lib/puppet/type/zabbix_template_host.rb b/lib/puppet/type/zabbix_template_host.rb index 9d1f42eda..3fc61d63c 100644 --- a/lib/puppet/type/zabbix_template_host.rb +++ b/lib/puppet/type/zabbix_template_host.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Puppet::Type.newtype(:zabbix_template_host) do @doc = <<-DOC Link or Unlink template to host. @@ -15,7 +17,7 @@ end newparam(:name, namevar: true) do - newvalues(%r{.+\@.+}) + newvalues(%r{.+@.+}) desc 'template_name@host_name' end diff --git a/lib/puppet/type/zabbix_userparameters.rb b/lib/puppet/type/zabbix_userparameters.rb index 46ddb7e24..13d81b489 100644 --- a/lib/puppet/type/zabbix_userparameters.rb +++ b/lib/puppet/type/zabbix_userparameters.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..')) Puppet::Type.newtype(:zabbix_userparameters) do ensurable do diff --git a/spec/acceptance/agent_spec.rb b/spec/acceptance/agent_spec.rb index 572f7417e..7035a39b2 100644 --- a/spec/acceptance/agent_spec.rb +++ b/spec/acceptance/agent_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' supported_versions.each do |version| @@ -44,14 +46,16 @@ class { 'zabbix::agent': listenip => '127.0.0.1', zabbix_version => '#{version}', } - EOS + EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end + describe file('/etc/zabbix/zabbix_agentd.conf') do its(:content) { is_expected.to match %r{ListenIP=127.0.0.1} } end end + context 'With ListenIP set to lo' do it 'works idempotently with no errors' do pp = <<-EOS @@ -61,10 +65,11 @@ class { 'zabbix::agent': listenip => 'lo', zabbix_version => '#{version}', } - EOS + EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end + context 'With ListenIP set to an IP-Address' do it 'works idempotently with no errors' do pp = <<-EOS @@ -74,10 +79,11 @@ class { 'zabbix::agent': listenip => '127.0.0.1', zabbix_version => '#{version}', } - EOS + EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end + describe file('/etc/zabbix/zabbix_agentd.conf') do its(:content) { is_expected.to match %r{ListenIP=127.0.0.1} } end diff --git a/spec/acceptance/server_spec.rb b/spec/acceptance/server_spec.rb index ddf610071..868c88b25 100644 --- a/spec/acceptance/server_spec.rb +++ b/spec/acceptance/server_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' describe 'zabbix::server class' do context 'default parameters' do diff --git a/spec/acceptance/zabbix_application_spec.rb b/spec/acceptance/zabbix_application_spec.rb index e5eaf46a0..b516b4ab3 100644 --- a/spec/acceptance/zabbix_application_spec.rb +++ b/spec/acceptance/zabbix_application_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' require 'serverspec_type_zabbixapi' @@ -44,13 +46,13 @@ class { 'zabbix': require => [ Class['postgresql::server'], Class['apache'], ], } - EOS + EOS pp2 = <<-EOS zabbix_application { 'TestApplication1': template => "#{template}", } - EOS + EOS # setup zabbix. Apache module isn't idempotent and requires a second run it 'works with no error on the first apply' do # Cleanup old database @@ -58,6 +60,7 @@ class { 'zabbix': apply_manifest(pp1, catch_failures: true) end + it 'works with no error on the second apply' do apply_manifest(pp1, catch_failures: true) end @@ -66,6 +69,7 @@ class { 'zabbix': it 'works with no error on the third apply' do apply_manifest(pp2, catch_failures: true) end + it 'works without changes on the fourth apply' do apply_manifest(pp2, catch_changes: true) end diff --git a/spec/acceptance/zabbix_host_spec.rb b/spec/acceptance/zabbix_host_spec.rb index adb721b9c..304dc1e28 100644 --- a/spec/acceptance/zabbix_host_spec.rb +++ b/spec/acceptance/zabbix_host_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' require 'serverspec_type_zabbixapi' @@ -9,6 +11,7 @@ next if zabbix_version == '5.4' && default[:platform] == 'el-7-x86_64' # No Zabbix 5.2 packages on Debian 11 next if zabbix_version == '5.2' && default[:platform] == 'debian-11-amd64' + context "create zabbix_host resources with zabbix version #{zabbix_version}" do # This will deploy a running Zabbix setup (server, web, db) which we can # use for custom type tests @@ -52,7 +55,7 @@ class { 'zabbix': manage_resources => true, require => [ Class['postgresql::server'], Class['apache'], ], } - EOS + EOS # setup zabbix. Apache module isn't idempotent and requires a second run it 'works with no error on the first apply' do @@ -61,6 +64,7 @@ class { 'zabbix': apply_manifest(pp1, catch_failures: true) end + it 'works with no error on the second apply' do apply_manifest(pp1, catch_failures: true) end @@ -85,11 +89,12 @@ class { 'zabbix': templates => #{template}, macros => [], } - EOS + EOS it 'works with no error on the third apply' do apply_manifest(pp2, catch_failures: true) end + it 'works without changes on the fourth apply' do apply_manifest(pp2, catch_changes: true) end @@ -115,11 +120,12 @@ class { 'zabbix': templates => #{template}, macros => [], } - EOS + EOS it 'creates hosts with SNMP interface and details without errors' do apply_manifest(pp3, catch_failures: true) end + it 'creates hosts with SNMP interface and details without changes' do apply_manifest(pp3, catch_changes: true) end @@ -135,27 +141,35 @@ class { 'zabbix': it 'is created' do expect(test1['host']).to eq('test1.example.com') end + it 'is in group TestgroupOne' do expect(test1['groups'].map { |g| g['name'] }).to eq(['TestgroupOne']) end + it 'has a correct interface dns configured' do expect(test1['interfaces'][0]['dns']).to eq('test1.example.com') end + it 'has a correct interface ip configured' do expect(test1['interfaces'][0]['ip']).to eq('127.0.0.1') end + it 'has a correct interface main configured' do expect(test1['interfaces'][0]['main']).to eq('1') end + it 'has a correct interface port configured' do expect(test1['interfaces'][0]['port']).to eq('10050') end + it 'has a correct interface type configured' do expect(test1['interfaces'][0]['type']).to eq('1') end + it 'has a correct interface useip configured' do expect(test1['interfaces'][0]['useip']).to eq('1') end + it 'has templates attached' do expect(test1['parentTemplates'].map { |t| t['host'] }.sort).to eq(template.sort) end @@ -167,27 +181,35 @@ class { 'zabbix': it 'is created' do expect(test2['host']).to eq('test2.example.com') end + it 'is in group Virtual machines' do expect(test2['groups'].map { |g| g['name'] }).to eq(['Virtual machines']) end + it 'has a correct interface dns configured' do expect(test2['interfaces'][0]['dns']).to eq('test2.example.com') end + it 'has a correct interface ip configured' do expect(test2['interfaces'][0]['ip']).to eq('127.0.0.2') end + it 'has a correct interface main configured' do expect(test2['interfaces'][0]['main']).to eq('1') end + it 'has a correct interface port configured' do expect(test2['interfaces'][0]['port']).to eq('1050') end + it 'has a correct interface type configured' do expect(test2['interfaces'][0]['type']).to eq('1') end + it 'has a correct interface useip configured' do expect(test2['interfaces'][0]['useip']).to eq('0') end + it 'has templates attached' do expect(test2['parentTemplates'].map { |t| t['host'] }.sort).to eq(template.sort) end @@ -202,30 +224,39 @@ class { 'zabbix': it 'is created' do expect(test3['host']).to eq('test3.example.com') end + it 'is in group Virtual machines' do expect(test3['groups'].map { |g| g['name'] }).to eq(['Virtual machines']) end + it 'has a correct interface dns configured' do expect(test3['interfaces'][0]['dns']).to eq('test3.example.com') end + it 'has a correct interface ip configured' do expect(test3['interfaces'][0]['ip']).to eq('127.0.0.3') end + it 'has a correct interface main configured' do expect(test3['interfaces'][0]['main']).to eq('1') end + it 'has a correct interface port configured' do expect(test3['interfaces'][0]['port']).to eq('161') end + it 'has a correct interface type configured' do expect(test3['interfaces'][0]['type']).to eq('2') end + it 'has a correct interface details configured' do expect(test3['interfaces'][0]['details']).to eq('version' => '2', 'bulk' => '0', 'community' => 'public') end + it 'has a correct interface useip configured' do expect(test3['interfaces'][0]['useip']).to eq('0') end + it 'has templates attached' do expect(test3['parentTemplates'].map { |t| t['host'] }.sort).to eq(template_snmp.sort) end @@ -237,30 +268,39 @@ class { 'zabbix': it 'is created' do expect(test4['host']).to eq('test4.example.com') end + it 'is in group Virtual machines' do expect(test4['groups'].map { |g| g['name'] }).to eq(['Virtual machines']) end + it 'has a correct interface dns configured' do expect(test4['interfaces'][0]['dns']).to eq('test4.example.com') end + it 'has a correct interface ip configured' do expect(test4['interfaces'][0]['ip']).to eq('127.0.0.4') end + it 'has a correct interface main configured' do expect(test4['interfaces'][0]['main']).to eq('1') end + it 'has a correct interface port configured' do expect(test4['interfaces'][0]['port']).to eq('161') end + it 'has a correct interface type configured' do expect(test4['interfaces'][0]['type']).to eq('1') end + it 'has a correct interface details configured' do expect(test4['interfaces'][0]['details']).to eq([]) end + it 'has a correct interface useip configured' do expect(test4['interfaces'][0]['useip']).to eq('0') end + it 'has templates attached' do expect(test4['parentTemplates'].map { |t| t['host'] }.sort).to eq(template.sort) end diff --git a/spec/acceptance/zabbix_hostgroup_spec.rb b/spec/acceptance/zabbix_hostgroup_spec.rb index c67463219..ad7e872ce 100644 --- a/spec/acceptance/zabbix_hostgroup_spec.rb +++ b/spec/acceptance/zabbix_hostgroup_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' require 'serverspec_type_zabbixapi' @@ -8,6 +10,7 @@ next if zabbix_version == '5.4' && default[:platform] == 'el-7-x86_64' # No Zabbix 5.2 packages on Debian 11 next if zabbix_version == '5.2' && default[:platform] == 'debian-11-amd64' + context "create zabbix_hostgroup resources with zabbix version #{zabbix_version}" do # This will deploy a running Zabbix setup (server, web, db) which we can # use for custom type tests diff --git a/spec/acceptance/zabbix_proxy_spec.rb b/spec/acceptance/zabbix_proxy_spec.rb index 6334d423c..8859a101a 100644 --- a/spec/acceptance/zabbix_proxy_spec.rb +++ b/spec/acceptance/zabbix_proxy_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' require 'serverspec_type_zabbixapi' @@ -9,6 +11,7 @@ next if zabbix_version == '5.4' && default[:platform] == 'el-7-x86_64' # No Zabbix 5.2 packages on Debian 11 next if zabbix_version == '5.2' && default[:platform] == 'debian-11-amd64' + context "create zabbix_proxy resources with zabbix version #{zabbix_version}" do # This will deploy a running Zabbix setup (server, web, db) which we can # use for custom type tests @@ -42,6 +45,7 @@ class { 'zabbix': apply_manifest(pp1, catch_failures: true) end + it 'works with no error on second apply' do apply_manifest(pp1, catch_failures: true) end @@ -92,12 +96,15 @@ class { 'zabbix': it 'has a interfaces dns configured' do expect(proxy2['interface']['dns']).to eq('ZabbixProxy2') end + it 'has a interfaces ip configured' do expect(proxy2['interface']['ip']).to eq('127.0.0.3') end + it 'has a interfaces port configured' do expect(proxy2['interface']['port']).to eq('10055') end + it 'has a interfaces useip configured' do expect(proxy2['interface']['useip']).to eq('0') end @@ -138,12 +145,15 @@ class { 'zabbix': it 'has a interfaces dns configured' do expect(proxy1['interface']['dns']).to eq('ZabbixProxy1') end + it 'has a interfaces ip configured' do expect(proxy1['interface']['ip']).to eq('127.0.0.3') end + it 'has a interfaces port configured' do expect(proxy1['interface']['port']).to eq('10055') end + it 'has a interfaces useip configured' do expect(proxy1['interface']['useip']).to eq('0') end diff --git a/spec/acceptance/zabbix_template_host_spec.rb b/spec/acceptance/zabbix_template_host_spec.rb index cd6b0e3b3..1701240e9 100644 --- a/spec/acceptance/zabbix_template_host_spec.rb +++ b/spec/acceptance/zabbix_template_host_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' require 'serverspec_type_zabbixapi' @@ -8,6 +10,7 @@ next if zabbix_version == '5.4' && default[:platform] == 'el-7-x86_64' # No Zabbix 5.2 packages on Debian 11 next if zabbix_version == '5.2' && default[:platform] == 'debian-11-amd64' + context "create zabbix_template_host resources with zabbix version #{zabbix_version}" do template = case zabbix_version when '4.0' diff --git a/spec/acceptance/zabbix_template_spec.rb b/spec/acceptance/zabbix_template_spec.rb index 9d00fe92f..76c22695f 100644 --- a/spec/acceptance/zabbix_template_spec.rb +++ b/spec/acceptance/zabbix_template_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' require 'serverspec_type_zabbixapi' @@ -8,6 +10,7 @@ next if zabbix_version == '5.4' && default[:platform] == 'el-7-x86_64' # No Zabbix 5.2 packages on Debian 11 next if zabbix_version == '5.2' && default[:platform] == 'debian-11-amd64' + context "create zabbix_template resources with zabbix version #{zabbix_version}" do # This will deploy a running Zabbix setup (server, web, db) which we can # use for custom type tests diff --git a/spec/classes/agent_spec.rb b/spec/classes/agent_spec.rb index 65f781fa3..e896d2c96 100644 --- a/spec/classes/agent_spec.rb +++ b/spec/classes/agent_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix::agent' do @@ -13,7 +15,7 @@ end on_supported_os(baseline_os_hash).each do |os, facts| - context "on #{os} " do + context "on #{os}" do systemd_fact = case facts[:osfamily] when 'Archlinux', 'Fedora', 'Gentoo' { systemd: true } @@ -47,10 +49,11 @@ facts.merge(systemd_fact) end - if facts[:osfamily] == 'Gentoo' + case facts[:osfamily] + when 'Gentoo' package_name = 'zabbix' service_name = 'zabbix-agentd' - elsif facts[:osfamily] == 'windows' + when 'windows' package_name = 'zabbix-agent' service_name = 'Zabbix Agent' else @@ -65,9 +68,9 @@ if facts[:kernel] == 'windows' it do is_expected.to contain_package(package_name).with( - ensure: '4.4.5', + ensure: '4.4.5', provider: 'chocolatey', - tag: 'zabbix' + tag: 'zabbix' ) end else @@ -77,6 +80,7 @@ with_tag('zabbix'). that_requires('Class[zabbix::repo]') end + it do is_expected.to contain_service(service_name). with_ensure('running'). diff --git a/spec/classes/database_mysql_spec.rb b/spec/classes/database_mysql_spec.rb index b3cbcc700..29c4c4b81 100644 --- a/spec/classes/database_mysql_spec.rb +++ b/spec/classes/database_mysql_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix::database::mysql' do @@ -11,7 +13,8 @@ on_supported_os(baseline_os_hash).each do |os, facts| next if facts[:os]['name'] == 'windows' - context "on #{os} " do + + context "on #{os}" do let :facts do facts end diff --git a/spec/classes/database_postgresql_spec.rb b/spec/classes/database_postgresql_spec.rb index 54c961924..0eb26fe55 100644 --- a/spec/classes/database_postgresql_spec.rb +++ b/spec/classes/database_postgresql_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix::database::postgresql' do @@ -11,28 +13,29 @@ on_supported_os(baseline_os_hash).each do |os, facts| next if facts[:os]['name'] == 'windows' - context "on #{os} " do + + context "on #{os}" do let :facts do facts end supported_versions.each do |zabbix_version| - case facts[:os]['name'] - when 'CentOS', 'RedHat', 'OracleLinux', 'VirtuozzoLinux' - # Path on RedHat - path = if zabbix_version == '5.4' + path = case facts[:os]['name'] + when 'CentOS', 'RedHat', 'OracleLinux', 'VirtuozzoLinux' + # Path on RedHat + if zabbix_version == '5.4' '/usr/share/doc/zabbix-sql-scripts/postgresql/' else "/usr/share/doc/zabbix-*-pgsql-#{zabbix_version}*/" end - else - # Path on Debian - path = if zabbix_version == '5.4' + else + # Path on Debian + if zabbix_version == '5.4' '/usr/share/doc/zabbix-sql-scripts/postgresql/' else '/usr/share/doc/zabbix-*-pgsql' end - end + end describe "when zabbix_type is server and version is #{zabbix_version}" do let :params do diff --git a/spec/classes/database_spec.rb b/spec/classes/database_spec.rb index ed5ee2b4a..25349fba7 100644 --- a/spec/classes/database_spec.rb +++ b/spec/classes/database_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix::database' do @@ -7,7 +9,8 @@ on_supported_os(baseline_os_hash).each do |os, facts| next if facts[:os]['name'] == 'windows' - context "on #{os} " do + + context "on #{os}" do let :facts do facts end diff --git a/spec/classes/javagateway_spec.rb b/spec/classes/javagateway_spec.rb index a38d38b86..64c975980 100644 --- a/spec/classes/javagateway_spec.rb +++ b/spec/classes/javagateway_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix::javagateway' do @@ -7,7 +9,8 @@ on_supported_os(baseline_os_hash).each do |os, facts| next if facts[:os]['name'] == 'windows' - context "on #{os} " do + + context "on #{os}" do let :facts do facts end diff --git a/spec/classes/proxy_spec.rb b/spec/classes/proxy_spec.rb index b42e45c77..b02b2e1bd 100644 --- a/spec/classes/proxy_spec.rb +++ b/spec/classes/proxy_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix::proxy' do @@ -7,7 +9,8 @@ on_supported_os(baseline_os_hash).each do |os, facts| next if facts[:os]['name'] == 'windows' - context "on #{os} " do + + context "on #{os}" do # rubocop:disable RSpec/EmptyExampleGroup let :facts do facts end diff --git a/spec/classes/repo_spec.rb b/spec/classes/repo_spec.rb index 502217e4b..4aa48abed 100644 --- a/spec/classes/repo_spec.rb +++ b/spec/classes/repo_spec.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix::repo' do on_supported_os(baseline_os_hash).each do |os, facts| - context "on #{os} " do + context "on #{os}" do # rubocop:disable RSpec/EmptyExampleGroup let :facts do facts end @@ -39,6 +41,7 @@ it { is_expected.to contain_apt__source('zabbix').with_location('http://repo.zabbix.com/zabbix/4.0/debian/') } end + context 'on Debian 10 and Zabbix 5.0' do let :params do { diff --git a/spec/classes/sender_spec.rb b/spec/classes/sender_spec.rb index 8890df667..e7ac5493b 100644 --- a/spec/classes/sender_spec.rb +++ b/spec/classes/sender_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix::sender' do @@ -7,7 +9,8 @@ on_supported_os(baseline_os_hash).each do |os, facts| next if facts[:os]['name'] == 'windows' - context "on #{os} " do + + context "on #{os}" do let :facts do facts end @@ -20,6 +23,7 @@ it { is_expected.to contain_package('zabbix-sender').with_ensure('present') } it { is_expected.to contain_package('zabbix-sender').with_name('zabbix-sender') } end + context 'when declaring manage_repo is true' do let :params do { diff --git a/spec/classes/server_spec.rb b/spec/classes/server_spec.rb index 58cb8bb6c..894c5a63a 100644 --- a/spec/classes/server_spec.rb +++ b/spec/classes/server_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'deep_merge' @@ -9,7 +11,8 @@ on_supported_os(baseline_os_hash).each do |os, facts| next if facts[:osfamily] == 'Archlinux' # zabbix server is currently not supported on archlinux next if facts[:os]['name'] == 'windows' - context "on #{os} " do + + context "on #{os}" do systemd_fact = case facts[:osfamily] when 'Archlinux', 'Fedora', 'Gentoo' { systemd: true } diff --git a/spec/classes/userparameter_spec.rb b/spec/classes/userparameter_spec.rb index 3dde03a71..5d2ddb78e 100644 --- a/spec/classes/userparameter_spec.rb +++ b/spec/classes/userparameter_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix::userparameter' do @@ -6,7 +8,7 @@ end on_supported_os(baseline_os_hash).each do |os, facts| - context "on #{os} " do + context "on #{os}" do let :facts do facts end diff --git a/spec/classes/web_spec.rb b/spec/classes/web_spec.rb index 343b5d597..d416ad6ed 100644 --- a/spec/classes/web_spec.rb +++ b/spec/classes/web_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'deep_merge' @@ -22,7 +24,7 @@ # There are no zabbix 5.2 packages for Debian 11 next if facts[:os]['name'] == 'Debian' && facts[:os]['release']['major'] == '11' && zabbix_version == '5.2' - context "on #{os} " do + context "on #{os}" do let :facts do facts end @@ -83,9 +85,9 @@ if facts[:operatingsystemmajrelease].to_i == 7 && !%w[VirtuozzoLinux OracleLinux Scientific].include?(facts[:os]['name']) && zabbix_version =~ %r{5\.[024]} - ['zabbix-web-pgsql-scl', 'zabbix-web'] + %w[zabbix-web-pgsql-scl zabbix-web] else - ['zabbix-web-pgsql', 'zabbix-web'] + %w[zabbix-web-pgsql zabbix-web] end else ['zabbix-frontend-php', pgsqlpackage] @@ -119,7 +121,7 @@ 'php5-mysql' end - packages = facts[:osfamily] == 'RedHat' ? ['zabbix-web-mysql', 'zabbix-web'] : ['zabbix-frontend-php', mysqlpackage] + packages = facts[:osfamily] == 'RedHat' ? %w[zabbix-web-mysql zabbix-web] : ['zabbix-frontend-php', mysqlpackage] packages.each do |package| it { is_expected.to contain_package(package) } end @@ -158,6 +160,7 @@ with_zabbix_pass('zabbix'). with_apache_use_ssl(false) end + it do is_expected.to contain_file('/etc/zabbix/api.conf'). with_ensure('file'). @@ -169,6 +172,7 @@ with_content(%r{zabbix_pass = zabbix}). with_content(%r{apache_use_ssl = false}) end + it { is_expected.to contain_package('zabbixapi').with_provider('puppet_gem') } it { is_expected.to contain_file('/etc/zabbix/imported_templates').with_ensure('directory') } end diff --git a/spec/defines/startup_spec.rb b/spec/defines/startup_spec.rb index ec91c25ad..11e3d390f 100644 --- a/spec/defines/startup_spec.rb +++ b/spec/defines/startup_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix::startup', type: :define do # rubocop:disable RSpec/MultipleDescribes @@ -41,6 +43,7 @@ it { is_expected.to contain_exec('install_agent_zabbix-agent') } else it { is_expected.not_to compile.with_all_deps } + next end it { is_expected.not_to contain_class('systemd') } @@ -48,7 +51,9 @@ end end end + next if os_facts[:os]['family'] == 'windows' + context "#{os} on systemd" do ['true', true].each do |systemd_fact_state| let :facts do @@ -67,12 +72,14 @@ it { is_expected.to contain_class('systemd') } it { is_expected.to contain_systemd__unit_file('zabbix-agent.service') } it { is_expected.to contain_file('/etc/init.d/zabbix-agent').with_ensure('absent') } + it do is_expected.to contain_file('/etc/systemd/system/zabbix-agent.service').with( ensure: 'file', - mode: '0444' + mode: '0444' ) end + it { is_expected.to contain_file('/etc/systemd/system/zabbix-agent.service').with_content(%r{ExecStart=/usr/sbin/zabbix_agentd --foreground -c /something}) } it { is_expected.to contain_file('/etc/systemd/system/zabbix-agent.service').with_content(%r{PIDFile=/somethingelse}) } end @@ -81,6 +88,7 @@ end end end + describe 'zabbix::startup', type: :define do let(:title) { 'zabbix-server' } @@ -118,6 +126,7 @@ end else it { is_expected.not_to compile.with_all_deps } + next end it { is_expected.not_to contain_class('systemd') } @@ -126,7 +135,9 @@ end end end + next if os_facts[:os]['family'] == 'windows' + context 'on systemd' do ['true', true].each do |systemd_fact_state| let :facts do @@ -147,12 +158,14 @@ it { is_expected.to contain_class('systemd') } it { is_expected.to contain_systemd__unit_file('zabbix-server.service') } it { is_expected.to contain_file('/etc/init.d/zabbix-server').with_ensure('absent') } + it do is_expected.to contain_file('/etc/systemd/system/zabbix-server.service').with( ensure: 'file', - mode: '0444' + mode: '0444' ) end + it { is_expected.to contain_file('/etc/systemd/system/zabbix-server.service').with_content(%r{ExecStart=/usr/sbin/zabbix_server --foreground -c /something}) } it { is_expected.to contain_file('/etc/systemd/system/zabbix-server.service').with_content(%r{PIDFile=/somethingelse}) } it { is_expected.to contain_file('/etc/systemd/system/zabbix-server.service').with_content(%r{After=mysqld.service}) } diff --git a/spec/defines/userparameters_spec.rb b/spec/defines/userparameters_spec.rb index 5c1a52931..9e6f54a95 100644 --- a/spec/defines/userparameters_spec.rb +++ b/spec/defines/userparameters_spec.rb @@ -1,9 +1,12 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix::userparameters', type: :define do on_supported_os(baseline_os_hash).each do |os, facts| next if facts[:os]['name'] == 'windows' - context "on #{os} " do + + context "on #{os}" do let :facts do systemd_fact = case facts[:os]['family'] when 'Archlinux', 'Fedora', 'Gentoo' diff --git a/spec/serverspec_type_zabbixapi.rb b/spec/serverspec_type_zabbixapi.rb index 1f4365d02..3e47d1362 100644 --- a/spec/serverspec_type_zabbixapi.rb +++ b/spec/serverspec_type_zabbixapi.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'json' module Serverspec::Type @@ -56,6 +58,7 @@ def do_request(data) JSON.parse(result.stdout.chomp) end end + def zabbixapi(host, user, pass, method, params) Zabbixapi.new(host, user, pass, method, params) end diff --git a/spec/spec_helper_methods.rb b/spec/spec_helper_methods.rb index f18506c3b..0aeb70d43 100644 --- a/spec/spec_helper_methods.rb +++ b/spec/spec_helper_methods.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + def baseline_os_hash { supported_os: [ diff --git a/spec/support/acceptance/prepare_host.rb b/spec/support/acceptance/prepare_host.rb index 90991aff8..82590faed 100644 --- a/spec/support/acceptance/prepare_host.rb +++ b/spec/support/acceptance/prepare_host.rb @@ -1,18 +1,16 @@ +# frozen_string_literal: true + def prepare_host if fact('os.family') == 'RedHat' shell('rm -rf /etc/yum.repos.d/Zabbix*.repo; rm -rf /var/cache/yum/x86_64/*/Zabbix*; yum clean all --verbose') # The CentOS docker image has a yum config that won't install docs, to keep used space low # zabbix packages their SQL file as doc, we need that for bootstrapping the database - if fact('os.release.major').to_i == 7 - shell('sed -i "/nodocs/d" /etc/yum.conf') - end + shell('sed -i "/nodocs/d" /etc/yum.conf') if fact('os.release.major').to_i == 7 end # The Ubuntu 18.04+ docker image has a dpkg config that won't install docs, to keep used space low # zabbix packages their SQL file as doc, we need that for bootstrapping the database - if fact('os.distro.id') == 'Ubuntu' - shell('rm -f /etc/dpkg/dpkg.cfg.d/excludes') - end + shell('rm -f /etc/dpkg/dpkg.cfg.d/excludes') if fact('os.distro.id') == 'Ubuntu' # On Debian it seems that make is searching for mkdir in /usr/bin/ but mkdir # does not exist. Symlink it from /bin/mkdir to make it work. diff --git a/spec/support/acceptance/supported_versions.rb b/spec/support/acceptance/supported_versions.rb index 055efc1bb..10980cd52 100644 --- a/spec/support/acceptance/supported_versions.rb +++ b/spec/support/acceptance/supported_versions.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + def supported_versions %w[4.0 5.0 5.2 5.4] end diff --git a/spec/types/zabbix_host_spec.rb b/spec/types/zabbix_host_spec.rb index f1fa4ca6b..00557ab27 100644 --- a/spec/types/zabbix_host_spec.rb +++ b/spec/types/zabbix_host_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'zabbix_host' do diff --git a/spec/unit/puppet/provider/zabbix_host/ruby.rb b/spec/unit/puppet/provider/zabbix_host/ruby.rb index be83ce30b..4a9829336 100644 --- a/spec/unit/puppet/provider/zabbix_host/ruby.rb +++ b/spec/unit/puppet/provider/zabbix_host/ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'fakefs/spec_helpers' @@ -13,14 +15,14 @@ expect(provider).to be_an_instance_of Puppet::Type::Zabbix_host::ProviderRuby end - [:instances, :prefetch].each do |method| - it "should respond to the class method #{method}" do + %i[instances prefetch].each do |method| + it "responds to the class method #{method}" do expect(described_class).to respond_to(method) end end - [:create, :exists?, :destroy, :get_groupids, :get_templateids, :ipaddress, :use_ip, :port, :groups, :templates, :macros, :proxy].each do |method| - it "should respond to the instance method #{method}" do + %i[create exists? destroy get_groupids get_templateids ipaddress use_ip port groups templates macros proxy].each do |method| + it "responds to the instance method #{method}" do expect(described_class.new).to respond_to(method) end end diff --git a/spec/unit/puppet/provider/zabbix_hostgroup/ruby.rb b/spec/unit/puppet/provider/zabbix_hostgroup/ruby.rb index ff6b7ebc3..fd455e8b4 100644 --- a/spec/unit/puppet/provider/zabbix_hostgroup/ruby.rb +++ b/spec/unit/puppet/provider/zabbix_hostgroup/ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Puppet::Type.type(:zabbix_hostgroup).provider(:ruby) do @@ -12,14 +14,14 @@ expect(provider).to be_an_instance_of Puppet::Type::Zabbix_hostgroup::ProviderRuby end - [:instances, :prefetch].each do |method| - it "should respond to the class method #{method}" do + %i[instances prefetch].each do |method| + it "responds to the class method #{method}" do expect(described_class).to respond_to(method) end end - [:create, :exists?, :destroy].each do |method| - it "should respond to the instance method #{method}" do + %i[create exists? destroy].each do |method| + it "responds to the instance method #{method}" do expect(described_class.new).to respond_to(method) end end diff --git a/spec/unit/puppet/provider/zabbix_proxy/ruby.rb b/spec/unit/puppet/provider/zabbix_proxy/ruby.rb index c5f4b69bc..92889fe25 100644 --- a/spec/unit/puppet/provider/zabbix_proxy/ruby.rb +++ b/spec/unit/puppet/provider/zabbix_proxy/ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Puppet::Type.type(:zabbix_proxy).provider(:ruby) do @@ -12,14 +14,14 @@ expect(provider).to be_an_instance_of Puppet::Type::Zabbix_proxy::ProviderRuby end - [:instances, :prefetch].each do |method| - it "should respond to the class method #{method}" do + %i[instances prefetch].each do |method| + it "responds to the class method #{method}" do expect(described_class).to respond_to(method) end end - [:create, :exists?, :destroy].each do |method| - it "should respond to the instance method #{method}" do + %i[create exists? destroy].each do |method| + it "responds to the instance method #{method}" do expect(described_class.new).to respond_to(method) end end diff --git a/spec/unit/puppet/type/zabbix_host_spec.rb b/spec/unit/puppet/type/zabbix_host_spec.rb index fe1ae4527..6d62352ff 100644 --- a/spec/unit/puppet/type/zabbix_host_spec.rb +++ b/spec/unit/puppet/type/zabbix_host_spec.rb @@ -1,33 +1,35 @@ +# frozen_string_literal: true + require 'spec_helper' require 'unit/puppet/x/spec_zabbix_types' # rubocop:disable RSpec/LetBeforeExamples describe Puppet::Type.type(:zabbix_host) do describe 'when validating params' do - [ - :group_create, - :hostname + %i[ + group_create + hostname ].each do |param| - it "should have a #{param} parameter" do + it "has a #{param} parameter" do expect(described_class.attrtype(param)).to eq(:param) end end end describe 'when validating properties' do - [ - :group, - :groups, - :id, - :interfaceid, - :ipaddress, - :port, - :proxy, - :templates, - :macros, - :use_ip + %i[ + group + groups + id + interfaceid + ipaddress + port + proxy + templates + macros + use_ip ].each do |param| - it "should have a #{param} property" do + it "has a #{param} property" do expect(described_class.attrtype(param)).to eq(:property) end end @@ -35,12 +37,12 @@ describe 'munge_boolean' do { - true => true, - false => false, - 'true' => true, + true => true, + false => false, + 'true' => true, 'false' => false, - :true => true, - :false => false + :true => true, + :false => false }.each do |key, value| it "munges #{key.inspect} to #{value}" do expect(described_class.new(name: 'nobody').munge_boolean(key)).to eq value diff --git a/spec/unit/puppet/type/zabbix_hostgroup_spec.rb b/spec/unit/puppet/type/zabbix_hostgroup_spec.rb index e90e54001..732544e39 100644 --- a/spec/unit/puppet/type/zabbix_hostgroup_spec.rb +++ b/spec/unit/puppet/type/zabbix_hostgroup_spec.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + require 'spec_helper' describe Puppet::Type.type(:zabbix_hostgroup) do describe 'when validating attributes' do - [:name, :provider].each do |param| - it "should have a #{param} parameter" do + %i[name provider].each do |param| + it "has a #{param} parameter" do expect(described_class.attrtype(param)).to eq(:param) end end diff --git a/spec/unit/puppet/type/zabbix_proxy_spec.rb b/spec/unit/puppet/type/zabbix_proxy_spec.rb index 9a82176d0..d281209ee 100644 --- a/spec/unit/puppet/type/zabbix_proxy_spec.rb +++ b/spec/unit/puppet/type/zabbix_proxy_spec.rb @@ -1,25 +1,27 @@ +# frozen_string_literal: true + require 'spec_helper' describe Puppet::Type.type(:zabbix_proxy) do describe 'when validating params' do - [ - :hostname, - :provider + %i[ + hostname + provider ].each do |param| - it "should have a #{param} parameter" do + it "has a #{param} parameter" do expect(described_class.attrtype(param)).to eq(:param) end end end describe 'when validating properties' do - [ - :ipaddress, - :use_ip, - :mode, - :port + %i[ + ipaddress + use_ip + mode + port ].each do |param| - it "should have a #{param} property" do + it "has a #{param} property" do expect(described_class.attrtype(param)).to eq(:property) end end @@ -96,9 +98,11 @@ it 'resource has one relationship' do expect(relationships.size).to eq(1) end + it 'relationship target is the zabbix_proxy resource' do expect(relationships[0].target).to eq(resource) end + it 'relationship source is the config file' do expect(relationships[0].source).to eq(config_file) end diff --git a/spec/unit/puppet/type/zabbix_template_spec.rb b/spec/unit/puppet/type/zabbix_template_spec.rb index f8d1659a1..085a83271 100644 --- a/spec/unit/puppet/type/zabbix_template_spec.rb +++ b/spec/unit/puppet/type/zabbix_template_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'fakefs/spec_helpers' @@ -12,12 +14,12 @@ let(:provider) { stub('provider', class: provider_class, clear: nil) } describe 'when validating attributes' do - [ - :template_name, - :template_source, - :provider + %i[ + template_name + template_source + provider ].each do |param| - it "should have a #{param} parameter" do + it "has a #{param} parameter" do expect(described_class.attrtype(param)).to eq(:param) end end @@ -32,7 +34,7 @@ describe 'ensure' do let(:property) { resource.property(:ensure) } - [:present, :absent].each do |value| + %i[present absent].each do |value| it "suppports #{value} as a value to :ensure" do expect { described_class.new(template_name: 'My template', ensure: value) }.not_to raise_error end @@ -52,6 +54,7 @@ property.expects(:template_xmls_match?).returns true expect(property).to be_safe_insync(:present) end + it 'is not insync if ensure is set to :present but the source and server templates don\'t match' do property.should = :present property.expects(:template_xmls_match?).returns false @@ -124,18 +127,23 @@ it 'removes spaces after \'>\'s' do expect(property.clean_xml('> ')).to eq '>' end + it 'removes tabs after \'>\'s' do expect(property.clean_xml(">\t")).to eq '>' end + it 'removes newlines after \'>\'s' do expect(property.clean_xml(">\n")).to eq '>' end + it 'removes spaces before \'<\'s' do expect(property.clean_xml(' <')).to eq '<' end + it 'removes tabs before \'<\'s' do expect(property.clean_xml("\t<")).to eq '<' end + it 'replaces dates with DATEWASHERE' do expect(property.clean_xml('2016-11-30T12:06:25Z')).to eq 'DATEWASHERE' end @@ -145,6 +153,7 @@ it 'returns \'Template updated\' when resource is being updated' do expect(property.change_to_s(:present, :present)).to eq 'Template updated' end + it 'calls super otherwise' do expect(property.change_to_s(:absent, :present)).to eq 'created' end diff --git a/spec/unit/puppet/x/spec_zabbix_types.rb b/spec/unit/puppet/x/spec_zabbix_types.rb index 9fae9eb5b..91021deed 100644 --- a/spec/unit/puppet/x/spec_zabbix_types.rb +++ b/spec/unit/puppet/x/spec_zabbix_types.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' shared_examples 'generic namevar' do |name| @@ -27,7 +29,7 @@ end allowed.each do |value| - it "should support #{value.inspect} as a value to :ensure" do + it "supports #{value.inspect} as a value to :ensure" do expect { described_class.new(name: 'nobody', ensure: value) }.not_to raise_error end end @@ -52,7 +54,7 @@ context 'allowed' do allowed.each do |value| - it "should support #{value} as a value" do + it "supports #{value} as a value" do expect { described_class.new(:name => 'nobody', param => value) }. not_to raise_error end @@ -60,12 +62,10 @@ end context 'disallowed' do - unless disallowed.nil? - disallowed.each do |value| - it "rejects #{value} as a value" do - expect { described_class.new(:name => 'nobody', param => :value) }. - to raise_error(Puppet::Error) - end + disallowed&.each do |value| + it "rejects #{value} as a value" do + expect { described_class.new(:name => 'nobody', param => :value) }. + to raise_error(Puppet::Error) end end end @@ -77,7 +77,7 @@ expect(resource.should(param)).to be_nil end else - it "should default to #{default}" do + it "defaults to #{default}" do resource = described_class.new(name: 'nobody') expect(resource.should(param)).to eq default end @@ -107,7 +107,7 @@ expect(resource.should(param)).to be_nil end else - it "should default to #{default}" do + it "defaults to #{default}" do resource = described_class.new(name: 'nobody') expect(resource.should(param)).to eq default end