diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..12ed4ff --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,6 @@ +FROM puppet/pdk:latest + +# [Optional] Uncomment this section to install additional packages. +# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ +# && apt-get -y install --no-install-recommends + diff --git a/.devcontainer/README.md b/.devcontainer/README.md new file mode 100644 index 0000000..a719361 --- /dev/null +++ b/.devcontainer/README.md @@ -0,0 +1,38 @@ +# devcontainer + + +For format details, see https://aka.ms/devcontainer.json. + +For config options, see the README at: +https://github.com/microsoft/vscode-dev-containers/tree/v0.140.1/containers/puppet + +``` json +{ + "name": "Puppet Development Kit (Community)", + "dockerFile": "Dockerfile", + + // Set *default* container specific settings.json values on container create. + "settings": { + "terminal.integrated.profiles.linux": { + "bash": { + "path": "bash", + } + } + }, + + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "puppet.puppet-vscode", + "rebornix.Ruby" + ], + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + "forwardPorts": [], + + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": "pdk --version", +} +``` + + + diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..cdd65d2 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,17 @@ +{ + "name": "Puppet Development Kit (Community)", + "dockerFile": "Dockerfile", + + "settings": { + "terminal.integrated.profiles.linux": { + "bash": { + "path": "bash" + } + } + }, + + "extensions": [ + "puppet.puppet-vscode", + "rebornix.Ruby" + ] +} diff --git a/.fixtures.yml b/.fixtures.yml new file mode 100644 index 0000000..6470f44 --- /dev/null +++ b/.fixtures.yml @@ -0,0 +1,6 @@ +# This file can be used to install module dependencies for unit testing +# See https://github.com/puppetlabs/puppetlabs_spec_helper#using-fixtures for details +--- +fixtures: + forge_modules: + stdlib: "puppetlabs/stdlib" diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9032a01 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +*.rb eol=lf +*.erb eol=lf +*.pp eol=lf +*.sh eol=lf +*.epp eol=lf diff --git a/.gitignore b/.gitignore index 0676b68..988dcbb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,28 @@ -pkg/ -vendor/gems/ -.bundle/ +.git/ +.*.sw[op] +.metadata +.yardoc +.yardwarns +*.iml +/.bundle/ +/.idea/ +/.vagrant/ +/coverage/ +/bin/ +/doc/ +/Gemfile.local +/Gemfile.lock +/junit/ +/log/ +/pkg/ +/spec/fixtures/manifests/ +/spec/fixtures/modules/ +/tmp/ +/vendor/ +/convert_report.txt +/update_report.txt +.DS_Store +.project +.envrc +/inventory.yaml +/spec/fixtures/litmus_inventory.yaml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..6d5e786 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,53 @@ +--- +stages: + - syntax + - unit + +default: + cache: + paths: + - vendor/bundle + + before_script: &before_script + - bundle -v + - rm Gemfile.lock || true + - "# Update system gems if requested. This is useful to temporarily workaround troubles in the test runner" + - "# Set `rubygems_version` in the .sync.yml to set a value" + - "# Ignore exit code of SIGPIPE'd yes to not fail with shell's pipefail set" + - '[ -z "$RUBYGEMS_VERSION" ] || (yes || true) | gem update --system $RUBYGEMS_VERSION' + - gem --version + - bundle -v + - bundle install --without system_tests --path vendor/bundle --jobs $(nproc) + +validate lint check rubocop-Ruby 2.5.7-Puppet ~> 6: + stage: syntax + image: ruby:2.5.7 + script: + - bundle exec rake validate lint check rubocop + variables: + PUPPET_GEM_VERSION: '~> 6' + +parallel_spec-Ruby 2.5.7-Puppet ~> 6: + stage: unit + image: ruby:2.5.7 + script: + - bundle exec rake parallel_spec + variables: + PUPPET_GEM_VERSION: '~> 6' + +validate lint check rubocop-Ruby 2.7.2-Puppet ~> 7: + stage: syntax + image: ruby:2.7.2 + script: + - bundle exec rake validate lint check rubocop + variables: + PUPPET_GEM_VERSION: '~> 7' + +parallel_spec-Ruby 2.7.2-Puppet ~> 7: + stage: unit + image: ruby:2.7.2 + script: + - bundle exec rake parallel_spec + variables: + PUPPET_GEM_VERSION: '~> 7' + diff --git a/.pdkignore b/.pdkignore new file mode 100644 index 0000000..a956c8f --- /dev/null +++ b/.pdkignore @@ -0,0 +1,46 @@ +.git/ +.*.sw[op] +.metadata +.yardoc +.yardwarns +*.iml +/.bundle/ +/.idea/ +/.vagrant/ +/coverage/ +/bin/ +/doc/ +/Gemfile.local +/Gemfile.lock +/junit/ +/log/ +/pkg/ +/spec/fixtures/manifests/ +/spec/fixtures/modules/ +/tmp/ +/vendor/ +/convert_report.txt +/update_report.txt +.DS_Store +.project +.envrc +/inventory.yaml +/spec/fixtures/litmus_inventory.yaml +/appveyor.yml +/.editorconfig +/.fixtures.yml +/Gemfile +/.gitattributes +/.gitignore +/.gitlab-ci.yml +/.pdkignore +/.puppet-lint.rc +/Rakefile +/rakelib/ +/.rspec +/.rubocop.yml +/.yardopts +/spec/ +/.vscode/ +/.sync.yml +/.devcontainer/ diff --git a/.puppet-lint.rc b/.puppet-lint.rc new file mode 100644 index 0000000..cc96ece --- /dev/null +++ b/.puppet-lint.rc @@ -0,0 +1 @@ +--relative diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..16f9cdb --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..31e8248 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,519 @@ +--- +require: +- rubocop-performance +- rubocop-rspec +AllCops: + DisplayCopNames: true + TargetRubyVersion: '2.5' + Include: + - "**/*.rb" + Exclude: + - bin/* + - ".vendor/**/*" + - "**/Gemfile" + - "**/Rakefile" + - pkg/**/* + - spec/fixtures/**/* + - vendor/**/* + - "**/Puppetfile" + - "**/Vagrantfile" + - "**/Guardfile" +Layout/LineLength: + Description: People have wide screens, use them. + Max: 200 +RSpec/BeforeAfterAll: + Description: Beware of using after(:all) as it may cause state to leak between tests. + A necessary evil in acceptance testing. + Exclude: + - spec/acceptance/**/*.rb +RSpec/HookArgument: + Description: Prefer explicit :each argument, matching existing module's style + EnforcedStyle: each +RSpec/DescribeSymbol: + Exclude: + - spec/unit/facter/**/*.rb +Style/BlockDelimiters: + Description: Prefer braces for chaining. Mostly an aesthetical choice. Better to + be consistent then. + EnforcedStyle: braces_for_chaining +Style/ClassAndModuleChildren: + Description: Compact style reduces the required amount of indentation. + EnforcedStyle: compact +Style/EmptyElse: + Description: Enforce against empty else clauses, but allow `nil` for clarity. + EnforcedStyle: empty +Style/FormatString: + Description: Following the main puppet project's style, prefer the % format format. + EnforcedStyle: percent +Style/FormatStringToken: + Description: Following the main puppet project's style, prefer the simpler template + tokens over annotated ones. + EnforcedStyle: template +Style/Lambda: + Description: Prefer the keyword for easier discoverability. + EnforcedStyle: literal +Style/RegexpLiteral: + Description: Community preference. See https://github.com/voxpupuli/modulesync_config/issues/168 + EnforcedStyle: percent_r +Style/TernaryParentheses: + Description: Checks for use of parentheses around ternary conditions. Enforce parentheses + on complex expressions for better readability, but seriously consider breaking + it up. + EnforcedStyle: require_parentheses_when_complex +Style/TrailingCommaInArguments: + Description: Prefer always trailing comma on multiline argument lists. This makes + diffs, and re-ordering nicer. + EnforcedStyleForMultiline: comma +Style/TrailingCommaInArrayLiteral: + Description: Prefer always trailing comma on multiline literals. This makes diffs, + and re-ordering nicer. + EnforcedStyleForMultiline: comma +Style/SymbolArray: + Description: Using percent style obscures symbolic intent of array's contents. + EnforcedStyle: brackets +RSpec/MessageSpies: + EnforcedStyle: receive +Style/Documentation: + Exclude: + - lib/puppet/parser/functions/**/* + - spec/**/* +Style/WordArray: + EnforcedStyle: brackets +Performance/AncestorsInclude: + Enabled: true +Performance/BigDecimalWithNumericArgument: + Enabled: true +Performance/BlockGivenWithExplicitBlock: + Enabled: true +Performance/CaseWhenSplat: + Enabled: true +Performance/ConstantRegexp: + Enabled: true +Performance/MethodObjectAsBlock: + Enabled: true +Performance/RedundantSortBlock: + Enabled: true +Performance/RedundantStringChars: + Enabled: true +Performance/ReverseFirst: + Enabled: true +Performance/SortReverse: + Enabled: true +Performance/Squeeze: + Enabled: true +Performance/StringInclude: + Enabled: true +Performance/Sum: + Enabled: true +Style/CollectionMethods: + Enabled: true +Style/MethodCalledOnDoEndBlock: + Enabled: true +Style/StringMethods: + Enabled: true +Bundler/InsecureProtocolSource: + Enabled: false +Gemspec/DuplicatedAssignment: + Enabled: false +Gemspec/OrderedDependencies: + Enabled: false +Gemspec/RequiredRubyVersion: + Enabled: false +Gemspec/RubyVersionGlobalsUsage: + Enabled: false +Layout/ArgumentAlignment: + Enabled: false +Layout/BeginEndAlignment: + Enabled: false +Layout/ClosingHeredocIndentation: + Enabled: false +Layout/EmptyComment: + Enabled: false +Layout/EmptyLineAfterGuardClause: + Enabled: false +Layout/EmptyLinesAroundArguments: + Enabled: false +Layout/EmptyLinesAroundAttributeAccessor: + Enabled: false +Layout/EndOfLine: + Enabled: false +Layout/FirstArgumentIndentation: + Enabled: false +Layout/HashAlignment: + Enabled: false +Layout/HeredocIndentation: + Enabled: false +Layout/LeadingEmptyLines: + Enabled: false +Layout/SpaceAroundMethodCallOperator: + Enabled: false +Layout/SpaceInsideArrayLiteralBrackets: + Enabled: false +Layout/SpaceInsideReferenceBrackets: + Enabled: false +Lint/BigDecimalNew: + Enabled: false +Lint/BooleanSymbol: + Enabled: false +Lint/ConstantDefinitionInBlock: + Enabled: false +Lint/DeprecatedOpenSSLConstant: + Enabled: false +Lint/DisjunctiveAssignmentInConstructor: + Enabled: false +Lint/DuplicateElsifCondition: + Enabled: false +Lint/DuplicateRequire: + Enabled: false +Lint/DuplicateRescueException: + Enabled: false +Lint/EmptyConditionalBody: + Enabled: false +Lint/EmptyFile: + Enabled: false +Lint/ErbNewArguments: + Enabled: false +Lint/FloatComparison: + Enabled: false +Lint/HashCompareByIdentity: + Enabled: false +Lint/IdentityComparison: + Enabled: false +Lint/InterpolationCheck: + Enabled: false +Lint/MissingCopEnableDirective: + Enabled: false +Lint/MixedRegexpCaptureTypes: + Enabled: false +Lint/NestedPercentLiteral: + Enabled: false +Lint/NonDeterministicRequireOrder: + Enabled: false +Lint/OrderedMagicComments: + Enabled: false +Lint/OutOfRangeRegexpRef: + Enabled: false +Lint/RaiseException: + Enabled: false +Lint/RedundantCopEnableDirective: + Enabled: false +Lint/RedundantRequireStatement: + Enabled: false +Lint/RedundantSafeNavigation: + Enabled: false +Lint/RedundantWithIndex: + Enabled: false +Lint/RedundantWithObject: + Enabled: false +Lint/RegexpAsCondition: + Enabled: false +Lint/ReturnInVoidContext: + Enabled: false +Lint/SafeNavigationConsistency: + Enabled: false +Lint/SafeNavigationWithEmpty: + Enabled: false +Lint/SelfAssignment: + Enabled: false +Lint/SendWithMixinArgument: + Enabled: false +Lint/ShadowedArgument: + Enabled: false +Lint/StructNewOverride: + Enabled: false +Lint/ToJSON: + Enabled: false +Lint/TopLevelReturnWithArgument: + Enabled: false +Lint/TrailingCommaInAttributeDeclaration: + Enabled: false +Lint/UnreachableLoop: + Enabled: false +Lint/UriEscapeUnescape: + Enabled: false +Lint/UriRegexp: + Enabled: false +Lint/UselessMethodDefinition: + Enabled: false +Lint/UselessTimes: + Enabled: false +Metrics/AbcSize: + Enabled: false +Metrics/BlockLength: + Enabled: false +Metrics/BlockNesting: + Enabled: false +Metrics/ClassLength: + Enabled: false +Metrics/CyclomaticComplexity: + Enabled: false +Metrics/MethodLength: + Enabled: false +Metrics/ModuleLength: + Enabled: false +Metrics/ParameterLists: + Enabled: false +Metrics/PerceivedComplexity: + Enabled: false +Migration/DepartmentName: + Enabled: false +Naming/AccessorMethodName: + Enabled: false +Naming/BlockParameterName: + Enabled: false +Naming/HeredocDelimiterCase: + Enabled: false +Naming/HeredocDelimiterNaming: + Enabled: false +Naming/MemoizedInstanceVariableName: + Enabled: false +Naming/MethodParameterName: + Enabled: false +Naming/RescuedExceptionsVariableName: + Enabled: false +Naming/VariableNumber: + Enabled: false +Performance/BindCall: + Enabled: false +Performance/DeletePrefix: + Enabled: false +Performance/DeleteSuffix: + Enabled: false +Performance/InefficientHashSearch: + Enabled: false +Performance/UnfreezeString: + Enabled: false +Performance/UriDefaultParser: + Enabled: false +RSpec/Be: + Enabled: false +RSpec/Capybara/CurrentPathExpectation: + Enabled: false +RSpec/Capybara/FeatureMethods: + Enabled: false +RSpec/Capybara/VisibilityMatcher: + Enabled: false +RSpec/ContextMethod: + Enabled: false +RSpec/ContextWording: + Enabled: false +RSpec/DescribeClass: + Enabled: false +RSpec/EmptyHook: + Enabled: false +RSpec/EmptyLineAfterExample: + Enabled: false +RSpec/EmptyLineAfterExampleGroup: + Enabled: false +RSpec/EmptyLineAfterHook: + Enabled: false +RSpec/ExampleLength: + Enabled: false +RSpec/ExampleWithoutDescription: + Enabled: false +RSpec/ExpectChange: + Enabled: false +RSpec/ExpectInHook: + Enabled: false +RSpec/FactoryBot/AttributeDefinedStatically: + Enabled: false +RSpec/FactoryBot/CreateList: + Enabled: false +RSpec/FactoryBot/FactoryClassName: + Enabled: false +RSpec/HooksBeforeExamples: + Enabled: false +RSpec/ImplicitBlockExpectation: + Enabled: false +RSpec/ImplicitSubject: + Enabled: false +RSpec/LeakyConstantDeclaration: + Enabled: false +RSpec/LetBeforeExamples: + Enabled: false +RSpec/MissingExampleGroupArgument: + Enabled: false +RSpec/MultipleExpectations: + Enabled: false +RSpec/MultipleMemoizedHelpers: + Enabled: false +RSpec/MultipleSubjects: + Enabled: false +RSpec/NestedGroups: + Enabled: false +RSpec/PredicateMatcher: + Enabled: false +RSpec/ReceiveCounts: + Enabled: false +RSpec/ReceiveNever: + Enabled: false +RSpec/RepeatedExampleGroupBody: + Enabled: false +RSpec/RepeatedExampleGroupDescription: + Enabled: false +RSpec/RepeatedIncludeExample: + Enabled: false +RSpec/ReturnFromStub: + Enabled: false +RSpec/SharedExamples: + Enabled: false +RSpec/StubbedMock: + Enabled: false +RSpec/UnspecifiedException: + Enabled: false +RSpec/VariableDefinition: + Enabled: false +RSpec/VoidExpect: + Enabled: false +RSpec/Yield: + Enabled: false +Security/Open: + Enabled: false +Style/AccessModifierDeclarations: + Enabled: false +Style/AccessorGrouping: + Enabled: false +Style/AsciiComments: + Enabled: false +Style/BisectedAttrAccessor: + Enabled: false +Style/CaseLikeIf: + Enabled: false +Style/ClassEqualityComparison: + Enabled: false +Style/ColonMethodDefinition: + Enabled: false +Style/CombinableLoops: + Enabled: false +Style/CommentedKeyword: + Enabled: false +Style/Dir: + Enabled: false +Style/DoubleCopDisableDirective: + Enabled: false +Style/EmptyBlockParameter: + Enabled: false +Style/EmptyLambdaParameter: + Enabled: false +Style/Encoding: + Enabled: false +Style/EvalWithLocation: + Enabled: false +Style/ExpandPathArguments: + Enabled: false +Style/ExplicitBlockArgument: + Enabled: false +Style/ExponentialNotation: + Enabled: false +Style/FloatDivision: + Enabled: false +Style/FrozenStringLiteralComment: + Enabled: false +Style/GlobalStdStream: + Enabled: false +Style/HashAsLastArrayItem: + Enabled: false +Style/HashLikeCase: + Enabled: false +Style/HashTransformKeys: + Enabled: false +Style/HashTransformValues: + Enabled: false +Style/IfUnlessModifier: + Enabled: false +Style/KeywordParametersOrder: + Enabled: false +Style/MinMax: + Enabled: false +Style/MixinUsage: + Enabled: false +Style/MultilineWhenThen: + Enabled: false +Style/NegatedUnless: + Enabled: false +Style/NumericPredicate: + Enabled: false +Style/OptionalBooleanParameter: + Enabled: false +Style/OrAssignment: + Enabled: false +Style/RandomWithOffset: + Enabled: false +Style/RedundantAssignment: + Enabled: false +Style/RedundantCondition: + Enabled: false +Style/RedundantConditional: + Enabled: false +Style/RedundantFetchBlock: + Enabled: false +Style/RedundantFileExtensionInRequire: + Enabled: false +Style/RedundantRegexpCharacterClass: + Enabled: false +Style/RedundantRegexpEscape: + Enabled: false +Style/RedundantSelfAssignment: + Enabled: false +Style/RedundantSort: + Enabled: false +Style/RescueStandardError: + Enabled: false +Style/SingleArgumentDig: + Enabled: false +Style/SlicingWithRange: + Enabled: false +Style/SoleNestedConditional: + Enabled: false +Style/StderrPuts: + Enabled: false +Style/StringConcatenation: + Enabled: false +Style/Strip: + Enabled: false +Style/SymbolProc: + Enabled: false +Style/TrailingBodyOnClass: + Enabled: false +Style/TrailingBodyOnMethodDefinition: + Enabled: false +Style/TrailingBodyOnModule: + Enabled: false +Style/TrailingCommaInHashLiteral: + Enabled: false +Style/TrailingMethodEndStatement: + Enabled: false +Style/UnpackFirst: + Enabled: false +Lint/DuplicateBranch: + Enabled: false +Lint/DuplicateRegexpCharacterClassElement: + Enabled: false +Lint/EmptyBlock: + Enabled: false +Lint/EmptyClass: + Enabled: false +Lint/NoReturnInBeginEndBlocks: + Enabled: false +Lint/ToEnumArguments: + Enabled: false +Lint/UnexpectedBlockArity: + Enabled: false +Lint/UnmodifiedReduceAccumulator: + Enabled: false +Performance/CollectionLiteralInLoop: + Enabled: false +Style/ArgumentsForwarding: + Enabled: false +Style/CollectionCompact: + Enabled: false +Style/DocumentDynamicEvalDefinition: + Enabled: false +Style/NegatedIfElseCondition: + Enabled: false +Style/NilLambda: + Enabled: false +Style/RedundantArgument: + Enabled: false +Style/SwapValues: + Enabled: false diff --git a/.sync.yml b/.sync.yml new file mode 100644 index 0000000..8c2c98e --- /dev/null +++ b/.sync.yml @@ -0,0 +1,8 @@ +# This file can be used to customize the files managed by PDK. +# +# See https://github.com/puppetlabs/pdk-templates/blob/main/README.md +# for more information. +# +# See https://github.com/puppetlabs/pdk-templates/blob/main/config_defaults.yml +# for the default values. +--- {} diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..444e17a --- /dev/null +++ b/.travis.yml @@ -0,0 +1,45 @@ +--- +os: linux +dist: xenial +language: ruby +cache: bundler +before_install: + - bundle -v + - rm -f Gemfile.lock + - "# Update system gems if requested. This is useful to temporarily workaround troubles in the test runner" + - "# See https://github.com/puppetlabs/pdk-templates/commit/705154d5c437796b821691b707156e1b056d244f for an example of how this was used" + - "# Ignore exit code of SIGPIPE'd yes to not fail with shell's pipefail set" + - '[ -z "$RUBYGEMS_VERSION" ] || (yes || true) | gem update --system $RUBYGEMS_VERSION' + - gem --version + - bundle -v +script: + - 'bundle exec rake $CHECK' +bundler_args: --without system_tests +rvm: + - 2.5.7 +stages: + - static + - spec + - acceptance + - + if: tag =~ ^v\d + name: deploy +jobs: + fast_finish: true + include: + - + env: CHECK="validate lint check rubocop" + stage: static + - + env: PUPPET_GEM_VERSION="~> 6.0" CHECK=parallel_spec + rvm: 2.5.7 + stage: spec + - + env: DEPLOY_TO_FORGE=yes + stage: deploy +branches: + only: + - main + - /^v\d/ +notifications: + email: false diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..2f1e4f7 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "puppet.puppet-vscode", + "rebornix.Ruby" + ] +} diff --git a/.yardopts b/.yardopts new file mode 100644 index 0000000..29c933b --- /dev/null +++ b/.yardopts @@ -0,0 +1 @@ +--markup markdown diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..066e4da --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,11 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +## Release 2.2.0 + +**Features** + +**Bugfixes** + +**Known Issues** diff --git a/Gemfile b/Gemfile index 8de3dac..4eaa0c3 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,72 @@ -source :rubygems +source ENV['GEM_SOURCE'] || 'https://rubygems.org' -gem 'puppet' -gem 'facter' -gem 'rspec-puppet' -gem 'rake' +def location_for(place_or_version, fake_version = nil) + git_url_regex = %r{\A(?(https?|git)[:@][^#]*)(#(?.*))?} + file_url_regex = %r{\Afile:\/\/(?.*)} + + if place_or_version && (git_url = place_or_version.match(git_url_regex)) + [fake_version, { git: git_url[:url], branch: git_url[:branch], require: false }].compact + elsif place_or_version && (file_url = place_or_version.match(file_url_regex)) + ['>= 0', { path: File.expand_path(file_url[:path]), require: false }] + else + [place_or_version, { require: false }] + end +end + +group :development do + gem "json", '= 2.1.0', require: false if Gem::Requirement.create(['>= 2.5.0', '< 2.7.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "json", '= 2.3.0', require: false if Gem::Requirement.create(['>= 2.7.0', '< 3.0.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "json", '= 2.5.1', require: false if Gem::Requirement.create(['>= 3.0.0', '< 3.0.5']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "json", '= 2.6.1', require: false if Gem::Requirement.create(['>= 3.1.0', '< 3.1.3']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "json", '= 2.6.3', require: false if Gem::Requirement.create(['>= 3.2.0', '< 4.0.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "voxpupuli-puppet-lint-plugins", '~> 4.0', require: false + gem "facterdb", '~> 1.18', require: false + gem "metadata-json-lint", '>= 2.0.2', '< 4.0.0', require: false + gem "puppetlabs_spec_helper", '~> 5.0', require: false + gem "rspec-puppet-facts", '~> 2.0', require: false + gem "codecov", '~> 0.2', require: false + gem "dependency_checker", '~> 0.2', require: false + gem "parallel_tests", '= 3.12.1', require: false + gem "pry", '~> 0.10', require: false + gem "simplecov-console", '~> 0.5', require: false + gem "puppet-debugger", '~> 1.0', require: false + gem "rubocop", '= 1.6.1', require: false + gem "rubocop-performance", '= 1.9.1', require: false + gem "rubocop-rspec", '= 2.0.1', require: false + gem "rb-readline", '= 0.5.5', require: false, platforms: [:mswin, :mingw, :x64_mingw] +end +group :system_tests do + gem "puppet_litmus", '< 1.0.0', require: false, platforms: [:ruby, :x64_mingw] + gem "serverspec", '~> 2.41', require: false +end + +puppet_version = ENV['PUPPET_GEM_VERSION'] +facter_version = ENV['FACTER_GEM_VERSION'] +hiera_version = ENV['HIERA_GEM_VERSION'] + +gems = {} + +gems['puppet'] = location_for(puppet_version) + +# If facter or hiera versions have been specified via the environment +# variables + +gems['facter'] = location_for(facter_version) if facter_version +gems['hiera'] = location_for(hiera_version) if hiera_version + +gems.each do |gem_name, gem_params| + gem gem_name, *gem_params +end + +# Evaluate Gemfile.local and ~/.gemfile if they exist +extra_gemfiles = [ + "#{__FILE__}.local", + File.join(Dir.home, '.gemfile'), +] + +extra_gemfiles.each do |gemfile| + if File.file?(gemfile) && File.readable?(gemfile) + eval(File.read(gemfile), binding) + end +end +# vim: syntax=ruby diff --git a/Gemfile.lock b/Gemfile.lock index 482dcc1..994dcd8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,42 +1,462 @@ GEM - remote: http://rubygems.org/ + remote: https://rubygems.org/ specs: - CFPropertyList (2.2.8) - diff-lcs (1.3) - facter (2.4.6) - CFPropertyList (~> 2.2.6) - hiera (3.2.2) - json_pure (1.8.6) - puppet (4.8.2) - CFPropertyList (~> 2.2.6) - facter (> 2.0, < 4) - hiera (>= 2.0, < 4) - json_pure (~> 1.8) - rake (12.0.0) - rspec (3.5.0) - rspec-core (~> 3.5.0) - rspec-expectations (~> 3.5.0) - rspec-mocks (~> 3.5.0) - rspec-core (3.5.4) - rspec-support (~> 3.5.0) - rspec-expectations (3.5.0) + CFPropertyList (2.3.6) + addressable (2.8.6) + public_suffix (>= 2.0.2, < 6.0) + ansi (1.5.0) + ast (2.4.2) + awesome_print (1.9.2) + aws-eventstream (1.3.0) + aws-partitions (1.881.0) + aws-sdk-core (3.190.3) + aws-eventstream (~> 1, >= 1.3.0) + aws-partitions (~> 1, >= 1.651.0) + aws-sigv4 (~> 1.8) + jmespath (~> 1, >= 1.6.1) + aws-sdk-ec2 (1.433.0) + aws-sdk-core (~> 3, >= 3.188.0) + aws-sigv4 (~> 1.1) + aws-sigv4 (1.8.0) + aws-eventstream (~> 1, >= 1.0.2) + bindata (2.4.15) + bolt (3.27.4) + CFPropertyList (>= 2.2) + addressable (~> 2.5) + aws-sdk-ec2 (~> 1) + concurrent-ruby (~> 1.0, < 1.2.0) + ffi (>= 1.9.25, < 2.0.0) + hiera-eyaml (~> 3) + jwt (~> 2.2) + logging (~> 2.2) + minitar (~> 0.6) + net-scp (>= 1.2, < 5.0) + net-ssh (>= 4.0, < 8.0) + net-ssh-krb (~> 0.5) + orchestrator_client (~> 0.5) + puppet (>= 6.18.0) + puppet-resource_api (>= 1.8.1) + puppet-strings (>= 2.3.0, < 5.0) + puppetfile-resolver (>= 0.6.2, < 1.0) + r10k (~> 3.10) + ruby_smb (~> 1.0) + terminal-table (~> 3.0) + winrm (~> 2.0) + winrm-fs (~> 1.3) + builder (3.2.4) + codecov (0.6.0) + simplecov (>= 0.15, < 0.22) + coderay (1.1.3) + colored2 (3.1.2) + concurrent-ruby (1.1.10) + connection_pool (2.4.1) + cri (2.15.11) + deep_merge (1.2.2) + dependency_checker (0.3.0) + parallel + puppet_forge (>= 2.2, < 4.0) + rake (~> 13.0) + semantic_puppet (~> 1.0) + diff-lcs (1.5.0) + docile (1.4.0) + docker-api (2.2.0) + excon (>= 0.47.0) + multi_json + domain_name (0.6.20240107) + erubi (1.12.0) + excon (0.109.0) + facter (4.5.1) + hocon (~> 1.3) + thor (>= 1.0.1, < 2.0) + facterdb (1.23.0) + facter (< 5.0.0) + jgrep + faraday (1.10.3) + faraday-em_http (~> 1.0) + faraday-em_synchrony (~> 1.0) + faraday-excon (~> 1.1) + faraday-httpclient (~> 1.0) + faraday-multipart (~> 1.0) + faraday-net_http (~> 1.0) + faraday-net_http_persistent (~> 1.0) + faraday-patron (~> 1.0) + faraday-rack (~> 1.0) + faraday-retry (~> 1.0) + ruby2_keywords (>= 0.0.4) + faraday-em_http (1.0.0) + faraday-em_synchrony (1.0.0) + faraday-excon (1.1.0) + faraday-httpclient (1.0.1) + faraday-multipart (1.0.4) + multipart-post (~> 2) + faraday-net_http (1.0.1) + faraday-net_http_persistent (1.2.0) + faraday-patron (1.0.0) + faraday-rack (1.0.0) + faraday-retry (1.0.3) + faraday_middleware (1.2.0) + faraday (~> 1.0) + fast_gettext (2.3.0) + ffi (1.16.3) + ffi-compiler (1.0.1) + ffi (>= 1.0.0) + rake + forwardable (1.3.3) + gettext (3.4.9) + erubi + locale (>= 2.0.5) + prime + racc + text (>= 1.3.0) + gettext-setup (1.1.0) + fast_gettext (~> 2.1) + gettext (~> 3.4) + locale + gssapi (1.3.1) + ffi (>= 1.0.1) + gyoku (1.4.0) + builder (>= 2.1.2) + rexml (~> 3.0) + hiera (3.12.0) + hiera-eyaml (3.4.0) + highline + optimist + highline (2.1.0) + hocon (1.4.0) + honeycomb-beeline (3.0.1) + libhoney (>= 1.14.2) + http (5.1.1) + addressable (~> 2.8) + http-cookie (~> 1.0) + http-form_data (~> 2.2) + llhttp-ffi (~> 0.4.0) + http-cookie (1.0.5) + domain_name (~> 0.5) + http-form_data (2.3.0) + httpclient (2.8.3) + jgrep (1.5.4) + jmespath (1.6.2) + json (2.3.0) + json-schema (4.1.1) + addressable (>= 2.8) + jwt (2.7.1) + libhoney (2.2.0) + addressable (~> 2.0) + excon + http (>= 2.0, < 6.0) + little-plugger (1.1.4) + llhttp-ffi (0.4.0) + ffi-compiler (~> 1.0) + rake (~> 13.0) + locale (2.1.3) + log4r (1.1.10) + logging (2.3.1) + little-plugger (~> 1.1) + multi_json (~> 1.14) + metadata-json-lint (3.0.3) + json-schema (>= 2.8, < 5.0) + spdx-licenses (~> 1.0) + method_source (1.0.0) + minitar (0.9) + mocha (1.16.1) + molinillo (0.8.0) + multi_json (1.15.0) + multipart-post (2.3.0) + net-http-persistent (4.0.2) + connection_pool (~> 2.2) + net-scp (4.0.0) + net-ssh (>= 2.6.5, < 8.0.0) + net-ssh (7.2.1) + net-ssh-krb (0.5.1) + gssapi (~> 1.3.0) + net-ssh (>= 2.0) + net-telnet (0.1.1) + nori (2.6.0) + optimist (3.1.0) + orchestrator_client (0.7.0) + faraday (~> 1.4) + net-http-persistent (~> 4.0) + parallel (1.24.0) + parallel_tests (3.12.1) + parallel + parser (3.3.0.4) + ast (~> 2.4.1) + racc + pathspec (1.1.3) + pluginator (1.5.0) + prime (0.1.2) + forwardable + singleton + pry (0.14.2) + coderay (~> 1.1) + method_source (~> 1.0) + public_suffix (5.0.4) + puppet (7.23.0-universal-darwin) + CFPropertyList (~> 2.2) + concurrent-ruby (~> 1.0, < 1.2.0) + deep_merge (~> 1.0) + facter (> 2.0.1, < 5) + fast_gettext (>= 1.1, < 3) + hiera (>= 3.2.1, < 4) + locale (~> 2.1) + multi_json (~> 1.10) + puppet-resource_api (~> 1.5) + scanf (~> 1.0) + semantic_puppet (~> 1.0) + puppet-debugger (1.3.0) + awesome_print (~> 1.7) + bundler + facterdb (>= 0.4.0) + pluginator (~> 1.5.0) + puppet (>= 5.5) + rb-readline (>= 0.5.5) + table_print (>= 1.0.0) + tty-pager (~> 0.13) + puppet-lint (3.4.0) + puppet-lint-absolute_classname-check (3.1.0) + puppet-lint (>= 1.0, < 4) + puppet-lint-anchor-check (1.1.0) + puppet-lint (>= 1.1, < 4) + puppet-lint-file_ensure-check (1.1.0) + puppet-lint (>= 1.0, < 4) + puppet-lint-leading_zero-check (1.1.0) + puppet-lint (>= 1.0, < 4.0) + puppet-lint-lookup_in_parameter-check (1.1.0) + puppet-lint (>= 2.0, < 4) + puppet-lint-manifest_whitespace-check (0.2.9) + puppet-lint (>= 1.0, < 4) + puppet-lint-optional_default-check (1.1.0) + puppet-lint (>= 2.1, < 4) + puppet-lint-param-docs (1.7.6) + puppet-lint (>= 1.1, < 4.0) + puppet-lint-param-types (1.0.0) + puppet-lint (>= 1.1, < 4) + puppet-lint-params_empty_string-check (1.1.0) + puppet-lint (>= 2.5, < 4) + puppet-lint-resource_reference_syntax (1.2.0) + puppet-lint (>= 1.0, < 4) + puppet-lint-strict_indent-check (2.1.0) + puppet-lint (>= 1.0, < 4) + puppet-lint-topscope-variable-check (1.2.0) + puppet-lint (>= 2.0, < 4) + puppet-lint-trailing_comma-check (1.0.0) + puppet-lint (>= 1.0, < 4) + puppet-lint-unquoted_string-check (2.2.0) + puppet-lint (>= 2.1, < 4) + puppet-lint-variable_contains_upcase (1.4.0) + puppet-lint (>= 1.0, < 5) + puppet-lint-version_comparison-check (1.1.0) + puppet-lint (>= 1.0, < 4) + puppet-modulebuilder (0.3.0) + minitar (~> 0.9) + pathspec (>= 0.2.1, < 2.0.0) + puppet-resource_api (1.9.0) + hocon (>= 1.0) + puppet-strings (4.1.0) + rgen (~> 0.9) + yard (~> 0.9) + puppet-syntax (3.3.0) + puppet (>= 5) + rake + puppet_forge (3.2.0) + faraday (~> 1.3) + faraday_middleware (~> 1.0) + minitar + semantic_puppet (~> 1.0) + puppet_litmus (0.36.2) + bolt (~> 3.0) + docker-api (>= 1.34, < 3.0.0) + honeycomb-beeline + parallel + puppet-modulebuilder (>= 0.2.1, < 1.0.0) + retryable (~> 3.0) + rspec + rspec_honeycomb_formatter + tty-spinner (>= 0.5.0, < 1.0.0) + puppetfile-resolver (0.6.2) + molinillo (~> 0.6) + semantic_puppet (~> 1.0) + puppetlabs_spec_helper (5.0.3) + mocha (~> 1.0) + pathspec (>= 0.2, < 2.0.0) + puppet-lint (>= 2.5.2, < 4.0.0) + puppet-syntax (~> 3.0) + rspec-github (~> 2.0) + rspec-puppet (~> 2.0) + r10k (3.16.0) + colored2 (= 3.1.2) + cri (>= 2.15.10) + fast_gettext (>= 1.1.0, < 3.0.0) + gettext (>= 3.0.2, < 4.0.0) + gettext-setup (>= 0.24, < 2.0.0) + jwt (>= 2.2.3, < 2.8.0) + log4r (= 1.1.10) + minitar (~> 0.9) + multi_json (~> 1.10) + puppet_forge (>= 2.3.0, < 4.0.0) + racc (1.7.3) + rainbow (3.1.1) + rake (13.1.0) + rb-readline (0.5.5) + regexp_parser (2.9.0) + retryable (3.0.5) + rexml (3.2.6) + rgen (0.9.1) + rspec (3.12.0) + rspec-core (~> 3.12.0) + rspec-expectations (~> 3.12.0) + rspec-mocks (~> 3.12.0) + rspec-core (3.12.2) + rspec-support (~> 3.12.0) + rspec-expectations (3.12.3) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.5.0) - rspec-mocks (3.5.0) + rspec-support (~> 3.12.0) + rspec-github (2.4.0) + rspec-core (~> 3.0) + rspec-its (1.3.0) + rspec-core (>= 3.0.0) + rspec-expectations (>= 3.0.0) + rspec-mocks (3.12.6) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.5.0) - rspec-puppet (2.5.0) + rspec-support (~> 3.12.0) + rspec-puppet (2.12.0) rspec - rspec-support (3.5.0) + rspec-puppet-facts (2.0.5) + facter + facterdb (>= 0.5.0) + puppet + rspec-support (3.12.1) + rspec_honeycomb_formatter (0.2.1) + honeycomb-beeline + rspec-core (~> 3.0) + rubocop (1.6.1) + parallel (~> 1.10) + parser (>= 2.7.1.5) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml + rubocop-ast (>= 1.2.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 2.0) + rubocop-ast (1.30.0) + parser (>= 3.2.1.0) + rubocop-performance (1.9.1) + rubocop (>= 0.90.0, < 2.0) + rubocop-ast (>= 0.4.0) + rubocop-rspec (2.0.1) + rubocop (~> 1.0) + rubocop-ast (>= 1.1.0) + ruby-progressbar (1.13.0) + ruby2_keywords (0.0.5) + ruby_smb (1.1.0) + bindata + rubyntlm + windows_error + rubyntlm (0.6.3) + rubyzip (2.3.2) + scanf (1.0.0) + semantic_puppet (1.1.0) + serverspec (2.42.3) + multi_json + rspec (~> 3.0) + rspec-its + specinfra (~> 2.72) + sfl (2.3) + simplecov (0.21.2) + docile (~> 1.1) + simplecov-html (~> 0.11) + simplecov_json_formatter (~> 0.1) + simplecov-console (0.9.1) + ansi + simplecov + terminal-table + simplecov-html (0.12.3) + simplecov_json_formatter (0.1.4) + singleton (0.2.0) + spdx-licenses (1.3.0) + specinfra (2.87.0) + net-scp + net-ssh (>= 2.7) + net-telnet (= 0.1.1) + sfl + strings (0.2.1) + strings-ansi (~> 0.2) + unicode-display_width (>= 1.5, < 3.0) + unicode_utils (~> 1.4) + strings-ansi (0.2.0) + table_print (1.5.7) + terminal-table (3.0.2) + unicode-display_width (>= 1.1.1, < 3) + text (1.3.1) + thor (1.3.0) + tty-cursor (0.7.1) + tty-pager (0.14.0) + strings (~> 0.2.0) + tty-screen (~> 0.8) + tty-screen (0.8.2) + tty-spinner (0.9.3) + tty-cursor (~> 0.7) + unicode-display_width (1.8.0) + unicode_utils (1.4.0) + voxpupuli-puppet-lint-plugins (4.0.0) + puppet-lint (~> 3.1) + puppet-lint-absolute_classname-check (~> 3.1) + puppet-lint-anchor-check (~> 1.1) + puppet-lint-file_ensure-check (~> 1.1) + puppet-lint-leading_zero-check (~> 1.0) + puppet-lint-lookup_in_parameter-check (~> 1.0) + puppet-lint-manifest_whitespace-check (~> 0.2.7, < 1.0.0) + puppet-lint-optional_default-check (~> 1.1) + puppet-lint-param-docs (>= 1.7.6, < 2.0.0) + puppet-lint-param-types (~> 1.0) + puppet-lint-params_empty_string-check (~> 1.0) + puppet-lint-resource_reference_syntax (~> 1.1) + puppet-lint-strict_indent-check (~> 2.1) + puppet-lint-topscope-variable-check (~> 1.1) + puppet-lint-trailing_comma-check (~> 1.0) + puppet-lint-unquoted_string-check (~> 2.2) + puppet-lint-variable_contains_upcase (~> 1.2) + puppet-lint-version_comparison-check (~> 1.1) + windows_error (0.1.5) + winrm (2.3.6) + builder (>= 2.1.2) + erubi (~> 1.8) + gssapi (~> 1.2) + gyoku (~> 1.0) + httpclient (~> 2.2, >= 2.2.0.2) + logging (>= 1.6.1, < 3.0) + nori (~> 2.0) + rubyntlm (~> 0.6.0, >= 0.6.3) + winrm-fs (1.3.5) + erubi (~> 1.8) + logging (>= 1.6.1, < 3.0) + rubyzip (~> 2.0) + winrm (~> 2.0) + yard (0.9.34) PLATFORMS - ruby + x86_64-darwin-21 DEPENDENCIES - facter - puppet - rake - rspec-puppet + codecov (~> 0.2) + dependency_checker (~> 0.2) + facterdb (~> 1.18) + json (= 2.3.0) + metadata-json-lint (>= 2.0.2, < 4.0.0) + parallel_tests (= 3.12.1) + pry (~> 0.10) + puppet (= 7.23.0) + puppet-debugger (~> 1.0) + puppet_litmus (< 1.0.0) + puppetlabs_spec_helper (~> 5.0) + rb-readline (= 0.5.5) + rspec-puppet-facts (~> 2.0) + rubocop (= 1.6.1) + rubocop-performance (= 1.9.1) + rubocop-rspec (= 2.0.1) + serverspec (~> 2.41) + simplecov-console (~> 0.5) + voxpupuli-puppet-lint-plugins (~> 4.0) BUNDLED WITH - 1.13.6 + 2.3.26 diff --git a/README.md b/README.md index 35cc3b9..322981f 100644 --- a/README.md +++ b/README.md @@ -94,3 +94,73 @@ such subnet in the given IP/CIDR ip_supernet returns the IP/CIDR of size `$supernet_prefixlength` which contains `$cidr` + +### `ip_range_size($cidr_1, $cidr_2)` + +ip_range_size takes a starting IP/CIDR and an ending IP/CIDR, and returns the +number of contiguious addresses between them, inclusive of the starting and +ending addresses. Network size, network or broadcast addresses are NOT taken +into account, only the number of addresses of any type between start and end of +the range. + +## Comparison Functions + +The following functions are designed for use in comparing IP/CIDRs. Unlike the +functions documented above, these are namespaced in this module and implemented +in the v4 API. + +### `ipcalc::ip_compare($left_cidr, $right_cidr)` + +ipcalc::ip_compare is designed to be used with Puppet's built-in `sort()` +function. It takes two IP/CIDRs, and returns: +- `-1` if the left IP/CIDR is smaller than the right +- `0` if the left and right IP/CIDRs are equal +- `1` if the left IP/CIDR is greater than the right + +When used in the optional block that can be passed to `sort()` (see examples in +the function's documentation), this allows `sort()` to sort an Array of IP +addresses into native order instead of string order - i.e., `1.1.1.2` will sort +lower than `1.1.1.10`, where string sorting would instead compare the first +seven characters, see `1.1.1.2` as greater than `1.1.1.1`, and sort them into +the wrong order. + +### `ipcalc::greater_than($left_cidr, $right_cidr)` + +Return `true` if the left IP/CIDR is greater than the right. This makes the +most sense when using Puppet's dotted function notation: +``` +$left_ip.ipcalc::greater_than($right_ip) +``` + +### `ipcalc::equal_or_greater_than($left_cidr, $right_cidr)` + +Return `true` if the left IP/CIDR is equal-to-or-greater-than the right. This +makes the most sense when using Puppet's dotted function notation: +``` +$left_ip.ipcalc::equal_or_greater_than($right_ip) +``` + +### `ipcalc::equal_to($left_cidr, $right_cidr)` + +Return `true` if the left and right IP/CIDRs are equal. This makes the most +sense when using Puppet's dotted function notation: +``` +$left_ip.ipcalc::less_than($right_ip) +``` + +### `ipcalc::equal_or_less_than($left_cidr, $right_cidr)` + +Return `true` if the left IP/CIDR is equal-to-or-less-than the right. This +makes the most sense when using Puppet's dotted function notation: +``` +$left_ip.ipcalc::equal_or_less_than($right_ip) +``` + +### `ipcalc::less_than($left_cidr, $right_cidr)` + +Return `true` if the left IP/CIDR is less than the right. This makes the most +sense when using Puppet's dotted function notation: +``` +$left_ip.ipcalc::less_than($right_ip) +``` + diff --git a/Rakefile b/Rakefile index a55b932..0f8754e 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,89 @@ -require 'rake' -require 'rspec/core/rake_task' +# frozen_string_literal: true -RSpec::Core::RakeTask.new(:spec) do |t| - t.pattern = 'spec/*/*_spec.rb' +require 'bundler' +require 'puppet_litmus/rake_tasks' if Bundler.rubygems.find_name('puppet_litmus').any? +require 'puppetlabs_spec_helper/rake_tasks' +require 'puppet-syntax/tasks/puppet-syntax' +require 'puppet_blacksmith/rake_tasks' if Bundler.rubygems.find_name('puppet-blacksmith').any? +require 'github_changelog_generator/task' if Bundler.rubygems.find_name('github_changelog_generator').any? +require 'puppet-strings/tasks' if Bundler.rubygems.find_name('puppet-strings').any? + +def changelog_user + return unless Rake.application.top_level_tasks.include? "changelog" + returnVal = nil || JSON.load(File.read('metadata.json'))['author'] + raise "unable to find the changelog_user in .sync.yml, or the author in metadata.json" if returnVal.nil? + puts "GitHubChangelogGenerator user:#{returnVal}" + returnVal +end + +def changelog_project + return unless Rake.application.top_level_tasks.include? "changelog" + + returnVal = nil + returnVal ||= begin + metadata_source = JSON.load(File.read('metadata.json'))['source'] + metadata_source_match = metadata_source && metadata_source.match(%r{.*\/([^\/]*?)(?:\.git)?\Z}) + + metadata_source_match && metadata_source_match[1] + end + + raise "unable to find the changelog_project in .sync.yml or calculate it from the source in metadata.json" if returnVal.nil? + + puts "GitHubChangelogGenerator project:#{returnVal}" + returnVal +end + +def changelog_future_release + return unless Rake.application.top_level_tasks.include? "changelog" + returnVal = "v%s" % JSON.load(File.read('metadata.json'))['version'] + raise "unable to find the future_release (version) in metadata.json" if returnVal.nil? + puts "GitHubChangelogGenerator future_release:#{returnVal}" + returnVal +end + +PuppetLint.configuration.send('disable_relative') + + +if Bundler.rubygems.find_name('github_changelog_generator').any? + GitHubChangelogGenerator::RakeTask.new :changelog do |config| + raise "Set CHANGELOG_GITHUB_TOKEN environment variable eg 'export CHANGELOG_GITHUB_TOKEN=valid_token_here'" if Rake.application.top_level_tasks.include? "changelog" and ENV['CHANGELOG_GITHUB_TOKEN'].nil? + config.user = "#{changelog_user}" + config.project = "#{changelog_project}" + config.future_release = "#{changelog_future_release}" + config.exclude_labels = ['maintenance'] + config.header = "# Change log\n\nAll notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org)." + config.add_pr_wo_labels = true + config.issues = false + config.merge_prefix = "### UNCATEGORIZED PRS; LABEL THEM ON GITHUB" + config.configure_sections = { + "Changed" => { + "prefix" => "### Changed", + "labels" => ["backwards-incompatible"], + }, + "Added" => { + "prefix" => "### Added", + "labels" => ["enhancement", "feature"], + }, + "Fixed" => { + "prefix" => "### Fixed", + "labels" => ["bug", "documentation", "bugfix"], + }, + } + end +else + desc 'Generate a Changelog from GitHub' + task :changelog do + raise < 1.15' + condition: "Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.3.0')" +EOM + end end -task :default => :spec diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..a70c01e --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,49 @@ +--- +version: 1.1.x.{build} +skip_branch_with_pr: true +branches: + only: + - main + - release +skip_commits: + message: /^\(?doc\)?.*/ +clone_depth: 10 +init: + - SET + - 'mkdir C:\ProgramData\PuppetLabs\code && exit 0' + - 'mkdir C:\ProgramData\PuppetLabs\facter && exit 0' + - 'mkdir C:\ProgramData\PuppetLabs\hiera && exit 0' + - 'mkdir C:\ProgramData\PuppetLabs\puppet\var && exit 0' +environment: + matrix: + - + RUBY_VERSION: 25-x64 + CHECK: validate lint check rubocop + - + PUPPET_GEM_VERSION: ~> 6.0 + RUBY_VERSION: 25 + CHECK: parallel_spec + - + PUPPET_GEM_VERSION: ~> 6.0 + RUBY_VERSION: 25-x64 + CHECK: parallel_spec +matrix: + fast_finish: true +install: + - set PATH=C:\Ruby%RUBY_VERSION%\bin;%PATH% + - bundle install --jobs 4 --retry 2 --without system_tests + - type Gemfile.lock +build: off +test_script: + - bundle exec puppet -V + - ruby -v + - gem -v + - bundle -v + - bundle exec rake %CHECK% +notifications: + - provider: Email + to: + - nobody@nowhere.com + on_build_success: false + on_build_failure: false + on_build_status_changed: false diff --git a/data/common.yaml b/data/common.yaml new file mode 100644 index 0000000..2fbf0ff --- /dev/null +++ b/data/common.yaml @@ -0,0 +1 @@ +--- {} diff --git a/hiera.yaml b/hiera.yaml new file mode 100644 index 0000000..545fff3 --- /dev/null +++ b/hiera.yaml @@ -0,0 +1,21 @@ +--- +version: 5 + +defaults: # Used for any hierarchy level that omits these keys. + datadir: data # This path is relative to hiera.yaml's directory. + data_hash: yaml_data # Use the built-in YAML backend. + +hierarchy: + - name: "osfamily/major release" + paths: + # Used to distinguish between Debian and Ubuntu + - "os/%{facts.os.name}/%{facts.os.release.major}.yaml" + - "os/%{facts.os.family}/%{facts.os.release.major}.yaml" + # Used for Solaris + - "os/%{facts.os.family}/%{facts.kernelrelease}.yaml" + - name: "osfamily" + paths: + - "os/%{facts.os.name}.yaml" + - "os/%{facts.os.family}.yaml" + - name: 'common' + path: 'common.yaml' diff --git a/lib/puppet/functions/ipcalc/equal_or_greater_than.rb b/lib/puppet/functions/ipcalc/equal_or_greater_than.rb new file mode 100644 index 0000000..9df6833 --- /dev/null +++ b/lib/puppet/functions/ipcalc/equal_or_greater_than.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require 'ipaddr' + +Puppet::Functions.create_function(:"ipcalc::equal_or_greater_than") do + # Compare two IP addresses and return a Boolean indicating whether the left operand is equal to + # (or possibly greater than) the right. + # + # This will work with either plain addresses or CIDR-notation addresses. If a plain address is + # supplied, a full-width netmask is assumed; that is, '127.0.0.1' and '127.0.0.1/32' are + # equivalent, just as 'fe80::1' and 'fe80::1/128' are equivalent. The netmask is also assessed if + # all bits of the address are equivalent; an address with a longer prefix is considered "larger." + # If addresses of mixed families are given, an error is raised and the catalog will fail. There + # is no obvious implied relation between the two, and even the underlying Ruby implementation will + # refuse to make comparisons between addresses of different families. + # + # @param left + # The left address for comparison. If no netmask is given, full-width is assumed. + # @param right + # The right address for comparison. If no netmask is given, full-width is assumed. + # @return [Boolean] + # `true` if the left operand is equal to (or greater than) the right, or `false` if it's less + # than the right. + # @example Use with dotted function notation + # $left_ip.equal_or_greater_than($right_ip) ? { + # true => { 'We did it, go team, left operand is equal or bigger' }, + # default => { 'I\'m afraid I have some bad news...' }, + # } + dispatch :equal_or_greater_than do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V4', :right + return_type 'Boolean' + end + + dispatch :equal_or_greater_than do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V6', :right + return_type 'Boolean' + end + + # Refuse to handle addresses in different families. There isn't a clear precedence between the + # two families; if you need to establish an order, you can use type comparison to handle this in + # your Puppet code. + argument_mismatch :mixed_families do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V6', :right + end + + argument_mismatch :mixed_families do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V4', :right + end + + def normalize_addr(addr) + # When initializing a new IPAddr, if a prefix length is given, the returned IPAddr will be the + # base network address and the subnet mask for that network, not the address that was passed + # in. If we want to compare the full address, we need to separate the prefix and initialize a + # new IPAddr with the address alone, and then separately create an IPAddr instance to represent + # the network address. + (address, prefix) = addr.split('/') + ip_address = IPAddr.new(address) + network_address = prefix ? IPAddr.new(addr) : ip_address + [ip_address, network_address] + end + + def equal_or_greater_than(left, right) + (left_addr, left_netaddr) = normalize_addr(left) + (right_addr, right_netaddr) = normalize_addr(right) + + if left_addr == right_addr + left_netaddr.prefix >= right_netaddr.prefix + else + left_addr >= right_addr + end + end + + def mixed_families(*) + 'both addresses must be in the same family' + end +end diff --git a/lib/puppet/functions/ipcalc/equal_or_less_than.rb b/lib/puppet/functions/ipcalc/equal_or_less_than.rb new file mode 100644 index 0000000..3cdcb7f --- /dev/null +++ b/lib/puppet/functions/ipcalc/equal_or_less_than.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require 'ipaddr' + +Puppet::Functions.create_function(:"ipcalc::equal_or_less_than") do + # Compare two IP addresses and return a Boolean indicating whether the left operand is equal to + # (or possibly less than) the right. + # + # This will work with either plain addresses or CIDR-notation addresses. If a plain address is + # supplied, a full-width netmask is assumed; that is, '127.0.0.1' and '127.0.0.1/32' are + # equivalent, just as 'fe80::1' and 'fe80::1/128' are equivalent. The netmask is also assessed if + # all bits of the address are equivalent; an address with a longer prefix is considered "larger." + # If addresses of mixed families are given, an error is raised and the catalog will fail. There + # is no obvious implied relation between the two, and even the underlying Ruby implementation will + # refuse to make comparisons between addresses of different families. + # + # @param left + # The left address for comparison. If no netmask is given, full-width is assumed. + # @param right + # The right address for comparison. If no netmask is given, full-width is assumed. + # @return [Boolean] + # `true` if the left operand is equal to (or less than) the right, or `false` if it's greater + # than the right. + # @example Use with dotted function notation + # $left_ip.equal_or_less_than($right_ip) ? { + # true => { 'We did it, go team, left operand is equal or smaller' }, + # default => { 'I\'m afraid I have some bad news...' }, + # } + dispatch :equal_or_less_than do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V4', :right + return_type 'Boolean' + end + + dispatch :equal_or_less_than do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V6', :right + return_type 'Boolean' + end + + # Refuse to handle addresses in different families. There isn't a clear precedence between the + # two families; if you need to establish an order, you can use type comparison to handle this in + # your Puppet code. + argument_mismatch :mixed_families do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V6', :right + end + + argument_mismatch :mixed_families do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V4', :right + end + + def normalize_addr(addr) + # When initializing a new IPAddr, if a prefix length is given, the returned IPAddr will be the + # base network address and the subnet mask for that network, not the address that was passed + # in. If we want to compare the full address, we need to separate the prefix and initialize a + # new IPAddr with the address alone, and then separately create an IPAddr instance to represent + # the network address. + (address, prefix) = addr.split('/') + ip_address = IPAddr.new(address) + network_address = prefix ? IPAddr.new(addr) : ip_address + [ip_address, network_address] + end + + def equal_or_less_than(left, right) + (left_addr, left_netaddr) = normalize_addr(left) + (right_addr, right_netaddr) = normalize_addr(right) + + if left_addr == right_addr + left_netaddr.prefix <= right_netaddr.prefix + else + left_addr <= right_addr + end + end + + def mixed_families(*) + 'both addresses must be in the same family' + end +end diff --git a/lib/puppet/functions/ipcalc/equal_to.rb b/lib/puppet/functions/ipcalc/equal_to.rb new file mode 100644 index 0000000..a5a1975 --- /dev/null +++ b/lib/puppet/functions/ipcalc/equal_to.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +require 'ipaddr' + +Puppet::Functions.create_function(:"ipcalc::equal_to") do + # Compare two IP addresses and return a Boolean indicating whether the left operand is equal to + # the right. This differs from comparing the address Strings in that it compares native 32-bit + # (or for IPv6, 128-bit) binary values, regardless of their human-readable representations. For + # IPv4 addresses this is usually not much of a problem, as representations are simpler and + # zero-padded values are relatively rare (enough that Stdlib::IP::Address::V4 filters them out as + # invalid). For IPv6 addresses, however, IPv6's collapsed notation can be a very serious problem, + # and this function allows IPv6 addresses with un-collapsed runs of zeroes, leading zeroes, etc. + # to be compared meaningfully without needing to worry about normalizing their human-readable + # representations first. + # + # This will work with either plain addresses or CIDR-notation addresses. If a plain address is + # supplied, a full-width netmask is assumed; that is, '127.0.0.1' and '127.0.0.1/32' are + # equivalent, just as 'fe80::1' and 'fe80::1/128' are equivalent. The netmask is also assessed if + # all bits of the address are equivalent; an address with a longer prefix is considered "larger." + # Addresses of mixed families are always considered unequal. + # + # @param left + # The left address for comparison. If no netmask is given, full-width is assumed. + # @param right + # The right address for comparison. If no netmask is given, full-width is assumed. + # @return [Boolean] + # `true` if the operands are equal, or `false` otherwise. + # @example Use with dotted function notation + # $left_ip.equal_to($right_ip) ? { + # true => { 'We did it, go team, the operands are equal' }, + # default => { 'I\'m afraid I have some bad news...' }, + # } + dispatch :equal_to do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V4', :right + return_type 'Boolean' + end + + dispatch :equal_to do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V6', :right + return_type 'Boolean' + end + + # We always consider mixed address families to be unequal. + dispatch :mixed_families do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V6', :right + end + + dispatch :mixed_families do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V4', :right + end + + def normalize_addr(addr) + # When initializing a new IPAddr, if a prefix length is given, the returned IPAddr will be the + # base network address and the subnet mask for that network, not the address that was passed + # in. If we want to compare the full address, we need to separate the prefix and initialize a + # new IPAddr with the address alone, and then separately create an IPAddr instance to represent + # the network address. + (address, prefix) = addr.split('/') + ip_address = IPAddr.new(address) + network_address = prefix ? IPAddr.new(addr) : ip_address + [ip_address, network_address] + end + + def equal_to(left, right) + (left_addr, left_netaddr) = normalize_addr(left) + (right_addr, right_netaddr) = normalize_addr(right) + + if left_addr == right_addr + left_netaddr.prefix == right_netaddr.prefix + else + false + end + end + + def mixed_families(*) + false + end +end diff --git a/lib/puppet/functions/ipcalc/greater_than.rb b/lib/puppet/functions/ipcalc/greater_than.rb new file mode 100644 index 0000000..4486480 --- /dev/null +++ b/lib/puppet/functions/ipcalc/greater_than.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +require 'ipaddr' + +Puppet::Functions.create_function(:"ipcalc::greater_than") do + # Compare two IP addresses and return a Boolean indicating whether the left operand is greater + # than the right. + # + # This will work with either plain addresses or CIDR-notation addresses. If a plain address is + # supplied, a full-width netmask is assumed; that is, '127.0.0.1' and '127.0.0.1/32' are + # equivalent, just as 'fe80::1' and 'fe80::1/128' are equivalent. The netmask is also assessed if + # all bits of the address are equivalent; an address with a longer prefix is considered "larger." + # If addresses of mixed families are given, an error is raised and the catalog will fail. There + # is no obvious implied relation between the two, and even the underlying Ruby implementation will + # refuse to make comparisons between addresses of different families. + # + # @param left + # The left address for comparison. If no netmask is given, full-width is assumed. + # @param right + # The right address for comparison. If no netmask is given, full-width is assumed. + # @return [Boolean] + # `true` if the left operand is greater than the right, or `false` otherwise. + # @example Use with dotted function notation + # $left_ip.ipcalc::greater_than($right_ip) ? { + # true => { 'We did it, go team, left operand is bigger' }, + # default => { 'I\'m afraid I have some bad news...' }, + # } + dispatch :greater_than do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V4', :right + return_type 'Boolean' + end + + dispatch :greater_than do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V6', :right + return_type 'Boolean' + end + + # Refuse to handle addresses in different families. There isn't a clear precedence between the + # two families; if you need to establish an order, you can use type comparison to handle this in + # your Puppet code. + argument_mismatch :mixed_families do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V6', :right + end + + argument_mismatch :mixed_families do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V4', :right + end + + def normalize_addr(addr) + # When initializing a new IPAddr, if a prefix length is given, the returned IPAddr will be the + # base network address and the subnet mask for that network, not the address that was passed + # in. If we want to compare the full address, we need to separate the prefix and initialize a + # new IPAddr with the address alone, and then separately create an IPAddr instance to represent + # the network address. + (address, prefix) = addr.split('/') + ip_address = IPAddr.new(address) + network_address = prefix ? IPAddr.new(addr) : ip_address + [ip_address, network_address] + end + + def greater_than(left, right) + (left_addr, left_netaddr) = normalize_addr(left) + (right_addr, right_netaddr) = normalize_addr(right) + + if left_addr == right_addr + left_netaddr.prefix > right_netaddr.prefix + else + left_addr > right_addr + end + end + + def mixed_families(*) + 'both addresses must be in the same family' + end +end diff --git a/lib/puppet/functions/ipcalc/ip_compare.rb b/lib/puppet/functions/ipcalc/ip_compare.rb new file mode 100644 index 0000000..2b4f9dc --- /dev/null +++ b/lib/puppet/functions/ipcalc/ip_compare.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +require 'ipaddr' + +Puppet::Functions.create_function(:"ipcalc::ip_compare") do + # Compare two IP addresses and return a `sort()`-compatible Integer comparison result. + # + # This will work with either plain addresses or CIDR-notation addresses; if a plain address is + # supplied, a full-width netmask is assumed. That is, '127.0.0.1' and '127.0.0.1/32' are + # equivalent, just as 'fe80::1' and 'fe80::1/128' are equivalent. The netmask is also assessed if + # all bits of the address are equivalent; an address with a longer prefix is considered "larger." + # + # @param left + # The left address for comparison. If no netmask is given, a full-width mask is assumed. + # @param right + # The right address for comparison. If no netmask is given, a full-width mask is assumed. + # @return [Integer[-1, 1]] + # Either 1, 0, or -1 if the left operand is larger than, equal to, or smaller than the right, + # respectively. + # @example Usage with `sort()` + # $my_addr_array.sort |$left, $right| { + # ipcalc::ip_compare($left, $right) + # } + dispatch :ip_compare do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V4', :right + return_type 'Integer[-1, 1]' + end + + dispatch :ip_compare do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V6', :right + return_type 'Integer[-1, 1]' + end + + # We refuse to handle addresses in different families. There isn't a clear precedence between + # them, and if needed, any relative ordering required can be handled in your Puppet code by + # assessing the types handed to `sort()`. There are examples of this in the documentation for the + # Puppet `sort()` function, but a specific example for sorting all IPv6 addresses before all IPv4 + # address is included for reference. + # @example Usage with `sort()` where IPv6 sorts above IPv4 + # $my_ip_array.sort |$left, $right| { + # case [$left, $right] { + # [Stdlib::IP::Address::V6, Stdlib::IP::Address::V4]: { 1 } + # [Stdlib::IP::Address::V4, Stdlib::IP::Address::V6]: { -1 } + # default: { ipcalc::ip_compare($left, $right) } + # } + # } + argument_mismatch :mixed_families do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V6', :right + end + + argument_mismatch :mixed_families do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V4', :right + end + + def normalize_addr(addr) + # When initializing a new IPAddr, if a prefix length is given, the returned IPAddr will be the + # base network address and the subnet mask for that network, not the address that was passed + # in. If we want to compare the full address, we need to separate the prefix and initialize a + # new IPAddr with the address alone, and then separately create an IPAddr instance to represent + # the network address. + (address, prefix) = addr.split('/') + ip_address = IPAddr.new(address) + network_address = prefix ? IPAddr.new(addr) : ip_address + [ip_address, network_address] + end + + def ip_compare(left, right) + (left_addr, left_netaddr) = normalize_addr(left) + (right_addr, right_netaddr) = normalize_addr(right) + + if left_addr == right_addr + left_netaddr.prefix <=> right_netaddr.prefix + else + left_addr <=> right_addr + end + end + + def mixed_families(*) + 'both addresses must be in the same family' + end +end diff --git a/lib/puppet/functions/ipcalc/less_than.rb b/lib/puppet/functions/ipcalc/less_than.rb new file mode 100644 index 0000000..5eaa3a6 --- /dev/null +++ b/lib/puppet/functions/ipcalc/less_than.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +require 'ipaddr' + +Puppet::Functions.create_function(:"ipcalc::less_than") do + # Compare two IP addresses and return a Boolean indicating whether the left operand is less than + # the right. + # + # This will work with either plain addresses or CIDR-notation addresses. If a plain address is + # supplied, a full-width netmask is assumed; that is, '127.0.0.1' and '127.0.0.1/32' are + # equivalent, just as 'fe80::1' and 'fe80::1/128' are equivalent. The netmask is also assessed if + # all bits of the address are equivalent; an address with a longer prefix is considered "larger." + # If addresses of mixed families are given, an error is raised and the catalog will fail. There + # is no obvious implied relation between the two, and even the underlying Ruby implementation will + # refuse to make comparisons between addresses of different families. + # + # @param left + # The left address for comparison. If no netmask is given, full-width is assumed. + # @param right + # The right address for comparison. If no netmask is given, full-width is assumed. + # @return [Boolean] + # `true` if the left operand is less than the right, or `false` otherwise. + # @example Use with dotted function notation + # $left_ip.less_than($right_ip) ? { + # true => { 'We did it, go team, left operand is smaller' }, + # default => { 'I\'m afraid I have some bad news...' }, + # } + dispatch :less_than do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V4', :right + return_type 'Boolean' + end + + dispatch :less_than do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V6', :right + return_type 'Boolean' + end + + # Refuse to handle addresses in different families. There isn't a clear precedence between the + # two families; if you need to establish an order, you can use type comparison to handle this in + # your Puppet code. + argument_mismatch :mixed_families do + param 'Stdlib::IP::Address::V4', :left + param 'Stdlib::IP::Address::V6', :right + end + + argument_mismatch :mixed_families do + param 'Stdlib::IP::Address::V6', :left + param 'Stdlib::IP::Address::V4', :right + end + + def normalize_addr(addr) + # When initializing a new IPAddr, if a prefix length is given, the returned IPAddr will be the + # base network address and the subnet mask for that network, not the address that was passed + # in. If we want to compare the full address, we need to separate the prefix and initialize a + # new IPAddr with the address alone, and then separately create an IPAddr instance to represent + # the network address. + (address, prefix) = addr.split('/') + ip_address = IPAddr.new(address) + network_address = prefix ? IPAddr.new(addr) : ip_address + [ip_address, network_address] + end + + def less_than(left, right) + (left_addr, left_netaddr) = normalize_addr(left) + (right_addr, right_netaddr) = normalize_addr(right) + + if left_addr == right_addr + left_netaddr.prefix < right_netaddr.prefix + else + left_addr < right_addr + end + end + + def mixed_families(*) + 'both addresses must be in the same family' + end +end diff --git a/lib/puppet/parser/functions/ip_address.rb b/lib/puppet/parser/functions/ip_address.rb index 7aef4de..d5178f2 100644 --- a/lib/puppet/parser/functions/ip_address.rb +++ b/lib/puppet/parser/functions/ip_address.rb @@ -1,14 +1,14 @@ # ex: syntax=ruby ts=2 sw=2 si et begin require 'puppetx/ip' -rescue LoadError => e +rescue LoadError # work around for puppet bug SERVER-973 Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') end module Puppet::Parser::Functions - newfunction(:ip_address, :type => :rvalue) do |args| + newfunction(:ip_address, type: :rvalue) do |args| PuppetX::Ip.new(args[0]).to_s end end diff --git a/lib/puppet/parser/functions/ip_broadcast.rb b/lib/puppet/parser/functions/ip_broadcast.rb index ae6ff37..3bb1820 100644 --- a/lib/puppet/parser/functions/ip_broadcast.rb +++ b/lib/puppet/parser/functions/ip_broadcast.rb @@ -1,14 +1,14 @@ # ex: syntax=ruby ts=2 sw=2 si et begin require 'puppetx/ip' -rescue LoadError => e +rescue LoadError # work around for puppet bug SERVER-973 Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') end module Puppet::Parser::Functions - newfunction(:ip_broadcast, :type => :rvalue) do |args| + newfunction(:ip_broadcast, type: :rvalue) do |args| PuppetX::Ip.new(args[0]).broadcast((args[1] || '0').to_i) end end diff --git a/lib/puppet/parser/functions/ip_increment.rb b/lib/puppet/parser/functions/ip_increment.rb index 5b12c79..757aca3 100644 --- a/lib/puppet/parser/functions/ip_increment.rb +++ b/lib/puppet/parser/functions/ip_increment.rb @@ -1,14 +1,14 @@ # ex: syntax=ruby ts=2 sw=2 si et begin require 'puppetx/ip' -rescue LoadError => e +rescue LoadError # work around for puppet bug SERVER-973 Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') end module Puppet::Parser::Functions - newfunction(:ip_increment, :type => :rvalue) do |args| + newfunction(:ip_increment, type: :rvalue) do |args| PuppetX::Ip.new(args[0]).increment((args[1] || '1').to_i) end end diff --git a/lib/puppet/parser/functions/ip_netmask.rb b/lib/puppet/parser/functions/ip_netmask.rb index 382ed20..867a9ef 100644 --- a/lib/puppet/parser/functions/ip_netmask.rb +++ b/lib/puppet/parser/functions/ip_netmask.rb @@ -1,14 +1,14 @@ # ex: syntax=ruby ts=2 sw=2 si et begin require 'puppetx/ip' -rescue LoadError => e +rescue LoadError # work around for puppet bug SERVER-973 Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') end module Puppet::Parser::Functions - newfunction(:ip_netmask, :type => :rvalue) do |args| + newfunction(:ip_netmask, type: :rvalue) do |args| PuppetX::Ip.new(args[0]).netmask end end diff --git a/lib/puppet/parser/functions/ip_network.rb b/lib/puppet/parser/functions/ip_network.rb index da75d86..2cda0a3 100644 --- a/lib/puppet/parser/functions/ip_network.rb +++ b/lib/puppet/parser/functions/ip_network.rb @@ -1,14 +1,14 @@ # ex: syntax=ruby ts=2 sw=2 si et begin require 'puppetx/ip' -rescue LoadError => e +rescue LoadError # work around for puppet bug SERVER-973 Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') end module Puppet::Parser::Functions - newfunction(:ip_network, :type => :rvalue) do |args| + newfunction(:ip_network, type: :rvalue) do |args| PuppetX::Ip.new(args[0]).network((args[1] || '0').to_i) end end diff --git a/lib/puppet/parser/functions/ip_network_size.rb b/lib/puppet/parser/functions/ip_network_size.rb index f66e918..cac5012 100644 --- a/lib/puppet/parser/functions/ip_network_size.rb +++ b/lib/puppet/parser/functions/ip_network_size.rb @@ -1,14 +1,14 @@ # ex: syntax=ruby ts=2 sw=2 si et begin require 'puppetx/ip' -rescue LoadError => e +rescue LoadError # work around for puppet bug SERVER-973 Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') end module Puppet::Parser::Functions - newfunction(:ip_network_size, :type => :rvalue) do |args| + newfunction(:ip_network_size, type: :rvalue) do |args| PuppetX::Ip.new(args[0]).network_size end end diff --git a/lib/puppet/parser/functions/ip_offset.rb b/lib/puppet/parser/functions/ip_offset.rb index b90c322..88b7162 100644 --- a/lib/puppet/parser/functions/ip_offset.rb +++ b/lib/puppet/parser/functions/ip_offset.rb @@ -1,14 +1,14 @@ # ex: syntax=ruby ts=2 sw=2 si et begin require 'puppetx/ip' -rescue LoadError => e +rescue LoadError # work around for puppet bug SERVER-973 Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') end module Puppet::Parser::Functions - newfunction(:ip_offset, :type => :rvalue) do |args| + newfunction(:ip_offset, type: :rvalue) do |args| PuppetX::Ip.new(args[0]).offset end end diff --git a/lib/puppet/parser/functions/ip_prefixlength.rb b/lib/puppet/parser/functions/ip_prefixlength.rb index 9e0f677..6029dbe 100644 --- a/lib/puppet/parser/functions/ip_prefixlength.rb +++ b/lib/puppet/parser/functions/ip_prefixlength.rb @@ -1,14 +1,14 @@ # ex: syntax=ruby ts=2 sw=2 si et begin require 'puppetx/ip' -rescue LoadError => e +rescue LoadError # work around for puppet bug SERVER-973 Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') end module Puppet::Parser::Functions - newfunction(:ip_prefixlength, :type => :rvalue) do |args| + newfunction(:ip_prefixlength, type: :rvalue) do |args| PuppetX::Ip.new(args[0]).prefixlength end end diff --git a/lib/puppet/parser/functions/ip_range_size.rb b/lib/puppet/parser/functions/ip_range_size.rb new file mode 100644 index 0000000..6fcec9e --- /dev/null +++ b/lib/puppet/parser/functions/ip_range_size.rb @@ -0,0 +1,16 @@ +# ex: syntax=ruby ts=2 sw=2 si et +# frozen_string_literal: true + +begin + require 'puppetx/ip' +rescue LoadError + # work around for puppet bug SERVER-973 + Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') + require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') +end + +module Puppet::Parser::Functions + newfunction(:ip_range_size, type: :rvalue) do |args| + PuppetX::Ip.new(args[0]).range_size(args[1]).to_i + end +end diff --git a/lib/puppet/parser/functions/ip_split.rb b/lib/puppet/parser/functions/ip_split.rb index 7a2f606..672fcc7 100644 --- a/lib/puppet/parser/functions/ip_split.rb +++ b/lib/puppet/parser/functions/ip_split.rb @@ -1,14 +1,14 @@ # ex: syntax=ruby ts=2 sw=2 si et begin require 'puppetx/ip' -rescue LoadError => e +rescue LoadError # work around for puppet bug SERVER-973 Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') end module Puppet::Parser::Functions - newfunction(:ip_split, :type => :rvalue) do |args| + newfunction(:ip_split, type: :rvalue) do |args| PuppetX::Ip.new(args[0]).split end end diff --git a/lib/puppet/parser/functions/ip_subnet.rb b/lib/puppet/parser/functions/ip_subnet.rb index e5bb73d..5650b7a 100644 --- a/lib/puppet/parser/functions/ip_subnet.rb +++ b/lib/puppet/parser/functions/ip_subnet.rb @@ -1,14 +1,14 @@ # ex: syntax=ruby ts=2 sw=2 si et begin require 'puppetx/ip' -rescue LoadError => e +rescue LoadError # work around for puppet bug SERVER-973 Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') end module Puppet::Parser::Functions - newfunction(:ip_subnet, :type => :rvalue) do |args| + newfunction(:ip_subnet, type: :rvalue) do |args| PuppetX::Ip.new(args[0]).subnet(args[1].to_i, (args[2] || '0').to_i) end end diff --git a/lib/puppet/parser/functions/ip_supernet.rb b/lib/puppet/parser/functions/ip_supernet.rb index c90d66e..09c97ba 100644 --- a/lib/puppet/parser/functions/ip_supernet.rb +++ b/lib/puppet/parser/functions/ip_supernet.rb @@ -1,14 +1,14 @@ # ex: syntax=ruby ts=2 sw=2 si et begin require 'puppetx/ip' -rescue LoadError => e +rescue LoadError # work around for puppet bug SERVER-973 Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') end module Puppet::Parser::Functions - newfunction(:ip_supernet, :type => :rvalue) do |args| + newfunction(:ip_supernet, type: :rvalue) do |args| PuppetX::Ip.new(args[0]).supernet(args[1].to_i) end end diff --git a/lib/puppetx/ip.rb b/lib/puppetx/ip.rb index 2ff3ea6..e6a6c49 100644 --- a/lib/puppetx/ip.rb +++ b/lib/puppetx/ip.rb @@ -4,21 +4,20 @@ module PuppetX; end class PuppetX::Ip - attr_reader :cidr, :address, :prefixlength def initialize(cidr) @cidr = IPAddr.new(cidr) - (addr, mask) = cidr.split(/\//) + (addr, mask) = cidr.split(%r{/}) @address = IPAddr.new(addr) if mask.nil? prefixlen = unicast_prefixlength - elsif mask =~ /\A\d+\z/ + elsif %r{\A\d+\z}.match?(mask) prefixlen = mask.to_i else m = IPAddr.new(mask) if m.family != @address.family - raise ArgumentError, "address family is not same" + raise ArgumentError, 'address family is not same' end prefixlen = m.to_i.to_s(2).count('1') end @@ -30,12 +29,12 @@ def netmask end def network_size - 2 ** (unicast_prefixlength - prefixlength) + 2**(unicast_prefixlength - prefixlength) end def network(offset = 0) - raise ArgumentError, 'offset out of bounds' if offset >= network_size or offset < 0 - "#{IPAddr.new(cidr.to_i + offset, cidr.family).to_s}/#{prefixlength}" + raise ArgumentError, 'offset out of bounds' if (offset >= network_size) || (offset < 0) + "#{IPAddr.new(cidr.to_i + offset, cidr.family)}/#{prefixlength}" end def broadcast(offset = 0) @@ -50,6 +49,15 @@ def offset address.to_i - cidr.to_i end + def range_size(range_cidr) + other_address = IPAddr.new(range_cidr.split(%r{/})[0]) + raise ArgumentError, 'Both addresses must be in same family' if other_address.family != address.family + + # Range size should be inclusive of starting and ending addresses, hence +1 + range_subtraction = other_address.to_i - address.to_i + range_subtraction.abs + 1 + end + def split [ subnet(prefixlength + 1, 0), subnet(prefixlength + 1, 1) ] end @@ -57,9 +65,9 @@ def split def subnet(subnet_prefixlength, index) raise IPAddr::InvalidPrefixError, 'invalid subnet prefix' if subnet_prefixlength > unicast_prefixlength raise ArgumentError, 'subnet prefix too long' if subnet_prefixlength <= prefixlength - subnet_size = 2 ** (unicast_prefixlength - subnet_prefixlength) + subnet_size = 2**(unicast_prefixlength - subnet_prefixlength) raise ArgumentError, 'subnet index out of bounds' if (subnet_size * index) >= network_size - "#{IPAddr.new(cidr.to_i + subnet_size * index, cidr.family).to_s}/#{subnet_prefixlength}" + "#{IPAddr.new(cidr.to_i + subnet_size * index, cidr.family)}/#{subnet_prefixlength}" end def supernet(supernet_prefixlength) @@ -73,7 +81,16 @@ def to_s address.to_s end -private + def family + case address.family + when Socket::AF_INET + 'IPv4' + when Socket::AF_INET6 + 'IPv6' + end + end + + private def unicast_prefixlength case address.family diff --git a/metadata.json b/metadata.json index bade0eb..1964aac 100644 --- a/metadata.json +++ b/metadata.json @@ -1,12 +1,78 @@ { - "name": "inkblot/ipcalc", - "version": "2.2.0", + "name": "inkblot-ipcalc", + "version": "2.3.0", "author": "inkblot", - "license": "Apache-2.0", "summary": "IP and subnet puppet parser functions", + "license": "Apache-2.0", "source": "git://github.com/inkblot/puppet-ipcalc.git", "project_page": "https://github.com/inkblot/puppet-ipcalc", "issues_url": "https://github.com/inkblot/puppet-ipcalc/issues", - "tags": [ "ip", "address", "netmask", "cidr", "ipcalc" ], - "dependencies": [] + "dependencies": [ + + ], + "operatingsystem_support": [ + { + "operatingsystem": "CentOS", + "operatingsystemrelease": [ + "6", + "7" + ] + }, + { + "operatingsystem": "OracleLinux", + "operatingsystemrelease": [ + "6", + "7" + ] + }, + { + "operatingsystem": "RedHat", + "operatingsystemrelease": [ + "6", + "7", + "8" + ] + }, + { + "operatingsystem": "Scientific", + "operatingsystemrelease": [ + "7" + ] + }, + { + "operatingsystem": "Debian", + "operatingsystemrelease": [ + "10" + ] + }, + { + "operatingsystem": "Ubuntu", + "operatingsystemrelease": [ + "18.04" + ] + }, + { + "operatingsystem": "windows", + "operatingsystemrelease": [ + "2019", + "10" + ] + } + ], + "requirements": [ + { + "name": "puppet", + "version_requirement": ">= 5.0.0 < 8.0.0" + } + ], + "tags": [ + "ip", + "address", + "netmask", + "cidr", + "ipcalc" + ], + "pdk-version": "2.7.1", + "template-url": "pdk-default#2.7.4", + "template-ref": "tags/2.7.4-0-g58edf57" } diff --git a/pdk.yaml b/pdk.yaml new file mode 100644 index 0000000..4bef4bd --- /dev/null +++ b/pdk.yaml @@ -0,0 +1,2 @@ +--- +ignore: [] diff --git a/spec/default_facts.yml b/spec/default_facts.yml new file mode 100644 index 0000000..f777abf --- /dev/null +++ b/spec/default_facts.yml @@ -0,0 +1,8 @@ +# Use default_module_facts.yml for module specific facts. +# +# Facts specified here will override the values provided by rspec-puppet-facts. +--- +ipaddress: "172.16.254.254" +ipaddress6: "FE80:0000:0000:0000:AAAA:AAAA:AAAA" +is_pe: false +macaddress: "AA:AA:AA:AA:AA:AA" diff --git a/spec/functions/equal_or_greater_than_spec.rb b/spec/functions/equal_or_greater_than_spec.rb new file mode 100644 index 0000000..88e5149 --- /dev/null +++ b/spec/functions/equal_or_greater_than_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'ipcalc::equal_or_greater_than' do + it { is_expected.to run.with_params('127.0.0.1', '127.0.0.2').and_return(false) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.2/24').and_return(false) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.2').and_return(false) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.1/24').and_return(true) } + it { is_expected.to run.with_params('127.0.0.2', '127.0.0.1').and_return(true) } + it { is_expected.to run.with_params('127.0.0.1', '127.0.0.1').and_return(true) } + it { is_expected.to run.with_params('127.0.0.1/24', '127.0.0.1/24').and_return(true) } + it { is_expected.to run.with_params('fe80::1', 'fe80::2').and_return(false) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::2/120').and_return(false) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::2').and_return(false) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::1/120').and_return(true) } + it { is_expected.to run.with_params('fe80::2', 'fe80::1').and_return(true) } + it { is_expected.to run.with_params('fe80::1', 'fe80::1').and_return(true) } + it { is_expected.to run.with_params('fe80::1/120', 'fe80::1/120').and_return(true) } + it { is_expected.to run.with_params(nil).and_raise_error(StandardError) } + it { is_expected.to run.with_params('127.0.0.1', 'fe80::1').and_raise_error(StandardError) } + it { is_expected.to run.with_params('fe80::1', '127.0.0.1').and_raise_error(StandardError) } +end diff --git a/spec/functions/equal_or_less_than_spec.rb b/spec/functions/equal_or_less_than_spec.rb new file mode 100644 index 0000000..54747bb --- /dev/null +++ b/spec/functions/equal_or_less_than_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'ipcalc::equal_or_less_than' do + it { is_expected.to run.with_params('127.0.0.1', '127.0.0.2').and_return(true) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.2/24').and_return(true) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.2').and_return(true) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.1/24').and_return(false) } + it { is_expected.to run.with_params('127.0.0.2', '127.0.0.1').and_return(false) } + it { is_expected.to run.with_params('127.0.0.1', '127.0.0.1').and_return(true) } + it { is_expected.to run.with_params('127.0.0.1/24', '127.0.0.1/24').and_return(true) } + it { is_expected.to run.with_params('fe80::1', 'fe80::2').and_return(true) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::2/120').and_return(true) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::2').and_return(true) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::1/120').and_return(false) } + it { is_expected.to run.with_params('fe80::2', 'fe80::1').and_return(false) } + it { is_expected.to run.with_params('fe80::1', 'fe80::1').and_return(true) } + it { is_expected.to run.with_params('fe80::1/120', 'fe80::1/120').and_return(true) } + it { is_expected.to run.with_params(nil).and_raise_error(StandardError) } + it { is_expected.to run.with_params('127.0.0.1', 'fe80::1').and_raise_error(StandardError) } + it { is_expected.to run.with_params('fe80::1', '127.0.0.1').and_raise_error(StandardError) } +end diff --git a/spec/functions/equal_to_spec.rb b/spec/functions/equal_to_spec.rb new file mode 100644 index 0000000..df69f10 --- /dev/null +++ b/spec/functions/equal_to_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'ipcalc::equal_to' do + it { is_expected.to run.with_params('127.0.0.1', '127.0.0.2').and_return(false) } + it { is_expected.to run.with_params('127.0.0.2', '127.0.0.1').and_return(false) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.1/24').and_return(false) } + # Zero-padded values are rejected by type validation and by the underlying Ruby implementation + it { is_expected.to run.with_params('127.00.0.1', '127.0.0.1').and_raise_error(StandardError) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.1').and_return(true) } + it { is_expected.to run.with_params('127.0.0.1', '127.0.0.1').and_return(true) } + it { is_expected.to run.with_params('127.0.0.1/24', '127.0.0.1/24').and_return(true) } + it { is_expected.to run.with_params('fe80::1', 'fe80::2').and_return(false) } + it { is_expected.to run.with_params('fe80::2', 'fe80::1').and_return(false) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::1/120').and_return(false) } + it { is_expected.to run.with_params('fe80:0::001', 'fe80::1').and_return(true) } + it { is_expected.to run.with_params('fe80::1', 'fe80::1').and_return(true) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::1').and_return(true) } + it { is_expected.to run.with_params('fe80::1/120', 'fe80::1/120').and_return(true) } + it { is_expected.to run.with_params(nil).and_raise_error(StandardError) } + it { is_expected.to run.with_params('127.0.0.1', 'fe80::1').and_return(false) } + it { is_expected.to run.with_params('fe80::1', '127.0.0.1').and_return(false) } +end diff --git a/spec/functions/greater_than_spec.rb b/spec/functions/greater_than_spec.rb new file mode 100644 index 0000000..609a557 --- /dev/null +++ b/spec/functions/greater_than_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'ipcalc::greater_than' do + it { is_expected.to run.with_params('127.0.0.1', '127.0.0.2').and_return(false) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.2/24').and_return(false) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.2').and_return(false) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.1/24').and_return(true) } + it { is_expected.to run.with_params('127.0.0.2', '127.0.0.1').and_return(true) } + it { is_expected.to run.with_params('fe80::1', 'fe80::2').and_return(false) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::2/120').and_return(false) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::2').and_return(false) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::1/120').and_return(true) } + it { is_expected.to run.with_params('fe80::2', 'fe80::1').and_return(true) } + it { is_expected.to run.with_params(nil).and_raise_error(StandardError) } + it { is_expected.to run.with_params('127.0.0.1', 'fe80::1').and_raise_error(StandardError) } + it { is_expected.to run.with_params('fe80::1', '127.0.0.1').and_raise_error(StandardError) } +end diff --git a/spec/functions/ip_address_spec.rb b/spec/functions/ip_address_spec.rb index e015075..436a788 100644 --- a/spec/functions/ip_address_spec.rb +++ b/spec/functions/ip_address_spec.rb @@ -1,13 +1,12 @@ require 'spec_helper' describe 'ip_address' do - it { should run.with_params('10.0.0.1/24').and_return('10.0.0.1') } - it { should run.with_params('10.0.0.1/255.255.255.0').and_return('10.0.0.1') } - it { should run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('fd00::1/24').and_return('fd00::1') } - it { should run.with_params('fd00::1/129').and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('fd00:g0:1/24').and_raise_error(IPAddr::InvalidAddressError) } - + it { is_expected.to run.with_params('10.0.0.1/24').and_return('10.0.0.1') } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.0').and_return('10.0.0.1') } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('fd00::1/24').and_return('fd00::1') } + it { is_expected.to run.with_params('fd00::1/129').and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('fd00:g0:1/24').and_raise_error(IPAddr::InvalidAddressError) } end diff --git a/spec/functions/ip_broadcast_spec.rb b/spec/functions/ip_broadcast_spec.rb index 054078f..32ad425 100644 --- a/spec/functions/ip_broadcast_spec.rb +++ b/spec/functions/ip_broadcast_spec.rb @@ -1,18 +1,17 @@ require 'spec_helper' describe 'ip_broadcast' do - it { should run.with_params('10.0.0.1/24').and_return('10.0.0.255/24') } - it { should run.with_params('10.0.0.1/24',2).and_return('10.0.0.253/24') } - it { should run.with_params('10.0.0.1/24',255).and_return('10.0.0.0/24') } - it { should run.with_params('10.0.0.1/24',256).and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/24',999).and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('fd00::1/120').and_return('fd00::ff/120') } - it { should run.with_params('fd00::1/120',2).and_return('fd00::fd/120') } - it { should run.with_params('fd00::1/120',255).and_return('fd00::/120') } - it { should run.with_params('fd00::1/120',256).and_raise_error(ArgumentError) } - it { should run.with_params('fd00::1/129').and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/24').and_return('10.0.0.255/24') } + it { is_expected.to run.with_params('10.0.0.1/24', 2).and_return('10.0.0.253/24') } + it { is_expected.to run.with_params('10.0.0.1/24', 255).and_return('10.0.0.0/24') } + it { is_expected.to run.with_params('10.0.0.1/24', 256).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/24', 999).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('fd00::1/120').and_return('fd00::ff/120') } + it { is_expected.to run.with_params('fd00::1/120', 2).and_return('fd00::fd/120') } + it { is_expected.to run.with_params('fd00::1/120', 255).and_return('fd00::/120') } + it { is_expected.to run.with_params('fd00::1/120', 256).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('fd00::1/129').and_raise_error(ArgumentError) } end - diff --git a/spec/functions/ip_compare_spec.rb b/spec/functions/ip_compare_spec.rb new file mode 100644 index 0000000..2908224 --- /dev/null +++ b/spec/functions/ip_compare_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'ipcalc::ip_compare' do + it { is_expected.to run.with_params('127.0.0.1', '127.0.0.2').and_return(-1) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.2/24').and_return(-1) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.2').and_return(-1) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.1/24').and_return(1) } + it { is_expected.to run.with_params('127.0.0.2', '127.0.0.1').and_return(1) } + it { is_expected.to run.with_params('fe80::1', 'fe80::2').and_return(-1) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::2/120').and_return(-1) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::2').and_return(-1) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::1/120').and_return(1) } + it { is_expected.to run.with_params('fe80::2', 'fe80::1').and_return(1) } + it { is_expected.to run.with_params(nil).and_raise_error(StandardError) } + it { is_expected.to run.with_params('127.0.0.1', 'fe80::1').and_raise_error(StandardError) } + it { is_expected.to run.with_params('fe80::1', '127.0.0.1').and_raise_error(StandardError) } +end diff --git a/spec/functions/ip_increment_spec.rb b/spec/functions/ip_increment_spec.rb index 4dca7a7..f942f81 100644 --- a/spec/functions/ip_increment_spec.rb +++ b/spec/functions/ip_increment_spec.rb @@ -1,11 +1,11 @@ require 'spec_helper' describe 'ip_increment' do - it { should run.with_params('10.0.0.254/23','2').and_return('10.0.1.0/23') } - it { should run.with_params('10.0.0.1/24').and_return('10.0.0.2/24') } - it { should run.with_params('10.0.0.255/24').and_raise_error(ArgumentError) } - it { should run.with_params('fd00::fe/119','2').and_return('fd00::100/119') } - it { should run.with_params('fd00::1/120').and_return('fd00::2/120') } - it { should run.with_params('fd00::ff/120').and_raise_error(ArgumentError) } - it { should run.with_params('2600:3c00::f03c:91ff:fe96:432a/64',5).and_return('2600:3c00::f03c:91ff:fe96:432f/64') } + it { is_expected.to run.with_params('10.0.0.254/23', '2').and_return('10.0.1.0/23') } + it { is_expected.to run.with_params('10.0.0.1/24').and_return('10.0.0.2/24') } + it { is_expected.to run.with_params('10.0.0.255/24').and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('fd00::fe/119', '2').and_return('fd00::100/119') } + it { is_expected.to run.with_params('fd00::1/120').and_return('fd00::2/120') } + it { is_expected.to run.with_params('fd00::ff/120').and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('2600:3c00::f03c:91ff:fe96:432a/64', 5).and_return('2600:3c00::f03c:91ff:fe96:432f/64') } end diff --git a/spec/functions/ip_netmask_spec.rb b/spec/functions/ip_netmask_spec.rb index 5e5cace..249c101 100644 --- a/spec/functions/ip_netmask_spec.rb +++ b/spec/functions/ip_netmask_spec.rb @@ -1,11 +1,11 @@ require 'spec_helper' describe 'ip_netmask' do - it { should run.with_params('10.0.0.1/24').and_return('255.255.255.0') } - it { should run.with_params('10.0.0.1/255.255.255.0').and_return('255.255.255.0') } - it { should run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('fd00::1/120').and_return('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00') } - it { should run.with_params('fd00::1/129').and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('10.0.0.1/24').and_return('255.255.255.0') } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.0').and_return('255.255.255.0') } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('fd00::1/120').and_return('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00') } + it { is_expected.to run.with_params('fd00::1/129').and_raise_error(IPAddr::InvalidPrefixError) } end diff --git a/spec/functions/ip_network_size_spec.rb b/spec/functions/ip_network_size_spec.rb index b67692b..fb6bf37 100644 --- a/spec/functions/ip_network_size_spec.rb +++ b/spec/functions/ip_network_size_spec.rb @@ -1,11 +1,11 @@ require 'spec_helper' describe 'ip_network_size' do - it { should run.with_params('10.0.0.1/24').and_return(256) } - it { should run.with_params('10.0.0.1/255.255.255.0').and_return(256) } - it { should run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('fd00::1/120').and_return(256) } - it { should run.with_params('fd00::1/129').and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('10.0.0.1/24').and_return(256) } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.0').and_return(256) } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('fd00::1/120').and_return(256) } + it { is_expected.to run.with_params('fd00::1/129').and_raise_error(IPAddr::InvalidPrefixError) } end diff --git a/spec/functions/ip_network_spec.rb b/spec/functions/ip_network_spec.rb index 00cdab9..518e42e 100644 --- a/spec/functions/ip_network_spec.rb +++ b/spec/functions/ip_network_spec.rb @@ -1,18 +1,17 @@ require 'spec_helper' describe 'ip_network' do - it { should run.with_params('10.0.0.1/24').and_return('10.0.0.0/24') } - it { should run.with_params('10.0.0.1/24',2).and_return('10.0.0.2/24') } - it { should run.with_params('10.0.0.1/24',255).and_return('10.0.0.255/24') } - it { should run.with_params('10.0.0.1/24',256).and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/24',999).and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('fd00::1/120').and_return('fd00::/120') } - it { should run.with_params('fd00::1/120',2).and_return('fd00::2/120') } - it { should run.with_params('fd00::1/120',255).and_return('fd00::ff/120') } - it { should run.with_params('fd00::1/120',256).and_raise_error(ArgumentError) } - it { should run.with_params('fd00::1/129').and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/24').and_return('10.0.0.0/24') } + it { is_expected.to run.with_params('10.0.0.1/24', 2).and_return('10.0.0.2/24') } + it { is_expected.to run.with_params('10.0.0.1/24', 255).and_return('10.0.0.255/24') } + it { is_expected.to run.with_params('10.0.0.1/24', 256).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/24', 999).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('fd00::1/120').and_return('fd00::/120') } + it { is_expected.to run.with_params('fd00::1/120', 2).and_return('fd00::2/120') } + it { is_expected.to run.with_params('fd00::1/120', 255).and_return('fd00::ff/120') } + it { is_expected.to run.with_params('fd00::1/120', 256).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('fd00::1/129').and_raise_error(ArgumentError) } end - diff --git a/spec/functions/ip_offset_spec.rb b/spec/functions/ip_offset_spec.rb index 5b5f44e..e802573 100644 --- a/spec/functions/ip_offset_spec.rb +++ b/spec/functions/ip_offset_spec.rb @@ -1,11 +1,11 @@ require 'spec_helper' describe 'ip_offset' do - it { should run.with_params('10.0.0.12/24').and_return(12) } - it { should run.with_params('10.0.0.12/255.255.255.0').and_return(12) } - it { should run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('fd00::10/120').and_return(16) } - it { should run.with_params('fd00::1/129').and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.12/24').and_return(12) } + it { is_expected.to run.with_params('10.0.0.12/255.255.255.0').and_return(12) } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('fd00::10/120').and_return(16) } + it { is_expected.to run.with_params('fd00::1/129').and_raise_error(ArgumentError) } end diff --git a/spec/functions/ip_prefixlength_spec.rb b/spec/functions/ip_prefixlength_spec.rb index fe6a5a6..ebf82d7 100644 --- a/spec/functions/ip_prefixlength_spec.rb +++ b/spec/functions/ip_prefixlength_spec.rb @@ -1,12 +1,11 @@ require 'spec_helper' describe 'ip_prefixlength' do - it { should run.with_params('10.0.0.1/24').and_return(24) } - it { should run.with_params('10.0.0.1/255.255.255.0').and_return(24) } - it { should run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('fd00::1/24').and_return(24) } - it { should run.with_params('fd00::1/129').and_raise_error(IPAddr::InvalidPrefixError) } - + it { is_expected.to run.with_params('10.0.0.1/24').and_return(24) } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.0').and_return(24) } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('fd00::1/24').and_return(24) } + it { is_expected.to run.with_params('fd00::1/129').and_raise_error(IPAddr::InvalidPrefixError) } end diff --git a/spec/functions/ip_range_size_spec.rb b/spec/functions/ip_range_size_spec.rb new file mode 100644 index 0000000..fe99f78 --- /dev/null +++ b/spec/functions/ip_range_size_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'ip_range_size' do + # please note that these tests are examples only + # you will need to replace the params and return value + # with your expectations + it 'given adjacent IPv4 CIDR addresses, returns range size of 2' do + is_expected.to run.with_params('192.168.1.1/32', '192.168.1.2/32').and_return(2) + end + it 'given adjacent IPv6 CIDR addresses, returns range size of 2' do + is_expected.to run.with_params('2001:db8::8c28:c929:72db:49fd/64', '2001:db8::8c28:c929:72db:49fe/64').and_return(2) + end + it 'given adjacent IPv4 plain addresses, returns range size of 2' do + is_expected.to run.with_params('192.168.1.1', '192.168.1.2').and_return(2) + end + it 'given adjacent IPv6 plain addresses, returns range size of 2' do + is_expected.to run.with_params('2001:db8::8c28:c929:72db:49fd', '2001:db8::8c28:c929:72db:49fe').and_return(2) + end + it 'given adjacent IPv4 plain addresses in reversed order, returns range size of 2' do + is_expected.to run.with_params('192.168.1.2', '192.168.1.1').and_return(2) + end + it 'given adjacent IPv6 plain addresses in reversed order, returns range size of 2' do + is_expected.to run.with_params('2001:db8::8c28:c929:72db:49fe', '2001:db8::8c28:c929:72db:49fd').and_return(2) + end + it 'given IPv4 CIDR addresses with same offset in different /24 networks, returns range size of 257' do + is_expected.to run.with_params('192.168.1.1/24', '192.168.2.1/24').and_return(257) + end + it 'given IPv6 CIDR addresses with same offset in different /56 networks, returns range size of 257' do + is_expected.to run.with_params('2001:db8::8c28:c929:72db:49fe/56', '2001:db8::8c28:c929:72db:4afe/56').and_return(257) + end + it 'given IPv4 plain addresses in wildly different networks, returns correct range size' do + is_expected.to run.with_params('10.0.57.1', '192.168.2.1').and_return(3_064_449_281) + end + it 'given IPv6 plain addresses in wildly different networks, returns correct range size' do + is_expected.to run + .with_params('2001:db8::8c28:c929:72db:49fe/64', 'fe80::9656:d028:8652:66b6/64') + .and_return(295_747_758_515_978_496_797_848_443_371_614_837_945) + end + it 'given addresses in different families, raises an exception' do + is_expected.to run + .with_params('10.0.57.1', 'fe80::9656:d028:8652:66b6') + .and_raise_error(ArgumentError, %r{addresses must be in same family}) + is_expected.to run + .with_params('fe80::9656:d028:8652:66b6', '10.0.57.1') + .and_raise_error(ArgumentError, %r{addresses must be in same family}) + end +end diff --git a/spec/functions/ip_split_spec.rb b/spec/functions/ip_split_spec.rb index 434d7f1..e02c98b 100644 --- a/spec/functions/ip_split_spec.rb +++ b/spec/functions/ip_split_spec.rb @@ -1,11 +1,11 @@ require 'spec_helper' describe 'ip_split' do - it { should run.with_params('10.0.0.1/23').and_return(["10.0.0.0/24", "10.0.1.0/24"]) } - it { should run.with_params('10.0.0.1/255.255.254.0').and_return(["10.0.0.0/24", "10.0.1.0/24"]) } - it { should run.with_params('10.0.0.1/255.255.255.255').and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('fd00::1/119').and_return(["fd00::/120", "fd00::100/120"]) } + it { is_expected.to run.with_params('10.0.0.1/23').and_return(['10.0.0.0/24', '10.0.1.0/24']) } + it { is_expected.to run.with_params('10.0.0.1/255.255.254.0').and_return(['10.0.0.0/24', '10.0.1.0/24']) } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.255').and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.256').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('10.0.0.1/33').and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('999.999.999.999/32').and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('fd00::1/119').and_return(['fd00::/120', 'fd00::100/120']) } end diff --git a/spec/functions/ip_subnet_spec.rb b/spec/functions/ip_subnet_spec.rb index 75c88a1..a260851 100644 --- a/spec/functions/ip_subnet_spec.rb +++ b/spec/functions/ip_subnet_spec.rb @@ -1,21 +1,21 @@ require 'spec_helper' describe 'ip_subnet' do - it { should run.with_params('10.0.0.1/23',24,1).and_return("10.0.1.0/24") } - it { should run.with_params('10.0.0.1/255.255.254.0',24,0).and_return("10.0.0.0/24") } - it { should run.with_params('10.0.0.1/23',24,2).and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/23',-1,0).and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/23',23,1).and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/23',22,1).and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/32',33,1).and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('10.0.0.1/255.255.255.256',32,1).and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('10.0.0.1/33',32,1).and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('999.999.999.999/32',32,1).and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('fd00::1/119',120,1).and_return("fd00::100/120") } - it { should run.with_params('fd00::1/119',120,2).and_raise_error(ArgumentError) } - it { should run.with_params('fd00::1/119',-1,0).and_raise_error(ArgumentError) } - it { should run.with_params('fd00::1/119',119,1).and_raise_error(ArgumentError) } - it { should run.with_params('fd00::1/119',118,1).and_raise_error(ArgumentError) } - it { should run.with_params('fd00::1/128',129,1).and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('fd00::1/129',128,1).and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('10.0.0.1/23', 24, 1).and_return('10.0.1.0/24') } + it { is_expected.to run.with_params('10.0.0.1/255.255.254.0', 24, 0).and_return('10.0.0.0/24') } + it { is_expected.to run.with_params('10.0.0.1/23', 24, 2).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/23', -1, 0).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/23', 23, 1).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/23', 22, 1).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/32', 33, 1).and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.256', 32, 1).and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('10.0.0.1/33', 32, 1).and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('999.999.999.999/32', 32, 1).and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('fd00::1/119', 120, 1).and_return('fd00::100/120') } + it { is_expected.to run.with_params('fd00::1/119', 120, 2).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('fd00::1/119', -1, 0).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('fd00::1/119', 119, 1).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('fd00::1/119', 118, 1).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('fd00::1/128', 129, 1).and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('fd00::1/129', 128, 1).and_raise_error(IPAddr::InvalidAddressError) } end diff --git a/spec/functions/ip_supernet_spec.rb b/spec/functions/ip_supernet_spec.rb index 6af145e..ca1d317 100644 --- a/spec/functions/ip_supernet_spec.rb +++ b/spec/functions/ip_supernet_spec.rb @@ -1,16 +1,16 @@ require 'spec_helper' describe 'ip_supernet' do - it { should run.with_params('10.0.0.1/23',22).and_return("10.0.0.0/22") } - it { should run.with_params('10.0.0.1/23',24).and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/23',-1).and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/255.255.254.0',22).and_return("10.0.0.0/22") } - it { should run.with_params('10.0.0.1/255.255.254.0',24).and_raise_error(ArgumentError) } - it { should run.with_params('10.0.0.1/255.255.255.256',32).and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('10.0.0.1/33',32).and_raise_error(IPAddr::InvalidPrefixError) } - it { should run.with_params('999.999.999.999/32',32).and_raise_error(IPAddr::InvalidAddressError) } - it { should run.with_params('fd00::1/119',118).and_return("fd00::/118") } - it { should run.with_params('fd00::1/119',120).and_raise_error(ArgumentError) } - it { should run.with_params('fd00::1/119',-1).and_raise_error(ArgumentError) } - it { should run.with_params('fd00::1/129',128).and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('10.0.0.1/23', 22).and_return('10.0.0.0/22') } + it { is_expected.to run.with_params('10.0.0.1/23', 24).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/23', -1).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/255.255.254.0', 22).and_return('10.0.0.0/22') } + it { is_expected.to run.with_params('10.0.0.1/255.255.254.0', 24).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('10.0.0.1/255.255.255.256', 32).and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('10.0.0.1/33', 32).and_raise_error(IPAddr::InvalidPrefixError) } + it { is_expected.to run.with_params('999.999.999.999/32', 32).and_raise_error(IPAddr::InvalidAddressError) } + it { is_expected.to run.with_params('fd00::1/119', 118).and_return('fd00::/118') } + it { is_expected.to run.with_params('fd00::1/119', 120).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('fd00::1/119', -1).and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('fd00::1/129', 128).and_raise_error(IPAddr::InvalidPrefixError) } end diff --git a/spec/functions/less_than_spec.rb b/spec/functions/less_than_spec.rb new file mode 100644 index 0000000..363e83b --- /dev/null +++ b/spec/functions/less_than_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: false + +require 'spec_helper' + +describe 'ipcalc::less_than' do + it { is_expected.to run.with_params('127.0.0.1', '127.0.0.2').and_return(true) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.2/24').and_return(true) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.2').and_return(true) } + it { is_expected.to run.with_params('127.0.0.1/32', '127.0.0.1/24').and_return(false) } + it { is_expected.to run.with_params('127.0.0.2', '127.0.0.1').and_return(false) } + it { is_expected.to run.with_params('fe80::1', 'fe80::2').and_return(true) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::2/120').and_return(true) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::2').and_return(true) } + it { is_expected.to run.with_params('fe80::1/128', 'fe80::1/120').and_return(false) } + it { is_expected.to run.with_params('fe80::2', 'fe80::1').and_return(false) } + it { is_expected.to run.with_params(nil).and_raise_error(StandardError) } + it { is_expected.to run.with_params('127.0.0.1', 'fe80::1').and_raise_error(StandardError) } + it { is_expected.to run.with_params('fe80::1', '127.0.0.1').and_raise_error(StandardError) } +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5223407..9b1fa6f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,73 @@ -require 'rspec-puppet' +# frozen_string_literal: true -fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures')) +RSpec.configure do |c| + c.mock_with :rspec +end + +require 'puppetlabs_spec_helper/module_spec_helper' +require 'rspec-puppet-facts' + +require 'spec_helper_local' if File.file?(File.join(File.dirname(__FILE__), 'spec_helper_local.rb')) + +include RspecPuppetFacts + +default_facts = { + puppetversion: Puppet.version, + facterversion: Facter.version, +} + +default_fact_files = [ + File.expand_path(File.join(File.dirname(__FILE__), 'default_facts.yml')), + File.expand_path(File.join(File.dirname(__FILE__), 'default_module_facts.yml')), +] + +default_fact_files.each do |f| + next unless File.exist?(f) && File.readable?(f) && File.size?(f) + + begin + default_facts.merge!(YAML.safe_load(File.read(f), [], [], true)) + rescue => e + RSpec.configuration.reporter.message "WARNING: Unable to load #{f}: #{e}" + end +end + +# read default_facts and merge them over what is provided by facterdb +default_facts.each do |fact, value| + add_custom_fact fact, value +end RSpec.configure do |c| - c.module_path = File.join(fixture_path, 'modules') - c.manifest_dir = File.join(fixture_path, 'manifests') + c.default_facts = default_facts + c.before :each do + # set to strictest setting for testing + # by default Puppet runs at warning level + Puppet.settings[:strict] = :warning + Puppet.settings[:strict_variables] = true + end + c.filter_run_excluding(bolt: true) unless ENV['GEM_BOLT'] + c.after(:suite) do + end + + # Filter backtrace noise + backtrace_exclusion_patterns = [ + %r{spec_helper}, + %r{gems}, + ] + + if c.respond_to?(:backtrace_exclusion_patterns) + c.backtrace_exclusion_patterns = backtrace_exclusion_patterns + elsif c.respond_to?(:backtrace_clean_patterns) + c.backtrace_clean_patterns = backtrace_exclusion_patterns + end +end + +# Ensures that a module is defined +# @param module_name Name of the module +def ensure_module_defined(module_name) + module_name.split('::').reduce(Object) do |last_module, next_module| + last_module.const_set(next_module, Module.new) unless last_module.const_defined?(next_module, false) + last_module.const_get(next_module, false) + end end + +# 'spec_overrides' from sync.yml will appear below this line