Skip to content

Commit 4c9d322

Browse files
!feature
Add puppetcore switch to Gemfile. This ensures that when the PUPPET_FORGE_TOKEN is set, then the puppetcore gems are used. If this variable is not set, the bundle install will still work as normal with the public puppet and facter gems. NOTE, however, that the corresponding tests will only pass if the puppetcore gems are present on CI and used. Signed-off-by: Gavin Didrichsen <[email protected]>
1 parent 12904c3 commit 4c9d322

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

Gemfile

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,20 @@ facter_version = ENV['FACTER_GEM_VERSION']
5252
hiera_version = ENV['HIERA_GEM_VERSION']
5353

5454
gems = {}
55+
puppet_version = ENV.fetch('PUPPET_GEM_VERSION', nil)
56+
facter_version = ENV.fetch('FACTER_GEM_VERSION', nil)
57+
hiera_version = ENV.fetch('HIERA_GEM_VERSION', nil)
5558

56-
gems['puppet'] = location_for(puppet_version)
57-
58-
# If facter or hiera versions have been specified via the environment
59-
# variables
59+
# If PUPPET_FORGE_TOKEN is set then use authenticated source for both puppet and facter, since facter is a transitive dependency of puppet
60+
# Otherwise, do as before and use location_for to fetch gems from the default source
61+
if !ENV['PUPPET_FORGE_TOKEN'].to_s.empty?
62+
gems['puppet'] = ['~> 8.11', { require: false, source: 'https://rubygems-puppetcore.puppet.com' }]
63+
gems['facter'] = ['~> 4.11', { require: false, source: 'https://rubygems-puppetcore.puppet.com' }]
64+
else
65+
gems['puppet'] = location_for(puppet_version)
66+
gems['facter'] = location_for(facter_version) if facter_version
67+
end
6068

61-
gems['facter'] = location_for(facter_version) if facter_version
6269
gems['hiera'] = location_for(hiera_version) if hiera_version
6370

6471
gems.each do |gem_name, gem_params|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'bundler'
5+
6+
RSpec.describe 'Gemfile.lock verification' do
7+
let(:parser) { Bundler::LockfileParser.new(Bundler.read_file(Bundler.default_lockfile)) }
8+
let(:private_source) { 'https://rubygems-puppetcore.puppet.com/' }
9+
10+
# Helper method to get source remotes for a specific gem
11+
def get_gem_source_remotes(gem_name)
12+
spec = parser.specs.find { |s| s.name == gem_name }
13+
return [] unless spec
14+
15+
source = spec.source
16+
return [] unless source.is_a?(Bundler::Source::Rubygems)
17+
18+
source.remotes.map(&:to_s)
19+
end
20+
21+
context 'when the environment is configured with a valid PUPPET_FORGE_TOKEN' do
22+
it 'returns puppet from puppetcore' do
23+
remotes = get_gem_source_remotes('puppet')
24+
expect(remotes).to eq([private_source]),
25+
"Expected puppet to come from puppetcore, got: #{remotes.join(', ')}"
26+
end
27+
28+
it 'returns facter from puppetcore' do
29+
remotes = get_gem_source_remotes('facter')
30+
expect(remotes).to eq([private_source]),
31+
"Expected facter to come from puppetcore, got: #{remotes.join(', ')}"
32+
end
33+
34+
it 'has PUPPET_FORGE_TOKEN set' do
35+
expect(ENV.fetch('PUPPET_FORGE_TOKEN', nil)).not_to be_nil,
36+
'Expected PUPPET_FORGE_TOKEN to be set'
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)