From cd4ee5e537fbee5cadfc2e4117efafdebffd78b0 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Mon, 7 Aug 2023 12:18:22 +0000 Subject: [PATCH 01/18] Convert to PDK v3.0.0 --- .gitattributes | 5 + .gitignore | 31 +- .pdkignore | 43 +++ .puppet-lint.rc | 1 + .rspec | 2 + .rubocop.yml | 723 ++++++++++++++++++++++++++++++++++++++++ .sync.yml | 8 + .vscode/extensions.json | 6 + .yardopts | 1 + Gemfile | 77 ++++- Rakefile | 91 ++++- data/common.yaml | 1 + hiera.yaml | 21 ++ metadata.json | 55 ++- spec/default_facts.yml | 8 + spec/spec_helper.rb | 74 +++- 16 files changed, 1117 insertions(+), 30 deletions(-) create mode 100644 .gitattributes create mode 100644 .pdkignore create mode 100644 .puppet-lint.rc create mode 100644 .rspec create mode 100644 .rubocop.yml create mode 100644 .sync.yml create mode 100644 .vscode/extensions.json create mode 100644 .yardopts create mode 100644 data/common.yaml create mode 100644 hiera.yaml create mode 100644 spec/default_facts.yml 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 dcf5baf..3f15512 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,28 @@ -spec/fixtures -pkg/* - +.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/.pdkignore b/.pdkignore new file mode 100644 index 0000000..584438f --- /dev/null +++ b/.pdkignore @@ -0,0 +1,43 @@ +.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 +/.fixtures.yml +/Gemfile +/.gitattributes +/.gitignore +/.pdkignore +/.puppet-lint.rc +/Rakefile +/rakelib/ +/.rspec +/..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..5be1f9f --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,723 @@ +--- +require: +- rubocop-performance +- rubocop-rspec +AllCops: + DisplayCopNames: true + TargetRubyVersion: '2.6' + 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/GemFilename: + Enabled: false +Bundler/InsecureProtocolSource: + Enabled: false +Capybara/CurrentPathExpectation: + Enabled: false +Capybara/VisibilityMatcher: + 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/FeatureMethods: + Enabled: false +RSpec/ContainExactly: + 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/MatchArray: + 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/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 +Capybara/MatchStyle: + Enabled: false +Capybara/NegationMatcher: + Enabled: false +Capybara/SpecificActions: + Enabled: false +Capybara/SpecificFinders: + Enabled: false +Capybara/SpecificMatcher: + Enabled: false +Gemspec/DeprecatedAttributeAssignment: + Enabled: false +Gemspec/DevelopmentDependencies: + Enabled: false +Gemspec/RequireMFA: + Enabled: false +Layout/LineContinuationLeadingSpace: + Enabled: false +Layout/LineContinuationSpacing: + Enabled: false +Layout/LineEndStringConcatenationIndentation: + Enabled: false +Layout/SpaceBeforeBrackets: + Enabled: false +Lint/AmbiguousAssignment: + Enabled: false +Lint/AmbiguousOperatorPrecedence: + Enabled: false +Lint/AmbiguousRange: + Enabled: false +Lint/ConstantOverwrittenInRescue: + Enabled: false +Lint/DeprecatedConstants: + Enabled: false +Lint/DuplicateBranch: + Enabled: false +Lint/DuplicateMagicComment: + Enabled: false +Lint/DuplicateRegexpCharacterClassElement: + Enabled: false +Lint/EmptyBlock: + Enabled: false +Lint/EmptyClass: + Enabled: false +Lint/EmptyInPattern: + Enabled: false +Lint/IncompatibleIoSelectWithFiberScheduler: + Enabled: false +Lint/LambdaWithoutLiteralBlock: + Enabled: false +Lint/NoReturnInBeginEndBlocks: + Enabled: false +Lint/NonAtomicFileOperation: + Enabled: false +Lint/NumberedParameterAssignment: + Enabled: false +Lint/OrAssignmentToConstant: + Enabled: false +Lint/RedundantDirGlobSort: + Enabled: false +Lint/RefinementImportMethods: + Enabled: false +Lint/RequireRangeParentheses: + Enabled: false +Lint/RequireRelativeSelfPath: + Enabled: false +Lint/SymbolConversion: + Enabled: false +Lint/ToEnumArguments: + Enabled: false +Lint/TripleQuotes: + Enabled: false +Lint/UnexpectedBlockArity: + Enabled: false +Lint/UnmodifiedReduceAccumulator: + Enabled: false +Lint/UselessRescue: + Enabled: false +Lint/UselessRuby2Keywords: + Enabled: false +Metrics/CollectionLiteralLength: + Enabled: false +Naming/BlockForwarding: + Enabled: false +Performance/CollectionLiteralInLoop: + Enabled: false +Performance/ConcurrentMonotonicTime: + Enabled: false +Performance/MapCompact: + Enabled: false +Performance/RedundantEqualityComparisonBlock: + Enabled: false +Performance/RedundantSplitRegexpArgument: + Enabled: false +Performance/StringIdentifierArgument: + Enabled: false +RSpec/BeEq: + Enabled: false +RSpec/BeNil: + Enabled: false +RSpec/ChangeByZero: + Enabled: false +RSpec/ClassCheck: + Enabled: false +RSpec/DuplicatedMetadata: + Enabled: false +RSpec/ExcessiveDocstringSpacing: + Enabled: false +RSpec/FactoryBot/ConsistentParenthesesStyle: + Enabled: false +RSpec/FactoryBot/FactoryNameStyle: + Enabled: false +RSpec/FactoryBot/SyntaxMethods: + Enabled: false +RSpec/IdenticalEqualityAssertion: + Enabled: false +RSpec/NoExpectationExample: + Enabled: false +RSpec/PendingWithoutReason: + Enabled: false +RSpec/Rails/AvoidSetupHook: + Enabled: false +RSpec/Rails/HaveHttpStatus: + Enabled: false +RSpec/Rails/InferredSpecType: + Enabled: false +RSpec/Rails/MinitestAssertions: + Enabled: false +RSpec/Rails/TravelAround: + Enabled: false +RSpec/RedundantAround: + Enabled: false +RSpec/SkipBlockInsideExample: + Enabled: false +RSpec/SortMetadata: + Enabled: false +RSpec/SubjectDeclaration: + Enabled: false +RSpec/VerifiedDoubleReference: + Enabled: false +Security/CompoundHash: + Enabled: false +Security/IoMethods: + Enabled: false +Style/ArgumentsForwarding: + Enabled: false +Style/ArrayIntersect: + Enabled: false +Style/CollectionCompact: + Enabled: false +Style/ComparableClamp: + Enabled: false +Style/ConcatArrayLiterals: + Enabled: false +Style/DirEmpty: + Enabled: false +Style/DocumentDynamicEvalDefinition: + Enabled: false +Style/EmptyHeredoc: + Enabled: false +Style/EndlessMethod: + Enabled: false +Style/EnvHome: + Enabled: false +Style/FetchEnvVar: + Enabled: false +Style/FileEmpty: + Enabled: false +Style/FileRead: + Enabled: false +Style/FileWrite: + Enabled: false +Style/HashConversion: + Enabled: false +Style/HashExcept: + Enabled: false +Style/IfWithBooleanLiteralBranches: + Enabled: false +Style/InPatternThen: + Enabled: false +Style/MagicCommentFormat: + Enabled: false +Style/MapCompactWithConditionalBlock: + Enabled: false +Style/MapToHash: + Enabled: false +Style/MapToSet: + Enabled: false +Style/MinMaxComparison: + Enabled: false +Style/MultilineInPatternThen: + Enabled: false +Style/NegatedIfElseCondition: + Enabled: false +Style/NestedFileDirname: + Enabled: false +Style/NilLambda: + Enabled: false +Style/NumberedParameters: + Enabled: false +Style/NumberedParametersLimit: + Enabled: false +Style/ObjectThen: + Enabled: false +Style/OpenStructUse: + Enabled: false +Style/OperatorMethodCall: + Enabled: false +Style/QuotedSymbols: + Enabled: false +Style/RedundantArgument: + Enabled: false +Style/RedundantConstantBase: + Enabled: false +Style/RedundantDoubleSplatHashBraces: + Enabled: false +Style/RedundantEach: + Enabled: false +Style/RedundantHeredocDelimiterQuotes: + Enabled: false +Style/RedundantInitialize: + Enabled: false +Style/RedundantSelfAssignmentBranch: + Enabled: false +Style/RedundantStringEscape: + Enabled: false +Style/SelectByRegexp: + Enabled: false +Style/StringChars: + 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/.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/Gemfile b/Gemfile index d4c141f..add1873 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,73 @@ -source :rubygems +source ENV['GEM_SOURCE'] || 'https://rubygems.org' -puppetversion = ENV['PUPPET_VERSION'] -gem 'puppet', puppetversion, :require => false -gem 'puppet-lint' -gem 'rspec-puppet' -gem 'puppetlabs_spec_helper', '>= 0.4.0' +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 "racc", '~> 1.4.0', require: false if Gem::Requirement.create(['>= 2.7.0', '< 3.0.0']).satisfied_by?(Gem::Version.new(RUBY_VERSION.dup)) + gem "voxpupuli-puppet-lint-plugins", '~> 5.0', require: false + gem "facterdb", '~> 1.18', require: false + gem "metadata-json-lint", '~> 3.0', require: false + gem "puppetlabs_spec_helper", '~> 6.0', require: false + gem "rspec-puppet-facts", '~> 2.0', require: false + gem "codecov", '~> 0.2', require: false + gem "dependency_checker", '~> 1.0.0', 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.48.1', require: false + gem "rubocop-performance", '= 1.16.0', require: false + gem "rubocop-rspec", '= 2.19.0', 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', 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/Rakefile b/Rakefile index 184b9b5..74415a9 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,88 @@ -require 'rubygems' +# frozen_string_literal: true + +require 'bundler' +require 'puppet_litmus/rake_tasks' if Gem.loaded_specs.key? 'puppet_litmus' require 'puppetlabs_spec_helper/rake_tasks' -require 'puppet-lint' -PuppetLint.configuration.send("disable_80chars") -PuppetLint.configuration.send("disable_autoloader_layout") -PuppetLint.configuration.send("disable_quoted_booleans") +require 'puppet-syntax/tasks/puppet-syntax' +require 'github_changelog_generator/task' if Gem.loaded_specs.key? 'github_changelog_generator' +require 'puppet-strings/tasks' if Gem.loaded_specs.key? 'puppet-strings' + +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 Gem.loaded_specs.key? 'github_changelog_generator' + 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 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/metadata.json b/metadata.json index 59abfd5..05114f3 100644 --- a/metadata.json +++ b/metadata.json @@ -2,39 +2,64 @@ "name": "thias-sysctl", "version": "1.0.7", "author": "Matthias Saou", - "license": "Apache-2.0", "summary": "Sysctl module", + "license": "Apache-2.0", "source": "git://github.com/thias/puppet-sysctl.git", "project_page": "https://github.com/thias/puppet-sysctl", "issues_url": "https://github.com/thias/puppet-sysctl/issues", - "tags": [ "sysctl", "os" ], + "dependencies": [ + + ], "operatingsystem_support": [ { - "operatingsystem": "RedHat", - "operatingsystemrelease": [ "5", "6", "7", "8" ] + "operatingsystem": "RedHat", + "operatingsystemrelease": [ + "5", + "6", + "7", + "8" + ] }, { - "operatingsystem": "CentOS", - "operatingsystemrelease": [ "5", "6", "7", "8" ] + "operatingsystem": "CentOS", + "operatingsystemrelease": [ + "5", + "6", + "7", + "8" + ] }, { - "operatingsystem": "Debian", - "operatingsystemrelease": [ "6", "7", "8" ] + "operatingsystem": "Debian", + "operatingsystemrelease": [ + "6", + "7", + "8" + ] }, { - "operatingsystem": "Ubuntu", - "operatingsystemrelease": [ "12", "14" ] + "operatingsystem": "Ubuntu", + "operatingsystemrelease": [ + "12", + "14" + ] } ], "requirements": [ { - "name": "pe", - "version_requirement": "3.x" + "name": "pe", + "version_requirement": "3.x" }, { - "name": "puppet", - "version_requirement": ">=2.7.20 <8.0.0" + "name": "puppet", + "version_requirement": ">=2.7.20 <8.0.0" } ], - "dependencies": [] + "tags": [ + "sysctl", + "os" + ], + "pdk-version": "3.0.0", + "template-url": "pdk-default#3.0.0", + "template-ref": "tags/3.0.0-0-g056e50d" } 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/spec_helper.rb b/spec/spec_helper.rb index dc7e9f4..6820ceb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,74 @@ -require 'rubygems' +# frozen_string_literal: true + +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), permitted_classes: [], permitted_symbols: [], aliases: true)) + rescue StandardError => 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.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 + RSpec::Puppet::Coverage.report!(0) + 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 From d27063d6579abf91e95bd5fc01a251330e62e4e9 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Mon, 7 Aug 2023 12:20:05 +0000 Subject: [PATCH 02/18] Add OS facts to unit tests --- spec/classes/sysctl_base_spec.rb | 6 ++++++ spec/defines/sysctl_init_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/spec/classes/sysctl_base_spec.rb b/spec/classes/sysctl_base_spec.rb index a1d47a2..93745b5 100644 --- a/spec/classes/sysctl_base_spec.rb +++ b/spec/classes/sysctl_base_spec.rb @@ -1,6 +1,12 @@ require 'spec_helper' describe 'sysctl::base', :type => :class do + let(:facts) do + { + osfamily: 'RedHat', + operatingsystemmajrelease: '8', + } + end it { should create_class('sysctl::base') } it { should contain_file('/etc/sysctl.d') } diff --git a/spec/defines/sysctl_init_spec.rb b/spec/defines/sysctl_init_spec.rb index 1f8db67..aa164ec 100644 --- a/spec/defines/sysctl_init_spec.rb +++ b/spec/defines/sysctl_init_spec.rb @@ -1,6 +1,13 @@ require 'spec_helper' describe 'sysctl', :type => :define do + let(:facts) do + { + osfamily: 'RedHat', + operatingsystemmajrelease: '8', + } + end + let(:title) { 'net.ipv4.ip_forward'} context 'present' do From 8f15022500a4a5430a6f0c2ee59487771db7eb65 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Mon, 7 Aug 2023 13:34:09 +0000 Subject: [PATCH 03/18] Satisfy puppet-lint --- manifests/base.pp | 53 ++++++++++++++++------- manifests/init.pp | 73 ++++++++++++++++++-------------- manifests/params.pp | 15 +++---- spec/classes/sysctl_base_spec.rb | 8 +++- spec/defines/sysctl_init_spec.rb | 8 +++- 5 files changed, 98 insertions(+), 59 deletions(-) diff --git a/manifests/base.pp b/manifests/base.pp index a0b20c2..b1976d3 100644 --- a/manifests/base.pp +++ b/manifests/base.pp @@ -1,19 +1,44 @@ -# Class: sysctl::base +# @summary +# Common part for the sysctl definition. Not meant to be used on its own. # -# Common part for the sysctl definition. Not meant to be used on its own. +# @param purge +# Boolean to choose if $sysctl_dir_path should get purged. +# +# @param values +# Hash of sysctl keys and their values to managed. +# +# @param hiera_merge_values +# Boolean to choose if $values to be used should be searched in hiera. +# +# @param symlink99 +# Boolean to choose if 99-sysctl.conf should be created. +# +# @param sysctl_dir +# Boolean to choose if $sysctl_dir_path should be managed. +# +# @param sysctl_dir_path +# Absolute path of sysctl directory. +# +# @param sysctl_dir_owner +# Owner for sysctl directory. +# +# @param sysctl_dir_group +# Group for sysctl directory. +# +# @param sysctl_dir_mode +# Mode for sysctl directory. # class sysctl::base ( - $purge = false, - $values = undef, - $hiera_merge_values = false, - $symlink99 = $::sysctl::params::symlink99, - $sysctl_dir = $::sysctl::params::sysctl_dir, - $sysctl_dir_path = $::sysctl::params::sysctl_dir_path, - $sysctl_dir_owner = $::sysctl::params::sysctl_dir_owner, - $sysctl_dir_group = $::sysctl::params::sysctl_dir_group, - $sysctl_dir_mode = $::sysctl::params::sysctl_dir_mode, -) inherits ::sysctl::params { - + Boolean $purge = false, + Optional[Hash] $values = undef, + Boolean $hiera_merge_values = false, + Boolean $symlink99 = $sysctl::params::symlink99, + Boolean $sysctl_dir = $sysctl::params::sysctl_dir, + String[1] $sysctl_dir_path = $sysctl::params::sysctl_dir_path, + String[1] $sysctl_dir_owner = $sysctl::params::sysctl_dir_owner, + String[1] $sysctl_dir_group = $sysctl::params::sysctl_dir_group, + String[1] $sysctl_dir_mode = $sysctl::params::sysctl_dir_mode, +) inherits sysctl::params { # Hiera support if $hiera_merge_values == true { $values_real = hiera_hash('sysctl::base::values', {}) @@ -48,6 +73,4 @@ } } } - } - diff --git a/manifests/init.pp b/manifests/init.pp index 70e2ee5..5ffe53e 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,31 +1,46 @@ -# Define: sysctl +# @summary +# Manage sysctl variable values. # -# Manage sysctl variable values. +# @example +# Sample Usage : +# sysctl { 'net.ipv6.bindv6only': value => '1' } # -# Parameters: -# $value: -# The value for the sysctl parameter. Mandatory, unless $ensure is 'absent'. -# $prefix: -# Optional prefix for the sysctl.d file to be created. Default: none. -# $ensure: -# Whether the variable's value should be 'present' or 'absent'. -# Defaults to 'present'. +# @param ensure +# Whether the variable's value should be 'present' or 'absent'. +# Defaults to 'present'. # -# Sample Usage : -# sysctl { 'net.ipv6.bindv6only': value => '1' } +# @param value +# The value for the sysctl parameter. Mandatory, unless $ensure is 'absent'. +# +# @param prefix +# Optional prefix for the sysctl.d file to be created. Default: none. +# +# @param suffix +# Optional suffix for the sysctl.d file to be created. Default: '.conf'. +# +# @param comment +# Comment(s) to be added to the sysctl.d file. +# +# @param content +# Content for the sysctl.d file to be used instead of the template. +# +# @param source +# Source file for the sysctl.d file to be used instead of the template. +# +# @param enforce +# Enforce configured value during each run (can't work with custom files). # define sysctl ( - $ensure = undef, - $value = undef, - $prefix = undef, - $suffix = '.conf', - $comment = undef, - $content = undef, - $source = undef, - $enforce = true, + Optional[Enum['present', 'absent']] $ensure = undef, + Optional[String[1]] $value = undef, + Optional[String[1]] $prefix = undef, + String $suffix = '.conf', + Optional[Variant[Array, String[1]]] $comment = undef, + Optional[String[1]] $content = undef, + Optional[String[1]] $source = undef, + Boolean $enforce = true, ) { - - include '::sysctl::base' + include sysctl::base # If we have a prefix, then add the dash to it if $prefix { @@ -48,7 +63,6 @@ } if $ensure != 'absent' { - # Present # The permanent change @@ -68,7 +82,7 @@ # The immediate change + re-check on each run "just in case" exec { "sysctl-${title}": command => "sysctl -p /etc/sysctl.d/${sysctl_d_file}", - path => [ '/usr/sbin', '/sbin', '/usr/bin', '/bin' ], + path => ['/usr/sbin', '/sbin', '/usr/bin', '/bin'], refreshonly => true, require => File["/etc/sysctl.d/${sysctl_d_file}"], } @@ -76,7 +90,7 @@ # For the few original values from the main file exec { "update-sysctl.conf-${title}": command => "sed -i -e 's#^${title} *=.*#${title} = ${value}#' /etc/sysctl.conf", - path => [ '/usr/sbin', '/sbin', '/usr/bin', '/bin' ], + path => ['/usr/sbin', '/sbin', '/usr/bin', '/bin'], refreshonly => true, onlyif => "grep -E '^${title} *=' /etc/sysctl.conf", } @@ -90,21 +104,16 @@ $qvalue = shellquote("${value}") # lint:endignore exec { "enforce-sysctl-value-${qtitle}": - unless => "/usr/bin/test \"$(/sbin/sysctl -n ${qtitle})\" = ${qvalue}", - command => "/sbin/sysctl -w ${qtitle}=${qvalue}", + unless => "/usr/bin/test \"$(/sbin/sysctl -n ${qtitle})\" = ${qvalue}", + command => "/sbin/sysctl -w ${qtitle}=${qvalue}", } } - } else { - # Absent # We cannot restore values, since defaults can not be known... reboot :-/ file { "/etc/sysctl.d/${sysctl_d_file}": ensure => absent, } - } - } - diff --git a/manifests/params.pp b/manifests/params.pp index f302c2c..090eb4d 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -1,16 +1,17 @@ +# @api private class sysctl::params { - # Keep the original symlink if we purge, to avoid ping-pong with initscripts - if ($::osfamily == 'RedHat' and - versioncmp($::operatingsystemmajrelease, '7') >= 0) or - ($::osfamily == 'Debian' and - versioncmp($::operatingsystemmajrelease, '8') >= 0) { + if ( + $facts['os']['family'] == 'RedHat' and versioncmp($facts['os']['release']['major'], '7') >= 0 + ) or ( + $facts['os']['family'] == 'Debian' and versioncmp($facts['os']['release']['major'], '8') >= 0 + ) { $symlink99 = true } else { $symlink99 = false } - case $::osfamily { + case $facts['os']['family'] { 'FreeBSD': { $sysctl_dir = false } @@ -22,6 +23,4 @@ $sysctl_dir_mode = '0755' } } - } - diff --git a/spec/classes/sysctl_base_spec.rb b/spec/classes/sysctl_base_spec.rb index 93745b5..daa497b 100644 --- a/spec/classes/sysctl_base_spec.rb +++ b/spec/classes/sysctl_base_spec.rb @@ -3,8 +3,12 @@ describe 'sysctl::base', :type => :class do let(:facts) do { - osfamily: 'RedHat', - operatingsystemmajrelease: '8', + os: { + family: 'RedHat', + release: { + major: '8', + }, + }, } end diff --git a/spec/defines/sysctl_init_spec.rb b/spec/defines/sysctl_init_spec.rb index aa164ec..1d0636e 100644 --- a/spec/defines/sysctl_init_spec.rb +++ b/spec/defines/sysctl_init_spec.rb @@ -3,8 +3,12 @@ describe 'sysctl', :type => :define do let(:facts) do { - osfamily: 'RedHat', - operatingsystemmajrelease: '8', + os: { + family: 'RedHat', + release: { + major: '8', + }, + }, } end From db4460065e00670b418d397490e70cd0513143f4 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Mon, 7 Aug 2023 13:44:36 +0000 Subject: [PATCH 04/18] Satisfy rubocop --- metadata.json | 4 ---- spec/classes/sysctl_base_spec.rb | 8 +++----- spec/defines/sysctl_init_spec.rb | 26 +++++++++++++------------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/metadata.json b/metadata.json index 05114f3..ee79900 100644 --- a/metadata.json +++ b/metadata.json @@ -46,10 +46,6 @@ } ], "requirements": [ - { - "name": "pe", - "version_requirement": "3.x" - }, { "name": "puppet", "version_requirement": ">=2.7.20 <8.0.0" diff --git a/spec/classes/sysctl_base_spec.rb b/spec/classes/sysctl_base_spec.rb index daa497b..a3ebaa3 100644 --- a/spec/classes/sysctl_base_spec.rb +++ b/spec/classes/sysctl_base_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'sysctl::base', :type => :class do +describe 'sysctl::base', type: :class do let(:facts) do { os: { @@ -12,8 +12,6 @@ } end - it { should create_class('sysctl::base') } - it { should contain_file('/etc/sysctl.d') } - + it { is_expected.to create_class('sysctl::base') } + it { is_expected.to contain_file('/etc/sysctl.d') } end - diff --git a/spec/defines/sysctl_init_spec.rb b/spec/defines/sysctl_init_spec.rb index 1d0636e..95a7b1b 100644 --- a/spec/defines/sysctl_init_spec.rb +++ b/spec/defines/sysctl_init_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'sysctl', :type => :define do +describe 'sysctl', type: :define do let(:facts) do { os: { @@ -12,25 +12,25 @@ } end - let(:title) { 'net.ipv4.ip_forward'} + let(:title) { 'net.ipv4.ip_forward' } context 'present' do - let(:params) { { :value => '1' } } + let(:params) { { value: '1' } } - it { should contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with( - :content => "net.ipv4.ip_forward = 1\n", - :ensure => nil - ) } + it { + is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with( + content: "net.ipv4.ip_forward = 1\n", + ensure: nil, + ) + } - it { should contain_exec('sysctl-net.ipv4.ip_forward') } - it { should contain_exec('update-sysctl.conf-net.ipv4.ip_forward')} + it { is_expected.to contain_exec('sysctl-net.ipv4.ip_forward') } + it { is_expected.to contain_exec('update-sysctl.conf-net.ipv4.ip_forward') } end context 'absent' do - let(:params) { { :ensure => 'absent' } } + let(:params) { { ensure: 'absent' } } - it { should contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_ensure('absent') } + it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_ensure('absent') } end - end - From 0514bab30553dedad95397982c33bf987589f452 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Mon, 7 Aug 2023 13:46:10 +0000 Subject: [PATCH 05/18] Add support for Puppet 8 --- metadata.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metadata.json b/metadata.json index ee79900..ec2ec23 100644 --- a/metadata.json +++ b/metadata.json @@ -48,7 +48,7 @@ "requirements": [ { "name": "puppet", - "version_requirement": ">=2.7.20 <8.0.0" + "version_requirement": ">=2.7.20 < 9.0.0" } ], "tags": [ From f13fda24731b1e7fdbd8416d906829a6797c2c6d Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Mon, 7 Aug 2023 13:48:03 +0000 Subject: [PATCH 06/18] Add Github actions for testing with Puppet 7 & 8 --- .../workflows/run-pdk-tests-on-puppet-7.yml | 34 +++++++++++++++++++ .../workflows/run-pdk-tests-on-puppet-8.yml | 34 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 .github/workflows/run-pdk-tests-on-puppet-7.yml create mode 100644 .github/workflows/run-pdk-tests-on-puppet-8.yml diff --git a/.github/workflows/run-pdk-tests-on-puppet-7.yml b/.github/workflows/run-pdk-tests-on-puppet-7.yml new file mode 100644 index 0000000..1b13f18 --- /dev/null +++ b/.github/workflows/run-pdk-tests-on-puppet-7.yml @@ -0,0 +1,34 @@ +name: Run PDK tests on Puppet 7.x + +on: + - push + - pull_request + +jobs: + validate-7: + runs-on: ubuntu-latest + steps: + - name: Clone repository + uses: actions/checkout@v2 + + - name: Run pdk validate + uses: puppets-epic-show-theatre/action-pdk-validate@v1 + with: + puppet-version: "7" + # [optional] A string indicating the Puppet version to validate against, such as "5.4.2" or "5.5". + # pe-version: "" + # [optional] A string indicating the PE version to validate against, such as "2017.3.5" or "2018.1". + + test-7: + runs-on: ubuntu-latest + steps: + - name: Clone repository + uses: actions/checkout@v2 + + - name: Run unit tests + uses: puppets-epic-show-theatre/action-pdk-test-unit@v1 + with: + puppet-version: "7" + # [optional] A string indicating the Puppet version to validate against, such as "5.4.2" or "5.5". + # pe-version: "" + # [optional] A string indicating the PE version to validate against, such as "2017.3.5" or "2018.1". diff --git a/.github/workflows/run-pdk-tests-on-puppet-8.yml b/.github/workflows/run-pdk-tests-on-puppet-8.yml new file mode 100644 index 0000000..12460e0 --- /dev/null +++ b/.github/workflows/run-pdk-tests-on-puppet-8.yml @@ -0,0 +1,34 @@ +name: Run PDK tests on Puppet 8.x + +on: + - push + - pull_request + +jobs: + validate-8: + runs-on: ubuntu-latest + steps: + - name: Clone repository + uses: actions/checkout@v2 + + - name: Run pdk validate + uses: puppets-epic-show-theatre/action-pdk-validate@v1 + with: + puppet-version: "8" + # [optional] A string indicating the Puppet version to validate against, such as "5.4.2" or "5.5". + # pe-version: "" + # [optional] A string indicating the PE version to validate against, such as "2017.3.5" or "2018.1". + + test-8: + runs-on: ubuntu-latest + steps: + - name: Clone repository + uses: actions/checkout@v2 + + - name: Run unit tests + uses: puppets-epic-show-theatre/action-pdk-test-unit@v1 + with: + puppet-version: "8" + # [optional] A string indicating the Puppet version to validate against, such as "5.4.2" or "5.5". + # pe-version: "" + # [optional] A string indicating the PE version to validate against, such as "2017.3.5" or "2018.1". From 8876a7dbdad1abb654188e06563bbe9f02a9d151 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Tue, 8 Aug 2023 11:12:14 +0000 Subject: [PATCH 07/18] Remove FreeBSD traces from params.pp --- manifests/params.pp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/manifests/params.pp b/manifests/params.pp index 090eb4d..f0aca34 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -11,16 +11,9 @@ $symlink99 = false } - case $facts['os']['family'] { - 'FreeBSD': { - $sysctl_dir = false - } - default: { - $sysctl_dir = true - $sysctl_dir_path = '/etc/sysctl.d' - $sysctl_dir_owner = 'root' - $sysctl_dir_group = 'root' - $sysctl_dir_mode = '0755' - } - } + $sysctl_dir = true + $sysctl_dir_path = '/etc/sysctl.d' + $sysctl_dir_owner = 'root' + $sysctl_dir_group = 'root' + $sysctl_dir_mode = '0755' } From 14933a82926b95b62d281cbd6d72c6c319093e19 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Tue, 8 Aug 2023 11:13:40 +0000 Subject: [PATCH 08/18] Refactor unit tests - utilize FacterDB for complete OS facts - add tests to become more feature complete --- spec/classes/base_spec.rb | 161 ++++++++++++++++++++++++++ spec/classes/sysctl_base_spec.rb | 17 --- spec/defines/init_spec.rb | 188 +++++++++++++++++++++++++++++++ spec/defines/sysctl_init_spec.rb | 36 ------ 4 files changed, 349 insertions(+), 53 deletions(-) create mode 100644 spec/classes/base_spec.rb delete mode 100644 spec/classes/sysctl_base_spec.rb create mode 100644 spec/defines/init_spec.rb delete mode 100644 spec/defines/sysctl_init_spec.rb diff --git a/spec/classes/base_spec.rb b/spec/classes/base_spec.rb new file mode 100644 index 0000000..43c87ec --- /dev/null +++ b/spec/classes/base_spec.rb @@ -0,0 +1,161 @@ +require 'spec_helper' + +describe 'sysctl::base', type: :class do + on_supported_os.sort.each do |os, facts| + # define os specific defaults + symlink99 = if (facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'].to_i >= 7) || + (facts[:os]['family'] == 'Debian' && facts[:os]['release']['major'].to_i >= 8) + true + else + false + end + + describe "on #{os} with default values for parameters" do + let(:facts) { facts } + + it { is_expected.to create_class('sysctl::base') } + it { is_expected.to contain_class('sysctl::params') } + + it do + is_expected.to contain_file('/etc/sysctl.d').only_with( + 'ensure' => 'directory', + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0755', + 'purge' => false, + 'recurse' => false, + ) + end + + if symlink99 == true + it do + is_expected.to contain_file('/etc/sysctl.d/99-sysctl.conf').only_with( + 'ensure' => 'link', + 'owner' => 'root', + 'group' => 'root', + 'target' => '../sysctl.conf', + ) + end + else + it { is_expected.not_to contain_file('/etc/sysctl.d/99-sysctl.conf') } + end + end + end + + describe 'parameters on supported OS' do + # tests should be OS independent, so we only test one OS + test_on = { + supported_os: [ + { + 'operatingsystem' => 'RedHat', + 'operatingsystemrelease' => ['8'], + }, + ], + } + on_supported_os(test_on).sort.each do |_os, os_facts| + let(:facts) { os_facts } + + context 'with with purge set to valid true' do + let(:params) { { purge: true } } + + it { is_expected.to contain_file('/etc/sysctl.d').with_purge(true) } + it { is_expected.to contain_file('/etc/sysctl.d').with_recurse(true) } + end + + context 'with with values set to valid value' do + let(:params) do + { + values: { + 'net.ipv4.ip_forward' => { + 'value' => '1', + }, + 'net.core.somaxconn' => { + 'value' => '65536', + }, + 'vm.swappiness' => { + 'ensure' => 'absent', + }, + } + } + end + + it { is_expected.to contain_sysctl('net.ipv4.ip_forward').with_value('1') } + it { is_expected.to contain_sysctl('net.core.somaxconn').with_value('65536') } + it { is_expected.to contain_sysctl('vm.swappiness').with_ensure('absent') } + + # [only here to reach 100% resource coverage] + it { is_expected.to contain_exec('enforce-sysctl-value-net.ipv4.ip_forward') } + it { is_expected.to contain_exec('sysctl-net.ipv4.ip_forward') } + it { is_expected.to contain_exec('update-sysctl.conf-net.ipv4.ip_forward') } + it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf') } + it { is_expected.to contain_exec('enforce-sysctl-value-net.core.somaxconn') } + it { is_expected.to contain_exec('sysctl-net.core.somaxconn') } + it { is_expected.to contain_exec('update-sysctl.conf-net.core.somaxconn') } + it { is_expected.to contain_file('/etc/sysctl.d/net.core.somaxconn.conf') } + it { is_expected.to contain_file('/etc/sysctl.d/vm.swappiness.conf') } + # [/only here to reach 100% resource coverage] + end + + context 'with with symlink99 set to valid true' do + let(:params) { { symlink99: true } } + + it { is_expected.to contain_file('/etc/sysctl.d/99-sysctl.conf') } + end + + context 'with with symlink99 set to valid true when sysctl_dir_path is set to /test/ing (directory outside /etc)' do + let(:params) { { symlink99: true, sysctl_dir_path: '/test/ing' } } + + it { is_expected.not_to contain_file('/test/ing/99-sysctl.conf') } + it { is_expected.to contain_file('/test/ing') } + it { is_expected.to have_file_resource_count(1) } + end + + context 'with with symlink99 set to valid true when sysctl_dir_path is set to /etc/testing (directory inside /etc)' do + let(:params) { { symlink99: true, sysctl_dir_path: '/etc/testing' } } + + it { is_expected.to contain_file('/etc/testing/99-sysctl.conf') } + end + + context 'with with symlink99 set to valid false' do + let(:params) { { symlink99: false } } + + it { is_expected.not_to contain_file('/etc/sysctl.d/99-sysctl.conf') } + it { is_expected.to have_file_resource_count(1) } # only '/etc/sysctl.d' + end + + context 'with with sysctl_dir set to valid false' do + let(:params) { { sysctl_dir: false } } + + it { is_expected.not_to contain_file('/etc/sysctl.d/99-sysctl.conf') } + it { is_expected.to have_file_resource_count(0) } + end + + context 'with with sysctl_dir_path set to valid value' do + let(:params) { { sysctl_dir_path: '/etc/testing' } } + + it { is_expected.to contain_file('/etc/testing') } + it { is_expected.to contain_file('/etc/testing/99-sysctl.conf') } + end + + context 'with with sysctl_dir_owner set to valid value' do + let(:params) { { sysctl_dir_owner: 'testing' } } + + it { is_expected.to contain_file('/etc/sysctl.d').with_owner('testing') } + it { is_expected.to contain_file('/etc/sysctl.d/99-sysctl.conf').with_owner('testing') } + end + + context 'with with sysctl_dir_group set to valid value' do + let(:params) { { sysctl_dir_group: 'testing' } } + + it { is_expected.to contain_file('/etc/sysctl.d').with_group('testing') } + it { is_expected.to contain_file('/etc/sysctl.d/99-sysctl.conf').with_group('testing') } + end + + context 'with with sysctl_dir_mode set to valid value' do + let(:params) { { sysctl_dir_mode: '0242' } } + + it { is_expected.to contain_file('/etc/sysctl.d').with_mode('0242') } + end + end + end +end diff --git a/spec/classes/sysctl_base_spec.rb b/spec/classes/sysctl_base_spec.rb deleted file mode 100644 index a3ebaa3..0000000 --- a/spec/classes/sysctl_base_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'spec_helper' - -describe 'sysctl::base', type: :class do - let(:facts) do - { - os: { - family: 'RedHat', - release: { - major: '8', - }, - }, - } - end - - it { is_expected.to create_class('sysctl::base') } - it { is_expected.to contain_file('/etc/sysctl.d') } -end diff --git a/spec/defines/init_spec.rb b/spec/defines/init_spec.rb new file mode 100644 index 0000000..1b3adb7 --- /dev/null +++ b/spec/defines/init_spec.rb @@ -0,0 +1,188 @@ +require 'spec_helper' + +describe 'sysctl', type: :define do + let(:title) { 'net.ipv4.ip_forward' } + + on_supported_os.sort.each do |os, facts| + # define os specific defaults + symlink99 = if (facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'].to_i >= 7) || + (facts[:os]['family'] == 'Debian' && facts[:os]['release']['major'].to_i >= 8) + true + else + false + end + + describe "on #{os} with default values for parameters" do + let(:facts) { facts } + + it { is_expected.to contain_class('sysctl::base') } + # [only here to reach 100% resource coverage] + it { is_expected.to contain_class('sysctl::params') } + it { is_expected.to contain_file('/etc/sysctl.d/99-sysctl.conf') } if symlink99 == true + # [/only here to reach 100% resource coverage] + + it do + is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').only_with( + ensure: nil, + owner: 'root', + group: 'root', + mode: '0644', + content: "net.ipv4.ip_forward = \n", + source: nil, + notify: ['Exec[sysctl-net.ipv4.ip_forward]', 'Exec[update-sysctl.conf-net.ipv4.ip_forward]'], + ) + end + + it do + is_expected.to contain_exec('sysctl-net.ipv4.ip_forward').only_with( + command: 'sysctl -p /etc/sysctl.d/net.ipv4.ip_forward.conf', + path: ['/usr/sbin', '/sbin', '/usr/bin', '/bin'], + refreshonly: true, + require: 'File[/etc/sysctl.d/net.ipv4.ip_forward.conf]', + ) + end + + it do + is_expected.to contain_exec('update-sysctl.conf-net.ipv4.ip_forward').only_with( + command: "sed -i -e 's#^net.ipv4.ip_forward *=.*#net.ipv4.ip_forward = #' /etc/sysctl.conf", + path: ['/usr/sbin', '/sbin', '/usr/bin', '/bin'], + refreshonly: true, + onlyif: "grep -E '^net.ipv4.ip_forward *=' /etc/sysctl.conf", + ) + end + + it do + is_expected.to contain_exec('enforce-sysctl-value-net.ipv4.ip_forward').only_with( + unless: '/usr/bin/test "$(/sbin/sysctl -n net.ipv4.ip_forward)" = ""', + command: '/sbin/sysctl -w net.ipv4.ip_forward=""', + ) + end + end + end + + describe 'parameters on supported OS' do + # tests should be OS independent, so we only test one OS + test_on = { + supported_os: [ + { + 'operatingsystem' => 'RedHat', + 'operatingsystemrelease' => ['8'], + }, + ], + } + on_supported_os(test_on).sort.each do |_os, os_facts| + let(:facts) { os_facts } + + context 'with ensure set to valid absent' do + let(:params) { { ensure: 'absent' } } + + it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').only_with_ensure('absent') } + end + + context 'with value set to valid 1' do + let(:params) { { value: '1' } } + + it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_content("net.ipv4.ip_forward = 1\n") } + + it do + is_expected.to contain_exec('update-sysctl.conf-net.ipv4.ip_forward').with_command( + "sed -i -e 's#^net.ipv4.ip_forward *=.*#net.ipv4.ip_forward = 1#' /etc/sysctl.conf", + ) + end + + it do + is_expected.to contain_exec('enforce-sysctl-value-net.ipv4.ip_forward').only_with( + unless: '/usr/bin/test "$(/sbin/sysctl -n net.ipv4.ip_forward)" = 1', + command: '/sbin/sysctl -w net.ipv4.ip_forward=1', + ) + end + end + + context 'with prefix set to valid .testing' do + let(:params) { { prefix: 'testing' } } + + it { is_expected.to contain_file('/etc/sysctl.d/testing-net.ipv4.ip_forward.conf') } + it do + is_expected.to contain_exec('sysctl-net.ipv4.ip_forward').with( + command: 'sysctl -p /etc/sysctl.d/testing-net.ipv4.ip_forward.conf', + require: 'File[/etc/sysctl.d/testing-net.ipv4.ip_forward.conf]', + ) + end + end + + context 'with prefix set to valid .testing when ensure is set to absent' do + let(:params) { { prefix: 'testing', ensure: 'absent' } } + + it { is_expected.to contain_file('/etc/sysctl.d/testing-net.ipv4.ip_forward.conf') } + end + + context 'with suffix set to valid .testing' do + let(:params) { { suffix: '.testing' } } + + it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.testing') } + it do + is_expected.to contain_exec('sysctl-net.ipv4.ip_forward').with( + command: 'sysctl -p /etc/sysctl.d/net.ipv4.ip_forward.testing', + require: 'File[/etc/sysctl.d/net.ipv4.ip_forward.testing]', + ) + end + end + + context 'with suffix set to valid .testing when ensure is set to absent' do + let(:params) { { suffix: '.testing', ensure: 'absent' } } + + it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.testing') } + end + + context 'with comment set to valid string testing' do + let(:params) { { comment: 'testing' } } + + it do + is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with( + content: "# testing\nnet.ipv4.ip_forward = \n", + ) + end + end + + context 'with comment set to valid array [test, ing]' do + let(:params) { { comment: ['test', 'ing'] } } + + it do + is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with( + content: "# test\n# ing\nnet.ipv4.ip_forward = \n", + ) + end + end + + context 'with content set to valid testing' do + let(:params) { { content: 'testing' } } + + it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_content('testing') } + + it do + is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with( + content: 'testing', + ) + end + end + + context 'with source set to valid testing' do + let(:params) { { source: 'testing' } } + + it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_source('testing') } + + it do + is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with( + source: 'testing', + ) + end + end + + context 'with enforce set to valid false' do + let(:params) { { enforce: false } } + + it { is_expected.not_to contain_exec('enforce-sysctl-value-net.ipv4.ip_forward') } + end + end + end +end diff --git a/spec/defines/sysctl_init_spec.rb b/spec/defines/sysctl_init_spec.rb deleted file mode 100644 index 95a7b1b..0000000 --- a/spec/defines/sysctl_init_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'spec_helper' - -describe 'sysctl', type: :define do - let(:facts) do - { - os: { - family: 'RedHat', - release: { - major: '8', - }, - }, - } - end - - let(:title) { 'net.ipv4.ip_forward' } - - context 'present' do - let(:params) { { value: '1' } } - - it { - is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with( - content: "net.ipv4.ip_forward = 1\n", - ensure: nil, - ) - } - - it { is_expected.to contain_exec('sysctl-net.ipv4.ip_forward') } - it { is_expected.to contain_exec('update-sysctl.conf-net.ipv4.ip_forward') } - end - - context 'absent' do - let(:params) { { ensure: 'absent' } } - - it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_ensure('absent') } - end -end From 4ab87ab8a6950c693f34dd4ab42ef84ff926e61c Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Tue, 8 Aug 2023 11:14:07 +0000 Subject: [PATCH 09/18] Set resource coverage percentage to 100% --- .sync.yml | 6 +++++- spec/spec_helper.rb | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.sync.yml b/.sync.yml index 8c2c98e..8379b4f 100644 --- a/.sync.yml +++ b/.sync.yml @@ -5,4 +5,8 @@ # # See https://github.com/puppetlabs/pdk-templates/blob/main/config_defaults.yml # for the default values. ---- {} +--- +spec/spec_helper.rb: + coverage_report: true + minimum_code_coverage_percentage: 100 + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6820ceb..61df294 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -46,7 +46,7 @@ end c.filter_run_excluding(bolt: true) unless ENV['GEM_BOLT'] c.after(:suite) do - RSpec::Puppet::Coverage.report!(0) + RSpec::Puppet::Coverage.report!(100) end # Filter backtrace noise From 0dbdfe37a639bc0dcf3a909ff09b9a74f7dfccdb Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Tue, 8 Aug 2023 11:53:59 +0000 Subject: [PATCH 10/18] Move static values from params.pp to base.pp --- manifests/base.pp | 10 +++++----- manifests/params.pp | 6 ------ 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/manifests/base.pp b/manifests/base.pp index b1976d3..e4fa2d9 100644 --- a/manifests/base.pp +++ b/manifests/base.pp @@ -33,11 +33,11 @@ Optional[Hash] $values = undef, Boolean $hiera_merge_values = false, Boolean $symlink99 = $sysctl::params::symlink99, - Boolean $sysctl_dir = $sysctl::params::sysctl_dir, - String[1] $sysctl_dir_path = $sysctl::params::sysctl_dir_path, - String[1] $sysctl_dir_owner = $sysctl::params::sysctl_dir_owner, - String[1] $sysctl_dir_group = $sysctl::params::sysctl_dir_group, - String[1] $sysctl_dir_mode = $sysctl::params::sysctl_dir_mode, + Boolean $sysctl_dir = true, + String[1] $sysctl_dir_path = '/etc/sysctl.d', + String[1] $sysctl_dir_owner = 'root', + String[1] $sysctl_dir_group = 'root', + String[1] $sysctl_dir_mode = '0755', ) inherits sysctl::params { # Hiera support if $hiera_merge_values == true { diff --git a/manifests/params.pp b/manifests/params.pp index f0aca34..267a96b 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -10,10 +10,4 @@ } else { $symlink99 = false } - - $sysctl_dir = true - $sysctl_dir_path = '/etc/sysctl.d' - $sysctl_dir_owner = 'root' - $sysctl_dir_group = 'root' - $sysctl_dir_mode = '0755' } From e45be680eff50b345a26c725bae41f14205c6427 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Tue, 8 Aug 2023 11:56:24 +0000 Subject: [PATCH 11/18] Let sysctl::ensure have a better default value --- manifests/init.pp | 6 ++---- spec/defines/init_spec.rb | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index 5ffe53e..3443ef7 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -31,7 +31,7 @@ # Enforce configured value during each run (can't work with custom files). # define sysctl ( - Optional[Enum['present', 'absent']] $ensure = undef, + Enum['present', 'absent'] $ensure = 'present', Optional[String[1]] $value = undef, Optional[String[1]] $prefix = undef, String $suffix = '.conf', @@ -62,9 +62,7 @@ $file_source = undef } - if $ensure != 'absent' { - # Present - + if $ensure == 'present' { # The permanent change file { "/etc/sysctl.d/${sysctl_d_file}": ensure => $ensure, diff --git a/spec/defines/init_spec.rb b/spec/defines/init_spec.rb index 1b3adb7..50a0265 100644 --- a/spec/defines/init_spec.rb +++ b/spec/defines/init_spec.rb @@ -23,7 +23,7 @@ it do is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').only_with( - ensure: nil, + ensure: 'present', owner: 'root', group: 'root', mode: '0644', From b5e4a4354a45f3b397f898a667528feba6e6d881 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Tue, 8 Aug 2023 12:16:55 +0000 Subject: [PATCH 12/18] Directly reuse value of purge for recurse --- manifests/base.pp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/manifests/base.pp b/manifests/base.pp index e4fa2d9..825a93d 100644 --- a/manifests/base.pp +++ b/manifests/base.pp @@ -50,11 +50,6 @@ } if $sysctl_dir { - if $purge { - $recurse = true - } else { - $recurse = false - } file { $sysctl_dir_path: ensure => 'directory', owner => $sysctl_dir_owner, @@ -62,7 +57,7 @@ mode => $sysctl_dir_mode, # Magic hidden here purge => $purge, - recurse => $recurse, + recurse => $purge, } if $symlink99 and $sysctl_dir_path =~ /^\/etc\/[^\/]+$/ { file { "${sysctl_dir_path}/99-sysctl.conf": From 2f36addd0f78540739df489d4333bfc321eae6b1 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Tue, 8 Aug 2023 12:33:05 +0000 Subject: [PATCH 13/18] Use empty hash instead of undef for $values --- manifests/base.pp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/manifests/base.pp b/manifests/base.pp index 825a93d..dbd6ad1 100644 --- a/manifests/base.pp +++ b/manifests/base.pp @@ -30,7 +30,7 @@ # class sysctl::base ( Boolean $purge = false, - Optional[Hash] $values = undef, + Hash $values = {}, Boolean $hiera_merge_values = false, Boolean $symlink99 = $sysctl::params::symlink99, Boolean $sysctl_dir = true, @@ -45,9 +45,8 @@ } else { $values_real = $values } - if $values_real != undef { - create_resources(sysctl,$values_real) - } + + create_resources(sysctl,$values_real) if $sysctl_dir { file { $sysctl_dir_path: From b097c1fa217833472e553e7543b99c2af51881cc Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Tue, 8 Aug 2023 12:38:14 +0000 Subject: [PATCH 14/18] Use ternary operator for $symlink99 instead of if/else --- manifests/params.pp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/manifests/params.pp b/manifests/params.pp index 267a96b..abf89c7 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -1,13 +1,6 @@ # @api private class sysctl::params { # Keep the original symlink if we purge, to avoid ping-pong with initscripts - if ( - $facts['os']['family'] == 'RedHat' and versioncmp($facts['os']['release']['major'], '7') >= 0 - ) or ( - $facts['os']['family'] == 'Debian' and versioncmp($facts['os']['release']['major'], '8') >= 0 - ) { - $symlink99 = true - } else { - $symlink99 = false - } + $symlink99 = ($facts['os']['family'] == 'RedHat' and versioncmp($facts['os']['release']['major'], '7') >= 0) or + ($facts['os']['family'] == 'Debian' and versioncmp($facts['os']['release']['major'], '8') >= 0) } From 79a2bf7f00f78918e313fd4f6ac8f7713aa90aa1 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Tue, 8 Aug 2023 12:50:49 +0000 Subject: [PATCH 15/18] Remove redundant code for $source --- manifests/init.pp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index 3443ef7..2345e87 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -54,12 +54,10 @@ $sysctl_d_file = regsubst($_sysctl_d_file, '[/ ]', '_', 'G') # If we have an explicit content or source, use them - if $content or $source { + if $content { $file_content = $content - $file_source = $source } else { $file_content = template("${module_name}/sysctl.d-file.erb") - $file_source = undef } if $ensure == 'present' { @@ -70,7 +68,7 @@ group => 'root', mode => '0644', content => $file_content, - source => $file_source, + source => $source, notify => [ Exec["sysctl-${title}"], Exec["update-sysctl.conf-${title}"], From fb1b5dd4d17c0b805cf126640c1c749933177489 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Tue, 8 Aug 2023 12:59:09 +0000 Subject: [PATCH 16/18] Add path to enforce exec - Fixes PR#43 & PR#68 --- manifests/init.pp | 5 +++-- spec/defines/init_spec.rb | 10 ++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index 2345e87..97d594a 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -100,8 +100,9 @@ $qvalue = shellquote("${value}") # lint:endignore exec { "enforce-sysctl-value-${qtitle}": - unless => "/usr/bin/test \"$(/sbin/sysctl -n ${qtitle})\" = ${qvalue}", - command => "/sbin/sysctl -w ${qtitle}=${qvalue}", + unless => "test \"$(sysctl -n ${qtitle})\" = ${qvalue}", + command => "sysctl -w ${qtitle}=${qvalue}", + path => ['/usr/sbin', '/sbin', '/usr/bin', '/bin'], } } } else { diff --git a/spec/defines/init_spec.rb b/spec/defines/init_spec.rb index 50a0265..c1432c1 100644 --- a/spec/defines/init_spec.rb +++ b/spec/defines/init_spec.rb @@ -53,8 +53,9 @@ it do is_expected.to contain_exec('enforce-sysctl-value-net.ipv4.ip_forward').only_with( - unless: '/usr/bin/test "$(/sbin/sysctl -n net.ipv4.ip_forward)" = ""', - command: '/sbin/sysctl -w net.ipv4.ip_forward=""', + unless: 'test "$(sysctl -n net.ipv4.ip_forward)" = ""', + command: 'sysctl -w net.ipv4.ip_forward=""', + path: ['/usr/sbin', '/sbin', '/usr/bin', '/bin'], ) end end @@ -92,8 +93,9 @@ it do is_expected.to contain_exec('enforce-sysctl-value-net.ipv4.ip_forward').only_with( - unless: '/usr/bin/test "$(/sbin/sysctl -n net.ipv4.ip_forward)" = 1', - command: '/sbin/sysctl -w net.ipv4.ip_forward=1', + unless: 'test "$(sysctl -n net.ipv4.ip_forward)" = 1', + command: 'sysctl -w net.ipv4.ip_forward=1', + path: ['/usr/sbin', '/sbin', '/usr/bin', '/bin'], ) end end From 6d040bc26808252af9a91f5b76750e3f927b1973 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Tue, 8 Aug 2023 13:21:44 +0000 Subject: [PATCH 17/18] Add comment to managed files - Fixes PR#52 --- spec/defines/init_spec.rb | 26 ++++++++++---------------- templates/sysctl.d-file.erb | 3 +++ 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/spec/defines/init_spec.rb b/spec/defines/init_spec.rb index c1432c1..d525f71 100644 --- a/spec/defines/init_spec.rb +++ b/spec/defines/init_spec.rb @@ -3,6 +3,12 @@ describe 'sysctl', type: :define do let(:title) { 'net.ipv4.ip_forward' } + header = <<-END.gsub(%r{^\s+\|}, '') + |# This file is being maintained by Puppet. + |# DO NOT EDIT + | + END + on_supported_os.sort.each do |os, facts| # define os specific defaults symlink99 = if (facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'].to_i >= 7) || @@ -27,7 +33,7 @@ owner: 'root', group: 'root', mode: '0644', - content: "net.ipv4.ip_forward = \n", + content: header + "net.ipv4.ip_forward = \n", source: nil, notify: ['Exec[sysctl-net.ipv4.ip_forward]', 'Exec[update-sysctl.conf-net.ipv4.ip_forward]'], ) @@ -83,7 +89,7 @@ context 'with value set to valid 1' do let(:params) { { value: '1' } } - it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_content("net.ipv4.ip_forward = 1\n") } + it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_content(header + "net.ipv4.ip_forward = 1\n") } it do is_expected.to contain_exec('update-sysctl.conf-net.ipv4.ip_forward').with_command( @@ -141,7 +147,7 @@ it do is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with( - content: "# testing\nnet.ipv4.ip_forward = \n", + content: header + "# testing\nnet.ipv4.ip_forward = \n", ) end end @@ -151,7 +157,7 @@ it do is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with( - content: "# test\n# ing\nnet.ipv4.ip_forward = \n", + content: header + "# test\n# ing\nnet.ipv4.ip_forward = \n", ) end end @@ -160,24 +166,12 @@ let(:params) { { content: 'testing' } } it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_content('testing') } - - it do - is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with( - content: 'testing', - ) - end end context 'with source set to valid testing' do let(:params) { { source: 'testing' } } it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_source('testing') } - - it do - is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with( - source: 'testing', - ) - end end context 'with enforce set to valid false' do diff --git a/templates/sysctl.d-file.erb b/templates/sysctl.d-file.erb index 0d4863c..1ae40d7 100644 --- a/templates/sysctl.d-file.erb +++ b/templates/sysctl.d-file.erb @@ -1,3 +1,6 @@ +# This file is being maintained by Puppet. +# DO NOT EDIT + <% if @comment.is_a?(Array) -%> <% @comment.each do |line| -%> # <%= line %> From e56facbb43d6892f640c9bc7061cda2cded59b66 Mon Sep 17 00:00:00 2001 From: Phil Friderici Date: Wed, 25 Oct 2023 11:15:28 +0000 Subject: [PATCH 18/18] Use stdlib for stricter parameter validations --- .fixtures.yml | 3 +++ manifests/base.pp | 18 +++++++++--------- manifests/init.pp | 2 +- metadata.json | 5 ++++- spec/defines/init_spec.rb | 4 ++-- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/.fixtures.yml b/.fixtures.yml index 2250308..8ecf049 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -1,4 +1,7 @@ fixtures: + repositories: + stdlib: + repo: "https://github.com/puppetlabs/puppetlabs-stdlib.git" symlinks: sysctl: "#{source_dir}" diff --git a/manifests/base.pp b/manifests/base.pp index dbd6ad1..5e30838 100644 --- a/manifests/base.pp +++ b/manifests/base.pp @@ -29,15 +29,15 @@ # Mode for sysctl directory. # class sysctl::base ( - Boolean $purge = false, - Hash $values = {}, - Boolean $hiera_merge_values = false, - Boolean $symlink99 = $sysctl::params::symlink99, - Boolean $sysctl_dir = true, - String[1] $sysctl_dir_path = '/etc/sysctl.d', - String[1] $sysctl_dir_owner = 'root', - String[1] $sysctl_dir_group = 'root', - String[1] $sysctl_dir_mode = '0755', + Boolean $purge = false, + Hash $values = {}, + Boolean $hiera_merge_values = false, + Boolean $symlink99 = $sysctl::params::symlink99, + Boolean $sysctl_dir = true, + Stdlib::Absolutepath $sysctl_dir_path = '/etc/sysctl.d', + String[1] $sysctl_dir_owner = 'root', + String[1] $sysctl_dir_group = 'root', + Stdlib::Filemode $sysctl_dir_mode = '0755', ) inherits sysctl::params { # Hiera support if $hiera_merge_values == true { diff --git a/manifests/init.pp b/manifests/init.pp index 97d594a..a256bc6 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -37,7 +37,7 @@ String $suffix = '.conf', Optional[Variant[Array, String[1]]] $comment = undef, Optional[String[1]] $content = undef, - Optional[String[1]] $source = undef, + Optional[Stdlib::Filesource] $source = undef, Boolean $enforce = true, ) { include sysctl::base diff --git a/metadata.json b/metadata.json index ec2ec23..3b9a576 100644 --- a/metadata.json +++ b/metadata.json @@ -8,7 +8,10 @@ "project_page": "https://github.com/thias/puppet-sysctl", "issues_url": "https://github.com/thias/puppet-sysctl/issues", "dependencies": [ - + { + "name": "puppetlabs/stdlib", + "version_requirement": ">= 4.25.0 < 10.0.0" + } ], "operatingsystem_support": [ { diff --git a/spec/defines/init_spec.rb b/spec/defines/init_spec.rb index d525f71..b7eac14 100644 --- a/spec/defines/init_spec.rb +++ b/spec/defines/init_spec.rb @@ -169,9 +169,9 @@ end context 'with source set to valid testing' do - let(:params) { { source: 'testing' } } + let(:params) { { source: '/test/ing' } } - it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_source('testing') } + it { is_expected.to contain_file('/etc/sysctl.d/net.ipv4.ip_forward.conf').with_source('/test/ing') } end context 'with enforce set to valid false' do