├── .rubocop.yml ├── .gitignore ├── CONTRIBUTING.md ├── .github ├── dependabot.yml └── workflows │ ├── release.yml │ └── test.yml ├── spec ├── spec_helper.rb └── unit │ └── beaker │ └── module_install_helper_spec.rb ├── Gemfile ├── .rubocop_todo.yml ├── Rakefile ├── beaker-module_install_helper.gemspec ├── README.md ├── CHANGELOG.md ├── lib └── beaker │ └── module_install_helper.rb └── LICENSE /.rubocop.yml: -------------------------------------------------------------------------------- 1 | --- 2 | inherit_from: .rubocop_todo.yml 3 | 4 | inherit_gem: 5 | voxpupuli-rubocop: rubocop.yml 6 | 7 | Style/Documentation: 8 | Enabled: false 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | /.idea/ 11 | .vendor/ 12 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ### Contributing and adding features 2 | 3 | This package is currently being developed so any contributions are welcomed. The purpose of this package is to simplify the spec_helper_acceptance.rb files within each module and as such any contributions in this area are welcome, as long as backwards compatibility is kept in mind. 4 | 5 | Use this repo's issue tracker to track any issues you encounter -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | # raise PRs for gem updates 4 | - package-ecosystem: bundler 5 | directory: "/" 6 | schedule: 7 | interval: daily 8 | time: "13:00" 9 | open-pull-requests-limit: 10 10 | 11 | # Maintain dependencies for GitHub Actions 12 | - package-ecosystem: github-actions 13 | directory: "/" 14 | schedule: 15 | interval: daily 16 | time: "13:00" 17 | open-pull-requests-limit: 10 18 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | begin 4 | require 'simplecov' 5 | require 'simplecov-console' 6 | require 'codecov' 7 | rescue LoadError 8 | else 9 | SimpleCov.start do 10 | track_files 'lib/**/*.rb' 11 | 12 | add_filter '/spec' 13 | 14 | enable_coverage :branch 15 | 16 | # do not track vendored files 17 | add_filter '/vendor' 18 | add_filter '/.vendor' 19 | end 20 | 21 | SimpleCov.formatters = [ 22 | SimpleCov::Formatter::Console, 23 | SimpleCov::Formatter::Codecov, 24 | ] 25 | end 26 | 27 | require 'beaker/module_install_helper' 28 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source 'https://rubygems.org' 4 | 5 | gemspec 6 | 7 | group :test do 8 | gem 'beaker' 9 | gem 'bundler', '>= 1.9', '< 3' 10 | gem 'rake', '~> 13.0' 11 | gem 'rspec', '~> 3.0' 12 | end 13 | 14 | group :development do 15 | gem 'pry' 16 | gem 'pry-byebug' 17 | end 18 | 19 | group :coverage, optional: ENV.fetch('COVERAGE', nil) != 'yes' do 20 | gem 'codecov', require: false 21 | gem 'simplecov-console', require: false 22 | end 23 | 24 | group :release do 25 | gem 'faraday-retry', require: false 26 | gem 'github_changelog_generator', require: false 27 | end 28 | -------------------------------------------------------------------------------- /.rubocop_todo.yml: -------------------------------------------------------------------------------- 1 | # This configuration was generated by 2 | # `rubocop --auto-gen-config` 3 | # on 2023-05-05 15:14:01 UTC using RuboCop version 1.50.2. 4 | # The point is for the user to remove these configuration records 5 | # one by one as the offenses are removed from the code base. 6 | # Note that changes in the inspected code, or installation of new 7 | # versions of RuboCop, may require this file to be generated again. 8 | 9 | # Offense count: 10 10 | RSpec/AnyInstance: 11 | Exclude: 12 | - 'spec/unit/beaker/module_install_helper_spec.rb' 13 | 14 | # Offense count: 2 15 | # Configuration parameters: Max. 16 | RSpec/IndexedLet: 17 | Exclude: 18 | - 'spec/unit/beaker/module_install_helper_spec.rb' 19 | 20 | # Offense count: 7 21 | # Configuration parameters: AllowedVariables. 22 | Style/GlobalVars: 23 | Exclude: 24 | - 'lib/beaker/module_install_helper.rb' 25 | - 'spec/unit/beaker/module_install_helper_spec.rb' 26 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | tags: 6 | - '*' 7 | 8 | jobs: 9 | release: 10 | runs-on: ubuntu-latest 11 | if: github.repository_owner == 'voxpupuli' 12 | steps: 13 | - uses: actions/checkout@v4 14 | - name: Install Ruby 3.0 15 | uses: ruby/setup-ruby@v1 16 | with: 17 | ruby-version: '3.0' 18 | bundler: 'none' 19 | - name: Build gem 20 | run: gem build --strict --verbose *.gemspec 21 | - name: Publish gem to rubygems.org 22 | run: gem push *.gem 23 | env: 24 | GEM_HOST_API_KEY: '${{ secrets.RUBYGEMS_AUTH_TOKEN }}' 25 | - name: Setup GitHub packages access 26 | run: | 27 | mkdir -p ~/.gem 28 | echo ":github: Bearer ${{ secrets.GITHUB_TOKEN }}" >> ~/.gem/credentials 29 | chmod 0600 ~/.gem/credentials 30 | - name: Publish gem to GitHub packages 31 | run: gem push --key github --host https://rubygems.pkg.github.com/${{ github.repository_owner }} *.gem 32 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'bundler/gem_tasks' 4 | require 'rubocop/rake_task' 5 | 6 | RuboCop::RakeTask.new 7 | 8 | require 'rspec/core/rake_task' 9 | desc 'Run spec tests using rspec' 10 | RSpec::Core::RakeTask.new(:spec) do |t| 11 | t.rspec_opts = ['--color'] 12 | t.pattern = 'spec' 13 | end 14 | 15 | begin 16 | require 'rubygems' 17 | require 'github_changelog_generator/task' 18 | rescue LoadError 19 | # github_changelog_generator isn't available, so we won't define a rake task with it 20 | else 21 | GitHubChangelogGenerator::RakeTask.new :changelog do |config| 22 | config.header = <<-HEADER 23 | # Changelog 24 | 25 | # All notable changes to this project will be documented in this file. 26 | HEADER 27 | config.exclude_labels = %w[duplicate question invalid wontfix wont-fix skip-changelog] 28 | config.user = 'voxpupuli' 29 | config.project = 'beaker-module_install_helper' 30 | config.future_release = Gem::Specification.load("#{config.project}.gemspec").version 31 | end 32 | end 33 | 34 | task default: %w[ 35 | rubocop 36 | spec 37 | ] 38 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | pull_request: {} 5 | push: 6 | branches: 7 | - master 8 | 9 | env: 10 | BUNDLE_WITHOUT: release 11 | 12 | jobs: 13 | rubocop: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v4 17 | - name: Install Ruby ${{ matrix.ruby }} 18 | uses: ruby/setup-ruby@v1 19 | with: 20 | ruby-version: "2.7" 21 | bundler-cache: true 22 | - name: Run Rubocop 23 | run: bundle exec rake rubocop 24 | spec: 25 | runs-on: ubuntu-latest 26 | strategy: 27 | fail-fast: false 28 | matrix: 29 | include: 30 | - ruby: "2.7" 31 | coverage: "yes" 32 | - ruby: "3.0" 33 | - ruby: "3.1" 34 | - ruby: "3.2" 35 | env: 36 | COVERAGE: ${{ matrix.coverage }} 37 | name: RSpec - Ruby ${{ matrix.ruby }} 38 | steps: 39 | - uses: actions/checkout@v4 40 | - name: Install Ruby ${{ matrix.ruby }} 41 | uses: ruby/setup-ruby@v1 42 | with: 43 | ruby-version: ${{ matrix.ruby }} 44 | bundler-cache: true 45 | - name: spec tests 46 | run: bundle exec rake spec 47 | - name: Build gem 48 | run: gem build --strict --verbose *.gemspec 49 | 50 | tests: 51 | needs: 52 | - spec 53 | - rubocop 54 | runs-on: ubuntu-latest 55 | name: Test suite 56 | steps: 57 | - run: echo Test suite completed 58 | -------------------------------------------------------------------------------- /beaker-module_install_helper.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | lib = File.expand_path('lib', __dir__) 4 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = 'beaker-module_install_helper' 8 | spec.version = '2.0.0' 9 | spec.authors = ['Vox Pupuli'] 10 | spec.email = ['voxpupuli@groups.io'] 11 | 12 | spec.summary = 'A helper gem for use in a Puppet Modules ' \ 13 | 'spec_helper_acceptance.rb file' 14 | spec.description = 'A helper gem for use in a Puppet Modules ' \ 15 | 'spec_helper_acceptance.rb file to help install the ' \ 16 | 'module under test and its dependencies on the system ' \ 17 | 'under test' 18 | spec.homepage = 'https://github.com/voxpupuli/beaker-module_install_helper' 19 | spec.license = 'Apache-2.0' 20 | 21 | spec.files = `git ls-files`.split("\n") 22 | spec.executables = `git ls-files -- bin/*` 23 | .split("\n") 24 | .map { |f| File.basename(f) } 25 | spec.require_paths = ['lib'] 26 | 27 | ## Testing dependencies 28 | spec.add_development_dependency 'rspec', '~> 3.12' 29 | spec.add_development_dependency 'voxpupuli-rubocop', '~> 2.0' 30 | 31 | # Run time dependencies 32 | spec.add_runtime_dependency 'beaker', '>= 2.0', '< 6' 33 | spec.add_runtime_dependency 'beaker-puppet', '>= 1', '< 3' 34 | 35 | spec.required_ruby_version = '>= 2.7.0' 36 | end 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## beaker-module\_install\_helper 2 | 3 | [![License](https://img.shields.io/github/license/voxpupuli/beaker-module_install_helper.svg)](https://github.com/voxpupuli/beaker-module_install_helper/blob/master/LICENSE) 4 | [![Test](https://github.com/voxpupuli/beaker-module_install_helper/actions/workflows/test.yml/badge.svg)](https://github.com/voxpupuli/beaker-module_install_helper/actions/workflows/test.yml) 5 | [![Release](https://github.com/voxpupuli/beaker-module_install_helper/actions/workflows/release.yml/badge.svg)](https://github.com/voxpupuli/beaker-module_install_helper/actions/workflows/release.yml) 6 | [![RubyGem Version](https://img.shields.io/gem/v/beaker-module_install_helper.svg)](https://rubygems.org/gems/beaker-module_install_helper) 7 | [![RubyGem Downloads](https://img.shields.io/gem/dt/beaker-module_install_helper.svg)](https://rubygems.org/gems/beaker-module_install_helper) 8 | [![Donated by Puppet Inc](https://img.shields.io/badge/donated%20by-Puppet%20Inc-fb7047.svg)](#transfer-notice) 9 | 10 | **This gem is deprecated. Vox Pupuli now uses [beaker_puppet_helpers](https://github.com/voxpupuli/beaker_puppet_helpers)** 11 | 12 | This gem is simply an abstraction for the various functions that are performed within the `spec/spec_helper_acceptance.rb` files across the modules to standardise how these are implemented. 13 | 14 | ### Usage 15 | The below example will install the module from source on the host with role 'master', if that doesn't exist, on all hosts with role 'agent'. Otherwise it will be installed on all hosts. 16 | ```ruby 17 | require 'beaker/module_install_helper' 18 | install_module_dependencies 19 | install_module 20 | 21 | # Install a testing only dependency, not specified in metadata 22 | install_module_from_forge('puppetlabs-inifile', '>= 1.0.0 <= 50.0.0') 23 | ``` 24 | 25 | The below example will install the module from source on the specified host and install module dependencies specified in metadata.json on the host 26 | ```ruby 27 | require 'beaker/module_install_helper' 28 | install_module_dependencies_on(host) 29 | install_module_on(host) 30 | ``` 31 | 32 | ### Assumptions 33 | * Module under test has a valid metadata.json file at the root of the module directory. 34 | 35 | ### `install_module` 36 | This will call `install_module_on` on the hosts with role 'master'. If there are none, the module will be install on all hosts with the role 'agent', again, if there are none, the module will be installed on all hosts. 37 | 38 | ### `install_module_on(host)` 39 | This will install the module under test on the specified host using the local source. The module name will be derived from the name property of the module's metadata.json file, assuming it is in format author-modulename. 40 | 41 | ### `install_module_dependencies` 42 | This will call `install_module_dependencies_on` on the hosts with role 'master'. If there are none, the module will be install on all hosts with the role 'agent', again, if there are none, the module dependencies will be installed on all hosts. 43 | 44 | ### `install_module_dependencies_on` 45 | This will install a list of dependencies on the specified host from the forge, using the dependencies list specified in the metadata.json file, taking into consideration the version constraints if specified. 46 | 47 | **See:** [Alternative Forge Instances](#alternative-forge-instances) 48 | 49 | ### `install_module_from_forge(module_name, version_requirement)` 50 | This will call `install_module_from_forge_on` on the hosts with role 'master'. If there are none, the module will be installed on all hosts with the role 'agent', again, if there are none, the module will be installed on all hosts. 51 | 52 | ### `install_module_from_forge_on(hosts, module_name, version_requirement)` 53 | This will install a module from the forge on the given host(s). Module name must be specified in the `{author}-{module_name}` or `{author}/{module_name}` format. Version requirement must be specified to meet [this](https://docs.puppet.com/puppet/latest/modules_metadata.html#version-specifiers) criteria. 54 | 55 | **See:** [Alternative Forge Instances](#alternative-forge-instances) 56 | 57 | ### Alternative Forge Instances 58 | It is possible to use alternative forge instances rather than the production forge instance to install module dependencies by specifiying 2 environment variables, `BEAKER_FORGE_HOST` and `BEAKER_FORGE_API`. Both of these are required as the forge API is used under the hood to resolve version requirement boundary strings. 59 | 60 | **Example Using Staging Forge** 61 | ``` 62 | BEAKER_FORGE_HOST=https://module-staging.puppetlabs.com BEAKER_FORGE_API=https://api-module-staging.puppetlabs.com BEAKER_debug=true bundle exec rspec spec/acceptance 63 | ``` 64 | 65 | ### Support 66 | No support is supplied or implied. Use at your own risk. 67 | 68 | ## Transfer Notice 69 | 70 | This plugin was originally authored by [Puppet Inc](http://puppet.com). 71 | The maintainer preferred that [Vox Pupuli](https://voxpupuli.org) take ownership of the module for future improvement and maintenance. 72 | Existing pull requests and issues were transferred over, please fork and continue to contribute here. 73 | 74 | ## License 75 | 76 | This gem is licensed under the Apache-2 license. 77 | 78 | ## Release information 79 | 80 | To make a new release, please do: 81 | * update the version in the gemspec file 82 | * Install gems with `bundle install --with release --path .vendor` 83 | * generate the changelog with `bundle exec rake changelog` 84 | * Check if the new version matches the closed issues/PRs in the changelog 85 | * Create a PR with it 86 | * After it got merged, push a tag. GitHub actions will do the actual release to rubygems and GitHub Packages 87 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | # All notable changes to this project will be documented in this file. 4 | 5 | 6 | ## [2.0.0](https://github.com/voxpupuli/beaker-module_install_helper/tree/2.0.0) (2023-05-05) 7 | 8 | [Full Changelog](https://github.com/voxpupuli/beaker-module_install_helper/compare/v1.0.0...2.0.0) 9 | 10 | **Breaking changes:** 11 | 12 | - Drop Ruby 2.5/2.6 support; use voxpupuli-rubocop [\#42](https://github.com/voxpupuli/beaker-module_install_helper/pull/42) ([bastelfreak](https://github.com/bastelfreak)) 13 | - add rubocop to CI / bump min ruby to 2.5 [\#33](https://github.com/voxpupuli/beaker-module_install_helper/pull/33) ([jhoblitt](https://github.com/jhoblitt)) 14 | 15 | **Implemented enhancements:** 16 | 17 | - CI: Apply Vox Pupuli best practices & Add Ruby 3.2 support [\#39](https://github.com/voxpupuli/beaker-module_install_helper/pull/39) ([bastelfreak](https://github.com/bastelfreak)) 18 | 19 | **Closed issues:** 20 | 21 | - Forge stubbing doesn't work for non-forge forges [\#23](https://github.com/voxpupuli/beaker-module_install_helper/issues/23) 22 | 23 | **Merged pull requests:** 24 | 25 | - CI: Build gems with strictness and verbosity & dependencies: add version constraints [\#43](https://github.com/voxpupuli/beaker-module_install_helper/pull/43) ([bastelfreak](https://github.com/bastelfreak)) 26 | - Update beaker-puppet requirement from ~\> 1.0 to \>= 1, \< 3 [\#41](https://github.com/voxpupuli/beaker-module_install_helper/pull/41) ([dependabot[bot]](https://github.com/apps/dependabot)) 27 | - Bump actions/checkout from 2 to 3 [\#40](https://github.com/voxpupuli/beaker-module_install_helper/pull/40) ([dependabot[bot]](https://github.com/apps/dependabot)) 28 | - dependabot: check for github actions and gems [\#38](https://github.com/voxpupuli/beaker-module_install_helper/pull/38) ([bastelfreak](https://github.com/bastelfreak)) 29 | - Rubocop: Apply Vox Pupuli best practices & fix violations [\#37](https://github.com/voxpupuli/beaker-module_install_helper/pull/37) ([bastelfreak](https://github.com/bastelfreak)) 30 | - Fix incorrect Apache-2 license [\#36](https://github.com/voxpupuli/beaker-module_install_helper/pull/36) ([bastelfreak](https://github.com/bastelfreak)) 31 | - add basic Net::HTTP error handling [\#34](https://github.com/voxpupuli/beaker-module_install_helper/pull/34) ([jhoblitt](https://github.com/jhoblitt)) 32 | 33 | ## [v1.0.0](https://github.com/voxpupuli/beaker-module_install_helper/tree/v1.0.0) (2021-11-03) 34 | 35 | [Full Changelog](https://github.com/voxpupuli/beaker-module_install_helper/compare/v0.1.7...v1.0.0) 36 | 37 | **Closed issues:** 38 | 39 | - Master fails to build in Travis [\#25](https://github.com/voxpupuli/beaker-module_install_helper/issues/25) 40 | 41 | **Merged pull requests:** 42 | 43 | - Add beaker-puppet dependency, update gem versions, fix CI [\#31](https://github.com/voxpupuli/beaker-module_install_helper/pull/31) ([ekohl](https://github.com/ekohl)) 44 | - Add Dependabot & GH Actions for testing & release [\#27](https://github.com/voxpupuli/beaker-module_install_helper/pull/27) ([genebean](https://github.com/genebean)) 45 | - Allow passing in options to install\_module [\#26](https://github.com/voxpupuli/beaker-module_install_helper/pull/26) ([ekohl](https://github.com/ekohl)) 46 | - Don't attempt to modify possibly frozen strings [\#24](https://github.com/voxpupuli/beaker-module_install_helper/pull/24) ([beezly](https://github.com/beezly)) 47 | 48 | ## [v0.1.7](https://github.com/voxpupuli/beaker-module_install_helper/tree/v0.1.7) (2017-12-08) 49 | 50 | [Full Changelog](https://github.com/voxpupuli/beaker-module_install_helper/compare/v0.1.6...v0.1.7) 51 | 52 | **Merged pull requests:** 53 | 54 | - Release prep 0.1.7 [\#22](https://github.com/voxpupuli/beaker-module_install_helper/pull/22) ([pmcmaw](https://github.com/pmcmaw)) 55 | - Fix broken beaker dependency [\#21](https://github.com/voxpupuli/beaker-module_install_helper/pull/21) ([cdenneen](https://github.com/cdenneen)) 56 | 57 | ## [v0.1.6](https://github.com/voxpupuli/beaker-module_install_helper/tree/v0.1.6) (2017-12-08) 58 | 59 | [Full Changelog](https://github.com/voxpupuli/beaker-module_install_helper/compare/0.1.5...v0.1.6) 60 | 61 | **Merged pull requests:** 62 | 63 | - minor gemspec changes [\#20](https://github.com/voxpupuli/beaker-module_install_helper/pull/20) ([pmcmaw](https://github.com/pmcmaw)) 64 | - Release Prep for 0.1.6 [\#19](https://github.com/voxpupuli/beaker-module_install_helper/pull/19) ([pmcmaw](https://github.com/pmcmaw)) 65 | - \(BKR-1254\) Update for Unix::Host does not allow each [\#17](https://github.com/voxpupuli/beaker-module_install_helper/pull/17) ([cdenneen](https://github.com/cdenneen)) 66 | 67 | ## [0.1.5](https://github.com/voxpupuli/beaker-module_install_helper/tree/0.1.5) (2017-07-26) 68 | 69 | [Full Changelog](https://github.com/voxpupuli/beaker-module_install_helper/compare/0.1.4...0.1.5) 70 | 71 | **Merged pull requests:** 72 | 73 | - 0.1.5 fix uninitialized constant Net::HTTP [\#16](https://github.com/voxpupuli/beaker-module_install_helper/pull/16) ([hunner](https://github.com/hunner)) 74 | - Fix bug that causes a NoMethodError: undefined method `each' for nil:NilClass [\#15](https://github.com/voxpupuli/beaker-module_install_helper/pull/15) ([dhollinger](https://github.com/dhollinger)) 75 | - Correct markdown formatting [\#14](https://github.com/voxpupuli/beaker-module_install_helper/pull/14) ([ekohl](https://github.com/ekohl)) 76 | 77 | ## [0.1.4](https://github.com/voxpupuli/beaker-module_install_helper/tree/0.1.4) (2017-02-27) 78 | 79 | [Full Changelog](https://github.com/voxpupuli/beaker-module_install_helper/compare/0.1.3...0.1.4) 80 | 81 | **Merged pull requests:** 82 | 83 | - Fix get\_module\_source\_directory when module used with Rototiller [\#12](https://github.com/voxpupuli/beaker-module_install_helper/pull/12) ([wilson208](https://github.com/wilson208)) 84 | 85 | ## [0.1.3](https://github.com/voxpupuli/beaker-module_install_helper/tree/0.1.3) (2017-02-22) 86 | 87 | [Full Changelog](https://github.com/voxpupuli/beaker-module_install_helper/compare/0.1.2...0.1.3) 88 | 89 | **Merged pull requests:** 90 | 91 | - Fix beaker forge api not specified error [\#11](https://github.com/voxpupuli/beaker-module_install_helper/pull/11) ([wilson208](https://github.com/wilson208)) 92 | 93 | ## [0.1.2](https://github.com/voxpupuli/beaker-module_install_helper/tree/0.1.2) (2017-02-02) 94 | 95 | [Full Changelog](https://github.com/voxpupuli/beaker-module_install_helper/compare/0.1.1...0.1.2) 96 | 97 | **Merged pull requests:** 98 | 99 | - Minor version bump for release 0.1.2 [\#10](https://github.com/voxpupuli/beaker-module_install_helper/pull/10) ([wilson208](https://github.com/wilson208)) 100 | - \[MODULES-4312\] Install modules dependencies from alternative forge instances and install modules not specified in metadata.json [\#9](https://github.com/voxpupuli/beaker-module_install_helper/pull/9) ([wilson208](https://github.com/wilson208)) 101 | 102 | ## [0.1.1](https://github.com/voxpupuli/beaker-module_install_helper/tree/0.1.1) (2017-01-09) 103 | 104 | [Full Changelog](https://github.com/voxpupuli/beaker-module_install_helper/compare/0.1.0...0.1.1) 105 | 106 | **Merged pull requests:** 107 | 108 | - Release 0.1.1 containing fixes [\#8](https://github.com/voxpupuli/beaker-module_install_helper/pull/8) ([wilson208](https://github.com/wilson208)) 109 | 110 | ## [0.1.0](https://github.com/voxpupuli/beaker-module_install_helper/tree/0.1.0) (2017-01-04) 111 | 112 | [Full Changelog](https://github.com/voxpupuli/beaker-module_install_helper/compare/2992a8637097a36d1452be05348f11715a587bc7...0.1.0) 113 | 114 | **Merged pull requests:** 115 | 116 | - Add maintainers file [\#7](https://github.com/voxpupuli/beaker-module_install_helper/pull/7) ([wilson208](https://github.com/wilson208)) 117 | - Add CONTRIBUTING.md, a README update and version bump before initial release [\#6](https://github.com/voxpupuli/beaker-module_install_helper/pull/6) ([wilson208](https://github.com/wilson208)) 118 | - Implement install\_module\_dependencies\_on method [\#5](https://github.com/voxpupuli/beaker-module_install_helper/pull/5) ([wilson208](https://github.com/wilson208)) 119 | - \[MODULES-4152\] Implement install\_module and install\_module\_on methods [\#3](https://github.com/voxpupuli/beaker-module_install_helper/pull/3) ([wilson208](https://github.com/wilson208)) 120 | - \[MODULES-4157\] Basic setup with stub methods and placeholder tests [\#2](https://github.com/voxpupuli/beaker-module_install_helper/pull/2) ([wilson208](https://github.com/wilson208)) 121 | 122 | 123 | 124 | \* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)* 125 | -------------------------------------------------------------------------------- /lib/beaker/module_install_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'beaker' 4 | require 'beaker-puppet' 5 | 6 | # Provides method for use in module test setup to install the module under 7 | # test and it's dependencies on the specified hosts 8 | module Beaker 9 | module ModuleInstallHelper 10 | include Beaker::DSL 11 | 12 | # This method calls the install_module_on method for each host which is a 13 | # master, or if no master is present, on all agent nodes. 14 | def install_module(opts = {}) 15 | install_module_on(hosts_to_install_module_on, opts) 16 | end 17 | 18 | # This method will install the module under test on the specified host(s) from 19 | # the source on the local machine 20 | def install_module_on(host, opts = {}) 21 | opts = { 22 | source: $module_source_dir, 23 | module_name: module_name_from_metadata, 24 | }.merge(opts) 25 | copy_module_to(host, opts) 26 | end 27 | 28 | # This method calls the install_module_dependencies_on method for each 29 | # host which is a master, or if no master is present, on all agent nodes. 30 | def install_module_dependencies(deps = nil) 31 | install_module_dependencies_on(hosts_to_install_module_on, deps) 32 | end 33 | 34 | # This method will install the module under tests module dependencies on the 35 | # specified host(s) from the dependencies list in metadata.json 36 | def install_module_dependencies_on(hsts, deps = nil) 37 | hsts = [hsts] if hsts.is_a?(Hash) 38 | hsts = [hsts] unless hsts.respond_to?(:each) 39 | deps = module_dependencies_from_metadata if deps.nil? 40 | 41 | fh = ENV.fetch('BEAKER_FORGE_HOST', nil) 42 | 43 | hsts.each do |host| 44 | deps.each do |dep| 45 | if fh.nil? 46 | install_puppet_module_via_pmt_on(host, dep) 47 | else 48 | with_forge_stubbed_on(host) do 49 | install_puppet_module_via_pmt_on(host, dep) 50 | end 51 | end 52 | end 53 | end 54 | end 55 | 56 | def install_module_from_forge(mod_name, ver_req) 57 | install_module_from_forge_on(hosts_to_install_module_on, mod_name, ver_req) 58 | end 59 | 60 | def install_module_from_forge_on(hsts, mod_name, ver_req) 61 | sub_mod_name = mod_name.sub('/', '-') 62 | dependency = { 63 | module_name: sub_mod_name, 64 | version: module_version_from_requirement(sub_mod_name, ver_req), 65 | } 66 | 67 | install_module_dependencies_on(hsts, [dependency]) 68 | end 69 | 70 | # This method returns an array of dependencies from the metadata.json file 71 | # in the format of an array of hashes, containing :module_name and optionally 72 | # :version elements. If no dependencies are specified, empty array is returned 73 | def module_dependencies_from_metadata 74 | metadata = module_metadata 75 | return [] unless metadata.key?('dependencies') 76 | 77 | dependencies = [] 78 | metadata['dependencies'].each do |d| 79 | tmp = { module_name: d['name'].sub('/', '-') } 80 | 81 | if d.key?('version_requirement') 82 | tmp[:version] = module_version_from_requirement(tmp[:module_name], 83 | d['version_requirement']) 84 | end 85 | dependencies.push(tmp) 86 | end 87 | 88 | dependencies 89 | end 90 | 91 | # This method takes a module name and the version requirement string from the 92 | # metadata.json file, containing either lower bounds of version or both lower 93 | # and upper bounds. The function then uses the forge rest endpoint to find 94 | # the most recent release of the given module matching the version requirement 95 | def module_version_from_requirement(mod_name, vr_str) 96 | require 'net/http' 97 | uri = URI("#{forge_api}v3/modules/#{mod_name}") 98 | response = Net::HTTP.get_response(uri) 99 | raise "Puppetforge API error '#{uri}': '#{response.body}'" if response.code.to_i >= 400 100 | 101 | forge_data = JSON.parse(response.body) 102 | 103 | vrs = version_requirements_from_string(vr_str) 104 | 105 | # Here we iterate the releases of the given module and pick the most recent 106 | # that matches to version requirement 107 | forge_data['releases'].each do |rel| 108 | return rel['version'] if vrs.all? { |vr| vr.match?('', rel['version']) } 109 | end 110 | 111 | raise "No release version found matching '#{mod_name}' '#{vr_str}'" 112 | end 113 | 114 | # This method takes a version requirement string as specified in the link 115 | # below, with either simply a lower bound, or both lower and upper bounds and 116 | # returns an array of Gem::Dependency objects 117 | # https://docs.puppet.com/puppet/latest/modules_metadata.html 118 | def version_requirements_from_string(vr_str) 119 | ops = vr_str.scan(/[(<|>=)]{1,2}/i) 120 | vers = vr_str.scan(/[(0-9|.)]+/i) 121 | 122 | raise 'Invalid version requirements' if ops.count != 0 && 123 | ops.count != vers.count 124 | 125 | vrs = [] 126 | ops.each_with_index do |op, index| 127 | vrs.push(Gem::Dependency.new('', "#{op} #{vers[index]}")) 128 | end 129 | 130 | vrs 131 | end 132 | 133 | # This method will return array of all masters. If no masters exist, it will 134 | # return all agent nodes. If no nodes tagged master or agent exist, all nodes 135 | # will be returned 136 | def hosts_to_install_module_on 137 | masters = hosts_with_role(hosts, :master) 138 | return masters unless masters.empty? 139 | 140 | agents = hosts_with_role(hosts, :agent) 141 | return agents unless agents.empty? 142 | 143 | hosts 144 | end 145 | 146 | # This method will read the 'name' attribute from metadata.json file and 147 | # remove the first segment. E.g. puppetlabs-vcsrepo -> vcsrepo 148 | def module_name_from_metadata 149 | res = get_module_name module_metadata['name'] 150 | raise 'Error getting module name' unless res 151 | 152 | res[1] 153 | end 154 | 155 | # This method uses the module_source_directory path to read the metadata.json 156 | # file into a json array 157 | def module_metadata 158 | metadata_path = "#{$module_source_dir}/metadata.json" 159 | raise "Error loading metadata.json file from #{$module_source_dir}" unless File.exist?(metadata_path) 160 | 161 | JSON.parse(File.read(metadata_path)) 162 | end 163 | 164 | # Use this property to store the module_source_dir, so we don't traverse 165 | # the tree every time 166 | def get_module_source_directory(call_stack) 167 | matching_caller = call_stack.grep(/(spec_helper_acceptance|_spec)/i) 168 | 169 | raise 'Error finding module source directory' if matching_caller.empty? 170 | 171 | matching_caller = matching_caller[0] if matching_caller.is_a?(Array) 172 | search_in = matching_caller[/[^:]+/] 173 | 174 | module_source_dir = nil 175 | # here we go up the file tree and search the directories for a 176 | # valid metadata.json 177 | while module_source_dir.nil? && search_in != File.dirname(search_in) 178 | # remove last segment (file or folder, doesn't matter) 179 | search_in = File.dirname(search_in) 180 | 181 | # Append metadata.json, check it exists in the directory we're searching 182 | metadata_path = File.join(search_in, 'metadata.json') 183 | module_source_dir = search_in if File.exist?(metadata_path) 184 | end 185 | module_source_dir 186 | end 187 | 188 | def forge_host 189 | fh = ENV['BEAKER_FORGE_HOST'] # rubocop:disable Style/FetchEnvVar 190 | unless fh.nil? 191 | fh = "https://#{fh}" unless %r{^(https://|http://)}i.match?(fh) 192 | fh += '/' unless fh != %r{/$} 193 | return fh 194 | end 195 | 196 | 'https://forge.puppet.com/' 197 | end 198 | 199 | def forge_api 200 | fa = ENV['BEAKER_FORGE_API'] # rubocop:disable Style/FetchEnvVar 201 | unless fa.nil? 202 | fa = "https://#{fa}" unless %r{^(https://|http://)}i.match?(fa) 203 | fa += '/' unless fa != %r{/$} 204 | return fa 205 | end 206 | 207 | 'https://forgeapi.puppetlabs.com/' 208 | end 209 | end 210 | end 211 | 212 | # rubocop:disable Style/MixinUsage 213 | include Beaker::ModuleInstallHelper 214 | # rubocop:enable Style/MixinUsage 215 | # Use the caller (requirer) of this file to begin search for module source dir 216 | $module_source_dir = get_module_source_directory caller 217 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /spec/unit/beaker/module_install_helper_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | describe Beaker::ModuleInstallHelper do 6 | describe 'hosts_to_install_module_on' do 7 | context 'with a split master/agent setup' do 8 | let(:hosts) do 9 | [ 10 | { 'roles' => %w[master database dashboard classifier] }, 11 | { 'roles' => ['agent'] }, 12 | ] 13 | end 14 | 15 | it 'returns a node with master role' do 16 | expect(hosts_to_install_module_on.first['roles']).to include 'master' 17 | end 18 | end 19 | 20 | context 'with a split agent only setup' do 21 | let(:hosts) { [{ 'roles' => ['agent'] }] } 22 | 23 | it 'returns a node with master role' do 24 | expect(hosts_to_install_module_on.first['roles']).to include 'agent' 25 | end 26 | end 27 | end 28 | 29 | describe 'module_name_from_metadata' do 30 | let(:module_metadata) { { 'name' => 'puppetlabs-vcsrepo' } } 31 | 32 | it 'Removes author from name' do 33 | res = module_name_from_metadata 34 | expect(res).to eq('vcsrepo') 35 | end 36 | end 37 | 38 | describe 'module_metadata' do 39 | before do 40 | $module_source_dir = '/a/b/c/d' 41 | allow(File).to receive(:exist?) 42 | .with('/a/b/c/d/metadata.json') 43 | .and_return(true) 44 | allow(File).to receive(:read) 45 | .with('/a/b/c/d/metadata.json') 46 | .and_return('{"name": "puppetlabs-vcsrepo"}') 47 | end 48 | 49 | it 'returns hash with correct data' do 50 | expect(module_metadata['name']).to eq('puppetlabs-vcsrepo') 51 | end 52 | end 53 | 54 | describe 'get_module_source_directory' do 55 | let(:call_stack) { ['/a/b/c/d/e/f/g/spec_helper_acceptance.rb'] } 56 | let(:call_stack_no_metadata) { ['/test/test/test/spec_helper_acceptance.rb'] } 57 | 58 | before do 59 | allow(File).to receive(:exist?).with(anything).and_return(false) 60 | allow(File).to receive(:exist?).with('/a/metadata.json').and_return(true) 61 | end 62 | 63 | it 'traverses file tree until it finds a folder containing metadata.json' do 64 | expect(get_module_source_directory(call_stack)).to eq('/a') 65 | end 66 | 67 | it 'traverses file tree without a metadata.json file' do 68 | expect(get_module_source_directory(call_stack_no_metadata)).to be_nil 69 | end 70 | end 71 | 72 | describe 'install_module_on' do 73 | let(:module_source_dir) { '/a/b/c/d' } 74 | let(:host) { { 'roles' => %w[master database dashboard classifier] } } 75 | 76 | context 'without options' do 77 | before do 78 | $module_source_dir = '/a/b/c/d' 79 | allow(File).to receive(:exist?).and_return(true) 80 | allow(File).to receive(:read).and_return('{"name": "puppetlabs-vcsrepo"}') 81 | 82 | allow_any_instance_of(Beaker::DSL::InstallUtils::ModuleUtils) 83 | .to receive(:copy_module_to) 84 | .with(anything) 85 | .and_return(false) 86 | 87 | allow_any_instance_of(Beaker::DSL::InstallUtils::ModuleUtils) 88 | .to receive(:copy_module_to) 89 | .with(host, source: module_source_dir, module_name: 'vcsrepo') 90 | .and_return(true) 91 | end 92 | 93 | it 'copy module to given host' do 94 | expect(install_module_on(host)).to be true 95 | end 96 | end 97 | 98 | context 'with options' do 99 | before do 100 | $module_source_dir = '/a/b/c/d' 101 | allow(File).to receive(:exist?).and_return(true) 102 | allow(File).to receive(:read).and_return('{"name": "puppetlabs-vcsrepo"}') 103 | 104 | allow_any_instance_of(Beaker::DSL::InstallUtils::ModuleUtils) 105 | .to receive(:copy_module_to) 106 | .with(anything) 107 | .and_return(false) 108 | 109 | allow_any_instance_of(Beaker::DSL::InstallUtils::ModuleUtils) 110 | .to receive(:copy_module_to) 111 | .with(host, source: module_source_dir, module_name: 'vcsrepo', protocol: 'rsync') 112 | .and_return(true) 113 | end 114 | 115 | it 'copy module to given host' do 116 | expect(install_module_on(host, protocol: 'rsync')).to be true 117 | end 118 | end 119 | end 120 | 121 | describe 'module_version_matching_requirement' do 122 | context 'with simple version requirement, no upper bound' do 123 | it 'return latest matching version' do 124 | res = module_version_from_requirement('puppetlabs-ntp', '= 6.0.0') 125 | expect(res).to eql('6.0.0') 126 | end 127 | end 128 | 129 | context 'with version range requirement with an upper bound' do 130 | it 'return latest matching version' do 131 | res = module_version_from_requirement('puppetlabs-ntp', 132 | '>= 4.0.0 < 6.0.0') 133 | expect(res).to eql('5.0.0') 134 | end 135 | end 136 | end 137 | 138 | describe 'module_dependencies_from_metadata' do 139 | before do 140 | allow_any_instance_of(described_class) 141 | .to receive(:module_metadata) 142 | .and_return(input_metadata) 143 | end 144 | 145 | context 'with multiple dependencies with versions' do 146 | let(:input_metadata) do 147 | { 148 | 'name' => 'puppetlabs-vcsrepo', 149 | 'dependencies' => [ 150 | { 151 | 'name' => 'puppetlabs/stdlib', 152 | 'version_requirement' => '>= 4.13.1 <= 4.14.0', 153 | }, { 154 | 'name' => 'puppetlabs/concat', 155 | 'version_requirement' => '>= 2.0.0 <= 2.2.0', 156 | }, 157 | ], 158 | } 159 | end 160 | 161 | let(:desired) do 162 | [ 163 | { module_name: 'puppetlabs-stdlib', version: '4.14.0' }, 164 | { module_name: 'puppetlabs-concat', version: '2.2.0' }, 165 | ] 166 | end 167 | 168 | it 'returns dependencies array with 2 dependencies and their versions' do 169 | dependencies = module_dependencies_from_metadata 170 | expect(dependencies).to eq(desired) 171 | end 172 | end 173 | 174 | context 'with multiple dependencies without versions' do 175 | let(:input_metadata) do 176 | { 177 | 'name' => 'puppetlabs-vcsrepo', 178 | 'dependencies' => [ 179 | { 'name' => 'puppetlabs/stdlib' }, 180 | { 'name' => 'puppetlabs/concat' }, 181 | ], 182 | } 183 | end 184 | 185 | let(:desired) do 186 | [ 187 | { module_name: 'puppetlabs-stdlib' }, 188 | { module_name: 'puppetlabs-concat' }, 189 | ] 190 | end 191 | 192 | it 'returns dependencies array with 2 dependencies without version' do 193 | dependencies = module_dependencies_from_metadata 194 | expect(dependencies).to eq(desired) 195 | end 196 | end 197 | 198 | context 'with empty dependencies' do 199 | let(:input_metadata) do 200 | { 201 | 'name' => 'puppetlabs-vcsrepo', 202 | 'dependencies' => [], 203 | } 204 | end 205 | let(:desired) { [] } 206 | 207 | it 'returns empty dependencies array' do 208 | dependencies = module_dependencies_from_metadata 209 | expect(dependencies).to eq(desired) 210 | end 211 | end 212 | 213 | context 'with no dependencies' do 214 | let(:input_metadata) { { 'name' => 'puppetlabs-vcsrepo' } } 215 | let(:desired) { [] } 216 | 217 | it 'returns empty dependencies array' do 218 | dependencies = module_dependencies_from_metadata 219 | expect(dependencies).to eq(desired) 220 | end 221 | end 222 | end 223 | 224 | describe 'version_requirements_from_string' do 225 | context 'with simple version requirement containing lower bound' do 226 | let(:lower_bound) { '>= 2.0.0' } 227 | 228 | it 'return array with single gem version dependency objects' do 229 | res = version_requirements_from_string(lower_bound) 230 | expect(res).to eql([Gem::Dependency.new('', lower_bound)]) 231 | end 232 | end 233 | 234 | context 'with complex version requirement containing upper bounds' do 235 | let(:lower_bound) { '>= 2.0.0' } 236 | let(:upper_bound) { '< 3.0.0' } 237 | 238 | it 'return array with 2 gem version dependency objects' do 239 | res = version_requirements_from_string("#{lower_bound} #{upper_bound}") 240 | expect(res).to eql([Gem::Dependency.new('', lower_bound), 241 | Gem::Dependency.new('', upper_bound),]) 242 | end 243 | end 244 | end 245 | 246 | describe 'forge_host' do 247 | context 'without env variables specified' do 248 | it 'returns production forge host' do 249 | allow(ENV).to receive(:[]).with('BEAKER_FORGE_HOST').and_return(nil) 250 | 251 | expect(forge_host).to eq('https://forge.puppet.com/') 252 | end 253 | end 254 | 255 | context 'with BEAKER_FORGE_HOST env variable specified' do 256 | it 'returns specified forge host' do 257 | allow(ENV).to receive(:[]).with('BEAKER_FORGE_HOST').and_return('http://anotherhost1.com') 258 | 259 | expect(forge_host).to eq('http://anotherhost1.com') 260 | end 261 | end 262 | end 263 | 264 | describe 'forge_api' do 265 | context 'without env variables specified' do 266 | it 'returns production forge api' do 267 | allow(ENV).to receive(:[]).with('BEAKER_FORGE_HOST').and_return(nil) 268 | allow(ENV).to receive(:[]).with('BEAKER_FORGE_API').and_return(nil) 269 | 270 | expect(forge_api).to eq('https://forgeapi.puppetlabs.com/') 271 | end 272 | end 273 | 274 | context 'with BEAKER_FORGE_HOST and BEAKER_FORGE_API env variables specified' do 275 | it 'returns specified forge api with trailing slash' do 276 | allow(ENV).to receive(:[]).with('BEAKER_FORGE_HOST').and_return('custom') 277 | allow(ENV).to receive(:[]).with('BEAKER_FORGE_API').and_return('an-api-url/') 278 | 279 | expect(forge_api).to eq('https://an-api-url/') 280 | end 281 | end 282 | end 283 | 284 | describe 'install_module_dependencies_on' do 285 | before do 286 | allow_any_instance_of(described_class) 287 | .to receive(:module_metadata) 288 | .and_return(input_metadata) 289 | end 290 | 291 | context 'with 1 dependencies with version' do 292 | let(:a_host) { { name: 'a_host' } } 293 | let(:dependency) do 294 | { module_name: 'puppetlabs-stdlib', version: '4.14.0' } 295 | end 296 | let(:input_metadata) do 297 | { 298 | 'name' => 'puppetlabs-vcsrepo', 299 | 'dependencies' => [ 300 | { 301 | 'name' => 'puppetlabs/stdlib', 302 | 'version_requirement' => '>= 4.13.1 <= 4.14.0', 303 | }, 304 | ], 305 | } 306 | end 307 | 308 | it 'installs the modules' do 309 | expect_any_instance_of(Beaker::DSL::InstallUtils::ModuleUtils) 310 | .to receive(:install_puppet_module_via_pmt_on) 311 | .with(a_host, dependency) 312 | .exactly(1) 313 | 314 | install_module_dependencies_on(a_host) 315 | end 316 | end 317 | 318 | context 'with 2 dependencies without version' do 319 | let(:input_metadata) do 320 | { 321 | 'name' => 'puppetlabs-vcsrepo', 322 | 'dependencies' => [ 323 | { 'name' => 'puppetlabs/stdlib' }, 324 | { 'name' => 'puppetlabs/concat' }, 325 | ], 326 | } 327 | end 328 | let(:a_host) { { name: 'a_host' } } 329 | let(:dependency1) { { module_name: 'puppetlabs-concat' } } 330 | let(:dependency2) { { module_name: 'puppetlabs-stdlib' } } 331 | 332 | it 'installs both modules' do # rubocop:disable RSpec/ExampleLength,RSpec/MultipleExpectations 333 | expect_any_instance_of(Beaker::DSL::InstallUtils::ModuleUtils) 334 | .to receive(:install_puppet_module_via_pmt_on) 335 | .with(a_host, dependency1) 336 | .exactly(1) 337 | 338 | expect_any_instance_of(Beaker::DSL::InstallUtils::ModuleUtils) 339 | .to receive(:install_puppet_module_via_pmt_on) 340 | .with(a_host, dependency2) 341 | .exactly(1) 342 | 343 | install_module_dependencies_on(a_host) 344 | end 345 | end 346 | end 347 | 348 | describe 'install_module_from_forge_on' do 349 | let(:a_host) { { name: 'a_host' } } 350 | let(:dependency) { { module_name: 'puppetlabs-stdlib', version: '4.14.0' } } 351 | let(:input_module_name) { 'puppetlabs/stdlib' } 352 | let(:input_module_version_requirement) { '>= 4.13.1 <= 4.14.0' } 353 | 354 | it 'installs the module' do 355 | expect_any_instance_of(Beaker::DSL::InstallUtils::ModuleUtils) 356 | .to receive(:install_puppet_module_via_pmt_on) 357 | .with(a_host, dependency) 358 | .exactly(1) 359 | 360 | install_module_from_forge_on(a_host, input_module_name, input_module_version_requirement) 361 | end 362 | end 363 | 364 | describe 'module_version_from_requirement' do 365 | context 'when looking up resolvable version contraints for a valid module' do 366 | it 'gets a response', live_fire: true do 367 | ver = module_version_from_requirement('puppetlabs-vcsrepo', '>= 1 < 2') 368 | expect(ver).to eq '1.5.0' 369 | end 370 | end 371 | 372 | context 'when looking up *unresolvable* version contraints for a valid module' do 373 | it 'gets a response', live_fire: true do 374 | expect do 375 | module_version_from_requirement('puppetlabs-vcsrepo', '> 1.4 < 1.5') 376 | end.to raise_error(/^No release version found matching 'puppetlabs-vcsrepo' '> 1.4 < 1.5'/) 377 | end 378 | end 379 | 380 | context 'when looking up metadata for a *invalid* module name' do 381 | it 'gets a response', live_fire: true do 382 | expect do 383 | module_version_from_requirement('puppet-does-not-exist', '>= 1 < 2') 384 | end.to raise_error(/^Puppetforge API error/) 385 | end 386 | end 387 | end 388 | end 389 | --------------------------------------------------------------------------------