diff --git a/.rubocop.yml b/.rubocop.yml index 33c33fa..8a313f4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,534 +1,542 @@ +inherit_from: .rubocop_todo.yml + --- require: - rubocop-performance - rubocop-rspec AllCops: DisplayCopNames: true TargetRubyVersion: '2.4' 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 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/Caller: Enabled: true Performance/CaseWhenSplat: Enabled: true Performance/Casecmp: Enabled: true Performance/CollectionLiteralInLoop: + Exclude: + - spec/**/* Enabled: true Performance/CompareWithBlock: Enabled: true Performance/ConstantRegexp: Enabled: true Performance/Count: Enabled: true Performance/Detect: Enabled: true Performance/DoubleStartEndWith: Enabled: true Performance/EndWith: Enabled: true Performance/FixedSize: Enabled: true Performance/FlatMap: Enabled: true Performance/MethodObjectAsBlock: Enabled: true Performance/RangeInclude: Enabled: true Performance/RedundantBlockCall: Enabled: true Performance/RedundantMatch: Enabled: true Performance/RedundantMerge: Enabled: true Performance/RedundantSortBlock: Enabled: true Performance/RedundantStringChars: Enabled: true Performance/RegexpMatch: Enabled: true Performance/ReverseEach: Enabled: true Performance/ReverseFirst: Enabled: true Performance/Size: Enabled: true Performance/SortReverse: Enabled: true Performance/Squeeze: Enabled: true Performance/StartWith: Enabled: true Performance/StringInclude: Enabled: true Performance/StringReplacement: Enabled: true Performance/Sum: Enabled: true Performance/TimesMap: 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/DuplicateBranch: Enabled: false Lint/DuplicateElsifCondition: Enabled: false Lint/DuplicateRegexpCharacterClassElement: Enabled: false Lint/DuplicateRequire: Enabled: false Lint/DuplicateRescueException: Enabled: false Lint/EmptyBlock: Enabled: false Lint/EmptyClass: 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/NoReturnInBeginEndBlocks: 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/ToEnumArguments: Enabled: false Lint/ToJSON: Enabled: false Lint/TopLevelReturnWithArgument: Enabled: false Lint/TrailingCommaInAttributeDeclaration: Enabled: false Lint/UnexpectedBlockArity: Enabled: false Lint/UnmodifiedReduceAccumulator: 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 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/ArgumentsForwarding: Enabled: false Style/AsciiComments: Enabled: false Style/BisectedAttrAccessor: Enabled: false Style/CaseLikeIf: Enabled: false Style/ClassEqualityComparison: Enabled: false Style/CollectionCompact: Enabled: false Style/ColonMethodDefinition: Enabled: false Style/CombinableLoops: Enabled: false Style/CommentedKeyword: Enabled: false Style/Dir: Enabled: false Style/DocumentDynamicEvalDefinition: 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/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/NegatedIfElseCondition: Enabled: false Style/NegatedUnless: Enabled: false Style/NilLambda: Enabled: false Style/NumericPredicate: Enabled: false Style/OptionalBooleanParameter: Enabled: false Style/OrAssignment: Enabled: false Style/RandomWithOffset: Enabled: false Style/RedundantArgument: 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/SwapValues: 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 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index bb1d59c..9fb474c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,44 +1,478 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2020-11-18 11:06:43 +0000 using RuboCop version 0.49.1. +# on 2020-12-16 17:04:12 UTC using RuboCop version 1.6.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 55 -Lint/UnderscorePrefixedVariableName: +# Offense count: 2 +# Configuration parameters: Include. +# Include: **/*.gemfile, **/Gemfile, **/gems.rb +Bundler/DuplicatedGem: + Exclude: + - 'Gemfile' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation, Include. +# Include: **/*.gemfile, **/Gemfile, **/gems.rb +Bundler/OrderedGems: + Exclude: + - 'Gemfile' + +# Offense count: 24 +# Cop supports --auto-correct. +Layout/ClosingHeredocIndentation: + Exclude: + - 'spec/acceptance/docker_custom_source_spec.rb' + - 'spec/acceptance/docker_full_spec.rb' + - 'spec/acceptance/docker_spec.rb' + - 'spec/acceptance/stack_spec.rb' + - 'spec/spec_helper_acceptance_local.rb' + +# Offense count: 24 +# Cop supports --auto-correct. +Layout/EmptyLineAfterGuardClause: + Enabled: false + +# Offense count: 667 +# Cop supports --auto-correct. +# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. +# SupportedHashRocketStyles: key, separator, table +# SupportedColonStyles: key, separator, table +# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit +Layout/HashAlignment: Enabled: false -# Offense count: 38 +# Offense count: 10 +# Cop supports --auto-correct. +Layout/HeredocIndentation: + Exclude: + - 'Rakefile' + - 'spec/spec_helper_acceptance_local.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: normal, indented_internal_methods +Layout/IndentationConsistency: + Exclude: + - 'Guardfile' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: IndentationWidth, EnforcedStyle. +# SupportedStyles: spaces, tabs +Layout/IndentationStyle: + Exclude: + - 'Guardfile' + +# Offense count: 2 +# Cop supports --auto-correct. +Layout/SpaceAroundMethodCallOperator: + Exclude: + - 'spec/shared_examples/install.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: final_newline, final_blank_line +Layout/TrailingEmptyLines: + Exclude: + - 'Guardfile' + - 'Rakefile' + +# Offense count: 1 +# Cop supports --auto-correct. +Lint/InterpolationCheck: + Exclude: + - 'spec/acceptance/docker_full_spec.rb' + +# Offense count: 1 +# Configuration parameters: MaximumRangeSize. +Lint/MissingCopEnableDirective: + Exclude: + - 'lib/puppet/provider/docker_compose/ruby.rb' + +# Offense count: 1 +Lint/MixedRegexpCaptureTypes: + Exclude: + - 'Gemfile' + +# Offense count: 22 +# Configuration parameters: AllowKeywordBlockArguments. +Lint/UnderscorePrefixedVariableName: + Exclude: + - 'spec/helper/get_defaults.rb' + - 'spec/helper/get_values_init.rb' + - 'spec/shared_examples/compose.rb' + - 'spec/shared_examples/config.rb' + - 'spec/shared_examples/exec.rb' + - 'spec/shared_examples/image.rb' + - 'spec/shared_examples/install.rb' + - 'spec/shared_examples/machine.rb' + - 'spec/shared_examples/params.rb' + - 'spec/shared_examples/plugin.rb' + - 'spec/shared_examples/registry.rb' + +# Offense count: 7 Lint/UselessAssignment: Exclude: - 'spec/classes/networks_spec.rb' - 'spec/defines/run_spec.rb' - 'spec/helper/get_defaults.rb' - 'spec/shared_examples/compose.rb' - 'spec/shared_examples/image.rb' - 'spec/shared_examples/machine.rb' - - 'spec/shared_examples/run.rb' - - 'spec/shared_examples/service.rb' - - 'spec/shared_examples/services.rb' -# Offense count: 6 +# Offense count: 18 +# Configuration parameters: IgnoredMethods, CountRepeatedAttributes. +Metrics/AbcSize: + Max: 309 + +# Offense count: 131 +# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. +# IgnoredMethods: refine +Metrics/BlockLength: + Max: 712 + +# Offense count: 7 # Configuration parameters: CountBlocks. Metrics/BlockNesting: Max: 7 +# Offense count: 15 +# Configuration parameters: IgnoredMethods. +Metrics/CyclomaticComplexity: + Max: 36 + +# Offense count: 20 +# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. +Metrics/MethodLength: + Max: 485 + +# Offense count: 2 +# Configuration parameters: CountKeywordArgs, MaxOptionalParameters. +Metrics/ParameterLists: + Max: 9 + +# Offense count: 12 +# Configuration parameters: IgnoredMethods. +Metrics/PerceivedComplexity: + Max: 39 + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: lowercase, uppercase +Naming/HeredocDelimiterCase: + Exclude: + - 'spec/acceptance/compose_v3_spec.rb' + - 'spec/acceptance_swarm/swarm_spec.rb' + +# Offense count: 48 +# Configuration parameters: ForbiddenDelimiters. +# ForbiddenDelimiters: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$)) +Naming/HeredocDelimiterNaming: + Exclude: + - 'Rakefile' + - 'spec/acceptance/docker_custom_source_spec.rb' + - 'spec/acceptance/docker_full_spec.rb' + - 'spec/acceptance/machine_spec.rb' + - 'spec/spec_helper_acceptance_local.rb' + +# Offense count: 13 +# Configuration parameters: EnforcedStyle. +# SupportedStyles: snake_case, camelCase +Naming/VariableName: + Exclude: + - 'Rakefile' + +# Offense count: 1 +Security/Eval: + Exclude: + - 'Gemfile' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: AutoCorrect. +Security/JSONLoad: + Exclude: + - 'Rakefile' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: always, conditionals +Style/AndOr: + Exclude: + - 'Rakefile' + +# Offense count: 5 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods, AllowBracesOnProceduralOneLiners, BracesRequiredMethods. +# SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces +# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object +# FunctionalMethods: let, let!, subject, watch +# IgnoredMethods: lambda, proc, it +Style/BlockDelimiters: + Exclude: + - 'lib/puppet/provider/docker_compose/ruby.rb' + - 'lib/puppet/provider/docker_stack/ruby.rb' + - 'spec/spec_helper.rb' + +# Offense count: 4 +# Cop supports --auto-correct. +Style/CaseLikeIf: + Exclude: + - 'lib/puppet/parser/functions/docker_run_flags.rb' + - 'lib/puppet/provider/docker_network/ruby.rb' + - 'spec/shared_examples/plugin.rb' + - 'spec/shared_examples/registry.rb' + +# Offense count: 11 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: nested, compact +Style/ClassAndModuleChildren: + Exclude: + - 'lib/puppet/parser/functions/docker_exec_flags.rb' + - 'lib/puppet/parser/functions/docker_params_changed.rb' + - 'lib/puppet/parser/functions/docker_plugin_enable_flags.rb' + - 'lib/puppet/parser/functions/docker_plugin_install_flags.rb' + - 'lib/puppet/parser/functions/docker_plugin_remove_flags.rb' + - 'lib/puppet/parser/functions/docker_run_flags.rb' + - 'lib/puppet/parser/functions/docker_secrets_flags.rb' + - 'lib/puppet/parser/functions/docker_service_flags.rb' + - 'lib/puppet/parser/functions/docker_stack_flags.rb' + - 'lib/puppet/parser/functions/docker_swarm_init_flags.rb' + - 'lib/puppet/parser/functions/docker_swarm_join_flags.rb' + +# Offense count: 1 +Style/Documentation: + Exclude: + - 'spec/**/*' + - 'test/**/*' + - 'lib/puppet/parser/functions/docker_params_changed.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: empty, nil, both +Style/EmptyElse: + Exclude: + - 'lib/facter/docker.rb' + - 'spec/shared_examples/service.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: format, sprintf, percent +Style/FormatString: + Exclude: + - 'Rakefile' + - 'lib/puppet/parser/functions/docker_secrets_flags.rb' + - 'spec/helper/get_docker_secrets_flags.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: always, always_true, never +Style/FrozenStringLiteralComment: + Exclude: + - 'Gemfile' + - 'Guardfile' + +# Offense count: 12 +# Cop supports --auto-correct. +Style/GlobalStdStream: + Exclude: + - 'tasks/node_ls.rb' + - 'tasks/node_rm.rb' + - 'tasks/node_update.rb' + - 'tasks/service_create.rb' + - 'tasks/service_rm.rb' + - 'tasks/service_scale.rb' + - 'tasks/service_update.rb' + - 'tasks/swarm_init.rb' + - 'tasks/swarm_join.rb' + - 'tasks/swarm_leave.rb' + - 'tasks/swarm_token.rb' + - 'tasks/swarm_update.rb' + +# Offense count: 5 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: braces, no_braces +Style/HashAsLastArrayItem: + Exclude: + - 'Gemfile' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. +# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys +Style/HashSyntax: + Exclude: + - 'Guardfile' + # Offense count: 5 Style/IdenticalConditionalBranches: Exclude: - 'spec/helper/get_defaults.rb' - 'spec/helper/get_values_init.rb' +# Offense count: 95 +# Cop supports --auto-correct. +Style/IfUnlessModifier: + Enabled: false + # Offense count: 6 -Style/IfInsideElse: +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: line_count_dependent, lambda, literal +Style/Lambda: Exclude: - - 'lib/puppet/parser/functions/docker_service_flags.rb' - - 'spec/helper/get_docker_service_flags.rb' - - 'spec/shared_examples/image.rb' - - 'spec/shared_examples/install.rb' - - 'spec/shared_examples/service.rb' + - 'lib/puppet/parser/functions/docker_run_flags.rb' + - 'lib/puppet/parser/functions/docker_secrets_flags.rb' + - 'lib/puppet/provider/docker_network/ruby.rb' + - 'lib/puppet/provider/docker_volume/ruby.rb' + - 'spec/helper/get_docker_run_flags.rb' + - 'spec/helper/get_docker_secrets_flags.rb' + +# Offense count: 2 +Style/MixinUsage: + Exclude: + - 'spec/spec_helper.rb' + - 'spec/spec_helper_acceptance_local.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/RedundantCondition: + Exclude: + - 'spec/shared_examples/run.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/RedundantConditional: + Exclude: + - 'spec/helper/get_defaults.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +Style/RedundantInterpolation: + Exclude: + - 'Rakefile' + +# Offense count: 2 +# Cop supports --auto-correct. +Style/RedundantRegexpCharacterClass: + Exclude: + - 'spec/classes/compose_spec.rb' + +# Offense count: 53 +# Cop supports --auto-correct. +Style/RedundantRegexpEscape: + Exclude: + - 'Gemfile' + - 'Guardfile' + - 'Rakefile' + - 'spec/acceptance/docker_custom_source_spec.rb' + - 'spec/acceptance/docker_full_spec.rb' + - 'spec/acceptance/docker_spec.rb' + - 'spec/classes/compose_spec.rb' + +# Offense count: 123 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, AllowInnerSlashes. +# SupportedStyles: slashes, percent_r, mixed +Style/RegexpLiteral: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle. +# SupportedStyles: implicit, explicit +Style/RescueStandardError: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods. +# AllowedMethods: present?, blank?, presence, try, try! +Style/SafeNavigation: + Exclude: + - 'Rakefile' + +# Offense count: 5 +# Cop supports --auto-correct. +# Configuration parameters: AllowModifier. +Style/SoleNestedConditional: + Exclude: + - 'lib/facter/docker.rb' + - 'lib/puppet/provider/docker_network/ruby.rb' + - 'spec/classes/compose_spec.rb' + - 'spec/classes/machine_spec.rb' + - 'spec/defines/services_spec.rb' + +# Offense count: 34 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiterals: + Exclude: + - 'Gemfile' + - 'Rakefile' + +# Offense count: 9 +# Cop supports --auto-correct. +# Configuration parameters: MinSize. +# SupportedStyles: percent, brackets +Style/SymbolArray: + EnforcedStyle: brackets + +# Offense count: 2 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, AllowSafeAssignment. +# SupportedStyles: require_parentheses, require_no_parentheses, require_parentheses_when_complex +Style/TernaryParentheses: + Exclude: + - 'lib/puppet/provider/docker_compose/ruby.rb' + - 'lib/puppet/provider/docker_stack/ruby.rb' + +# Offense count: 156 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleForMultiline. +# SupportedStylesForMultiline: comma, consistent_comma, no_comma +Style/TrailingCommaInArguments: + Enabled: false + +# Offense count: 44 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleForMultiline. +# SupportedStylesForMultiline: comma, consistent_comma, no_comma +Style/TrailingCommaInArrayLiteral: + Enabled: false + +# Offense count: 189 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyleForMultiline. +# SupportedStylesForMultiline: comma, consistent_comma, no_comma +Style/TrailingCommaInHashLiteral: + Enabled: false + +# Offense count: 21 +# Cop supports --auto-correct. +# Configuration parameters: MinSize, WordRegex. +# SupportedStyles: percent, brackets +Style/WordArray: + EnforcedStyle: brackets + +# Offense count: 85 +# Cop supports --auto-correct. +# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Layout/LineLength: + Max: 255 diff --git a/lib/puppet/parser/functions/docker_exec_flags.rb b/lib/puppet/parser/functions/docker_exec_flags.rb index 416c96d..809675b 100644 --- a/lib/puppet/parser/functions/docker_exec_flags.rb +++ b/lib/puppet/parser/functions/docker_exec_flags.rb @@ -1,33 +1,31 @@ # frozen_string_literal: true require 'shellwords' # # docker_exec_flags.rb # module Puppet::Parser::Functions # Transforms a hash into a string of docker exec flags newfunction(:docker_exec_flags, type: :rvalue) do |args| opts = args[0] || {} flags = [] if opts['detach'] flags << '--detach=true' end if opts['interactive'] flags << '--interactive=true' end if opts['tty'] flags << '--tty=true' end - if opts['env'] - opts['env'].each do |namevaluepair| - flags << "--env #{namevaluepair}" - end + opts['env']&.each do |namevaluepair| + flags << "--env #{namevaluepair}" end flags.flatten.join(' ') end end diff --git a/lib/puppet/provider/docker_compose/ruby.rb b/lib/puppet/provider/docker_compose/ruby.rb index bfcca81..02c8792 100644 --- a/lib/puppet/provider/docker_compose/ruby.rb +++ b/lib/puppet/provider/docker_compose/ruby.rb @@ -1,106 +1,104 @@ # frozen_string_literal: true require 'deep_merge' Puppet::Type.type(:docker_compose).provide(:ruby) do desc 'Support for Puppet running Docker Compose' mk_resource_methods commands dockercompose: 'docker-compose' commands dockercmd: 'docker' has_command(:docker, command(:dockercmd)) do environment(HOME: '/root') end def exists? Puppet.info("Checking for compose project #{name}") compose_services = {} compose_containers = [] # get merged config using docker-compose config args = [compose_files, '-p', name, 'config'].insert(3, resource[:options]).compact compose_output = YAML.safe_load(execute([command(:dockercompose)] + args, combine: false)) # rubocop:disable Style/StringLiterals containers = docker([ 'ps', '--format', "{{.Label \"com.docker.compose.service\"}}-{{.Image}}", '--filter', "label=com.docker.compose.project=#{name}", ]).split("\n") compose_containers.push(*containers) compose_containers.uniq! compose_services = compose_output['services'] if compose_services.count != compose_containers.count return false end counts = Hash[*compose_services.each.map { |key, array| image = (array['image']) ? array['image'] : get_image(key, compose_services) Puppet.info("Checking for compose service #{key} #{image}") ["#{key}-#{image}", compose_containers.count("#{key}-#{image}")] }.flatten] # No containers found for the project if counts.empty? || # Containers described in the compose file are not running counts.any? { |_k, v| v.zero? } || # The scaling factors in the resource do not match the number of running containers resource[:scale] && counts.merge(resource[:scale]) != counts false else true end end def get_image(service_name, compose_services) image = compose_services[service_name]['image'] unless image if compose_services[service_name]['extends'] image = get_image(compose_services[service_name]['extends'], compose_services) elsif compose_services[service_name]['build'] image = "#{name}_#{service_name}" end end image end def create Puppet.info("Running compose project #{name}") args = [compose_files, '-p', name, 'up', '-d', '--remove-orphans'].insert(3, resource[:options]).insert(5, resource[:up_args]).compact dockercompose(args) return unless resource[:scale] instructions = resource[:scale].map { |k, v| "#{k}=#{v}" } Puppet.info("Scaling compose project #{name}: #{instructions.join(' ')}") args = [compose_files, '-p', name, 'scale'].insert(3, resource[:options]).compact + instructions dockercompose(args) end def destroy Puppet.info("Removing all containers for compose project #{name}") kill_args = [compose_files, '-p', name, 'kill'].insert(3, resource[:options]).compact dockercompose(kill_args) rm_args = [compose_files, '-p', name, 'rm', '--force', '-v'].insert(3, resource[:options]).compact dockercompose(rm_args) end def restart return unless exists? Puppet.info("Rebuilding and Restarting all containers for compose project #{name}") kill_args = [compose_files, '-p', name, 'kill'].insert(3, resource[:options]).compact dockercompose(kill_args) build_args = [compose_files, '-p', name, 'build'].insert(3, resource[:options]).compact dockercompose(build_args) create end def compose_files resource[:compose_files].map { |x| ['-f', x] }.flatten end - - private end diff --git a/lib/puppet/provider/docker_stack/ruby.rb b/lib/puppet/provider/docker_stack/ruby.rb index b1fc55d..c493859 100644 --- a/lib/puppet/provider/docker_stack/ruby.rb +++ b/lib/puppet/provider/docker_stack/ruby.rb @@ -1,94 +1,92 @@ # frozen_string_literal: true require 'deep_merge' Puppet::Type.type(:docker_stack).provide(:ruby) do desc 'Support for Puppet running Docker Stacks' mk_resource_methods commands dockercmd: 'docker' has_command(:docker, command(:dockercmd)) do environment(HOME: '/root') end def exists? Puppet.info("Checking for stack #{name}") stack_services = {} stack_containers = [] resource[:compose_files].each do |file| compose_file = YAML.safe_load(File.read(file), [], [], true) # rubocop:disable Style/StringLiterals containers = docker([ 'ps', '--format', "{{.Label \"com.docker.swarm.service.name\"}}-{{.Image}}", '--filter', "label=com.docker.stack.namespace=#{name}", ]).split("\n").each do |c| c.slice!("#{name}_") end stack_containers.push(*containers) stack_containers.uniq! # rubocop:enable Style/StringLiterals case compose_file['version'] when %r{^3(\.[0-7])?$} stack_services.merge!(compose_file['services']) else raise(Puppet::Error, "Unsupported docker compose file syntax version \"#{compose_file['version']}\"!") end end if stack_services.count != stack_containers.count return false end counts = Hash[*stack_services.each.map { |key, array| image = (array['image']) ? array['image'] : get_image(key, stack_services) image = "#{image}:latest" unless image.include?(':') Puppet.info("Checking for compose service #{key} #{image}") ["#{key}-#{image}", stack_containers.count("#{key}-#{image}")] }.flatten] # No containers found for the project if counts.empty? || # Containers described in the compose file are not running counts.any? { |_k, v| v.zero? } false else true end end def get_image(service_name, stack_services) image = stack_services[service_name]['image'] unless image if stack_services[service_name]['extends'] image = get_image(stack_services[service_name]['extends'], stack_services) elsif stack_services[service_name]['build'] image = "#{name}_#{service_name}" end end image end def create Puppet.info("Running stack #{name}") args = ['stack', 'deploy', compose_files, name].insert(1, bundle_file).insert(4, resource[:up_args]).compact docker(args) end def destroy Puppet.info("Removing docker stack #{name}") rm_args = ['stack', 'rm', name] docker(rm_args) end def bundle_file return resource[:bundle_file].map { |x| ['-c', x] }.flatten unless resource[:bundle_file].nil? end def compose_files resource[:compose_files].map { |x| ['-c', x] }.flatten end - - private end diff --git a/spec/acceptance/compose_v3_spec.rb b/spec/acceptance/compose_v3_spec.rb index 7109f20..bf2dfcf 100644 --- a/spec/acceptance/compose_v3_spec.rb +++ b/spec/acceptance/compose_v3_spec.rb @@ -1,159 +1,159 @@ # frozen_string_literal: true require 'spec_helper_acceptance' if os[:family] == 'windows' install_dir = '/cygdrive/c/Program Files/Docker' file_extension = '.exe' docker_args = 'docker_ee => true' tmp_path = 'C:/cygwin64/tmp' - test_container = if os[:release] =~ %r{2019} + test_container = if %r{2019}.match?(os[:release]) 'nanoserver' else 'nanoserver-sac2016' end else docker_args = if os[:name] == 'ubuntu' && os[:release][:full] == '14.04' "version => '18.06.1~ce~3-0~ubuntu'" else '' end install_dir = '/usr/local/bin' file_extension = '' tmp_path = '/tmp' test_container = 'debian' end describe 'docker compose' do before(:all) do retry_on_error_matching(60, 5, %r{connection failure running}) do install_code = <<-code class { 'docker': #{docker_args} } class { 'docker::compose': version => '1.23.2', } code apply_manifest(install_code, catch_failures: true) end end context 'Creating compose v3 projects', win_broken: true do let(:install_pp) do <<-MANIFEST docker_compose { 'web': compose_files => ['#{tmp_path}/docker-compose-v3.yml'], ensure => present, } MANIFEST end it 'is idempotent' do idempotent_apply(install_pp) end it 'has docker compose installed' do run_shell('docker-compose --help', expect_failures: false) end it 'finds a docker container' do run_shell('docker inspect web_compose_test_1', expect_failures: false) end end context 'creating compose projects with multi compose files', win_broken: true do before(:all) do install_pp = <<-MANIFEST docker_compose { 'web1': compose_files => ['#{tmp_path}/docker-compose-v3.yml', '#{tmp_path}/docker-compose-override-v3.yml'], ensure => present, } MANIFEST apply_manifest(install_pp, catch_failures: true) end - it "should find container with #{test_container} tag" do + it "finds container with #{test_container} tag" do run_shell("docker inspect web1_compose_test_1 | grep #{test_container}", acceptable_exit_codes: [0]) end end context 'Destroying project with multiple compose files', win_broken: true do let(:destroy_pp) do <<-MANIFEST docker_compose { 'web1': compose_files => ['#{tmp_path}/docker-compose-v3.yml', '#{tmp_path}/docker-compose-override-v3.yml'], ensure => absent, } MANIFEST end before(:all) do install_pp = <<-MANIFEST docker_compose { 'web1': compose_files => ['#{tmp_path}/docker-compose-v3.yml', '#{tmp_path}/docker-compose-override-v3.yml'], ensure => present, } MANIFEST apply_manifest(install_pp, catch_failures: true) end it 'is idempotent' do idempotent_apply(destroy_pp) end it 'does not find a docker container' do run_shell('docker inspect web1_compose_test_1', expect_failures: true) end end context 'Requesting a specific version of compose' do let(:version) do '1.21.2' end it 'is idempotent' do pp = <<-MANIFEST class { 'docker::compose': version => '#{version}', } MANIFEST idempotent_apply(pp) end it 'has installed the requested version' do run_shell('docker-compose --version', expect_failures: false) do |r| expect(r.stdout).to match(%r{#{version}}) end end end context 'Removing docker compose' do let(:version) do '1.21.2' end it 'is idempotent' do pp = <<-MANIFEST class { 'docker::compose': ensure => absent, version => '#{version}', } MANIFEST idempotent_apply(pp) end it 'has removed the relevant files' do run_shell("test -e \"#{install_dir}/docker-compose#{file_extension}\"", expect_failures: true) run_shell("test -e \"#{install_dir}/docker-compose-#{version}#{file_extension}\"", expect_failures: true) end after(:all) do install_pp = <<-MANIFEST class { 'docker': #{docker_args}} class { 'docker::compose': } MANIFEST apply_manifest(install_pp, catch_failures: true) end end end diff --git a/spec/acceptance/docker_full_spec.rb b/spec/acceptance/docker_full_spec.rb index f6cf791..1f356e8 100644 --- a/spec/acceptance/docker_full_spec.rb +++ b/spec/acceptance/docker_full_spec.rb @@ -1,998 +1,998 @@ # frozen_string_literal: true require 'spec_helper_acceptance' if os[:kernel] == 'windows' docker_args = 'docker_ee => true' default_image = 'winamd64/hello-seattle' default_image_tag = if os[:release][:major] == '2019' 'nanoserver' else 'nanoserver-sac2016' end default_digest = 'sha256:dcba85354678b50608b8c40ec6d17cce063a224aa0e12b6a55dc47b67f039e75' default_local_digest = 'sha256:8421d9a84432575381bfabd248f1eb56f3aa21d9d7cd2511583c68c9b7511d10' second_image = 'winamd64/hola-mundo' default_dockerfile = 'C:/Users/Administrator/AppData/Local/Temp/Dockerfile' dockerfile_test = 'C:/Windows/Dockerfile_test.txt' # The default args are set because: # restart => 'always' - there is no service created to manage containers # net => 'nat' - docker uses bridged by default when running a container. When installing docker on windows the default network is NAT. default_docker_run_arg = "restart => 'always', net => 'nat'," default_run_command = 'ping 127.0.0.1 -t' docker_command = '"/cygdrive/c/Program Files/Docker/docker"' default_docker_exec_lr_command = 'cmd /c "ping 127.0.0.1 -t > C:\Users\Public\test_file.txt"' default_docker_exec_command = 'cmd /c "echo test > C:\Users\Public\test_file.txt"' docker_mount_path = 'C:/Users/Public/DockerVolume' storage_driver = 'windowsfilter' else docker_args = if os[:name] == 'ubuntu' && os[:release][:full] == '14.04' "version => '18.06.1~ce~3-0~ubuntu'" else '' end default_image = 'alpine' second_image = 'busybox' default_image_tag = '3.7' default_digest = 'sha256:3dcdb92d7432d56604d4545cbd324b14e647b313626d99b889d0626de158f73a' default_local_digest = 'sha256:8421d9a84432575381bfabd248f1eb56f3aa21d9d7cd2511583c68c9b7511d10' default_dockerfile = '/root/Dockerfile' dockerfile_test = "#{default_dockerfile}_test.txt" docker_command = 'docker' default_docker_run_arg = '' default_run_command = 'init' default_docker_exec_lr_command = '/bin/sh -c "touch /root/test_file.txt; while true; do echo hello world; sleep 1; done"' default_docker_exec_command = 'touch /root/test_file.txt' docker_mount_path = '/root' storage_driver = 'devicemapper' storage_driver = if os[:family] == 'Debian' && os[:release][:major] =~ %r{14.04|^8$} 'aufs' elsif os[:family] == 'RedHat' 'devicemapper' else 'overlay2' end end describe 'the Puppet Docker module' do context 'clean up before each test', win_broken: true do before(:each) do retry_on_error_matching(60, 5, %r{connection failure running}) do # Stop all container using systemd run_shell('ls -D -1 /etc/systemd/system/docker-container* | sed \'s/\/etc\/systemd\/system\///g\' | sed \'s/\.service//g\' | while read container; do service $container stop; done') # Delete all running containers run_shell("#{docker_command} rm -f $(#{docker_command} ps -a -q) || true") # Delete all existing images run_shell("#{docker_command} rmi -f $(#{docker_command} images -q) || true") # Check to make sure no images are present run_shell("#{docker_command} images | wc -l") do |r| - expect(r.stdout).to match(%r{^0|1$}) # rubocop:disable RSpec/ExpectInHook: + expect(r.stdout).to match(%r{^0|1$}) end # Check to make sure no running containers are present run_shell("#{docker_command} ps | wc -l") do |r| - expect(r.stdout).to match(%r{^0|1$}) # rubocop:disable RSpec/ExpectInHook: + expect(r.stdout).to match(%r{^0|1$}) end end end describe 'docker class' do context 'without any parameters', win_broken: true do let(:pp) { "class { 'docker': #{docker_args} }" } it 'runs successfully' do apply_manifest(pp, catch_failures: true) end it 'runs idempotently' do apply_manifest(pp, catch_changes: true) unless selinux == 'true' end it 'is start a docker process' do if os[:family] == 'windows' run_shell('powershell Get-Process -Name dockerd') do |r| expect(r.stdout).to match(%r{ProcessName}) end else run_shell('ps aux | grep docker') do |r| expect(r.stdout).to match(%r{dockerd -H unix:\/\/\/var\/run\/docker.sock}) end end end it 'installs a working docker client' do run_shell("#{docker_command} ps", expect_failures: false) end it 'stops a running container and remove container' do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_6': image => '#{default_image}', command => '#{default_run_command}', require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } EOS pp2 = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_6': ensure => 'absent', image => '#{default_image}', require => Docker::Image['#{default_image}'], } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 15 run_shell("#{docker_command} ps", expect_failures: false) apply_manifest(pp2, catch_failures: true) apply_manifest(pp2, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 15 run_shell("#{docker_command} inspect container-3-6", expect_failures: true) if os[:family] == 'windows' run_shell('test -f /cygdrive/c/Users/Administrator/AppData/Local/Temp/container-3-6.service', expect_failures: true) else run_shell('test -f /etc/systemd/system/container-3-6.service', expect_failures: true) end end end context 'passing a storage driver' do let(:pp) do <<-MANIFEST class {'docker': #{docker_args}, storage_driver => "#{storage_driver}", } MANIFEST end it 'applies manifest' do apply_manifest(pp, catch_failures: true) sleep 15 end it 'results in the docker daemon being configured with the specified storage driver' do run_shell("#{docker_command} info -f \"{{ .Driver}}\"") do |r| expect(r.stdout).to match %r{#{storage_driver}} end end end context 'passing a TCP address to bind to' do let(:pp) do <<-MANIFEST class { 'docker': tcp_bind => 'tcp://127.0.0.1:4444', #{docker_args} } MANIFEST end it 'runs idempotently' do idempotent_apply(pp) unless selinux == 'true' sleep 4 end it 'results in docker listening on the specified address' do if os[:family] == 'windows' run_shell('netstat -a -b') do |r| expect(r.stdout).to match(%r{127.0.0.1:4444}) end else run_shell('netstat -tulpn | grep docker') do |r| expect(r.stdout).to match(%r{tcp\s+0\s+0\s+127.0.0.1:4444\s+0.0.0.0\:\*\s+LISTEN\s+\d+\/docker}) end end end end context 'bound to a particular unix socket' do let(:pp) do <<-MANIFEST class { 'docker': socket_bind => 'unix:///var/run/docker.sock', #{docker_args} } MANIFEST end it 'runs idempotently' do idempotent_apply(pp) unless selinux == 'true' sleep 4 end it 'shows docker listening on the specified unix socket' do if os[:family] != 'windows' run_shell('ps aux | grep docker') do |r| expect(r.stdout).to match(%r{unix:\/\/\/var\/run\/docker.sock}) end end end end context 'uninstall docker' do after(:all) do pp = <<-EOS class {'docker': #{docker_args}, ensure => 'present' } EOS apply_manifest(pp, catch_failures: true) # Wait for reboot if windows sleep 300 if os[:family] == 'windows' end it 'uninstalls successfully' do pp = <<-EOS class {'docker': #{docker_args}, ensure => 'absent' } EOS apply_manifest(pp, catch_failures: true) sleep 4 run_shell('docker ps', expect_failures: true) end end end describe 'docker::image' do it 'successfullies download an image from the Docker Hub' do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': ensure => present, require => Class['docker'], } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 run_shell("#{docker_command} images") do |r| expect(r.stdout).to match(%r{#{default_image}}) end end it 'successfullies download an image based on a tag from the Docker Hub' do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': ensure => present, image_tag => '#{default_image_tag}', require => Class['docker'], } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 run_shell("#{docker_command} images") do |r| expect(r.stdout).to match(%r{#{default_image}\s+#{default_image_tag}}) end end it 'successfullies download an image based on a digest from the Docker Hub' do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': ensure => present, image_digest => '#{default_digest}', require => Class['docker'], } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 run_shell("#{docker_command} images --digests") do |r| expect(r.stdout).to match(%r{#{default_image}.*#{default_digest}}) end end it 'creates a new image based on a Dockerfile' do run_cmd = if os[:family] == 'windows' 'RUN echo test > C:\\Users\\Public\\Dockerfile_test.txt' else "RUN echo test > #{dockerfile_test}" end pp = <<-EOS class { 'docker': #{docker_args} } docker::image { 'alpine_with_file': docker_file => "#{default_dockerfile}", require => Class['docker'], } file { '#{default_dockerfile}': ensure => present, content => "FROM #{default_image}\n#{run_cmd}", before => Docker::Image['alpine_with_file'], } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 if os[:family] == 'windows' run_shell("#{docker_command} run alpine_with_file cmd /c dir Users\\\\Public") do |r| expect(r.stdout).to match(%r{_test.txt}) end else run_shell("#{docker_command} run alpine_with_file ls #{dockerfile_test}") do |r| expect(r.stdout).to match(%r{#{dockerfile_test}}) end end end it 'creates a new image based on a tar', win_broken: true do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], ensure => present, } docker::run { 'container_2_4': image => '#{default_image}', command => '/bin/sh -c "touch /root/test_file_for_tar_test.txt; while true; do echo hello world; sleep 1; done"', require => Docker::Image['alpine'], } EOS pp2 = <<-EOS class { 'docker': #{docker_args} } docker::image { 'alpine_from_commit': docker_tar => "/root/rootfs.tar" } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 # Commit currently running container as an image container_id = run_shell("#{docker_command} ps | awk 'FNR == 2 {print $1}'") run_shell("#{docker_command} commit #{container_id.stdout.strip} alpine_from_commit") # Stop all container using systemd run_shell('ls -D -1 /etc/systemd/system/docker-container* | sed \'s/\/etc\/systemd\/system\///g\' | sed \'s/\.service//g\' | while read container; do service $container stop; done') # Stop all running containers run_shell("#{docker_command} rm -f $(docker ps -a -q) || true") # Make sure no other containers are running run_shell("#{docker_command} ps | wc -l") do |r| expect(r.stdout).to match(%r{^1$}) end # Export new to a tar file run_shell("#{docker_command} save alpine_from_commit > /root/rootfs.tar") # Remove all images run_shell("#{docker_command} rmi $(docker images -q) || true") # Make sure no other images are present run_shell("#{docker_command} images | wc -l") do |r| expect(r.stdout).to match(%r{^1$}) end apply_manifest(pp2, catch_failures: true) apply_manifest(pp2, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 run_shell("#{docker_command} run alpine_from_commit ls /root") do |r| expect(r.stdout).to match(%r{test_file_for_tar_test.txt}) end end it 'successfullies delete the image' do pp1 = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': ensure => present, require => Class['docker'], } EOS apply_manifest(pp1, catch_failures: true) pp2 = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': ensure => absent, } EOS apply_manifest(pp2, catch_failures: true) apply_manifest(pp2, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 run_shell("#{docker_command} images") do |r| expect(r.stdout).not_to match(%r{#{default_image}}) end end end describe 'docker::run' do it 'starts a container with a configurable command' do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_1': image => '#{default_image}', command => '#{default_docker_exec_lr_command}', require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 container_id = run_shell("#{docker_command} ps | awk 'FNR == 2 {print $1}'") if os[:family] == 'windows' run_shell("#{docker_command} exec #{container_id.stdout.strip} cmd /c dir Users\\\\Public") do |r| expect(r.stdout).to match(%r{test_file.txt}) end else run_shell("#{docker_command} exec #{container_id.stdout.strip} ls /root") do |r| expect(r.stdout).to match(%r{test_file.txt}) end end container_name = run_shell("#{docker_command} ps | awk 'FNR == 2 {print $NF}'") expect(container_name.stdout.strip.to_s).to match(%r{(container-3-1|container_3_1)}) end it 'starts a container with port configuration' do pp = <<-EOS class { 'docker': #{docker_args}} docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_2': image => '#{default_image}', command => '#{default_run_command}', ports => ['4444'], expose => ['5555'], require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 run_shell("#{docker_command} ps") do |r| expect(r.stdout).to match(%r{#{default_run_command}.+5555\/tcp\, 0\.0\.0.0\:\d+\-\>4444\/tcp}) end end it 'starts a container with the hostname set' do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_3': image => '#{default_image}', command => '#{default_run_command}', hostname => 'testdomain.com', require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 container_id = run_shell("#{docker_command} ps | awk 'FNR == 2 {print $1}'") run_shell("#{docker_command} exec #{container_id.stdout.strip} hostname") do |r| expect(r.stdout).to match(%r{testdomain.com}) end end it 'starts a container while mounting local volumes' do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_4': image => '#{default_image}', command => '#{default_run_command}', volumes => ["#{docker_mount_path}:#{docker_mount_path}/mnt:rw"], require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } file { '#{docker_mount_path}': ensure => directory, before => File['#{docker_mount_path}/test_mount.txt'], } file { '#{docker_mount_path}/test_mount.txt': ensure => present, before => Docker::Run['container_3_4'], } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 container_id = run_shell("#{docker_command} ps | awk 'FNR == 2 {print $1}'") if os[:family] == 'windows' run_shell("#{docker_command} exec #{container_id.stdout.strip} cmd /c dir Users\\\\Public\\\\DockerVolume\\\\mnt") do |r| expect(r.stdout).to match(%r{test_mount.txt}) end else run_shell("#{docker_command} exec #{container_id.stdout.strip} ls /root/mnt") do |r| expect(r.stdout).to match(%r{test_mount.txt}) end end end # cpuset is not supported on Docker Windows # STDERR: C:/Program Files/Docker/docker.exe: Error response from daemon: invalid option: Windows does not support CpusetCpus. it 'starts a container with cpuset paramater set', win_broken: true do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_5_5': image => '#{default_image}', command => '#{default_run_command}', cpuset => ['0'], require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 run_shell('#{docker_command} inspect container_3_5_5') do |r| expect(r.stdout).to match(%r{"CpusetCpus"\: "0"}) end end # leagacy container linking was not implemented on Windows. --link is a legacy Docker feature: https://docs.docker.com/network/links/ it 'starts multiple linked containers', win_broken: true do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_5_1': image => '#{default_image}', command => '#{default_run_command}', require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 container1 = run_shell("#{docker_command} ps | awk 'FNR == 2 {print $NF}'") pp2 = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_5_2': image => '#{default_image}', command => '#{default_run_command}', depends => ['#{container1.stdout.strip}'], links => "#{container1.stdout.strip}:the_link", require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } EOS apply_manifest(pp2, catch_failures: true) apply_manifest(pp2, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 container2 = run_shell("#{docker_command} ps | awk 'FNR == 2 {print $NF}'") container_id = run_shell("#{docker_command} ps | awk 'FNR == 2 {print $1}'") run_shell("#{docker_command} inspect -f \"{{ .HostConfig.Links }}\" #{container_id.stdout.strip}") do |r| expect(r.stdout).to match("/#{container1.stdout.strip}:/#{container2.stdout.strip}/the_link") end end it 'stops a running container', win_broken: true do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_6': image => '#{default_image}', command => '#{default_run_command}', require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } EOS pp2 = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_6': image => '#{default_image}', running => false, require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 run_shell("#{docker_command} ps | wc -l") do |r| expect(r.stdout).to match(%r{^2$}) end apply_manifest(pp2, catch_failures: true) apply_manifest(pp2, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 run_shell("#{docker_command} ps | wc -l") do |r| expect(r.stdout).to match(%r{^1$}) end end it 'stops a running container and remove container' do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_6_1': image => '#{default_image}', command => '#{default_run_command}', require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } EOS pp2 = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_3_6_1': ensure => 'absent', image => '#{default_image}', require => Docker::Image['#{default_image}'], } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 15 run_shell("#{docker_command} inspect container_3_6_1", expect_failures: false) apply_manifest(pp2, catch_failures: true) apply_manifest(pp2, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 15 run_shell("#{docker_command} inspect container_3_6_1", expect_failures: true) end it 'allows dependency for ordering of independent run and image' do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': } docker::run { 'container_3_7_1': image => '#{default_image}', command => '#{default_run_command}', #{default_docker_run_arg} } docker::image { '#{second_image}': require => Docker::Run['container_3_7_1'], } docker::run { 'container_3_7_2': image => '#{second_image}', command => '#{default_run_command}', #{default_docker_run_arg} } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' end it 'restarts a unhealthy container' do pp5 = <<-EOS class { 'docker': #{docker_args} } docker::run { 'container_3_7_3': image => '#{default_image}', command => '#{default_run_command}', health_check_cmd => 'echo', restart_on_unhealthy => true, #{default_docker_run_arg} } EOS pp_delete = <<-EOS class { 'docker': #{docker_args} } docker::run { 'container_3_7_3': image => '#{default_image}', ensure => absent, } EOS if os[:family] == 'windows' apply_manifest(pp5, catch_failures: true) - elsif os[:release] =~ %r{14.04|^8$} + elsif %r{14.04|^8$}.match?(os[:release]) apply_manifest(pp5, catch_failures: true) do |r| expect(r.stdout).to match(%r{container_3_7_3}) end else apply_manifest(pp5, catch_failures: true) do |r| expect(r.stdout).to match(%r{docker-container_3_7_3-systemd-reload}) end end apply_manifest(pp_delete, catch_failures: true) end end it 'run with verify_digest' do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}:#{default_image_tag}': require => Class['docker'], } docker::run { '#{default_image}': image => '#{default_image}:#{default_image_tag}', verify_digest => '#{default_local_digest}', } EOS pp_invalid = <<-EOS docker::run { '#{default_image}': image => '#{default_image}:#{default_image_tag}', verify_digest => 'sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc000', } EOS apply_manifest(pp, catch_failures: true) run_shell('/usr/local/bin/docker-run-alpine-start.sh', expect_failures: false) do |r| expect(r.stdout.include?('Digest verify failed!')).to be false end apply_manifest(pp_invalid, catch_failures: true) run_shell('/usr/local/bin/docker-run-alpine-start.sh', expect_failures: true) do |r| expect(r.stdout.include?('Digest verify failed!')).to be true end end end describe 'docker::exec', win_broken: true do it 'runs a command inside an already running container' do pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': require => Class['docker'], } docker::run { 'container_4_1': image => '#{default_image}', command => '#{default_run_command}', require => Docker::Image['#{default_image}'], #{default_docker_run_arg} } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 15 container1 = run_shell("#{docker_command} ps | awk 'FNR == 2 {print $NF}'") pp2 = <<-EOS class { 'docker': #{docker_args} } docker::exec { 'test_command': container => '#{container1.stdout.strip}', command => '#{default_docker_exec_command}', tty => true, } EOS pp_delete = <<-EOS docker::run { 'container_4_1': image => '#{default_image}', ensure => absent, } EOS apply_manifest(pp2, catch_failures: true) # A sleep to give docker time to execute properly sleep 4 container_id = run_shell("#{docker_command} ps | awk 'FNR == 2 {print $1}'") if os[:family] == 'windows' run_shell("#{docker_command} exec #{container_id.stdout.strip} cmd /c dir Users\\\\Public") do |r| expect(r.stdout).to match(%r{test_file.txt}) end else run_shell("#{docker_command} exec #{container_id.stdout.strip} ls /root") do |r| expect(r.stdout).to match(%r{test_file.txt}) end end apply_manifest(pp_delete, catch_failures: true) end it 'onlies run if notified when refreshonly is true', win_broken: true do container_name = 'container_4_2' pp = <<-EOS class { 'docker': #{docker_args} } docker::image { '#{default_image}': } docker::run { '#{container_name}': image => '#{default_image}', command => '#{default_run_command}', #{default_docker_run_arg} } docker::exec { 'test_command': container => '#{container_name}', command => '#{default_docker_exec_command}', refreshonly => true, } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 if os[:family] == 'windows' run_shell("#{docker_command} exec #{container_name} cmd /c dir Users\\\\Public") do |r| expect(r.stdout).not_to match(%r{test_file.txt}) end else run_shell("#{docker_command} exec #{container_name} ls /root") do |r| expect(r.stdout).not_to match(%r{test_file.txt}) end end pp_extra = <<-EOS file { '#{default_dockerfile}_dummy_file': ensure => 'present', notify => Docker::Exec['test_command'], } EOS pp_delete = <<-EOS docker::run { '#{container_name}': image => '#{default_image}', ensure => absent, } EOS pp2 = pp + pp_extra apply_manifest(pp2, catch_failures: true) apply_manifest(pp2, catch_changes: true) unless selinux == 'true' # A sleep to give docker time to execute properly sleep 4 if os[:family] == 'windows' run_shell("#{docker_command} exec #{container_name} cmd /c dir Users\\\\Public") do |r| expect(r.stdout).to match(%r{home}) end else run_shell("#{docker_command} exec #{container_name} ls /") do |r| expect(r.stdout).to match(%r{home}) end end apply_manifest(pp_delete, catch_failures: true) end end end diff --git a/spec/classes/compose_spec.rb b/spec/classes/compose_spec.rb index b183a4c..3b0d7a9 100644 --- a/spec/classes/compose_spec.rb +++ b/spec/classes/compose_spec.rb @@ -1,92 +1,92 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'with default valus' => { }, 'with ensure => absent' => { 'ensure' => 'absent', }, 'with version => 1.7.0' => { 'version' => '1.7.0', }, 'when proxy is provided' => { 'version' => '1.7.0', 'proxy' => 'http://proxy.example.org:3128/', }, 'when proxy is not a http proxy' => { 'proxy' => 'this is not a URL', }, 'when proxy contains username and password' => { 'version' => '1.7.0', 'proxy' => 'http://user:password@proxy.example.org:3128/', }, 'when proxy IP is provided' => { 'version' => '1.7.0', 'proxy' => 'http://10.10.10.10:3128/', }, 'when base_url is provided' => { 'version' => '1.7.0', 'base_url' => 'http://example.org', }, 'when raw_url is provided' => { 'version' => '1.7.0', 'raw_url' => 'http://example.org', }, } describe 'docker::compose', type: :class do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'ensure' => 'present', 'version' => defaults['compose_version'], 'install_path' => defaults['compose_install_path'], 'symlink_name' => defaults['compose_symlink_name'], 'proxy' => :undef, 'base_url' => defaults['compose_base_url'], 'raw_url' => :undef, 'curl_ensure' => defaults['curl_ensure'], }.merge(local_params) let(:facts) do facts end let(:params) do params end if params['proxy'] != :undef - if params['proxy'] !~ %r{^((http[s]?)?:\/\/)?([^:^@]+:[^:^@]+@|)([\da-z\.-]+)\.([\da-z\.]{2,6})(:[\d])?([\/\w \.-]*)*\/?$} + unless %r{^((http[s]?)?:\/\/)?([^:^@]+:[^:^@]+@|)([\da-z\.-]+)\.([\da-z\.]{2,6})(:[\d])?([\/\w \.-]*)*\/?$}.match?(params['proxy']) it { is_expected.to compile.and_raise_error(%r{does not match}) } next end end include_examples 'compose', params, facts end end end end end diff --git a/spec/classes/images_spec.rb b/spec/classes/images_spec.rb index d952384..8d5255f 100644 --- a/spec/classes/images_spec.rb +++ b/spec/classes/images_spec.rb @@ -1,70 +1,70 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'with ensure => present' => { 'ensure' => 'present', }, 'with ensure => absent' => { 'ensure' => 'absent', }, } describe 'docker::images', type: :class do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'base' => { 'ensure' => 'present', 'image_tag' => :undef, 'image_digest' => :undef, 'force' => false, 'docker_file' => :undef, 'docker_dir' => :undef, 'docker_tar' => :undef, }, } params.each do |key, values| values.merge!(local_params.merge('image' => key)) let(:facts) do facts end let(:params) do { 'images' => { key => values, }, } end include_examples 'image', values, facts, defaults it { is_expected.to contain_docker__image(key) } end end end end end end diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index a7bb624..ee1521e 100755 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -1,224 +1,224 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'with default values' => { }, 'with docker_users set' => { 'docker_users' => [ 'some_random_user', 'foo', 'bar', ], }, 'with package_source set to docker-engine' => { 'package_source' => 'docker-engine', }, 'with package_source set to docker-ce' => { 'package_source' => 'docker-ce', }, 'with ensure set to absent' => { 'ensure' => 'absent', }, 'with ensure set to absent and given version' => { 'ensure' => 'absent', 'version' => '16', }, 'with ms parameter set' => { 'version' => '16', 'docker_msft_provider_version' => '123', 'nuget_package_provider_version' => '41', }, } describe 'docker', type: :class do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - if os =~ %r{windows} + if %r{windows}.match?(os) facts = windows_facts.merge(os_facts) default_params = { 'docker_ee' => true, 'docker_users' => [], } else facts = os_facts default_params = { 'docker_users' => [], } end ## ## get default values based on facts ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'acknowledge_unsupported_os' => false, 'apt_source_pin_level' => defaults['apt_source_pin_level'], 'bip' => defaults['bip'], 'bridge' => defaults['bridge'], 'daemon_environment_files' => [], 'default_gateway_ipv6' => defaults['default_gateway_ipv6'], 'default_gateway' => defaults['default_gateway'], 'dependent_packages' => defaults['dependent_packages'], 'dm_basesize' => defaults['dm_basesize'], 'dm_blkdiscard' => defaults['dm_blkdiscard'], 'dm_blocksize' => defaults['dm_blocksize'], 'dm_datadev' => defaults['dm_datadev'], 'dm_fs' => defaults['dm_fs'], 'dm_loopdatasize' => defaults['dm_loopdatasize'], 'dm_loopmetadatasize' => defaults['dm_loopmetadatasize'], 'dm_metadatadev' => defaults['dm_metadatadev'], 'dm_mkfsarg' => defaults['dm_mkfsarg'], 'dm_mountopt' => defaults['dm_mountopt'], 'dm_override_udev_sync_check' => defaults['dm_override_udev_sync_check'], 'dm_thinpooldev' => defaults['dm_thinpooldev'], 'dm_use_deferred_deletion' => defaults['dm_use_deferred_deletion'], 'dm_use_deferred_removal' => defaults['dm_use_deferred_removal'], 'dns_search' => defaults['dns_search'], 'dns' => defaults['dns'], 'docker_ce_channel' => defaults['docker_ce_channel'], 'docker_ce_key_id' => defaults['package_ce_key_id'], 'docker_ce_key_source' => defaults['package_ce_key_source'], 'docker_ce_package_name' => defaults['docker_ce_package_name'], 'docker_ce_release' => defaults['package_ce_release'], 'docker_ce_source_location' => defaults['package_ce_source_location'], 'docker_ce_start_command' => defaults['docker_ce_start_command'], 'docker_ee_key_id' => defaults['package_ee_key_id'], 'docker_ee_key_source' => defaults['package_ee_key_source'], 'docker_ee_package_name' => defaults['package_ee_package_name'], 'docker_ee_release' => defaults['package_ee_release'], 'docker_ee_repos' => defaults['package_ee_repos'], 'docker_ee_source_location' => defaults['package_ee_source_location'], 'docker_ee' => defaults['docker_ee'], 'docker_engine_package_name' => defaults['docker_engine_package_name'], 'docker_engine_start_command' => defaults['docker_engine_start_command'], 'docker_group' => defaults['docker_group'], 'docker_msft_provider_version' => defaults['docker_msft_provider_version'], 'docker_package_key_check_source' => defaults['package_key_check_source'], 'docker_package_key_id' => defaults['package_key_id'], 'docker_package_key_source' => defaults['package_key_source'], 'docker_package_location' => defaults['package_source_location'], 'docker_package_release' => defaults['package_release'], 'docker_users' => [], 'ensure' => defaults['package_ensure'], 'execdriver' => defaults['execdriver'], 'extra_parameters' => :undef, 'fixed_cidr' => defaults['fixed_cidr'], 'icc' => defaults['icc'], 'ip_forward' => defaults['ip_forward'], 'ip_masq' => defaults['ip_masq'], 'iptables' => defaults['iptables'], 'ipv6_cidr' => defaults['ipv6_cidr'], 'ipv6' => defaults['ipv6'], 'labels' => defaults['labels'], 'log_driver' => defaults['log_driver'], 'log_level' => defaults['log_level'], 'log_opt' => defaults['log_opt'], 'manage_package' => defaults['manage_package'], 'manage_service' => defaults['manage_service'], 'mtu' => defaults['mtu'], 'no_proxy' => defaults['no_proxy'], 'nuget_package_provider_version' => defaults['nuget_package_provider_version'], 'os_lc' => defaults['os_lc'], 'overlay2_override_kernel_check' => defaults['overlay2_override_kernel_check'], 'package_release' => defaults['package_release'], 'package_source' => defaults['package_source'], 'pin_upstream_package_source' => defaults['pin_upstream_package_source'], 'prerequired_packages' => defaults['prerequired_packages'], 'proxy' => defaults['proxy'], 'registry_mirror' => defaults['registry_mirror'], 'repo_opt' => defaults['repo_opt'], 'root_dir' => defaults['root_dir'], 'selinux_enabled' => defaults['selinux_enabled'], 'service_after_override' => defaults['service_after_override'], 'service_config_template' => defaults['service_config_template'], 'service_config' => defaults['service_config'], 'service_enable' => defaults['service_enable'], 'service_hasrestart' => defaults['service_hasrestart'], 'service_hasstatus' => defaults['service_hasstatus'], 'service_name' => defaults['service_name'], 'service_overrides_template' => defaults['service_overrides_template'], 'service_provider' => defaults['service_provider'], 'service_state' => defaults['service_state'], 'shell_values' => :undef, 'socket_bind' => defaults['socket_bind'], 'socket_group' => defaults['socket_group'], 'socket_override' => defaults['socket_override'], 'socket_overrides_template' => defaults['socket_overrides_template'], 'storage_auto_extend_pool' => defaults['storage_auto_extend_pool'], 'storage_chunk_size' => defaults['storage_chunk_size'], 'storage_config_template' => defaults['storage_config_template'], 'storage_config' => defaults['storage_config'], 'storage_data_size' => defaults['storage_data_size'], 'storage_devs' => defaults['storage_devs'], 'storage_driver' => defaults['storage_driver'], 'storage_growpart' => defaults['storage_growpart'], 'storage_min_data_size' => defaults['storage_min_data_size'], 'storage_pool_autoextend_percent' => defaults['storage_pool_autoextend_percent'], 'storage_pool_autoextend_threshold' => defaults['storage_pool_autoextend_threshold'], 'storage_root_size' => defaults['storage_root_size'], 'storage_setup_file' => defaults['storage_setup_file'], 'storage_vg' => defaults['storage_vg'], 'tcp_bind' => defaults['tcp_bind'], 'tls_cacert' => defaults['tls_cacert'], 'tls_cert' => defaults['tls_cert'], 'tls_enable' => defaults['tls_enable'], 'tls_key' => defaults['tls_key'], 'tls_verify' => defaults['tls_verify'], 'tmp_dir_config' => defaults['tmp_dir_config'], 'tmp_dir' => defaults['tmp_dir'], 'use_upstream_package_source' => defaults['use_upstream_package_source'], 'version' => defaults['version'], }.merge(default_params).merge(local_params) let(:facts) do facts end let(:params) do params end if params['ensure'] != 'absent' if params['package_source'] != :undef && facts[:os]['family'] =~ %r{windows} it { is_expected.to compile.and_raise_error(%r{Custom package source is currently not implemented on windows.}) } else it { is_expected.to contain_class('docker::repos').that_comes_before('Class[docker::install]') is_expected.to contain_class('docker::install').that_comes_before('Class[docker::config]') is_expected.to contain_class('docker::config').that_comes_before('Class[docker::service]') is_expected.to contain_class('docker::service') } include_examples 'params', facts include_examples 'repos', params, facts include_examples 'install', params, facts include_examples 'config', params, facts include_examples 'service', params, facts end else it { is_expected.to contain_class('docker::repos').that_comes_before('Class[docker::install]') is_expected.to contain_class('docker::install') } include_examples 'params', facts include_examples 'repos', params, facts include_examples 'install', params, facts end end end end end end diff --git a/spec/classes/machine_spec.rb b/spec/classes/machine_spec.rb index afca070..6283473 100644 --- a/spec/classes/machine_spec.rb +++ b/spec/classes/machine_spec.rb @@ -1,61 +1,61 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'with default value' => { }, } describe 'docker::machine', type: :class do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'ensure' => 'present', 'version' => defaults['machine_version'], 'install_path' => defaults['machine_install_path'], 'proxy' => :undef, 'url' => :undef, 'curl_ensure' => defaults['curl_ensure'], }.merge(local_params) let(:facts) do facts end let(:params) do params end if params['proxy'] != :undef - if params['proxy'] !~ '^((http[s]?)?:\/\/)?([^:^@]+:[^:^@]+@|)([\da-z\.-]+)\.([\da-z\.]{2,6})(:[\d])?([\/\w \.-]*)*\/?$' + unless '^((http[s]?)?:\/\/)?([^:^@]+:[^:^@]+@|)([\da-z\.-]+)\.([\da-z\.]{2,6})(:[\d])?([\/\w \.-]*)*\/?$'.match?(params['proxy']) it { is_expected.to compile.and_raise_error(%r{}) } next end end include_examples 'machine', params, facts, defaults end end end end end diff --git a/spec/classes/networks_spec.rb b/spec/classes/networks_spec.rb index b61287e..53ec229 100644 --- a/spec/classes/networks_spec.rb +++ b/spec/classes/networks_spec.rb @@ -1,68 +1,68 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'network-present' => { 'ensure' => 'present', 'driver' => 'overlay', 'subnet' => '192.168.1.0/24', 'gateway' => '192.168.1.1', 'ip_range' => '192.168.1.4/32', }, 'network-absent' => { 'ensure' => 'absent', 'driver' => 'overlay', 'subnet' => '192.168.1.0/24', 'gateway' => '192.168.1.1', 'ip_range' => '192.168.1.4/32', }, } describe 'docker::networks', type: :class do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = local_params let(:facts) do facts end let(:params) do { 'networks' => { title => local_params, }, } end it { is_expected.to contain_docker_network(title).with( 'ensure' => params['ensure'], 'driver' => params['driver'], 'subnet' => params['subnet'], 'gateway' => params['gateway'], 'ip_range' => params['ip_range'], ) } end end end end end diff --git a/spec/classes/plugins_spec.rb b/spec/classes/plugins_spec.rb index 1c32f71..012fda0 100644 --- a/spec/classes/plugins_spec.rb +++ b/spec/classes/plugins_spec.rb @@ -1,80 +1,80 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'foo/enabled:latest' => { 'enabled' => true, }, 'foo/disabled:latest' => { 'enabled' => false, }, } describe 'docker::plugins', type: :class do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'base' => { 'ensure' => 'present', 'enabled' => true, 'timeout' => :undef, 'plugin_alias' => :undef, 'disable_on_install' => false, 'disable_content_trust' => true, 'grant_all_permissions' => true, 'force_remove' => true, 'settings' => [], }, } params.each do |key, values| values.merge!(local_params.merge('plugin_name' => key)) let(:facts) do facts end let(:params) do { 'plugins' => { key => values, }, } end if facts[:os]['family'] == 'windows' it { is_expected.to compile.and_raise_error(%r{Feature not implemented on windows.}) } next end include_examples 'plugin', values, facts, defaults it { is_expected.to contain_docker__plugin(key) } end end end end end end diff --git a/spec/defines/exec_spec.rb b/spec/defines/exec_spec.rb index 8706416..b33051c 100644 --- a/spec/defines/exec_spec.rb +++ b/spec/defines/exec_spec.rb @@ -1,96 +1,96 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'with default values' => { }, 'when running detached' => { 'detach' => true, }, 'when running with tty' => { 'tty' => true, }, 'when running with interactive' => { 'interactive' => true, }, 'when running with onlyif "running"' => { 'interactive' => true, 'onlyif' => 'running', }, 'when running without onlyif custom command' => { 'interactive' => true, 'onlyif' => 'custom', }, 'when running without onlyif' => { 'interactive' => true, }, 'when running with unless' => { 'interactive' => true, 'unless' => 'some_command arg1', }, 'when running without unless' => { 'interactive' => true, }, 'with title that need sanitisation' => { 'detach' => true, 'sanitise_name' => true, }, 'with environment variables passed to exec' => { 'env' => [ 'FOO=BAR', 'FOO2=BAR2', ], }, } describe 'docker::exec', type: :define do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'command' => '/bin/echo foo', 'container' => 'some_conainer_name', 'detach' => false, 'env' => [], 'interactive' => false, 'onlyif' => :undef, 'refreshonly' => false, 'sanitise_name' => true, 'tty' => false, 'unless' => :undef, }.merge(local_params) let(:facts) do facts end let(:params) do params end let(:title) do title end include_examples 'exec', params, facts, defaults end end end end end diff --git a/spec/defines/image_spec.rb b/spec/defines/image_spec.rb index aeca24d..08cd7d3 100644 --- a/spec/defines/image_spec.rb +++ b/spec/defines/image_spec.rb @@ -1,182 +1,182 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'with default value' => { }, 'with ensure => absent' => { 'ensure' => 'absent', }, 'with ensure => absent and force => true' => { 'ensure' => 'absent', 'force' => true, }, 'with ensure => absent and image_tag => precise' => { 'ensure' => 'absent', 'image_tag' => 'precise', }, 'with docker_file => Dockerfile' => { 'docker_file' => 'Dockerfile', }, 'with ensure => present and docker_file => Dockerfile' => { 'ensure' => 'present', 'docker_file' => 'Dockerfile', }, 'with docker_dir => /tmp/docker_images/test1 and docker_file => /tmp/docker_images/test1/Dockerfile_altbuild' => { 'docker_dir' => '/tmp/docker_images/test1', 'docker_file' => '/tmp/docker_images/test1/Dockerfile_altbuild', }, 'with docker_dir => /tmp/docker_images/test1' => { 'docker_dir' => '/tmp/docker_images/test1', }, 'with ensure => present and docker_dir => /tmp/docker_images/test1' => { 'ensure' => 'present', 'docker_dir' => '/tmp/docker_images/test1', }, 'with ensure => present and image_tag => precise' => { 'ensure' => 'present', 'image_tag' => 'precise', }, 'with ensure => present and image_digest => sha256:deadbeef' => { 'ensure' => 'present', 'image_digest' => 'sha256:deadbeef', }, 'with ensure => present and image_tag => precise and docker_file => Dockerfile' => { 'ensure' => 'present', 'image_tag' => 'precise', 'docker_file' => 'Dockerfile', }, 'with ensure => present and image_tag => precise and docker_dir => /tmp/docker_images/test1' => { 'ensure' => 'present', 'image_tag' => 'precise', 'docker_dir' => '/tmp/docker_images/test1', }, 'with docker_tar => /tmp/docker_tars/test1.tar' => { 'docker_tar' => '/tmp/docker_tars/test1.tar', }, 'with ensure => present and docker_tar => /tmp/docker_tars/test1.tar' => { 'ensure' => 'present', 'docker_tar' => '/tmp/docker_tars/test1.tar', }, 'with docker_file => Dockerfile and docker_tar => /tmp/docker_tars/test1.tar' => { 'docker_file' => 'Dockerfile', 'docker_tar' => '/tmp/docker_tars/test1.tar', }, 'with docker_tar => /tmp/docker_tars/test1.tar and docker_dir => /tmp/docker_images/test1' => { 'docker_tar' => '/tmp/docker_tars/test1.tar', 'docker_dir' => '/tmp/docker_images/test1', }, 'with image_digest => sha256:deadbeef and docker_file => Dockerfile' => { 'image_digest' => 'sha256:deadbeef', 'docker_file' => 'Dockerfile', }, 'with image_digest => sha256:deadbeef and docker_dir => /tmp/docker_images/test1' => { 'image_digest' => 'sha256:deadbeef', 'docker_dir' => '/tmp/docker_images/test1', }, 'with image_digest => sha256:deadbeef and docker_tar => /tmp/docker_tars/test1.tar' => { 'image_digest' => 'sha256:deadbeef', 'docker_tar' => '/tmp/docker_tars/test1.tar', }, 'with ensure => latest' => { 'ensure' => 'latest', }, 'with ensure => latest and image_tag => precise' => { 'ensure' => 'latest', 'image_tag' => 'precise', }, 'with ensure => latest and image_digest => sha256:deadbeef' => { 'ensure' => 'latest', 'image_digest' => 'sha256:deadbeef', }, } describe 'docker::image', type: :define do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'ensure' => 'present', 'image' => 'base', 'image_tag' => :undef, 'image_digest' => :undef, 'force' => false, 'docker_file' => :undef, 'docker_dir' => :undef, 'docker_tar' => :undef, }.merge(local_params) let(:facts) do facts end let(:params) do params end let(:title) do params['image'] end if params['docker_file'] != :undef && params['docker_tar'] != :undef it { is_expected.to compile.and_raise_error(%r{docker::image must not have both \$docker_file and \$docker_tar set}) } next end if params['docker_dir'] != :undef && params['docker_tar'] != :undef it { is_expected.to compile.and_raise_error(%r{docker::image must not have both \$docker_dir and \$docker_tar set}) } next end if params['image_digest'] != :undef && params['docker_file'] != :undef it { is_expected.to compile.and_raise_error(%r{docker::image must not have both \$image_digest and \$docker_file set}) } next end if params['image_digest'] != :undef && params['docker_dir'] != :undef it { is_expected.to compile.and_raise_error(%r{docker::image must not have both \$image_digest and \$docker_dir set}) } next end if params['image_digest'] != :undef && params['docker_tar'] != :undef it { is_expected.to compile.and_raise_error(%r{docker::image must not have both \$image_digest and \$docker_tar set}) } next end include_examples 'image', params, facts, defaults end end end end end diff --git a/spec/defines/plugin_spec.rb b/spec/defines/plugin_spec.rb index 8a5d74e..1d4fc1b 100644 --- a/spec/defines/plugin_spec.rb +++ b/spec/defines/plugin_spec.rb @@ -1,78 +1,78 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'foo/setting:latest' => { 'settings' => [ 'VAR1=test', 'VAR2=value', ], }, 'foo/disabled:latest' => { 'enabled' => false, }, 'foo/force_remove:latest' => { 'ensure' => 'absent', 'force_remove' => true, }, } describe 'docker::plugin', type: :define do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'ensure' => 'present', 'plugin_name' => title, 'enabled' => true, 'timeout' => :undef, 'plugin_alias' => :undef, 'disable_on_install' => false, 'disable_content_trust' => true, 'grant_all_permissions' => true, 'force_remove' => true, 'settings' => [], }.merge(local_params) let(:facts) do facts end let(:params) do params end let(:title) do title end if facts[:os]['family'] == 'windows' it { is_expected.to compile.and_raise_error(%r{Feature not implemented on windows.}) } next end include_examples 'plugin', params, facts, defaults end end end end end diff --git a/spec/defines/registry_spec.rb b/spec/defines/registry_spec.rb index a41fe32..bae0e0d 100644 --- a/spec/defines/registry_spec.rb +++ b/spec/defines/registry_spec.rb @@ -1,152 +1,152 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'with ensure => absent' => { 'ensure' => 'absent', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false, }, 'with ensure => present' => { 'ensure' => 'present', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false, }, 'with ensure => present and username => user1' => { 'ensure' => 'present', 'username' => 'user1', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false, }, 'with ensure => present and password => secret' => { 'ensure' => 'present', 'password' => 'secret', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false, }, 'with ensure => present and email => user1@example.io' => { 'ensure' => 'present', 'email' => 'user1@example.io', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false, }, 'with ensure => present and username => user1, and password => secret and email => user1@example.io' => { 'ensure' => 'present', 'username' => 'user1', 'password' => 'secret', 'email' => 'user1@example.io', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false, }, 'with ensure => present and username => user1, and password => secret and email => user1@example.io and version < 1.11.0' => { 'ensure' => 'present', 'username' => 'user1', 'password' => 'secret', 'email' => 'user1@example.io', 'version' => '1.9.0', 'pass_hash' => 'test1234', 'receipt' => false, }, 'with username => user1, and password => secret' => { 'username' => 'user1', 'password' => 'secret', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false, }, 'with username => user1, and password => secret and local_user => testuser' => { 'username' => 'user1', 'password' => 'secret', 'local_user' => 'testuser', 'version' => '17.06', 'pass_hash' => 'test1234', 'receipt' => false, }, } describe 'docker::registry', type: :define do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - if os =~ %r{windows} + if %r{windows}.match?(os) facts = windows_facts.merge( 'docker_home_dirs' => { 'user' => '/home/user', 'root' => '/root', }, ).merge(os_facts) docker_params = { 'docker_ee' => true, } else facts = { docker_home_dirs: { 'user' => '/home/user', 'root' => '/root', }, }.merge(os_facts) docker_params = {} end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'server' => title, 'ensure' => 'present', 'username' => :undef, 'password' => :undef, 'pass_hash' => :undef, 'email' => :undef, 'local_user' => 'root', 'version' => defaults['version'], 'receipt' => true, }.merge(local_params) let(:facts) do facts end let(:params) do params end let(:title) do title end let :pre_condition do <<-MANIFEST function pw_hash($foo, $bar, $asdf) { return '$6$foobar$v8j5roVj0D8t.Ipwvk0RrMHiZfZRoBMeVQDywxUKFtdRI2EFRi2X6tbOigjpOsa9UDVzgIBtcl2ZEGcM.jnZZ.' } class { 'docker': version => "#{params['version']}", * => #{docker_params}, } MANIFEST end include_examples 'registry', title, params, facts, defaults end end end end end diff --git a/spec/defines/run_spec.rb b/spec/defines/run_spec.rb index 25471ce..81fa35c 100644 --- a/spec/defines/run_spec.rb +++ b/spec/defines/run_spec.rb @@ -1,365 +1,365 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'default values' => {}, 'when passing depends containers' => { 'depends' => [ 'foo', 'bar', 'foo_bar/baz', ], }, 'when passing after containers' => { 'after' => [ 'foo', 'bar', 'foo_bar/baz', ], }, 'when use_name is true' => { 'use_name' => true, }, 'when stopping the service' => { 'running' => false, }, 'when passing a cpuset' => { 'cpuset' => '3', }, 'when passing a multiple cpu cpuset' => { 'cpuset' => [ '0', '3', ], }, 'when passing a links option' => { 'links' => [ 'example:one', 'example:two', ], }, 'when passing a hostname' => { 'hostname' => 'example.com', }, 'when passing a username' => { 'username' => 'bob', }, 'when passing a port number' => { 'ports' => '4444', }, 'when passing a port to expose' => { 'expose' => '4666', }, 'when passing a label' => { 'labels' => 'key=value', }, 'when passing a hostentry' => { 'hostentries' => 'dummyhost:127.0.0.2', }, 'when connecting to shared data volumes' => { 'volumes_from' => '6446ea52fbc9', }, 'when connecting to several shared data volumes' => { 'volumes_from' => [ 'sample-linked-container-1', 'sample-linked-container-2', ], }, 'when passing several port numbers' => { 'ports' => [ '4444', '4555', ], }, 'when passing several labels' => { 'labels' => [ 'key1=value1', 'key2=value2', ], }, 'when passing several ports to expose' => { 'expose' => [ '4666', '4777', ], }, 'when passing serveral environment variables' => { 'env' => [ 'FOO=BAR', 'FOO2=BAR2', ], }, 'when passing an environment variable' => { 'env' => 'FOO=BAR', }, 'when passing serveral environment files' => { 'env_file' => [ '/etc/foo.env', '/etc/bar.env', ], }, 'when passing an environment file' => { 'env_file' => '/etc/foo.env', }, 'when passing serveral dns addresses' => { 'dns' => [ '8.8.8.8', '8.8.4.4', ], }, 'when passing a dns address' => { 'dns' => '8.8.8.8', }, 'when passing serveral sockets to connect to' => { 'socket_connect' => [ 'tcp://127.0.0.1:4567', 'tcp://127.0.0.2:4567', ], }, 'when passing a socket to connect to' => { 'socket_connect' => 'tcp://127.0.0.1:4567', }, 'when passing serveral dns search domains' => { 'dns_search' => [ 'my.domain.local', 'other-domain.de', ], }, 'when passing a dns search domain' => { 'dns_search' => 'my.domain.local', }, 'when disabling network' => { 'disable_network' => true, }, 'when running privileged' => { 'privileged' => true, }, 'when passing serveral extra parameters' => { 'extra_parameters' => ['--rm', '-w /tmp'], }, 'when passing an extra parameter' => { 'extra_parameters' => '-c 4', }, 'when passing a data volume' => { 'volumes' => '/var/log', }, 'when passing serveral data volume' => { 'volumes' => [ '/var/lib/couchdb', '/var/log', ], }, 'when pull_on_start is true' => { 'pull_on_start' => true, }, 'when pull_on_start is false' => { 'pull_on_start' => false, }, 'when before_start is set' => { 'before_start' => 'echo before_start', }, 'when before_start is not set' => { 'before_start' => false, }, 'when before_stop is set' => { 'before_stop' => 'echo before_stop', }, 'when before_stop is not set' => { 'before_stop' => false, }, 'when after_start is set' => { 'after_start' => 'echo after_start', }, 'when after_start is not set' => { 'after_start' => false, }, 'when after_stop is set' => { 'after_stop' => 'echo after_stop', }, 'when after_stop is not set' => { 'after_stop' => false, }, 'when docker_service is false' => { 'docker_service' => false, }, 'when docker_service is true' => { 'docker_service' => true, }, 'when docker_service is true and restart_service_on_docker_refresh is false' => { 'docker_service' => true, 'restart_service_on_docker_refresh' => false, }, 'when docker_service is my-docker' => { 'docker_service' => 'my-docker', }, 'when docker_service is my-docker and restart_service_on_docker_refresh is false' => { 'docker_service' => 'my-docker', 'restart_service_on_docker_refresh' => false, }, } describe 'docker::run', type: :define do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - if os =~ %r{windows} + if %r{windows}.match?(os) facts = windows_facts.merge(os_facts) os_params = { 'restart' => 'no', 'extra_parameters' => '-c 4', } docker_params = { 'docker_ee' => true, } else facts = {}.merge(os_facts) os_params = {} docker_params = {} end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'after_create' => :undef, 'after_service' => [], 'after_start' => false, 'after_stop' => false, 'after' => [], 'before_start' => false, 'before_stop' => false, 'command' => 'command', 'cpuset' => [], 'custom_unless' => [], 'depend_services' => ['docker.service'], 'depends' => [], 'detach' => :undef, 'disable_network' => false, 'dns_search' => [], 'dns' => [], 'docker_service' => false, 'ensure' => 'present', 'env_file' => [], 'env' => [], 'expose' => [], 'extra_parameters' => :undef, 'extra_systemd_parameters' => {}, 'health_check_cmd' => :undef, 'health_check_interval' => :undef, 'hostentries' => [], 'hostname' => false, 'image' => 'base', 'labels' => [], 'links' => [], 'lxc_conf' => [], 'manage_service' => true, 'memory_limit' => '0b', 'net' => 'bridge', 'ports' => [], 'privileged' => false, 'pull_on_start' => false, 'read_only' => false, 'remain_after_exit' => :undef, 'remove_container_on_start' => true, 'remove_container_on_stop' => true, 'remove_volume_on_start' => false, 'remove_volume_on_stop' => false, 'restart_on_unhealthy' => false, 'restart_service_on_docker_refresh' => true, 'restart_service' => true, 'restart' => :undef, 'running' => true, 'service_prefix' => 'docker-', 'service_provider' => :undef, 'socket_connect' => [], 'stop_wait_time' => 0, 'syslog_identifier' => :undef, 'systemd_restart' => 'on-failure', 'tty' => false, 'use_name' => false, 'username' => false, 'volumes_from' => [], 'volumes' => [], }.merge(os_params).merge(local_params) if params['docker_service'] && params['docker_service'].to_s != 'true' docker_params['service_name'] = params['docker_service'] end let(:facts) do facts end let(:params) do params end let(:title) do params['command'] end let :pre_condition do <<-MANIFEST class { 'docker': version => "#{params['version']}", * => #{docker_params}, } MANIFEST end if params['remove_volume_on_start'] && !params['remove_container_on_start'] it { is_expected.to compile.and_raise_error("In order to remove the volume on start for #{_title} you need to also remove the container") } next end if params['remove_volume_on_stop'] && !params['remove_container_on_stop'] it { is_expected.to compile.and_raise_error("In order to remove the volume on stop for #{_title} you need to also remove the container") } next end service_provider_real = case params['service_provider'] when :undef defaults['service_provider'] else params['service_provider'] end if !params['service_provider_real'] == 'systemd' && !params['service_provider_real'] == 'upstart' if facts[:os]['family'] != 'windows' it { is_expected.to compile.and_raise_error('Docker needs a Debian or RedHat based system.') } next elsif params['ensure'] == 'present' it { is_expected.to compile.and_raise_error('Restart parameter is required for Windows') } next end end include_examples 'run', params['command'], params, facts, defaults end end end end end diff --git a/spec/defines/secrets_spec.rb b/spec/defines/secrets_spec.rb index 5e3178f..e0a23d5 100644 --- a/spec/defines/secrets_spec.rb +++ b/spec/defines/secrets_spec.rb @@ -1,60 +1,60 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'with ensure present' => { 'secret_name' => 'test_secret', 'secret_path' => '/root/secret.txt', 'label' => ['test'], }, 'with ensure absent' => { 'ensure' => 'absent', 'secret_name' => 'test_secret', }, } describe 'docker::secrets', type: :define do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'ensure' => 'present', 'label' => [], 'secret_name' => :undef, 'secret_path' => :undef, }.merge(local_params) let(:facts) do facts end let(:params) do params end let(:title) do title end include_examples 'secrets', title, params, facts, defaults end end end end end diff --git a/spec/defines/services_spec.rb b/spec/defines/services_spec.rb index d07d3ec..0f5603e 100644 --- a/spec/defines/services_spec.rb +++ b/spec/defines/services_spec.rb @@ -1,120 +1,120 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'with ensure => present and service create' => { 'create' => true, 'service_name' => 'foo', 'image' => 'foo:bar', 'publish' => '80:80', 'replicas' => '5', 'extra_params' => ['--update-delay 1m', '--restart-window 30s'], 'env' => ['MY_ENV=1', 'MY_ENV2=2'], 'label' => ['com.example.foo="bar"', 'bar=baz'], 'mounts' => ['type=bind,src=/tmp/a,dst=/tmp/a', 'type=bind,src=/tmp/b,dst=/tmp/b,readonly'], 'networks' => ['overlay'], 'command' => 'echo hello world', }, 'multiple services declaration' => { 'service_name' => 'foo_2', 'image' => 'foo:bar', 'command' => ['echo', 'hello', 'world'], }, 'multiple publish ports and multiple networks' => { 'service_name' => 'foo_3', 'image' => 'foo:bar', 'publish' => ['80:8080', '9000:9000'], 'networks' => ['foo_1', 'foo_2'], }, 'with ensure => present and service update' => { 'create' => false, 'update' => true, 'service_name' => 'foo', 'image' => 'bar:latest', }, 'with ensure => present and service scale' => { 'create' => false, 'scale' => true, 'service_name' => 'bar', 'replicas' => '5', }, 'with ensure => absent' => { 'ensure' => 'absent', 'service_name' => 'foo', }, 'when adding a system user' => { 'user' => ['user1'], }, } describe 'docker::services', type: :define do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'ensure' => 'present', 'create' => true, 'update' => false, 'scale' => false, 'detach' => true, 'tty' => false, 'env' => [], 'label' => [], 'extra_params' => [], 'image' => :undef, 'service_name' => :undef, 'publish' => :undef, 'replicas' => :undef, 'user' => :undef, 'workdir' => :undef, 'host_socket' => :undef, 'registry_mirror' => :undef, 'mounts' => :undef, 'networks' => :undef, 'command' => :undef, }.merge(local_params) let(:facts) do facts end let(:params) do params end let(:title) do 'rspec_services' end if params['ensure'] == 'absent' if params['update'] || params['scale'] it { is_expected.to compile.and_raise_error('When removing a service you can not update it.') } next end end include_examples 'services', 'rspec_services', params, facts, defaults end end end end end diff --git a/spec/defines/stack_spec.rb b/spec/defines/stack_spec.rb index 965c3ea..5504d8c 100644 --- a/spec/defines/stack_spec.rb +++ b/spec/defines/stack_spec.rb @@ -1,73 +1,73 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'create stack with compose file' => { 'stack_name' => 'foo', 'compose_files' => ['/tmp/docker-compose.yaml'], 'resolve_image' => 'always', }, 'create stack with multiple compose files' => { 'stack_name' => 'foo', 'compose_files' => ['/tmp/docker-compose.yaml', '/tmp/docker-compose-2.yaml'], 'resolve_image' => 'always', }, 'with prune' => { 'stack_name' => 'foo', 'compose_files' => ['/tmp/docker-compose.yaml'], 'prune' => true, }, 'with ensure => absent' => { 'ensure' => 'absent', 'stack_name' => 'foo', }, } describe 'docker::stack', type: :define do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'ensure' => 'present', 'stack_name' => :undef, 'bundle_file' => :undef, 'compose_files' => :undef, 'prune' => false, 'with_registry_auth' => false, 'resolve_image' => :undef, }.merge(local_params) let(:facts) do facts end let(:params) do params end let(:title) do 'rspec_stack' end include_examples 'stack', 'rspec_stack', params, facts, defaults end end end end end diff --git a/spec/defines/swarm_spec.rb b/spec/defines/swarm_spec.rb index 12a9433..4338922 100644 --- a/spec/defines/swarm_spec.rb +++ b/spec/defines/swarm_spec.rb @@ -1,90 +1,90 @@ # frozen_string_literal: true require 'spec_helper' tests = { 'with ensure => present and swarm init' => { 'init' => true, 'advertise_addr' => '192.168.1.1', 'listen_addr' => '192.168.1.1', }, 'with ensure => present and swarm init and default-addr-pool and default_addr_pool_mask_length' => { 'init' => true, 'advertise_addr' => '192.168.1.1', 'listen_addr' => '192.168.1.1', 'default_addr_pool' => ['30.30.0.0/16', '40.40.0.0/16'], 'default_addr_pool_mask_length' => '24', }, 'with ensure => present and swarm join' => { 'join' => true, 'advertise_addr' => '192.168.1.1', 'listen_addr' => '192.168.1.1', 'token' => 'foo', 'manager_ip' => '192.168.1.2', }, 'with ensure => absent' => { 'ensure' => 'absent', 'join' => true, 'advertise_addr' => '192.168.1.1', 'listen_addr' => '192.168.1.1', 'token' => 'foo', 'manager_ip' => '192.168.1.2', }, } describe 'docker::swarm', type: :define do on_supported_os.each do |os, os_facts| ## ## set some needed facts ## - facts = if os =~ %r{windows} + facts = if %r{windows}.match?(os) windows_facts.merge(os_facts) else os_facts end ## ## get defaults values from params ## defaults = get_defaults(facts) context "on #{os}" do tests.each do |title, local_params| context title do params = { 'ensure' => 'present', 'init' => false, 'join' => false, 'advertise_addr' => :undef, 'autolock' => false, 'cert_expiry' => :undef, 'default_addr_pool' => :undef, 'default_addr_pool_mask_length' => :undef, 'dispatcher_heartbeat' => :undef, 'external_ca' => :undef, 'force_new_cluster' => false, 'listen_addr' => :undef, 'max_snapshots' => :undef, 'snapshot_interval' => :undef, 'token' => :undef, 'manager_ip' => :undef, }.merge(local_params) let(:facts) do facts end let(:params) do params end let(:title) do 'rspec_swarm' end include_examples 'swarm', 'rspec_swarm', params, facts, defaults end end end end end diff --git a/spec/helper/get_docker_service_flags.rb b/spec/helper/get_docker_service_flags.rb index 0dd7c42..07b493c 100644 --- a/spec/helper/get_docker_service_flags.rb +++ b/spec/helper/get_docker_service_flags.rb @@ -1,95 +1,93 @@ # frozen_string_literal: true require 'shellwords' def get_docker_service_flags(args) flags = [] if args['service_name'] && args['service_name'].to_s != 'undef' flags << "'#{args['service_name']}'" end if args['detach'].to_s != 'false' flags << '--detach' end if args['env'].is_a? Array args['env'].each do |env| flags << "--env #{env}" end end if args['label'].is_a? Array args['label'].each do |label| flags << "--label #{label}" end end if args['mounts'].is_a? Array args['mounts'].each do |mount| flags << "--mount #{mount}" end end if args['networks'].is_a? Array args['networks'].each do |network| flags << "--network #{network}" end end if args['publish'].is_a? Array args['publish'].each do |port| flags << "--publish #{port}" end elsif args['publish'] && args['publish'].to_s != 'undef' flags << "--publish '#{args['publish']}'" end if args['replicas'] && args['replicas'].to_s != 'undef' flags << "--replicas '#{args['replicas']}'" end if args['tty'].to_s != 'false' flags << '--tty' end if args['user'] && args['user'].to_s != 'undef' flags << "--user '#{args['user']}'" end if args['workdir'] && args['workdir'].to_s != 'undef' flags << "--workdir '#{args['workdir']}'" end if args['extra_params'].is_a? Array args['extra_params'].each do |param| flags << param end end if args['host_socket'] && args['host_socket'].to_s != 'undef' flags << "-H '#{args['host_socket']}'" end if args['registry_mirror'].is_a? Array args['registry_mirror'].each do |param| flags << "--registry-mirror='#{param}'" end - else - if args['registry_mirror'] && args['registry_mirror'].to_s != 'undef' - flags << "--registry-mirror='#{args['registry_mirror']}'" - end + elsif args['registry_mirror'] && args['registry_mirror'].to_s != 'undef' + flags << "--registry-mirror='#{args['registry_mirror']}'" end if args['image'] && args['image'].to_s != 'undef' flags << "'#{args['image']}'" end if args['command'].is_a? Array flags << args['command'].join(' ') elsif args['command'] && args['command'].to_s != 'undef' flags << args['command'].to_s end flags.flatten.join(' ') end diff --git a/spec/shared_examples/config.rb b/spec/shared_examples/config.rb index 8bb16bf..7501b1e 100644 --- a/spec/shared_examples/config.rb +++ b/spec/shared_examples/config.rb @@ -1,25 +1,25 @@ # frozen_string_literal: true shared_examples 'config' do |_params, _facts| docker_users = _params['docker_users'] it { is_expected.to contain_class('docker::config') } unless docker_users.empty? docker_users.each do |user| - if _facts[:os]['family'] =~ %r{windows} + if %r{windows}.match?(_facts[:os]['family']) it { is_expected.to contain_docker__windows_account(user) } else it { is_expected.to contain_docker__system_user(user) } include_examples 'system_user', user, _params['docker_group'] end end end end diff --git a/spec/shared_examples/image.rb b/spec/shared_examples/image.rb index 2e6236d..95cc4cb 100644 --- a/spec/shared_examples/image.rb +++ b/spec/shared_examples/image.rb @@ -1,148 +1,146 @@ # frozen_string_literal: true shared_examples 'image' do |_params, _facts, _defaults| docker_command = _defaults['docker_command'] docker_dir = _params['docker_dir'] docker_file = _params['docker_file'] docker_tar = _params['docker_tar'] ensure_value = _params['ensure'] force = _params['force'] image = _params['image'] image_digest = _params['image_digest'] image_tag = _params['image_tag'] if _facts[:os]['family'] == 'windows' update_docker_image_template = 'docker/windows/update_docker_image.ps1.erb' update_docker_image_path = "#{_facts['docker_user_temp_path']}/update_docker_image.ps1" exec_environment = "PATH=#{_facts['docker_program_files_path']}/Docker/" exec_timeout = 3000 update_docker_image_owner = nil exec_path = ["#{_facts['docker_program_files_path']}/Docker/"] exec_provider = 'powershell' else update_docker_image_template = 'docker/update_docker_image.sh.erb' update_docker_image_path = '/usr/local/bin/update_docker_image.sh' update_docker_image_owner = 'root' exec_environment = 'HOME=/root' exec_path = ['/bin', '/usr/bin'] exec_timeout = 0 exec_provider = nil end it { is_expected.to contain_file(update_docker_image_path).with( 'ensure' => 'present', 'owner' => update_docker_image_owner, 'group' => update_docker_image_owner, 'mode' => '0555', # 'content' => template($update_docker_image_template), ) } image_force = if force '-f ' else '' end if image_tag != :undef image_arg = "#{image}:#{image_tag}" image_remove = "#{docker_command} rmi #{image_force}#{image}:#{image_tag}" image_find = "#{docker_command} images -q #{image}:#{image_tag}" elsif image_digest != :undef image_arg = "#{image}@#{image_digest}" image_remove = "#{docker_command} rmi #{image_force}#{image}:#{image_digest}" image_find = "#{docker_command} images -q #{image}@#{image_digest}" else image_arg = image image_remove = "#{docker_command} rmi #{image_force}#{image}" image_find = "#{docker_command} images -q #{image}" end _image_find = if _facts[:os]['family'] == 'windows' "If (-not (#{image_find}) ) { Exit 1 }" else "#{image_find} | grep ." end image_install = if docker_dir != :undef && docker_file != :undef "#{docker_command} build -t #{image_arg} -f #{docker_file} #{docker_dir}" elsif docker_dir != :undef "#{docker_command} build -t #{image_arg} #{docker_dir}" elsif docker_file != :undef if _facts[:os]['family'] == 'windows' "Get-Content #{docker_file} -Raw | #{docker_command} build -t #{image_arg} -" else "#{docker_command} build -t #{image_arg} - < #{docker_file}" end elsif docker_tar != :undef "#{docker_command} load -i #{docker_tar}" + elsif _facts[:os]['family'] == 'windows' + "& #{update_docker_image_path} -DockerImage #{image_arg}" else - if _facts[:os]['family'] == 'windows' - "& #{update_docker_image_path} -DockerImage #{image_arg}" - else - "#{update_docker_image_path} #{image_arg}" - end + "#{update_docker_image_path} #{image_arg}" end if ensure_value == 'absent' it { is_expected.to contain_exec(image_remove).with( 'path' => exec_path, 'environment' => exec_environment, 'onlyif' => _image_find, 'provider' => exec_provider, 'timeout' => exec_timeout, 'logoutput' => true, ) } elsif ensure_value == 'latest' || image_tag == 'latest' it { is_expected.to contain_notify("Check if image #{image_arg} is in-sync").with( 'noop' => false, ).that_notifies( "Exec[#{image_install}]", ) is_expected.to contain_exec(image_install).with( 'environment' => exec_environment, 'path' => exec_path, 'timeout' => exec_timeout, 'returns' => ['0', '2'], 'provider' => exec_provider, 'logoutput' => true, ).that_requires( "File[#{update_docker_image_path}]", ).that_notifies( "Exec[echo 'Update of #{image_arg} complete']", ) is_expected.to contain_exec("echo 'Update of #{image_arg} complete'").with( 'environment' => exec_environment, 'path' => exec_path, 'timeout' => exec_timeout, 'provider' => exec_provider, 'logoutput' => true, 'refreshonly' => true, ).that_requires( "File[#{update_docker_image_path}]", ) } elsif ensure_value == 'present' it { is_expected.to contain_exec(image_install).with( 'unless' => _image_find, 'environment' => exec_environment, 'path' => exec_path, 'timeout' => exec_timeout, 'returns' => ['0', '2'], 'provider' => exec_provider, 'logoutput' => true, ).that_requires( "File[#{update_docker_image_path}]", ) } end end diff --git a/spec/shared_examples/install.rb b/spec/shared_examples/install.rb index a586e46..e7b87f7 100644 --- a/spec/shared_examples/install.rb +++ b/spec/shared_examples/install.rb @@ -1,158 +1,154 @@ # frozen_string_literal: true shared_examples 'install' do |_params, _facts| values = get_values_init(_params, _facts) ensure_value = if _params['version'] != :undef && _params['ensure'] != 'absent' _params['version'] else _params['ensure'] end if _params['manage_package'] docker_hash = if _params['repo_opt'] == :undef {} else { 'install_options' => _params['repo_opt'] } end if _params['package_source'] != :undef it { is_expected.to contain_class('docker::install') } provider_value = case _facts[:os]['family'] when 'Debian' 'dpkg' when 'RedHat' 'yum' else :undef end case _params['package_source'] when 'docker-engine' it { is_expected.to contain_package('docker').with( { 'ensure' => ensure_value, 'provider' => provider_value, 'source' => _params['package_source'], 'name' => _params['docker_engine_package_name'], }.merge(docker_hash), ) } when 'docker-ce' it { is_expected.to contain_package('docker').with( { 'ensure' => ensure_value, 'provider' => provider_value, 'source' => _params['package_source'], 'name' => _params['docker_ce_package_name'], }.merge(docker_hash), ) } end + elsif _facts[:os]['family'] != 'windows' + it { + is_expected.to contain_package('docker').with( + 'ensure' => ensure_value, + 'name' => values['docker_package_name'], + ) + } + + if ensure_value == 'absent' + _params['dependent_packages'].each do |dependent_package| + it { + is_expected.to contain_package(dependent_package).with( + 'ensure' => ensure_value, + ) + } + end + end + elsif ensure_value == 'absent' + it { + if _params['version'] != :undef + is_expected.to contain_exec('remove-docker-package').with( + 'command' => %r{-RequiredVersion #{_params['version']}}, + ) + end + + is_expected.to contain_exec('remove-docker-package').with( + 'command' => %r{\$package=Uninstall-Package #{_params['docker_ee_package_name']} -ProviderName \$dockerProviderName -Force}, + 'provider' => 'powershell', + 'unless' => %r{\$package=Get-Package #{_params['docker_ee_package_name']} -ProviderName \$dockerProviderName -ErrorAction Ignore}, + 'logoutput' => true, + ) + } else - if _facts[:os]['family'] != 'windows' + if _params['package_location'] it { - is_expected.to contain_package('docker').with( - 'ensure' => ensure_value, - 'name' => values['docker_package_name'], + is_expected.to contain_exec('install-docker-package').with( + 'command' => %r{Invoke-webrequest -UseBasicparsing -Outfile \$dockerLocation "#{_params['docker_download_url']}"}, + 'provider' => 'powershell', + 'unless' => %r{\$webRequest = \[System.Net.HttpWebRequest\]::Create("#{_params['docker_download_url']}");}, + 'logoutput' => true, + ).that_notifies( + 'Exec[service-restart-on-failure]', ) } - - if ensure_value == 'absent' - _params['dependent_packages'].each do |dependent_package| - it { - is_expected.to contain_package(dependent_package).with( - 'ensure' => ensure_value, - ) - } - end - end else - if ensure_value == 'absent' - it { - if _params['version'] != :undef - is_expected.to contain_exec('remove-docker-package').with( - 'command' => %r{-RequiredVersion #{_params['version']}}, - ) - end - - is_expected.to contain_exec('remove-docker-package').with( - 'command' => %r{\$package=Uninstall-Package #{_params['docker_ee_package_name']} -ProviderName \$dockerProviderName -Force}, - 'provider' => 'powershell', - 'unless' => %r{\$package=Get-Package #{_params['docker_ee_package_name']} -ProviderName \$dockerProviderName -ErrorAction Ignore}, - 'logoutput' => true, + it { + if _params['nuget_package_provider_version'] != :undef + is_expected. to contain_exec( + 'install-docker-package', + ).with_command( + %r{-RequiredVersion #{_params['nuget_package_provider_version']}}, + ).with_unless( + %r{\$module.Version.ToString\(\) -ne "#{_params['nuget_package_provider_version']}"}, ) - } - else - if _params['package_location'] - it { - is_expected.to contain_exec('install-docker-package').with( - 'command' => %r{Invoke-webrequest -UseBasicparsing -Outfile \$dockerLocation "#{_params['docker_download_url']}"}, - 'provider' => 'powershell', - 'unless' => %r{\$webRequest = \[System.Net.HttpWebRequest\]::Create("#{_params['docker_download_url']}");}, - 'logoutput' => true, - ).that_notifies( - 'Exec[service-restart-on-failure]', - ) - } - else - it { - if _params['nuget_package_provider_version'] != :undef - is_expected. to contain_exec( - 'install-docker-package', - ).with_command( - %r{-RequiredVersion #{_params['nuget_package_provider_version']}}, - ).with_unless( - %r{\$module.Version.ToString\(\) -ne "#{_params['nuget_package_provider_version']}"}, - ) - end - - if _params['docker_msft_provider_version'] != :undef - is_expected. to contain_exec( - 'install-docker-package', - ).with_command( - %r{-RequiredVersion #{_params['docker_msft_provider_version']}}, - ).with_unless( - %r{\$provider.Version.ToString\(\) -ne "#{_params['docker_msft_provider_version']}"}, - ) - end - - if _params['version'] != :undef - is_expected.to contain_exec( - 'install-docker-package', - ).with_command( - %r{-RequiredVersion #{_params['version']}}, - ).with_unless( - %r{\$package.Version.ToString\(\) -notmatch "#{_params['version']}"}, - ) - end + end - is_expected.to contain_exec('install-docker-package').with( - 'command' => %r{\$package=Install-Package #{_params['docker_ee_package_name']} -ProviderName \$dockerProviderName -Force}, - 'provider' => 'powershell', - 'unless' => %r{\$package=Get-Package #{_params['docker_ee_package_name']} -ProviderName \$dockerProviderName}, - 'logoutput' => true, - ).that_notifies( - 'Exec[service-restart-on-failure]', - ) - } + if _params['docker_msft_provider_version'] != :undef + is_expected. to contain_exec( + 'install-docker-package', + ).with_command( + %r{-RequiredVersion #{_params['docker_msft_provider_version']}}, + ).with_unless( + %r{\$provider.Version.ToString\(\) -ne "#{_params['docker_msft_provider_version']}"}, + ) end - it { - is_expected.to contain_exec('service-restart-on-failure').with( - 'command' => 'SC.exe failure Docker reset= 432000 actions= restart/30000/restart/60000/restart/60000', - 'refreshonly' => true, - 'logoutput' => true, - 'provider' => 'powershell', + if _params['version'] != :undef + is_expected.to contain_exec( + 'install-docker-package', + ).with_command( + %r{-RequiredVersion #{_params['version']}}, + ).with_unless( + %r{\$package.Version.ToString\(\) -notmatch "#{_params['version']}"}, ) - } - end + end + + is_expected.to contain_exec('install-docker-package').with( + 'command' => %r{\$package=Install-Package #{_params['docker_ee_package_name']} -ProviderName \$dockerProviderName -Force}, + 'provider' => 'powershell', + 'unless' => %r{\$package=Get-Package #{_params['docker_ee_package_name']} -ProviderName \$dockerProviderName}, + 'logoutput' => true, + ).that_notifies( + 'Exec[service-restart-on-failure]', + ) + } end + + it { + is_expected.to contain_exec('service-restart-on-failure').with( + 'command' => 'SC.exe failure Docker reset= 432000 actions= restart/30000/restart/60000/restart/60000', + 'refreshonly' => true, + 'logoutput' => true, + 'provider' => 'powershell', + ) + } end end end diff --git a/spec/shared_examples/registry.rb b/spec/shared_examples/registry.rb index c0ee40b..6efc170 100644 --- a/spec/shared_examples/registry.rb +++ b/spec/shared_examples/registry.rb @@ -1,123 +1,123 @@ # frozen_string_literal: true -shared_examples 'registry' do |_title, _params, _facts, _defaults| - server = _params['server'] - ensure_value = _params['ensure'] - username = _params['username'] - password = _params['password'] - pass_hash = _params['pass_hash'] - email = _params['email'] - local_user = _params['local_user'] - version = _params['version'] - receipt = _params['receipt'] +shared_examples 'registry' do |title, params, facts, defaults| + server = params['server'] + ensure_value = params['ensure'] + username = params['username'] + password = params['password'] + pass_hash = params['pass_hash'] + email = params['email'] + local_user = params['local_user'] + version = params['version'] + receipt = params['receipt'] - docker_command = _defaults['docker_command'] + docker_command = defaults['docker_command'] - if _facts[:os]['family'] == 'windows' - exec_environment = ["PATH=#{_facts['docker_program_files_path']}/Docker/"] + if facts[:os]['family'] == 'windows' + exec_environment = ["PATH=#{facts['docker_program_files_path']}/Docker/"] exec_timeout = 3000 - exec_path = ["#{_facts['docker_program_files_path']}/Docker/"] + exec_path = ["#{facts['docker_program_files_path']}/Docker/"] exec_provider = 'powershell' password_env = '$env:password' exec_user = nil else exec_environment = [] exec_path = ['/bin', '/usr/bin'] exec_timeout = 0 exec_provider = nil password_env = "\${password}" exec_user = local_user - local_user_home = _facts[:docker_home_dirs][local_user] + local_user_home = facts[:docker_home_dirs][local_user] end if ensure_value == 'present' if username != :undef && password != :undef && email != :undef && version != :undef && version =~ %r{1[.][1-9]0?} auth_cmd = "#{docker_command} login -u '#{username}' -p \"#{password_env}\" -e '#{email}' #{server}" auth_environment = "password=#{password}" elsif username != :undef && password != :undef auth_cmd = "#{docker_command} login -u '#{username}' -p \"#{password_env}\" #{server}" auth_environment = "password=#{password}" else auth_cmd = "#{docker_command} login #{server}" auth_environment = '' end else auth_cmd = "#{docker_command} logout #{server}" auth_environment = '' end - docker_auth = "#{_title}#{auth_environment}#{auth_cmd}#{local_user}" + docker_auth = "#{title}#{auth_environment}#{auth_cmd}#{local_user}" exec_env = if auth_environment != '' exec_environment << auth_environment << "docker_auth=#{docker_auth}" else exec_environment << "docker_auth=#{docker_auth}" end if receipt - if _facts[:os]['family'] != 'windows' + if facts[:os]['family'] != 'windows' server_strip = server.tr('/', '_') local_user_strip = local_user.gsub('[-_]', '') _pass_hash = case pass_hash when :undef pw_hash(docker_auth, 'SHA-512', local_user_strip) else pass_hash end - _auth_command = "#{auth_cmd} || rm -f \"/#{local_user_home}/registry-auth-puppet_receipt_#{server_strip}_#{local_user}\"" + auth_command = "#{auth_cmd} || rm -f \"/#{local_user_home}/registry-auth-puppet_receipt_#{server_strip}_#{local_user}\"" it { is_expected.to contain_file('/${local_user_home}/registry-auth-puppet_receipt_${server_strip}_${local_user}').with( 'ensure' => ensure_value, 'content' => _pass_hash, 'owner' => local_user, 'group' => local_user, ).that_notifies( - "Exec[#{_title} auth]", + "Exec[#{title} auth]", ) } else server_strip = server.gsub('[/:]', '_') - passfile = "#{_facts['docker_user_temp_path']}/registry-auth-puppet_receipt_#{server_strip}_#{local_user}" - _auth_command = "if (-not (#{auth_cmd})) { Remove-Item -Path #{passfile} -Force -Recurse -EA SilentlyContinue; exit 0 } else { exit 0 }" + passfile = "#{facts['docker_user_temp_path']}/registry-auth-puppet_receipt_#{server_strip}_#{local_user}" + auth_command = "if (-not (#{auth_cmd})) { Remove-Item -Path #{passfile} -Force -Recurse -EA SilentlyContinue; exit 0 } else { exit 0 }" if ensure_value == 'absent' it { is_expected.to contain_file(passfile).with( 'ensure' => ensure_value, ).that_notifies( - "Exec[#{_title} auth]", + "Exec[#{title} auth]", ) } elsif ensure_value == 'present' it { is_expected.to contain_exec(compute - hash).with( # 'command' => template('docker/windows/compute_hash.ps1.erb'), 'environment' => exec_env, 'provider' => exec_provider, 'logoutput' => true, # 'unless' => template('docker/windows/check_hash.ps1.erb'), ).that_notifies( - "Exec[#{_title} auth]", + "Exec[#{title} auth]", ) } end end else - _auth_command = auth_cmd + auth_command = auth_cmd end it { - is_expected.to contain_exec("#{_title} auth").with( + is_expected.to contain_exec("#{title} auth").with( 'environment' => exec_env, - 'command' => _auth_command, + 'command' => auth_command, 'user' => exec_user, 'path' => exec_path, 'timeout' => exec_timeout, 'provider' => exec_provider, 'refreshonly' => receipt, ) } end diff --git a/spec/shared_examples/repos.rb b/spec/shared_examples/repos.rb index 540b29a..9e3c0b4 100644 --- a/spec/shared_examples/repos.rb +++ b/spec/shared_examples/repos.rb @@ -1,111 +1,111 @@ # frozen_string_literal: true -shared_examples 'repos' do |_params, _facts| +shared_examples 'repos' do |params, facts| it { is_expected.to contain_class('docker::repos') } - values = get_values_init(_params, _facts) + values = get_values_init(params, facts) location = values['package_location'] key_source = values['package_key_source'] key_check_source = values['package_key_check_source'] - architecture = _facts[:os]['architecture'] + architecture = facts[:os]['architecture'] - unless _params['prerequired_packages'].empty? - _params['prerequired_packages'].each do |package| + unless params['prerequired_packages'].empty? + params['prerequired_packages'].each do |package| it { is_expected.to contain_package(package) } end end - case _facts[:os]['family'] + case facts[:os]['family'] when 'Debian' release = values['release'] package_key = values['package_key'] package_repos = values['package_repos'] - if _params['use_upstream_package_source'] + if params['use_upstream_package_source'] it { is_expected.to contain_apt__source('docker').with( 'location' => location, 'architecture' => architecture, 'release' => release, 'repos' => package_repos, 'key' => { 'id' => package_key, 'source' => key_source, }, 'include' => { 'src' => false, }, ) } url_split = location.split('/') repo_host = url_split[2] - pin_ensure = case _params['pin_upstream_package_source'] + pin_ensure = case params['pin_upstream_package_source'] when true 'present' else 'absent' end it { is_expected.to contain_apt__pin('docker').with( 'ensure' => pin_ensure, 'origin' => repo_host, - 'priority' => _params['apt_source_pin_level'], + 'priority' => params['apt_source_pin_level'], ) } - if _params['manage_package'] + if params['manage_package'] it { is_expected.to contain_class('apt') } - if _facts[:os]['name'] == 'Debian' && _facts[:os]['distro']['codename'] == 'wheezy' + if facts[:os]['name'] == 'Debian' && facts[:os]['distro']['codename'] == 'wheezy' it { is_expected.to contain_class('apt::backports') } end it { - _params['prerequired_packages'].each do |package| + params['prerequired_packages'].each do |package| is_expected.to contain_exec('apt_update').that_comes_before("package[#{package}]") end is_expected.to contain_apt__source('docker').that_comes_before('package[docker]') } end end when 'RedHat' - if _params['manage_package'] + if params['manage_package'] baseurl = case location when :undef nil else location end gpgkey = case key_source when :undef nil else key_source end gpgkey_check = key_check_source - if _params['use_upstream_package_source'] + if params['use_upstream_package_source'] it { is_expected.to contain_yumrepo('docker').with( 'descr' => 'Docker', 'baseurl' => baseurl, 'gpgkey' => gpgkey, 'gpgcheck' => gpgkey_check, ).that_comes_before('package[docker]') } end end end end diff --git a/spec/shared_examples/run.rb b/spec/shared_examples/run.rb index adb358f..ab780cb 100644 --- a/spec/shared_examples/run.rb +++ b/spec/shared_examples/run.rb @@ -1,464 +1,449 @@ # frozen_string_literal: true -shared_examples 'run' do |_title, _params, _facts, _defaults| - after = _params['after'] - after_create = _params['after_create'] - after_service = _params['after_service'] - after_start = _params['after_start'] - after_stop = _params['after_stop'] - before_start = _params['before_start'] - before_stop = _params['before_stop'] - command = _params['command'] - cpuset = _params['cpuset'] - custom_unless = _params['custom_unless'] - depend_services = _params['depend_services'] - depends = _params['depends'] - detach = _params['detach'] - disable_network = _params['disable_network'] - dns = _params['dns'] - dns_search = _params['dns_search'] - docker_service = _params['docker_service'] - ensure_value = _params['ensure'] - env = _params['env'] - env_file = _params['env_file'] - expose = _params['expose'] - extra_parameters = _params['extra_parameters'] - extra_systemd_parameters = _params['extra_systemd_parameters'] - health_check_cmd = _params['health_check_cmd'] - health_check_interval = _params['health_check_interval'] - hostentries = _params['hostentries'] - hostname = _params['hostname'] - image = _params['image'] - labels = _params['labels'] - links = _params['links'] - lxc_conf = _params['lxc_conf'] - manage_service = _params['manage_service'] - memory_limit = _params['memory_limit'] - net = _params['net'] - ports = _params['ports'] - privileged = _params['privileged'] - pull_on_start = _params['pull_on_start'] - read_only = _params['read_only'] - remain_after_exit = _params['remain_after_exit'] - remove_container_on_start = _params['remove_container_on_start'] - remove_container_on_stop = _params['remove_container_on_stop'] - remove_volume_on_start = _params['remove_volume_on_start'] - remove_volume_on_stop = _params['remove_volume_on_stop'] - restart = _params['restart'] - restart_on_unhealthy = _params['restart_on_unhealthy'] - restart_service = _params['restart_service'] - restart_service_on_docker_refresh = _params['restart_service_on_docker_refresh'] - running = _params['running'] - service_prefix = _params['service_prefix'] - service_provider = _params['service_provider'] - socket_connect = _params['socket_connect'] - stop_wait_time = _params['stop_wait_time'] - syslog_identifier = _params['syslog_identifier'] - systemd_restart = _params['systemd_restart'] - tty = _params['tty'] - use_name = _params['use_name'] - username = _params['username'] - volumes = _params['volumes'] - volumes_from = _params['volumes_from'] - docker_group = _defaults['docker_group'] +shared_examples 'run' do |title, params, facts, defaults| + # after = params['after'] + # after_create = params['after_create'] + # after_service = params['after_service'] + # after_start = params['after_start'] + # after_stop = params['after_stop'] + # before_start = params['before_start'] + # before_stop = params['before_stop'] + command = params['command'] + cpuset = params['cpuset'] + # custom_unless = params['custom_unless'] + # depend_services = params['depend_services'] + # depends = params['depends'] + # detach = params['detach'] + disable_network = params['disable_network'] + dns = params['dns'] + dns_search = params['dns_search'] + docker_service = params['docker_service'] + ensure_value = params['ensure'] + env = params['env'] + env_file = params['env_file'] + expose = params['expose'] + extra_parameters = params['extra_parameters'] + # extra_systemd_parameters = params['extra_systemd_parameters'] + health_check_cmd = params['health_check_cmd'] + health_check_interval = params['health_check_interval'] + hostentries = params['hostentries'] + hostname = params['hostname'] + image = params['image'] + labels = params['labels'] + links = params['links'] + lxc_conf = params['lxc_conf'] + manage_service = params['manage_service'] + memory_limit = params['memory_limit'] + net = params['net'] + ports = params['ports'] + privileged = params['privileged'] + # pull_on_start = params['pull_on_start'] + read_only = params['read_only'] + # remain_after_exit = params['remain_after_exit'] + # remove_container_on_start = params['remove_container_on_start'] + # remove_container_on_stop = params['remove_container_on_stop'] + # remove_volume_on_start = params['remove_volume_on_start'] + # remove_volume_on_stop = params['remove_volume_on_stop'] + restart = params['restart'] + restart_on_unhealthy = params['restart_on_unhealthy'] + # restart_service = params['restart_service'] + restart_service_on_docker_refresh = params['restart_service_on_docker_refresh'] + running = params['running'] + service_prefix = params['service_prefix'] + service_provider = params['service_provider'] + socket_connect = params['socket_connect'] + stop_wait_time = params['stop_wait_time'] + syslog_identifier = params['syslog_identifier'] + # systemd_restart = params['systemd_restart'] + tty = params['tty'] + use_name = params['use_name'] + username = params['username'] + volumes = params['volumes'] + volumes_from = params['volumes_from'] + docker_group = defaults['docker_group'] if socket_connect != [] sockopts = [socket_connect].join(',') - docker_command = "#{_defaults['docker_command']} -H #{sockopts}" + docker_command = "#{defaults['docker_command']} -H #{sockopts}" else - docker_command = _defaults['docker_command'] + docker_command = defaults['docker_command'] end if use_name it { - is_expected.to contain_notify("docker use_name warning: #{_title}").with( + is_expected.to contain_notify("docker use_name warning: #{title}").with( 'message' => 'The use_name parameter is no-longer required and will be removed in a future release', 'withpath' => true, ) } end service_provider_real = case service_provider.to_s when 'undef' - _defaults['service_provider'] + defaults['service_provider'] else service_provider end - valid_detach = if detach == :undef - case service_provider_real - when 'systemd' - false - else - _defaults['detach_service_in_init'] - end - else - detach - end - docker_run_flags = get_docker_run_flags( 'cpuset' => [cpuset], 'disable_network' => disable_network, 'dns_search' => [dns_search], 'dns' => [dns], 'env_file' => [env_file], 'env' => [env], 'expose' => [expose], - 'extra_params' => [extra_parameters], + 'xtra_params' => [extra_parameters], 'health_check_cmd' => health_check_cmd, 'health_check_interval' => health_check_interval, 'hostentries' => [hostentries], 'hostname' => hostname, 'labels' => [labels], 'links' => [links], 'lxc_conf' => [lxc_conf], 'memory_limit' => memory_limit, 'net' => net, - 'osfamily' => _facts[:os]['family'], + 'osfamily' => facts[:os]['family'], 'ports' => [ports], 'privileged' => privileged, 'read_only' => read_only, 'restart_on_unhealthy' => restart_on_unhealthy, 'socket_connect' => [socket_connect], 'tty' => tty, 'username' => username, 'volumes_from' => [volumes_from], 'volumes' => [volumes], ) - sanitised_title = _title.gsub('[^0-9A-Za-z.\-_]', '-') + sanitised_title = title.gsub('[^0-9A-Za-z.\-_]', '-') - if _facts[:os]['family'] == 'windows' - exec_environment = "PATH=#{_facts['docker_program_files_path']}/Docker/;#{_facts['docker_systemroot']}/System32/" + if facts[:os]['family'] == 'windows' + exec_environment = "PATH=#{facts['docker_program_files_path']}/Docker/;#{facts['docker_systemroot']}/System32/" exec_timeout = 3000 - exec_path = ["#{_facts['docker_program_files_path']}/Docker/"] + exec_path = ["#{facts['docker_program_files_path']}/Docker/"] exec_provider = 'powershell' - cidfile = "#{_facts['docker_user_temp_path']}/#{service_prefix}#{sanitised_title}.cid" + cidfile = "#{facts['docker_user_temp_path']}/#{service_prefix}#{sanitised_title}.cid" restart_check = "#{docker_command} inspect #{sanitised_title} -f '{{ if eq \\\"unhealthy\\\" .State.Health.Status }} {{ .Name }}{{ end }}' | findstr #{sanitised_title}" container_running_check = "\$state = #{docker_command} inspect #{sanitised_title} -f \"{{ .State.Running }}\"; if (\$state -ieq \"true\") { Exit 0 } else { Exit 1 }" else exec_environment = 'HOME=/root' exec_path = ['/bin', '/usr/bin'] exec_timeout = 0 exec_provider = nil cidfile = "/var/run/#{service_prefix}#{sanitised_title}.cid" restart_check = "#{docker_command} inspect #{sanitised_title} -f '{{ if eq \"unhealthy\" .State.Health.Status }} {{ .Name }}{{ end }}' | grep #{sanitised_title}" container_running_check = "#{docker_command} inspect #{sanitised_title} -f \"{{ .State.Running }}\" | grep true" end if restart_on_unhealthy it { - is_expected.to contain_exec("Restart unhealthy container #{_title} with docker").with( + is_expected.to contain_exec("Restart unhealthy container #{title} with docker").with( 'command' => "#{docker_command} restart #{sanitised_title}", 'onlyif' => restart_check, 'environment' => exec_environment, 'path' => exec_path, 'provider' => exec_provider, 'timeout' => exec_timeout, ) } end if restart.to_s != 'undef' if ensure_value == 'absent' it { - is_expected.to contain_exec("stop #{_title} with docker").with( + is_expected.to contain_exec("stop #{title} with docker").with( 'command' => "#{docker_command} stop --time=#{stop_wait_time} #{sanitised_title}", 'onlyif' => "#{docker_command} inspect #{sanitised_title}", 'environment' => exec_environment, 'path' => exec_path, 'provider' => exec_provider, 'timeout' => exec_timeout, ) - is_expected.to contain_exec("remove #{_title} with docker").with( + is_expected.to contain_exec("remove #{title} with docker").with( 'command' => "#{docker_command} rm -v #{sanitised_title}", 'onlyif' => "#{docker_command} inspect #{sanitised_title}", 'environment' => exec_environment, 'path' => exec_path, 'provider' => exec_provider, 'timeout' => exec_timeout, ) is_expected.to contain_file(cidfile).with( 'ensure' => 'absent', ) } else run_with_docker_command = [ "#{docker_command} run -d #{docker_run_flags}", "--name #{sanitised_title} --cidfile=#{cidfile}", "--restart=\"#{restart}\" #{image} #{command}", ] - inspect = [ - "#{docker_command} inspect #{sanitised_title}", - ] + # inspect = [ + # "#{docker_command} inspect #{sanitised_title}", + # ] - exec_unless = if custom_unless - custom_unless << inspect - else - inspect - end + # exec_unless = if custom_unless + # custom_unless << inspect + # else + # inspect + # end it { - is_expected.to contain_exec("run #{_title} with docker").with( + is_expected.to contain_exec("run #{title} with docker").with( 'command' => run_with_docker_command.join(' '), ## todo: ## fix the following strange behavior: ## expected that the catalogue would contain Exec[run command with docker] with unless set to [["docker inspect command"]] ## but it is set to [["docker inspect command"], "docker inspect command"] # 'unless' => exec_unless, 'environment' => exec_environment, 'path' => exec_path, 'provider' => exec_provider, 'timeout' => exec_timeout, ) } if !running it { - is_expected.to contain_exec("stop #{_title} with docker").with( + is_expected.to contain_exec("stop #{title} with docker").with( 'command' => "#{docker_command} stop --time=#{stop_wait_time} #{sanitised_title}", 'onlyif' => container_running_check, 'environment' => exec_environment, 'path' => exec_path, 'provider' => exec_provider, 'timeout' => exec_timeout, ) } else it { - is_expected.to contain_exec("start #{_title} with docker").with( + is_expected.to contain_exec("start #{title} with docker").with( 'command' => "#{docker_command} start #{sanitised_title}", 'unless' => container_running_check, 'environment' => exec_environment, 'path' => exec_path, 'provider' => exec_provider, 'timeout' => exec_timeout, ) } end end else case service_provider_real when 'systemd' hasstatus = true - init_template = 'docker/etc/systemd/system/docker-run.erb' initscript = "/etc/systemd/system/#{service_prefix}#{sanitised_title}.service" mode = '0644' startscript = "/usr/local/bin/docker-run-#{sanitised_title}-start.sh" - startstop_template = 'docker/usr/local/bin/docker-run.sh.epp' stopscript = "/usr/local/bin/docker-run-#{sanitised_title}-stop.sh" when 'upstart' hasstatus = true - init_template = 'docker/etc/init.d/docker-run.erb' initscript = "/etc/init.d/#{service_prefix}#{sanitised_title}" mode = '0750' startscript = nil - startstop_template = nil stopscript = nil else - hasstatus = _defaults['service_hasstatus'] + hasstatus = defaults['service_hasstatus'] end _syslog_identifier = if syslog_identifier syslog_identifier else "#{service_prefix}#{sanitised_title}" end if ensure_value == 'absent' - if _facts[:os]['family'] == 'windows' + if facts[:os]['family'] == 'windows' it { is_expected.to contain_exec("stop container #{service_prefix}#{sanitised_title}").with( 'command' => "#{docker_command} stop --time=#{stop_wait_time} #{sanitised_title}", 'onlyif' => "#{docker_command} inspect #{sanitised_title}", 'environment' => exec_environment, 'path' => exec_path, 'provider' => exec_provider, 'timeout' => exec_timeout, ).that_notifies( "Exec[remove container #{service_prefix}#{sanitised_title}]", ) } else it { is_expected.to contain_service("#{service_prefix}#{sanitised_title}").with( 'ensure' => false, 'enable' => false, 'hasstatus' => hasstatus, 'provider' => service_provider_real, ) } end it { is_expected.to contain_exec("remove container #{service_prefix}#{sanitised_title}").with( 'command' => "#{docker_command} rm -v #{sanitised_title}", 'onlyif' => "#{docker_command} inspect #{sanitised_title}", 'environment' => exec_environment, 'path' => exec_path, 'refreshonly' => true, 'provider' => exec_provider, 'timeout' => exec_timeout, ) } - if _facts[:os]['family'] != 'windows' + if facts[:os]['family'] != 'windows' it { is_expected.to contain_file("/etc/systemd/system/#{service_prefix}#{sanitised_title}.service").with( 'ensure' => 'absent', ) } if startscript it { is_expected.to contain_file(startscript).with( 'ensure' => 'absent', ) } end if stopscript it { is_expected.to contain_file(stopscript).with( 'ensure' => 'absent', ) } end else it { is_expected.to contain_file(cidfile).with( 'ensure' => 'absent', ) } end else if startscript it { is_expected.to contain_file(startscript).with( 'ensure' => 'file', 'owner' => 'root', 'group' => docker_group, 'mode' => '0770', ) } end if stopscript it { is_expected.to contain_file(stopscript).with( 'ensure' => 'file', 'owner' => 'root', 'group' => docker_group, 'mode' => '0770', ) } end it { is_expected.to contain_file(initscript).with( 'ensure' => 'file', 'owner' => 'root', 'group' => docker_group, 'mode' => mode, ) } if manage_service if !running it { is_expected.to contain_service("#{service_prefix}#{sanitised_title}").with( 'ensure' => running, 'enable' => false, 'hasstatus' => hasstatus, ).that_requires( "File[#{initscript}]", ) } else if initscript == "/etc/init.d/#{service_prefix}#{sanitised_title}" transition_onlyif = [ "/usr/bin/test -f /var/run/docker-#{sanitised_title}.cid &&", "/usr/bin/test -f /etc/init.d/#{service_prefix}#{sanitised_title}", ] it { is_expected.to contain_exec("/bin/sh /etc/init.d/#{service_prefix}#{sanitised_title} stop").with( 'onlyif' => transition_onlyif.join(' '), ).that_comes_before( "File[/var/run/#{service_prefix}#{sanitised_title}.cid]", ) is_expected.to contain_file("/var/run/#{service_prefix}#{sanitised_title}.cid").with( 'ensure' => 'absent', ).that_comes_before( "File[#{initscript}]", ) } end it { is_expected.to contain_service("#{service_prefix}#{sanitised_title}").with( 'ensure' => running, 'enable' => true, 'provider' => service_provider_real, 'hasstatus' => hasstatus, ).that_requires( "File[#{initscript}]", ) } if docker_service if docker_service.to_s == 'true' it { is_expected.to contain_service('docker').that_comes_before("Service[#{service_prefix}#{sanitised_title}]") } if restart_service_on_docker_refresh.to_s == 'true' it { is_expected.to contain_service('docker').that_notifies("Service[#{service_prefix}#{sanitised_title}]") } end else it { is_expected.to contain_service('docker').with('name' => docker_service).that_comes_before("Service[#{service_prefix}#{sanitised_title}]") } if restart_service_on_docker_refresh.to_s == 'true' it { is_expected.to contain_service('docker').with('name' => docker_service).that_notifies("Service[#{service_prefix}#{sanitised_title}]") } end end end end end if service_provider_real == 'systemd' it { is_expected.to contain_exec("docker-#{sanitised_title}-systemd-reload").with( 'path' => ['/bin/', '/sbin/', '/usr/bin/', '/usr/sbin/'], 'command' => 'systemctl daemon-reload', 'refreshonly' => true, ).that_requires( [ "File[#{initscript}]", "File[#{startscript}]", "File[#{stopscript}]", ], ).that_subscribes_to( [ "File[#{initscript}]", "File[#{startscript}]", "File[#{stopscript}]", ], ).that_comes_before( "Service[#{service_prefix}#{sanitised_title}]", ) } end end end end diff --git a/spec/shared_examples/secrets.rb b/spec/shared_examples/secrets.rb index ab0031c..e3e5c00 100644 --- a/spec/shared_examples/secrets.rb +++ b/spec/shared_examples/secrets.rb @@ -1,40 +1,40 @@ # frozen_string_literal: true -shared_examples 'secrets' do |_title, _params, _facts, _defaults| - ensure_value = _params['ensure'] - label = _params['label'] - secret_name = _params['secret_name'] - secret_path = _params['secret_path'] +shared_examples 'secrets' do |title, params, _facts, defaults| + ensure_value = params['ensure'] + label = params['label'] + secret_name = params['secret_name'] + secret_path = params['secret_path'] - docker_command = "#{_defaults['docker_command']} secret" + docker_command = "#{defaults['docker_command']} secret" if ensure_value == 'present' docker_secrets_flags = get_docker_secrets_flags( 'ensure' => ensure_value, 'label' => label, 'secret_name' => secret_name, 'secret_path' => secret_path, ) exec_secret = "#{docker_command} #{docker_secrets_flags}" unless_secret = "#{docker_command} inspect #{secret_name}" it { - is_expected.to contain_exec("#{_title} docker secret create").with( + is_expected.to contain_exec("#{title} docker secret create").with( 'command' => exec_secret, 'unless' => unless_secret, 'path' => ['/bin', '/usr/bin'], ) } end if ensure_value == 'absent' it { - is_expected.to contain_exec("#{_title} docker secret rm").with( + is_expected.to contain_exec("#{title} docker secret rm").with( 'command' => "#{docker_command} rm #{secret_name}", 'onlyif' => "#{docker_command} inspect #{secret_name}", 'path' => ['/bin', '/usr/bin'], ) } end end diff --git a/spec/shared_examples/service.rb b/spec/shared_examples/service.rb index 933e383..f3d7ef8 100644 --- a/spec/shared_examples/service.rb +++ b/spec/shared_examples/service.rb @@ -1,168 +1,166 @@ # frozen_string_literal: true -shared_examples 'service' do |_params, _facts| - dns_array = [_params['dns']] - dns_search_array = [_params['dns_search']] - extra_parameters_array = [_params['extra_parameters']] - labels_array = [_params['labels']] - shell_values_array = [_params['shell_values']] - tcp_bind_array = [_params['tcp_bind']] - - _service_config = if _params['service_config'] != :undef - _params['service_config'] - else - if _facts[:os]['family'] == 'Debian' - "/etc/default/#{_params['service_name']}" - else - nil - end - end - - _manage_service = case _params['manage_service'] - when true - 'Service[docker]' - else - [] - end - - if _facts[:os]['family'] == 'RedHat' +shared_examples 'service' do |params, facts| + # dns_array = [params['dns']] + # dns_search_array = [params['dns_search']] + # extra_parameters_array = [params['extra_parameters']] + # labels_array = [params['labels']] + # shell_values_array = [params['shell_values']] + # tcp_bind_array = [params['tcp_bind']] + + service_config = if params['service_config'] != :undef + params['service_config'] + elsif facts[:os]['family'] == 'Debian' + "/etc/default/#{params['service_name']}" + else + nil + end + + manage_service = case params['manage_service'] + when true + 'Service[docker]' + else + [] + end + + if facts[:os]['family'] == 'RedHat' it { - is_expected.to contain_file(_params['storage_setup_file']).with( + is_expected.to contain_file(params['storage_setup_file']).with( 'ensure' => 'file', 'force' => true, - 'before' => _manage_service, - 'notify' => _manage_service, + 'before' => manage_service, + 'notify' => manage_service, ) } end - if _facts[:os]['family'] == 'windows' + if facts[:os]['family'] == 'windows' [ - "#{_facts['docker_program_data_path']}/docker/", - "#{_facts['docker_program_data_path']}/docker/config/", + "#{facts['docker_program_data_path']}/docker/", + "#{facts['docker_program_data_path']}/docker/config/", ].each do |dir| it { is_expected.to contain_file(dir).with_ensure('directory') } end end - case _params['service_provider'] + case params['service_provider'] when 'systemd' it { is_expected.to contain_file('/etc/systemd/system/docker.service.d').with_ensure('directory') } - if _params['service_overrides_template'] + if params['service_overrides_template'] it { is_expected.to contain_file('/etc/systemd/system/docker.service.d/service-overrides.conf').with( 'ensure' => 'file', # 'content' => template($service_overrides_template), - 'before' => _manage_service, + 'before' => manage_service, ).that_notifies( 'Exec[docker-systemd-reload-before-service]', ) } end - if _params['socket_override'] + if params['socket_override'] it { is_expected.to contain_file('/etc/systemd/system/docker.socket.d').with_ensure('directory') } it { is_expected.to contain_file('/etc/systemd/system/docker.socket.d/socket-overrides.conf').with( 'ensure' => 'file', # 'content' => template($socket_overrides_template), ).that_comes_before( - _manage_service, + manage_service, ).that_notifies( 'Exec[docker-systemd-reload-before-service]', ) } end it { is_expected.to contain_exec('docker-systemd-reload-before-service').with( 'path' => ['/bin/', '/sbin/', '/usr/bin/', '/usr/sbin/'], 'command' => 'systemctl daemon-reload > /dev/null', 'refreshonly' => true, ).that_notifies( - _manage_service, + manage_service, ) } when 'upstart' it { is_expected.to contain_file('/etc/init.d/docker').with( 'ensure' => 'link', 'target' => '/lib/init/upstart-job', 'force' => true, ).that_notifies( - _manage_service, + manage_service, ) } end - if _params['storage_config'] != :undef + if params['storage_config'] != :undef it { - is_expected.to contain_file(_params['storage_config']).with( + is_expected.to contain_file(params['storage_config']).with( 'ensure' => 'file', 'force' => true, ).that_notifies( - _manage_service, + manage_service, ) } end - if _service_config + if service_config it { - is_expected.to contain_file(_service_config).with( + is_expected.to contain_file(service_config).with( 'ensure' => 'file', 'force' => true, ).that_notifies( - _manage_service, + manage_service, ) } end - if _params['manage_service'] - if _facts[:os]['family'] == 'windows' + if params['manage_service'] + if facts[:os]['family'] == 'windows' it { is_expected.to contain_reboot('pending_reboot').with( 'when' => 'pending', 'onlyif' => 'component_based_servicing', 'timeout' => 1, ) } end it { - _hasstatus = if _params['service_hasstatus'] == :undef + hasstatus = if params['service_hasstatus'] == :undef + nil + else + params['service_hasstatus'] + end + + hasrestart = if params['service_hasrestart'] == :undef nil else - _params['service_hasstatus'] + params['service_hasrestart'] end - _hasrestart = if _params['service_hasrestart'] == :undef - nil - else - _params['service_hasrestart'] - end - - _provider = if _params['service_provider'] == :undef - nil - else - _params['service_provider'] - end + provider = if params['service_provider'] == :undef + nil + else + params['service_provider'] + end is_expected.to contain_service('docker').with( - 'ensure' => _params['service_state'], - 'name' => _params['service_name'], - 'enable' => _params['service_enable'], - 'hasstatus' => _hasstatus, - 'hasrestart' => _hasrestart, - 'provider' => _provider, + 'ensure' => params['service_state'], + 'name' => params['service_name'], + 'enable' => params['service_enable'], + 'hasstatus' => hasstatus, + 'hasrestart' => hasrestart, + 'provider' => provider, ) } end end diff --git a/spec/shared_examples/services.rb b/spec/shared_examples/services.rb index efefdeb..3ccf20f 100644 --- a/spec/shared_examples/services.rb +++ b/spec/shared_examples/services.rb @@ -1,141 +1,135 @@ # frozen_string_literal: true -shared_examples 'services' do |_title, _params, _facts, _defaults| - command = _params['command'] - create = _params['create'] - detach = _params['detach'] - ensure_value = _params['ensure'] - env = _params['env'] - extra_params = _params['extra_params'] - host_socket = _params['host_socket'] - image = _params['image'] - label = _params['label'] - mounts = _params['mounts'] - networks = _params['networks'] - publish = _params['publish'] - registry_mirror = _params['registry_mirror'] - replicas = _params['replicas'] - scale = _params['scale'] - service_name = _params['service_name'] - tty = _params['tty'] - update = _params['update'] - user = _params['user'] - workdir = _params['workdir'] +shared_examples 'services' do |title, params, facts, defaults| + command = params['command'] + create = params['create'] + detach = params['detach'] + ensure_value = params['ensure'] + env = params['env'] + extra_params = params['extra_params'] + host_socket = params['host_socket'] + image = params['image'] + label = params['label'] + mounts = params['mounts'] + networks = params['networks'] + publish = params['publish'] + registry_mirror = params['registry_mirror'] + replicas = params['replicas'] + scale = params['scale'] + service_name = params['service_name'] + tty = params['tty'] + update = params['update'] + user = params['user'] + workdir = params['workdir'] - docker_command = "#{_defaults['docker_command']} service" + docker_command = "#{defaults['docker_command']} service" - if _facts[:os]['family'] == 'windows' - exec_environment = "PATH=#{_facts['docker_program_files_path']}/Docker/;#{_facts['docker_systemroot']}/System32/" - exec_path = ["#{_facts['docker_program_files_path']}/Docker/"] + if facts[:os]['family'] == 'windows' + exec_environment = "PATH=#{facts['docker_program_files_path']}/Docker/;#{facts['docker_systemroot']}/System32/" + exec_path = ["#{facts['docker_program_files_path']}/Docker/"] exec_provider = 'powershell' exec_timeout = 3000 else exec_environment = 'HOME=/root' exec_path = ['/bin', '/usr/bin'] exec_provider = nil exec_timeout = 0 end if create docker_service_create_flags = get_docker_service_flags( 'detach' => detach, 'env' => Array(env), 'service_name' => service_name, 'label' => Array(label), 'publish' => publish, 'replicas' => replicas, 'tty' => tty, 'user' => user, 'workdir' => workdir, 'extra_params' => Array(extra_params), 'image' => image, 'host_socket' => host_socket, 'registry_mirror' => registry_mirror, 'mounts' => mounts, 'networks' => networks, 'command' => command, ) - _service_name = if service_name == :undef - '' - else - service_name - end - exec_create = "#{docker_command} create --name #{docker_service_create_flags}" - unless_create = "docker service ps #{_service_name}" + unless_create = "docker service ps #{service_name == :undef ? '' : service_name}" it { - is_expected.to contain_exec("#{_title} docker service create").with( + is_expected.to contain_exec("#{title} docker service create").with( 'command' => exec_create, 'environment' => exec_environment, 'path' => exec_path, 'timeout' => exec_timeout, 'provider' => exec_provider, 'unless' => unless_create, ) } end if update docker_service_flags = get_docker_service_flags( 'detach' => detach, 'env' => Array(env), 'service_name' => service_name, 'label' => Array(label), 'publish' => publish, 'replicas' => replicas, 'tty' => tty, 'user' => user, 'workdir' => workdir, 'extra_params' => Array(extra_params), 'image' => image, 'host_socket' => host_socket, 'registry_mirror' => registry_mirror, ) exec_update = "#{docker_command} update #{docker_service_flags}" it { - is_expected.to contain_exec("#{_title} docker service update").with( + is_expected.to contain_exec("#{title} docker service update").with( 'command' => exec_update, 'environment' => exec_environment, 'path' => exec_path, 'provider' => exec_provider, 'timeout' => exec_timeout, ) } end if scale - docker_service_flags = get_docker_service_flags( - 'service_name' => service_name, - 'replicas' => replicas, - 'extra_params' => Array(extra_params), - ) + # docker_service_flags = get_docker_service_flags( + # 'service_name' => service_name, + # 'replicas' => replicas, + # 'extra_params' => Array(extra_params), + # ) exec_scale = "#{docker_command} scale #{service_name}=#{replicas}" it { - is_expected.to contain_exec("#{_title} docker service scale").with( + is_expected.to contain_exec("#{title} docker service scale").with( 'command' => exec_scale, 'environment' => exec_environment, 'path' => exec_path, 'timeout' => exec_timeout, 'provider' => exec_provider, ) } end if ensure_value == 'absent' it { - is_expected.to contain_exec("#{_title} docker service remove").with( + is_expected.to contain_exec("#{title} docker service remove").with( 'command' => "docker service rm #{service_name}", 'onlyif' => "docker service ps #{service_name}", 'path' => exec_path, 'provider' => exec_provider, 'timeout' => exec_timeout, ) } end end diff --git a/spec/shared_examples/stack.rb b/spec/shared_examples/stack.rb index cad13fb..22a86b5 100644 --- a/spec/shared_examples/stack.rb +++ b/spec/shared_examples/stack.rb @@ -1,59 +1,59 @@ # frozen_string_literal: true -shared_examples 'stack' do |_title, _params, _facts, _defaults| - ensure_value = _params['ensure'] - stack_name = _params['stack_name'] - bundle_file = _params['bundle_file'] - compose_files = _params['compose_files'] - prune = _params['prune'] - with_registry_auth = _params['with_registry_auth'] - resolve_image = _params['resolve_image'] +shared_examples 'stack' do |_title, params, facts, defaults| + ensure_value = params['ensure'] + stack_name = params['stack_name'] + bundle_file = params['bundle_file'] + compose_files = params['compose_files'] + prune = params['prune'] + with_registry_auth = params['with_registry_auth'] + resolve_image = params['resolve_image'] # deprecation('docker::stack','The docker stack define type will be deprecated in a future release. Please migrate to the docker_stack type/provider.') - docker_command = "#{_defaults['docker_command']} stack" + docker_command = "#{defaults['docker_command']} stack" - if _facts[:os]['family'] == 'windows' + if facts[:os]['family'] == 'windows' exec_path = ['C:/Program Files/Docker/'] check_stack = '$info = docker stack ls | select-string -pattern web if ($info -eq $null) { Exit 1 } else { Exit 0 }' provider = 'powershell' else exec_path = ['/bin', '/usr/bin'] check_stack = "#{docker_command} ls | grep #{stack_name}" provider = nil end if ensure_value == 'present' docker_stack_flags = get_docker_stack_flags( 'stack_name' => stack_name, 'bundle_file' => bundle_file, 'compose_files' => compose_files, 'prune' => prune, 'with_registry_auth' => with_registry_auth, 'resolve_image' => resolve_image, ) exec_stack = "#{docker_command} deploy #{docker_stack_flags} #{stack_name}" it { is_expected.to contain_exec("docker stack create #{stack_name}").with( 'command' => exec_stack, 'unless' => check_stack, 'path' => exec_path, 'provider' => provider, ) } end if ensure_value == 'absent' it { is_expected.to contain_exec("docker stack destroy #{stack_name}").with( 'command' => "#{docker_command} rm #{stack_name}", 'onlyif' => check_stack, 'path' => exec_path, 'provider' => provider, ) } end end diff --git a/spec/shared_examples/swarm.rb b/spec/shared_examples/swarm.rb index cfbb123..5a7357d 100644 --- a/spec/shared_examples/swarm.rb +++ b/spec/shared_examples/swarm.rb @@ -1,106 +1,106 @@ # frozen_string_literal: true -shared_examples 'swarm' do |_title, _params, _facts, _defaults| - ensure_value = _params['ensure'] - init = _params['init'] - join = _params['join'] - advertise_addr = _params['advertise_addr'] - autolock = _params['autolock'] - cert_expiry = _params['cert_expiry'] - default_addr_pool = _params['default_addr_pool'] - default_addr_pool_mask_length = _params['default_addr_pool_mask_length'] - dispatcher_heartbeat = _params['dispatcher_heartbeat'] - external_ca = _params['external_ca'] - force_new_cluster = _params['force_new_cluster'] - listen_addr = _params['listen_addr'] - max_snapshots = _params['max_snapshots'] - snapshot_interval = _params['snapshot_interval'] - token = _params['token'] - manager_ip = _params['manager_ip'] +shared_examples 'swarm' do |_title, params, facts, defaults| + ensure_value = params['ensure'] + init = params['init'] + join = params['join'] + advertise_addr = params['advertise_addr'] + autolock = params['autolock'] + cert_expiry = params['cert_expiry'] + default_addr_pool = params['default_addr_pool'] + default_addr_pool_mask_length = params['default_addr_pool_mask_length'] + dispatcher_heartbeat = params['dispatcher_heartbeat'] + external_ca = params['external_ca'] + force_new_cluster = params['force_new_cluster'] + listen_addr = params['listen_addr'] + max_snapshots = params['max_snapshots'] + snapshot_interval = params['snapshot_interval'] + token = params['token'] + manager_ip = params['manager_ip'] - if _facts[:os]['family'] == 'windows' - exec_environment = "PATH=#{_facts['docker_program_files_path']}/Docker/" - exec_path = ["#{_facts['docker_program_files_path']}/Docker/"] + if facts[:os]['family'] == 'windows' + exec_environment = "PATH=#{facts['docker_program_files_path']}/Docker/" + exec_path = ["#{facts['docker_program_files_path']}/Docker/"] exec_timeout = 3000 exec_provider = 'powershell' unless_init = '$info = docker info | select-string -pattern "Swarm: active" if ($info -eq $null) { Exit 1 } else { Exit 0 }' unless_join = '$info = docker info | select-string -pattern "Swarm: active" if ($info -eq $null) { Exit 1 } else { Exit 0 }' onlyif_leave = '$info = docker info | select-string -pattern "Swarm: active" if ($info -eq $null) { Exit 1 } else { Exit 0 }' else exec_environment = 'HOME=/root' exec_path = ['/bin', '/usr/bin'] exec_timeout = 0 exec_provider = nil unless_init = 'docker info | grep -w "Swarm: active"' unless_join = 'docker info | grep -w "Swarm: active"' onlyif_leave = 'docker info | grep -w "Swarm: active"' end - docker_command = "#{_defaults['docker_command']} swarm" + docker_command = "#{defaults['docker_command']} swarm" if init docker_swarm_init_flags = get_docker_swarm_init_flags( 'init' => init, 'advertise_addr' => advertise_addr, 'autolock' => autolock, 'cert_expiry' => cert_expiry, 'dispatcher_heartbeat' => dispatcher_heartbeat, 'default_addr_pool' => default_addr_pool, 'default_addr_pool_mask_length' => default_addr_pool_mask_length, 'external_ca' => external_ca, 'force_new_cluster' => force_new_cluster, 'listen_addr' => listen_addr, 'max_snapshots' => max_snapshots, 'snapshot_interval' => snapshot_interval, ) exec_init = "#{docker_command} #{docker_swarm_init_flags}" it { is_expected.to contain_exec('Swarm init').with( 'command' => exec_init, 'environment' => exec_environment, 'path' => exec_path, 'provider' => exec_provider, 'timeout' => exec_timeout, 'unless' => unless_init, ) } end if join docker_swarm_join_flags = get_docker_swarm_join_flags( 'join' => join, 'advertise_addr' => advertise_addr, 'listen_addr' => listen_addr, 'token' => token, ) exec_join = "#{docker_command} #{docker_swarm_join_flags} #{manager_ip}" it { is_expected.to contain_exec('Swarm join').with( 'command' => exec_join, 'environment' => exec_environment, 'path' => exec_path, 'provider' => exec_provider, 'timeout' => exec_timeout, 'unless' => unless_join, ) } end if ensure_value == 'absent' it { is_expected.to contain_exec('Leave swarm').with( 'command' => 'docker swarm leave --force', 'onlyif' => onlyif_leave, 'path' => exec_path, 'provider' => exec_provider, ) } end end diff --git a/spec/shared_examples/system_user.rb b/spec/shared_examples/system_user.rb index 9ea2ca4..4f16b1f 100644 --- a/spec/shared_examples/system_user.rb +++ b/spec/shared_examples/system_user.rb @@ -1,22 +1,18 @@ # frozen_string_literal: true shared_examples 'system_user' do |user, group| - _create_user = true - docker_group = group - if _create_user - it { - is_expected.to contain_user(user).with( - 'ensure' => 'present', - ).that_comes_before("Exec[docker-system-user-#{user}]") - } - end + it { + is_expected.to contain_user(user).with( + 'ensure' => 'present', + ).that_comes_before("Exec[docker-system-user-#{user}]") + } it { is_expected.to contain_exec("docker-system-user-#{user}").with( 'command' => "/usr/sbin/usermod -aG #{docker_group} #{user}", 'unless' => "/bin/cat /etc/group | grep '^#{docker_group}:' | grep -qw #{user}", ) } end diff --git a/spec/spec_helper_acceptance_local.rb b/spec/spec_helper_acceptance_local.rb index c802a0c..b492e65 100644 --- a/spec/spec_helper_acceptance_local.rb +++ b/spec/spec_helper_acceptance_local.rb @@ -1,195 +1,195 @@ # frozen_string_literal: true require 'puppet_litmus' require 'rspec/retry' require 'tempfile' include PuppetLitmus # This method allows a block to be passed in and if an exception is raised # that matches the 'error_matcher' matcher, the block will wait a set number # of seconds before retrying. # Params: # - max_retry_count - Max number of retries # - retry_wait_interval_secs - Number of seconds to wait before retry # - error_matcher - Matcher which the exception raised must match to allow retry # Example Usage: # retry_on_error_matching(3, 5, /OpenGPG Error/) do # apply_manifest(pp, :catch_failures => true) # end def retry_on_error_matching(max_retry_count = 3, retry_wait_interval_secs = 5, error_matcher = nil) try = 0 begin try += 1 yield rescue StandardError => e raise unless try < max_retry_count && (error_matcher.nil? || e.message =~ error_matcher) sleep retry_wait_interval_secs retry end end def create_remote_file(name, full_name, file_content) Tempfile.open name do |tempfile| File.open(tempfile.path, 'w') { |file| file.puts file_content } bolt_upload_file(tempfile.path, full_name) end end RSpec.configure do |c| # Add exclusive filter for Windows untill all the windows functionality is implemented c.filter_run_excluding win_broken: true # Readable test descriptions c.formatter = :documentation # show retry status in spec process c.verbose_retry = true # show exception that triggers a retry if verbose_retry is set to true c.display_try_failure_messages = true # Configure all nodes in nodeset c.before :suite do # Install module and dependencies # Due to RE-6764, running yum update renders the machine unable to install # other software. Thus this workaround. if os[:family] == 'redhat' run_shell('mv /etc/yum.repos.d/redhat.repo /etc/yum.repos.d/internal-mirror.repo', expect_failures: true) run_shell('rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm', expect_failures: true) run_shell('yum update -y -q') # run_shell('yum upgrade -y') end if os[:family] == 'debian' || os[:family] == 'ubuntu' run_shell('apt-get update -y') # run_shell('apt-get upgrade -y') run_shell('apt-get install -y lsb-release') run_shell('apt-get install -y net-tools') end run_shell('puppet module install puppetlabs-stdlib --version 4.24.0', expect_failures: true) run_shell('puppet module install puppetlabs-apt --version 4.4.1', expect_failures: true) run_shell('puppet module install puppetlabs-translate --version 1.0.0', expect_failures: true) run_shell('puppet module install puppetlabs-powershell --version 2.1.5', expect_failures: true) run_shell('puppet module install puppetlabs-reboot --version 2.0.0', expect_failures: true) # net-tools required for netstat utility being used by some tests if os[:family] == 'redhat' && os[:release].to_i == 7 run_shell('yum -y install lvm2 device-mapper device-mapper-persistent-data device-mapper-event device-mapper-libs device-mapper-event-libs') run_shell('yum install -y yum-utils net-tools') run_shell('yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo') run_shell('yum-config-manager --enable docker\*') end docker_compose_content_v3 = <<-EOS version: "3.4" x-images: &default-image alpine:3.8 services: compose_test: image: *default-image command: /bin/sh -c "while true; do echo hello world; sleep 1; done" EOS docker_compose_override_v3 = <<-EOS version: "3.4" x-images: &default-image debian:stable-slim services: compose_test: image: *default-image command: /bin/sh -c "while true; do echo hello world; sleep 1; done" EOS docker_stack_override_v3 = <<-EOS version: "3.4" x-images: &default-image debian:stable-slim services: compose_test: image: *default-image command: /bin/sh -c "while true; do echo hello world; sleep 1; done" EOS docker_compose_content_v3_windows = <<-EOS version: "3" services: compose_test: image: winamd64/hello-seattle command: cmd.exe /C "ping 8.8.8.8 -t" networks: default: external: name: nat EOS docker_compose_override_v3_windows = <<-EOS version: "3" services: compose_test: image: winamd64/hello-seattle:nanoserver command: cmd.exe /C "ping 8.8.8.8 -t" networks: default: external: name: nat EOS docker_compose_override_v3_windows2016 = <<-EOS version: "3" services: compose_test: image: winamd64/hello-seattle:nanoserver-sac2016 command: cmd.exe /C "ping 8.8.8.8 -t" networks: default: external: name: nat EOS docker_stack_content_windows = <<-EOS version: "3" services: compose_test: image: winamd64/hello-seattle command: cmd.exe /C "ping 8.8.8.8 -t" EOS docker_stack_override_windows = <<-EOS version: "3" services: compose_test: image: winamd64/hello-seattle:nanoserver EOS docker_stack_override_windows2016 = <<-EOS version: "3" services: compose_test: image: winamd64/hello-seattle:nanoserver-sac2016 EOS if os[:family] == 'windows' create_remote_file(host, '/tmp/docker-compose-v3.yml', docker_compose_content_v3_windows) create_remote_file(host, '/tmp/docker-stack.yml', docker_stack_content_windows) - if os[:release] =~ %r{2019} + if %r{2019}.match?(os[:release]) create_remote_file(host, '/tmp/docker-compose-override-v3.yml', docker_compose_override_v3_windows) create_remote_file(host, '/tmp/docker-stack-override.yml', docker_stack_override_windows) else create_remote_file(host, '/tmp/docker-compose-override-v3.yml', docker_compose_override_v3_windows2016) create_remote_file(host, '/tmp/docker-stack-override.yml', docker_stack_override_windows2016) end else create_remote_file(host, '/tmp/docker-compose-v3.yml', docker_compose_content_v3) create_remote_file(host, '/tmp/docker-stack.yml', docker_compose_content_v3) create_remote_file(host, '/tmp/docker-compose-override-v3.yml', docker_compose_override_v3) create_remote_file(host, '/tmp/docker-stack-override.yml', docker_stack_override_v3) end next unless os[:family] == 'windows' result = run_shell("ipconfig | findstr /i 'ipv4'") raise 'Could not retrieve ip address for Windows box' if result.exit_code != 0 ip = result.stdout.split("\n")[0].split(':')[1].strip retry_on_error_matching(60, 5, %r{connection failure running}) do @windows_ip = ip end apply_manifest("class { 'docker': docker_ee => true, extra_parameters => '\"insecure-registries\": [ \"#{@windows_ip}:5000\" ]' }", catch_failures: true) docker_path = 'C:\\Program Files\\Docker' run_shell("set PATH \"%PATH%;C:\\Users\\Administrator\\AppData\\Local\\Temp;#{docker_path}\"") puts 'Waiting for box to come online' sleep 300 end end