Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(CONT-722) Patch pe to puppet mapping #1221

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/pdk/util/puppet_version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'pdk'
require 'json'
require 'forwardable'

module PDK
module Util
Expand Down Expand Up @@ -105,7 +106,6 @@ def find_gem_for(version_str)
requirement_string = version.approximate_recommendation
requirement_string += '.0' unless version.segments.length == 1
latest_requirement = Gem::Requirement.create(requirement_string)

latest_available_gem = find_gem(latest_requirement)

if latest_available_gem.nil?
Expand All @@ -128,10 +128,16 @@ def find_gem_for(version_str)
def from_pe_version(version_str)
version = parse_specified_version(version_str)

gem_version = pe_version_map.find do |version_map|
version_map[:requirement].satisfied_by?(version)
end
# Due to the issue with concurrent ruby in older puppet gems
# we are locking the pe to puppet version mapping to the latest
# puppet version that is compatible with the pe version.
safe_versions = {
2023 => '7.23.0',
2021 => '7.23.0',
2019 => '6.29.0',
}

gem_version = safe_versions[version.segments[0]]
if gem_version.nil?
raise ArgumentError, 'Unable to map Puppet Enterprise version %{pe_version} to a Puppet version.' % {
pe_version: version_str,
Expand All @@ -140,10 +146,10 @@ def from_pe_version(version_str)

PDK.logger.info 'Puppet Enterprise %{pe_version} maps to Puppet %{puppet_version}.' % {
pe_version: version_str,
puppet_version: gem_version[:gem_version],
puppet_version: gem_version,
}

find_gem_for(gem_version[:gem_version])
find_gem_for(gem_version)
end

def from_module_metadata(metadata = nil)
Expand Down
65 changes: 26 additions & 39 deletions spec/unit/pdk/cli/console_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,8 @@

let(:rubygems_versions) do
%w[
5.4.0
5.3.5 5.3.4 5.3.3 5.3.2 5.3.1 5.3.0
5.2.0
5.1.0
5.0.1 5.0.0
4.10.10 4.10.9 4.10.8 4.10.7 4.10.6 4.10.5 4.10.4 4.10.1 4.10.0
4.9.4 4.9.3 4.9.2 4.9.1 4.9.0
4.8.2 4.8.1 4.8.0
4.7.1 4.7.0
4.6.2 4.6.1 4.6.0
4.5.3 4.5.2 4.5.1 4.5.0
4.4.2 4.4.1 4.4.0
4.3.2 4.3.1 4.3.0
4.2.3 4.2.2 4.2.1 4.2.0
7.23.0
6.29.0
]
end

Expand All @@ -59,15 +47,15 @@
allow(PDK::Util).to receive(:in_module_root?).and_return(true)
allow(PDK::Util::RubyVersion).to receive(:available_puppet_versions).and_return(versions)
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).and_return(true)
allow(PDK::CLI::Exec).to receive(:bundle_bin).and_return('/pdk/lib/pdk/util/private/ruby/2.4.4/bin/bundle')
allow(PDK::Util::PuppetVersion).to receive(:find_in_package_cache).and_return(gem_version: '6.4.0', ruby_version: '2.5.3')
allow(PDK::CLI::Exec).to receive(:bundle_bin).and_return('/pdk/lib/pdk/util/private/ruby/2.7.7/bin/bundle')
allow(PDK::Util::PuppetVersion).to receive(:find_in_package_cache).and_return(gem_version: '7.23.0', ruby_version: '2.7.7')
end

it 'invokes console with options' do
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '5.4.0')
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '7.23.0')
command = instance_double(PDK::CLI::Exec::InteractiveCommand)
allow(command).to receive(:context=).with(:pwd)
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '5.4.0')
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '7.23.0')
allow(command).to receive(:execute!).and_return(exit_code: 0)
allow(PDK::Util).to receive(:module_root).and_return('/modules/ntp')
args = [
Expand All @@ -77,7 +65,7 @@
'--modulepath=/modules/ntp/spec/fixtures/modules:/modules/ntp/modules'
]
expect(PDK::CLI::Exec::InteractiveCommand).to receive(:new).with(*args).and_return(command)
expect { console_cmd.run(['--puppet-version=5', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
expect { console_cmd.run(['--puppet-version=7', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
end
end

Expand All @@ -90,10 +78,10 @@
include_context 'not packaged install'

it 'invokes console with options' do
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '5.4.0')
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '7.23.0')
command = instance_double(PDK::CLI::Exec::InteractiveCommand)
allow(command).to receive(:context=).with(:pwd)
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '5.4.0')
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '7.23.0')
allow(command).to receive(:execute!).and_return(exit_code: 0)
allow(PDK::Util).to receive(:module_root).and_return('/modules/ntp')
args = [
Expand All @@ -102,7 +90,7 @@
'--modulepath=/modules/ntp/spec/fixtures/modules:/modules/ntp/modules'
]
expect(PDK::CLI::Exec::InteractiveCommand).to receive(:new).with(*args).and_return(command)
expect { console_cmd.run(['--puppet-version=5', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
expect { console_cmd.run(['--puppet-version=7', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
end
end

Expand All @@ -123,34 +111,33 @@
end

it 'can pass --puppet-version' do
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '5.4.0')
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '7.23.0')
command = instance_double(PDK::CLI::Exec::InteractiveCommand)
allow(command).to receive(:context=).with(:pwd)
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '5.4.0')
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '7.23.0')
allow(command).to receive(:execute!).and_return(exit_code: 0)
allow(PDK::CLI::Exec::InteractiveCommand).to receive(:new).and_return(command)
expect { console_cmd.run(['--puppet-version=5', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
expect { console_cmd.run(['--puppet-version=7', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
end

it 'can pass --pe-version' do
allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).with({ :"pe-version" => '2018.1' }, true)
.and_return(gemset: { puppet: '5.5.10' }, ruby_version: '2.5.3')
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '5.5.10')
allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).with({ :"pe-version" => '2023.0' }, true).and_return(gemset: { puppet: '7.23.0' }, ruby_version: '2.7.7')
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '7.23.0')
command = instance_double(PDK::CLI::Exec::InteractiveCommand)
allow(PDK::Util::RubyVersion).to receive(:versions).and_return('2.5.3' => '2.5.0')
allow(PDK::Util::RubyVersion).to receive(:versions).and_return('2.7.7' => '2.7.0')
allow(command).to receive(:context=).with(:pwd)
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '5.5.10')
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '7.23.0')
allow(command).to receive(:execute!).and_return(exit_code: 0)
allow(PDK::CLI::Exec::InteractiveCommand).to receive(:new).and_return(command)
expect { console_cmd.run(['--pe-version=2018.1', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
expect { console_cmd.run(['--pe-version=2023.0', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
end

it 'can pass --puppet-dev' do
allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).with({ :"puppet-dev" => '' }, true)
.and_return(gemset: { puppet: 'file:///home/user1/.pdk/cache/src/puppet' }, ruby_version: '2.5.3')
.and_return(gemset: { puppet: 'file:///home/user1/.pdk/cache/src/puppet' }, ruby_version: '2.7.7')
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: 'file:///home/user1/.pdk/cache/src/puppet')
command = instance_double(PDK::CLI::Exec::InteractiveCommand)
allow(PDK::Util::RubyVersion).to receive(:versions).and_return('2.5.3' => '2.5.0')
allow(PDK::Util::RubyVersion).to receive(:versions).and_return('2.7.7' => '2.7.0')
allow(command).to receive(:context=).with(:pwd)
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => 'file:///home/user1/.pdk/cache/src/puppet')
allow(command).to receive(:execute!).and_return(exit_code: 0)
Expand All @@ -160,10 +147,10 @@

describe 'windows', if: OS.windows? do
it 'can pass debugger options on windows' do
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '5.4.0')
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '7.23.0')
command = instance_double(PDK::CLI::Exec::InteractiveCommand)
allow(command).to receive(:context=).with(:pwd)
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '5.4.0')
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '7.23.0')
allow(command).to receive(:execute!).and_return(exit_code: 0)
allow(PDK::Util).to receive(:module_root).and_return('C:/modules/ntp')
args = [
Expand All @@ -172,15 +159,15 @@
'--modulepath=C:/modules/ntp/spec/fixtures/modules:C:/modules/ntp/modules'
]
expect(PDK::CLI::Exec::InteractiveCommand).to receive(:new).with(*args).and_return(command)
expect { console_cmd.run(['--puppet-version=5', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
expect { console_cmd.run(['--puppet-version=7', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
end
end

it 'can pass debugger options on linux' do
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '5.4.0')
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(puppet: '7.23.0')
command = instance_double(PDK::CLI::Exec::InteractiveCommand)
allow(command).to receive(:context=).with(:pwd)
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '5.4.0')
allow(command).to receive(:update_environment).with('PUPPET_GEM_VERSION' => '7.23.0')
allow(command).to receive(:execute!).and_return(exit_code: 0)
allow(PDK::Util).to receive(:module_root).and_return('/modules/ntp')
args = [
Expand All @@ -189,7 +176,7 @@
'--modulepath=/modules/ntp/spec/fixtures/modules:/modules/ntp/modules'
]
expect(PDK::CLI::Exec::InteractiveCommand).to receive(:new).with(*args).and_return(command)
expect { console_cmd.run(['--puppet-version=5', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
expect { console_cmd.run(['--puppet-version=7', '--run-once', '--quiet', '--execute=$foo = 123']) }.to exit_zero
end
end
end
Loading