diff --git a/.gitignore b/.gitignore index 641e153..b521749 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,18 @@ +pkg/ +Gemfile.lock +Gemfile.local +vendor/ +.vendor/ +spec/fixtures/manifests/ +spec/fixtures/modules/ +.vagrant/ +.bundle/ +coverage/ +log/ +.idea/ +.dependencies/ +.librarian/ +Puppetfile.lock +*.iml .*.sw? -pkg -spec/fixtures -.rspec_system -.vagrant -.bundle -vendor +.yardoc/ diff --git a/.msync.yml b/.msync.yml new file mode 100644 index 0000000..b42c487 --- /dev/null +++ b/.msync.yml @@ -0,0 +1 @@ +modulesync_config_version: '0.19.3' diff --git a/.pmtignore b/.pmtignore new file mode 100644 index 0000000..77f12ae --- /dev/null +++ b/.pmtignore @@ -0,0 +1 @@ +docs/ diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..2bf547f --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,531 @@ +require: rubocop-rspec +AllCops: + TargetRubyVersion: 1.9 + Include: + - ./**/*.rb + Exclude: + - files/**/* + - vendor/**/* + - .vendor/**/* + - pkg/**/* + - spec/fixtures/**/* +Lint/ConditionPosition: + Enabled: True + +Lint/ElseLayout: + Enabled: True + +Lint/UnreachableCode: + Enabled: True + +Lint/UselessComparison: + Enabled: True + +Lint/EnsureReturn: + Enabled: True + +Lint/HandleExceptions: + Enabled: True + +Lint/LiteralInCondition: + Enabled: True + +Lint/ShadowingOuterLocalVariable: + Enabled: True + +Lint/LiteralInInterpolation: + Enabled: True + +Style/HashSyntax: + Enabled: True + +Style/RedundantReturn: + Enabled: True + +Lint/AmbiguousOperator: + Enabled: True + +Lint/AssignmentInCondition: + Enabled: True + +Style/SpaceBeforeComment: + Enabled: True + +Style/AndOr: + Enabled: True + +Style/RedundantSelf: + Enabled: True + +Metrics/BlockLength: + Enabled: False + +# Method length is not necessarily an indicator of code quality +Metrics/MethodLength: + Enabled: False + +# Module length is not necessarily an indicator of code quality +Metrics/ModuleLength: + Enabled: False + +Style/WhileUntilModifier: + Enabled: True + +Lint/AmbiguousRegexpLiteral: + Enabled: True + +Security/Eval: + Enabled: True + +Lint/BlockAlignment: + Enabled: True + +Lint/DefEndAlignment: + Enabled: True + +Lint/EndAlignment: + Enabled: True + +Lint/DeprecatedClassMethods: + Enabled: True + +Lint/Loop: + Enabled: True + +Lint/ParenthesesAsGroupedExpression: + Enabled: True + +Lint/RescueException: + Enabled: True + +Lint/StringConversionInInterpolation: + Enabled: True + +Lint/UnusedBlockArgument: + Enabled: True + +Lint/UnusedMethodArgument: + Enabled: True + +Lint/UselessAccessModifier: + Enabled: True + +Lint/UselessAssignment: + Enabled: True + +Lint/Void: + Enabled: True + +Style/AccessModifierIndentation: + Enabled: True + +Style/AccessorMethodName: + Enabled: True + +Style/Alias: + Enabled: True + +Style/AlignArray: + Enabled: True + +Style/AlignHash: + Enabled: True + +Style/AlignParameters: + Enabled: True + +Metrics/BlockNesting: + Enabled: True + +Style/AsciiComments: + Enabled: True + +Style/Attr: + Enabled: True + +Style/BracesAroundHashParameters: + Enabled: True + +Style/CaseEquality: + Enabled: True + +Style/CaseIndentation: + Enabled: True + +Style/CharacterLiteral: + Enabled: True + +Style/ClassAndModuleCamelCase: + Enabled: True + +Style/ClassAndModuleChildren: + Enabled: False + +Style/ClassCheck: + Enabled: True + +# Class length is not necessarily an indicator of code quality +Metrics/ClassLength: + Enabled: False + +Style/ClassMethods: + Enabled: True + +Style/ClassVars: + Enabled: True + +Style/WhenThen: + Enabled: True + +Style/WordArray: + Enabled: True + +Style/UnneededPercentQ: + Enabled: True + +Style/Tab: + Enabled: True + +Style/SpaceBeforeSemicolon: + Enabled: True + +Style/TrailingBlankLines: + Enabled: True + +Style/SpaceInsideBlockBraces: + Enabled: True + +Style/SpaceInsideBrackets: + Enabled: True + +Style/SpaceInsideHashLiteralBraces: + Enabled: True + +Style/SpaceInsideParens: + Enabled: True + +Style/LeadingCommentSpace: + Enabled: True + +Style/SpaceBeforeFirstArg: + Enabled: True + +Style/SpaceAfterColon: + Enabled: True + +Style/SpaceAfterComma: + Enabled: True + +Style/SpaceAfterMethodName: + Enabled: True + +Style/SpaceAfterNot: + Enabled: True + +Style/SpaceAfterSemicolon: + Enabled: True + +Style/SpaceAroundEqualsInParameterDefault: + Enabled: True + +Style/SpaceAroundOperators: + Enabled: True + +Style/SpaceBeforeBlockBraces: + Enabled: True + +Style/SpaceBeforeComma: + Enabled: True + +Style/CollectionMethods: + Enabled: True + +Style/CommentIndentation: + Enabled: True + +Style/ColonMethodCall: + Enabled: True + +Style/CommentAnnotation: + Enabled: True + +# 'Complexity' is very relative +Metrics/CyclomaticComplexity: + Enabled: False + +Style/ConstantName: + Enabled: True + +Style/Documentation: + Enabled: False + +Style/DefWithParentheses: + Enabled: True + +Style/PreferredHashMethods: + Enabled: True + +Style/DotPosition: + EnforcedStyle: trailing + +Style/DoubleNegation: + Enabled: True + +Style/EachWithObject: + Enabled: True + +Style/EmptyLineBetweenDefs: + Enabled: True + +Style/IndentArray: + Enabled: True + +Style/IndentHash: + Enabled: True + +Style/IndentationConsistency: + Enabled: True + +Style/IndentationWidth: + Enabled: True + +Style/EmptyLines: + Enabled: True + +Style/EmptyLinesAroundAccessModifier: + Enabled: True + +Style/EmptyLiteral: + Enabled: True + +# Configuration parameters: AllowURI, URISchemes. +Metrics/LineLength: + Enabled: False + +Style/MethodCallWithoutArgsParentheses: + Enabled: True + +Style/MethodDefParentheses: + Enabled: True + +Style/LineEndConcatenation: + Enabled: True + +Style/TrailingWhitespace: + Enabled: True + +Style/StringLiterals: + Enabled: True + +Style/TrailingCommaInArguments: + Enabled: True + +Style/TrailingCommaInLiteral: + Enabled: True + +Style/GlobalVars: + Enabled: True + +Style/GuardClause: + Enabled: True + +Style/IfUnlessModifier: + Enabled: True + +Style/MultilineIfThen: + Enabled: True + +Style/NegatedIf: + Enabled: True + +Style/NegatedWhile: + Enabled: True + +Style/Next: + Enabled: True + +Style/SingleLineBlockParams: + Enabled: True + +Style/SingleLineMethods: + Enabled: True + +Style/SpecialGlobalVars: + Enabled: True + +Style/TrivialAccessors: + Enabled: True + +Style/UnlessElse: + Enabled: True + +Style/VariableInterpolation: + Enabled: True + +Style/VariableName: + Enabled: True + +Style/WhileUntilDo: + Enabled: True + +Style/EvenOdd: + Enabled: True + +Style/FileName: + Enabled: True + +Style/For: + Enabled: True + +Style/Lambda: + Enabled: True + +Style/MethodName: + Enabled: True + +Style/MultilineTernaryOperator: + Enabled: True + +Style/NestedTernaryOperator: + Enabled: True + +Style/NilComparison: + Enabled: True + +Style/FormatString: + Enabled: True + +Style/MultilineBlockChain: + Enabled: True + +Style/Semicolon: + Enabled: True + +Style/SignalException: + Enabled: True + +Style/NonNilCheck: + Enabled: True + +Style/Not: + Enabled: True + +Style/NumericLiterals: + Enabled: True + +Style/OneLineConditional: + Enabled: True + +Style/OpMethod: + Enabled: True + +Style/ParenthesesAroundCondition: + Enabled: True + +Style/PercentLiteralDelimiters: + Enabled: True + +Style/PerlBackrefs: + Enabled: True + +Style/PredicateName: + Enabled: True + +Style/RedundantException: + Enabled: True + +Style/SelfAssignment: + Enabled: True + +Style/Proc: + Enabled: True + +Style/RaiseArgs: + Enabled: True + +Style/RedundantBegin: + Enabled: True + +Style/RescueModifier: + Enabled: True + +# based on https://github.com/voxpupuli/modulesync_config/issues/168 +Style/RegexpLiteral: + EnforcedStyle: percent_r + Enabled: True + +Lint/UnderscorePrefixedVariableName: + Enabled: True + +Metrics/ParameterLists: + Enabled: False + +Lint/RequireParentheses: + Enabled: True + +Style/SpaceBeforeFirstArg: + Enabled: True + +Style/ModuleFunction: + Enabled: True + +Lint/Debugger: + Enabled: True + +Style/IfWithSemicolon: + Enabled: True + +Style/Encoding: + Enabled: True + +Style/BlockDelimiters: + Enabled: True + +Style/MultilineBlockLayout: + Enabled: True + +# 'Complexity' is very relative +Metrics/AbcSize: + Enabled: False + +# 'Complexity' is very relative +Metrics/PerceivedComplexity: + Enabled: False + +Lint/UselessAssignment: + Enabled: True + +Style/ClosingParenthesisIndentation: + Enabled: True + +# RSpec + +# We don't use rspec in this way +RSpec/DescribeClass: + Enabled: False + +# Example length is not necessarily an indicator of code quality +RSpec/ExampleLength: + Enabled: False + +RSpec/NamedSubject: + Enabled: False + +# disabled for now since they cause a lot of issues +# these issues aren't easy to fix +RSpec/RepeatedDescription: + Enabled: False + +# disabled for simpler testing on multiple platforms in case statement +RSpec/RepeatedExample: + Enabled: False + +RSpec/NestedGroups: + Enabled: False + +# disable Yaml safe_load. This is needed to support ruby2.0.0 development envs +Security/YAMLLoad: + Enabled: false + +Security/MarshalLoad: + Enabled: false diff --git a/.sync.yml b/.sync.yml new file mode 100644 index 0000000..0ab113d --- /dev/null +++ b/.sync.yml @@ -0,0 +1,9 @@ +Gemfile: + optional: + ':extra': + - gem: 'net-ldap' + version: '~> 0.12.1' + ':testextra': + - gem: 'rspec-mocks' + - gem: 'rspec-expectations' +mock_with: ':rspec diff --git a/.travis.yml b/.travis.yml index 528114d..83e7614 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,36 @@ --- -language: ruby -bundler_args: --without development system_tests -before_install: rm Gemfile.lock || true -rvm: - - 1.9.3 - - 2.0.0 - - 2.1.0 -script: bundle exec rake test -env: - - PUPPET_VERSION="~> 3.6.0" STRICT_VARIABLES=yes - - PUPPET_VERSION="~> 3.7.0" STRICT_VARIABLES=yes FUTURE_PARSER=yes - - PUPPET_VERSION="~> 3.8.3" STRICT_VARIABLES=yes FUTURE_PARSER=yes - - PUPPET_VERSION="~> 4.2.3" - - PUPPET_VERSION="~> 4.3.2" sudo: false +dist: trusty +language: ruby +cache: bundler +bundler_args: --without system_tests development +before_install: + - bundle -v + - rm Gemfile.lock || true + - gem update --system + - gem update bundler + - gem --version + - bundle -v +script: + - 'bundle exec rake $CHECK' +matrix: + fast_finish: true + include: + - rvm: 2.1.9 + env: PUPPET_VERSION="~> 4.0" CHECK=test + - rvm: 2.2.6 + env: PUPPET_VERSION="~> 4.0" CHECK=test + - rvm: 2.3.3 + env: PUPPET_VERSION="~> 4.0" CHECK=test + - rvm: 2.4.0 + env: PUPPET_VERSION="~> 4.0" CHECK=test + - rvm: 2.4.0 + env: PUPPET_VERSION="~> 4.0" CHECK=rubocop + - rvm: 2.4.0 + env: PUPPET_VERSION="~> 4.0" CHECK=build +branches: + only: + - master + - /^v\d/ +notifications: + email: false diff --git a/.yardopts b/.yardopts new file mode 100644 index 0000000..3687f51 --- /dev/null +++ b/.yardopts @@ -0,0 +1,2 @@ +--markup markdown +--output-dir docs/ diff --git a/Gemfile b/Gemfile index c516f72..4512719 100644 --- a/Gemfile +++ b/Gemfile @@ -1,27 +1,66 @@ -source "https://rubygems.org" +source ENV['GEM_SOURCE'] || 'https://rubygems.org' + +def location_for(place, fake_version = nil) + if place =~ /^(git[:@][^#]*)#(.*)/ + [fake_version, { :git => $1, :branch => $2, :require => false }].compact + elsif place =~ /^file:\/\/(.*)/ + ['>= 0', { :path => File.expand_path($1), :require => false }] + else + [place, { :require => false }] + end +end group :test do - gem "rake" - gem "puppet", ENV['PUPPET_VERSION'] || '~> 3.7.0' - gem "rspec", '< 3.2.0' - gem 'rspec-core' - gem 'rspec-mocks' - gem 'rspec-expectations' - gem "rspec-puppet", :git => 'https://github.com/rodjek/rspec-puppet.git' - gem "puppetlabs_spec_helper" - gem "metadata-json-lint" - gem "rspec-puppet-facts" - gem 'net-ldap', '~> 0.12.1' + gem 'puppetlabs_spec_helper', '~> 1.2.2', :require => false + gem 'rspec-puppet', '~> 2.5', :require => false + gem 'rspec-puppet-facts', :require => false + gem 'rspec-puppet-utils', :require => false + gem 'puppet-lint-absolute_classname-check', :require => false + gem 'puppet-lint-leading_zero-check', :require => false + gem 'puppet-lint-trailing_comma-check', :require => false + gem 'puppet-lint-version_comparison-check', :require => false + gem 'puppet-lint-classes_and_types_beginning_with_digits-check', :require => false + gem 'puppet-lint-unquoted_string-check', :require => false + gem 'puppet-lint-variable_contains_upcase', :require => false + gem 'metadata-json-lint', :require => false + gem 'puppet-blacksmith', :require => false + gem 'voxpupuli-release', :require => false, :git => 'https://github.com/voxpupuli/voxpupuli-release-gem.git' + gem 'puppet-strings', '~> 1.0.0', :require => false + gem 'redcarpet', :require => false + gem 'rubocop', '~> 0.47.0', :require => false if RUBY_VERSION >= '2.3.0' + gem 'rubocop-rspec', '~> 1.10.0', :require => false if RUBY_VERSION >= '2.3.0' + gem 'mocha', '>= 1.2.1', :require => false + gem 'coveralls', :require => false + gem 'simplecov-console', :require => false + gem 'github_changelog_generator', '~> 1.13.0', :require => false if RUBY_VERSION < '2.2.2' + gem 'rack', '~> 1.0', :require => false if RUBY_VERSION < '2.2.2' + gem 'github_changelog_generator', :require => false if RUBY_VERSION >= '2.2.2' end group :development do - gem "travis" - gem "travis-lint" - gem "vagrant-wrapper" - gem "puppet-blacksmith" + gem 'travis', :require => false + gem 'travis-lint', :require => false + gem 'guard-rake', :require => false end -group :system_tests do - gem "beaker" - gem "beaker-rspec" +group :extra do + gem 'net-ldap', '~> 0.12.1', :require => false end + +group :testextra do + gem 'rspec-mocks' + gem 'rspec-expectations' +end + + + +if facterversion = ENV['FACTER_GEM_VERSION'] + gem 'facter', facterversion.to_s, :require => false, :groups => [:test] +else + gem 'facter', :require => false, :groups => [:test] +end + +ENV['PUPPET_VERSION'].nil? ? puppetversion = '~> 4.0' : puppetversion = ENV['PUPPET_VERSION'].to_s +gem 'puppet', puppetversion, :require => false, :groups => [:test] + +# vim: syntax=ruby diff --git a/Rakefile b/Rakefile index 3608367..2def02f 100644 --- a/Rakefile +++ b/Rakefile @@ -1,56 +1,44 @@ require 'puppetlabs_spec_helper/rake_tasks' -require 'puppet/version' -require 'puppet/vendor/semantic/lib/semantic' unless Puppet.version.to_f < 3.6 -require 'puppet-lint/tasks/puppet-lint' -require 'puppet-syntax/tasks/puppet-syntax' +require 'puppet_blacksmith/rake_tasks' +require 'voxpupuli/release/rake_tasks' +require 'puppet-strings/tasks' -# These gems aren't always present, for instance -# on Travis with --without development -begin - require 'puppet_blacksmith/rake_tasks' -rescue LoadError -end - -Rake::Task[:lint].clear - -PuppetLint.configuration.relative = true -PuppetLint.configuration.send("disable_80chars") -PuppetLint.configuration.log_format = "%{path}:%{linenumber}:%{check}:%{KIND}:%{message}" +PuppetLint.configuration.log_format = '%{path}:%{line}:%{check}:%{KIND}:%{message}' PuppetLint.configuration.fail_on_warnings = true - -# Forsake support for Puppet 2.6.2 for the benefit of cleaner code. -# http://puppet-lint.com/checks/class_parameter_defaults/ -PuppetLint.configuration.send('disable_class_parameter_defaults') -# http://puppet-lint.com/checks/class_inherits_from_params_class/ +PuppetLint.configuration.send('relative') +PuppetLint.configuration.send('disable_140chars') PuppetLint.configuration.send('disable_class_inherits_from_params_class') +PuppetLint.configuration.send('disable_documentation') +PuppetLint.configuration.send('disable_single_quote_string_with_variables') -exclude_paths = [ - "bundle/**/*", - "pkg/**/*", - "vendor/**/*", - "spec/**/*", -] +exclude_paths = %w( + pkg/**/* + vendor/**/* + .vendor/**/* + spec/**/* +) PuppetLint.configuration.ignore_paths = exclude_paths PuppetSyntax.exclude_paths = exclude_paths -desc "Run acceptance tests" +desc 'Run acceptance tests' RSpec::Core::RakeTask.new(:acceptance) do |t| - t.pattern = 'spec/**/*.rb' + t.pattern = 'spec/acceptance' end -desc "Populate CONTRIBUTORS file" -task :contributors do - system("git log --format='%aN' | sort -u > CONTRIBUTORS") -end - -task :metadata do - sh "metadata-json-lint metadata.json" -end - -desc "Run syntax, lint, and spec tests." -task :test => [ - :syntax, - :lint, - :spec, - :metadata, +desc 'Run tests metadata_lint, release_checks' +task test: [ + :metadata_lint, + :release_checks, ] + +begin + require 'github_changelog_generator/task' + GitHubChangelogGenerator::RakeTask.new :changelog do |config| + version = (Blacksmith::Modulefile.new).version + config.future_release = "#{version}" + config.header = "# Change log\n\nAll notable changes to this project will be documented in this file.\nEach new release typically also includes the latest modulesync defaults.\nThese should not impact the functionality of the module." + config.exclude_labels = %w{duplicate question invalid wontfix modulesync} + end +rescue LoadError +end +# vim: syntax=ruby diff --git a/lib/puppet/parser/functions/ldapquery.rb b/lib/puppet/parser/functions/ldapquery.rb index 7518fd9..3110671 100644 --- a/lib/puppet/parser/functions/ldapquery.rb +++ b/lib/puppet/parser/functions/ldapquery.rb @@ -11,14 +11,14 @@ require_relative '../../../puppet_x/ldapquery' begin require 'net/ldap' rescue - Puppet.warn("Missing net/ldap gem required for ldapquery() function") + Puppet.warn('Missing net/ldap gem required for ldapquery() function') end Puppet::Parser::Functions.newfunction(:ldapquery, - :type => :rvalue) do |args| + type: :rvalue) do |args| if args.size > 4 - raise Puppet::ParseError, "Too many arguments received in ldapquery()" + raise Puppet::ParseError, 'Too many arguments received in ldapquery()' end filter, attributes, base, scope = args diff --git a/lib/puppet_x/ldapquery.rb b/lib/puppet_x/ldapquery.rb index c27b7b4..016bd3f 100644 --- a/lib/puppet_x/ldapquery.rb +++ b/lib/puppet_x/ldapquery.rb @@ -7,44 +7,44 @@ module PuppetX def initialize( filter, - attributes=[], - base=Puppet[:ldapbase], - scope='sub' + attributes = [], + base = Puppet[:ldapbase], + scope = 'sub' ) @filter = filter @attributes = attributes @base = base - if scope - if scope == 'sub' - @scope = Net::LDAP::SearchScope_WholeSubtree - elsif scope == 'base' - @scope = Net::LDAP::SearchScope_BaseObject - elsif scope == 'single' - @scope = Net::LDAP::SearchScope_SingleLevel - else - raise Puppet::ParseError, 'Received param "scope" not one of ["sub","base","single"]' - end + return unless scope + + if scope == 'sub' + @scope = Net::LDAP::SearchScope_WholeSubtree + elsif scope == 'base' + @scope = Net::LDAP::SearchScope_BaseObject + elsif scope == 'single' + @scope = Net::LDAP::SearchScope_SingleLevel + else + raise Puppet::ParseError, 'Received param "scope" not one of ["sub","base","single"]' end end - def get_config + def ldap_config # Load the configuration variables from Puppet required_vars = [ :ldapserver, - :ldapport, + :ldapport ] - required_vars.each {|r| + required_vars.each do |r| unless Puppet[r] - raise Puppet::ParseError, "Missing required setting '#{r.to_s}' in puppet.conf" + raise Puppet::ParseError, "Missing required setting '#{r}' in puppet.conf" end - } + end host = Puppet[:ldapserver] port = Puppet[:ldapport] - if Puppet[:ldapuser] and Puppet[:ldappassword] + if Puppet[:ldapuser] && Puppet[:ldappassword] user = Puppet[:ldapuser] password = Puppet[:ldappassword] end @@ -53,45 +53,45 @@ module PuppetX ca_file = "#{Puppet[:confdir]}/ldap_ca.pem" conf = { - :host => host, - :port => port, + host: host, + port: port } - if user != '' and password != '' + if (user != '') && (password != '') conf[:auth] = { - :method => :simple, - :username => user, - :password => password, + method: :simple, + username: user, + password: password } end if tls conf[:encryption] = { - :method => :simple_tls, - :tls_options => { :ca_file => ca_file } + method: :simple_tls, + tls_options: { ca_file: ca_file } } end - return conf + conf end - def get_entries() + def entries # Query the LDAP server for attributes using the filter # # Returns: An array of Net::LDAP::Entry objects - conf = self.get_config() + conf = ldap_config start_time = Time.now ldap = Net::LDAP.new(conf) search_args = { - :base => @base, - :attributes => @attributes, - :scope => @scope, - :time => 10, + base: @base, + attributes: @attributes, + scope: @scope, + time: 10 } - if @filter and @filter.length > 0 + if @filter && !@filter.empty? ldapfilter = Net::LDAP::Filter.construct(@filter) search_args[:filter] = ldapfilter end @@ -103,11 +103,11 @@ module PuppetX entries << entry end end_time = Time.now - time_delta = sprintf('%.3f', end_time - start_time) + time_delta = format('%.3f', end_time - start_time) Puppet.debug("ldapquery(): Searching #{@base} for #{@attributes} using #{@filter} took #{time_delta} seconds and returned #{entries.length} results") return entries - rescue Exception => e + rescue LdapError => e Puppet.debug("There was an error searching LDAP #{e.message}") Puppet.debug('Returning false') return false @@ -116,11 +116,9 @@ module PuppetX def parse_entries data = [] - entries = get_entries() entries.each do |entry| entry_data = {} entry.each do |attribute, values| - attr = attribute.to_s value_data = [] Array(values).flatten.each do |v| @@ -131,7 +129,7 @@ module PuppetX data << entry_data end Puppet.debug(data) - return data + data end def results diff --git a/spec/classes/coverage_spec.rb b/spec/classes/coverage_spec.rb new file mode 100644 index 0000000..de44654 --- /dev/null +++ b/spec/classes/coverage_spec.rb @@ -0,0 +1,4 @@ +require 'rspec-puppet' + +at_exit { RSpec::Puppet::Coverage.report! } +# vim: syntax=ruby diff --git a/spec/default_facts.yml b/spec/default_facts.yml new file mode 100644 index 0000000..13c4165 --- /dev/null +++ b/spec/default_facts.yml @@ -0,0 +1,14 @@ +# This file is managed via modulesync +# https://github.com/voxpupuli/modulesync +# https://github.com/voxpupuli/modulesync_config +# +# use default_module_facts.yaml for module specific +# facts. +# +# Hint if using with rspec-puppet-facts ("on_supported_os.each"): +# if a same named fact exists in facterdb it will be overridden. +--- +concat_basedir: "/tmp" +ipaddress: "172.16.254.254" +is_pe: false +macaddress: "AA:AA:AA:AA:AA:AA" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 488b606..4b10f5e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,10 +1,33 @@ require 'puppetlabs_spec_helper/module_spec_helper' -require 'rspec-puppet' +require 'rspec-puppet-facts' +include RspecPuppetFacts -fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures')) +if Dir.exist?(File.expand_path('../../lib', __FILE__)) + require 'coveralls' + require 'simplecov' + require 'simplecov-console' + SimpleCov.formatters = [ + SimpleCov::Formatter::HTMLFormatter, + SimpleCov::Formatter::Console, + Coveralls::SimpleCov::Formatter + ] + SimpleCov.start do + track_files 'lib/**/*.rb' + add_filter '/spec' + add_filter '/vendor' + add_filter '/.vendor' + end +end RSpec.configure do |c| - c.module_path = File.join(fixture_path, 'modules') - c.manifest_dir = File.join(fixture_path, 'manifests') - c.mock_framework = :rspec + default_facts = { + puppetversion: Puppet.version, + facterversion: Facter.version + } + default_facts.merge!(YAML.load(File.read(File.expand_path('../default_facts.yml', __FILE__)))) if File.exist?(File.expand_path('../default_facts.yml', __FILE__)) + default_facts.merge!(YAML.load(File.read(File.expand_path('../default_module_facts.yml', __FILE__)))) if File.exist?(File.expand_path('../default_module_facts.yml', __FILE__)) + c.default_facts = default_facts + c.mock_with :rspec end + +# vim: syntax=ruby diff --git a/spec/unit/ldapquery_spec.rb b/spec/unit/ldapquery_spec.rb index 24c618e..091b181 100644 --- a/spec/unit/ldapquery_spec.rb +++ b/spec/unit/ldapquery_spec.rb @@ -4,62 +4,64 @@ require 'net/ldap' describe 'PuppetX::LDAPquery' do describe 'results' do - let (:conf) { { - :host => 'ldap.example.com', - :port => 9009, - } } + let(:conf) do + { + host: 'ldap.example.com', + port: 9009 + } + end - let (:base) { 'dc=example,dc=com' } + let(:base) { 'dc=example,dc=com' } - it "should fail with no filter" do + it 'fails with no filter' do filter = '' attributes = ['uid'] expect { PuppetX::LDAPquery.new(filter, attributes).results }.to raise_error end - it "should not fail when using defaults in puppet.conf" do + it 'does not fail when using defaults in puppet.conf' do filter = '(uid=zach)' attributes = ['uid'] l = PuppetX::LDAPquery.new(filter, attributes, base) - expect { l.get_config }.to_not raise_error + expect { l.ldap_config }.not_to raise_error end - it 'should return the desired results' do + it 'returns the desired results' do filter = '(uid=zach)' attributes = ['uid'] - wanted = [{"dn"=>["uid=zach,ou=users,dc=puppetlabs,dc=com"], "uid"=>["zach"]}] - entries = Marshal.load(File.read("spec/fixtures/entries_single.obj")) + wanted = [{ 'dn' => ['uid=zach,ou=users,dc=puppetlabs,dc=com'], 'uid' => ['zach'] }] + entries = Marshal.load(File.read('spec/fixtures/entries_single.obj')) l = PuppetX::LDAPquery.new(filter, attributes, base) - expect(l).to receive(:get_entries).and_return(entries) + allow(l).to receive(:entries).and_return(entries) expect(l.results).to eq(wanted) end - context "a multivalued attribute is requested" do - it 'should return the attribute values as an array to the attribute' do + context 'a multivalued attribute is requested' do + it 'returns the attribute values as an array to the attribute' do filter = '(uid=zach)' attributes = ['objectClass'] - wanted =[{"dn"=>["uid=zach,ou=users,dc=puppetlabs,dc=com"], "objectclass"=>["posixAccount", "shadowAccount", "inetOrgPerson", "puppetPerson", "ldapPublicKey", "top"]}] + wanted = [{ 'dn' => ['uid=zach,ou=users,dc=puppetlabs,dc=com'], 'objectclass' => %w(posixAccount shadowAccount inetOrgPerson puppetPerson ldapPublicKey top) }] - entries = Marshal.load(File.read("spec/fixtures/entries_objectClass.obj")) + entries = Marshal.load(File.read('spec/fixtures/entries_objectClass.obj')) l = PuppetX::LDAPquery.new(filter, attributes, base) - expect(l).to receive(:get_entries).and_return(entries) + allow(l).to receive(:entries).and_return(entries) expect(l.results).to eq(wanted) end - it 'should return the attributes without new lines' do + it 'returns the attributes without new lines' do filter = '(uid=zach)' attributes = ['sshPublicKey'] - wanted = [{"dn"=>["uid=zach,ou=users,dc=puppetlabs,dc=com"], "sshpublickey"=>["ssh-rsa AAAAB...1== user@somewhere", "ssh-rsa AAAAB...2== user@somewhereelse"]}] + wanted = [{ 'dn' => ['uid=zach,ou=users,dc=puppetlabs,dc=com'], 'sshpublickey' => ['ssh-rsa AAAAB...1== user@somewhere', 'ssh-rsa AAAAB...2== user@somewhereelse'] }] - entries = Marshal.load(File.read("spec/fixtures/entries_multivalue.obj")) + entries = Marshal.load(File.read('spec/fixtures/entries_multivalue.obj')) l = PuppetX::LDAPquery.new(filter, attributes, base) - expect(l).to receive(:get_entries).and_return(entries) + allow(l).to receive(:entries).and_return(entries) expect(l.results).to eq(wanted) end end