Skip to content

Commit a2e1384

Browse files
Merge pull request #473 from puppetlabs/feat-add_forge_api_key_fixtures
(CAT-1984) - Add forge auth to fixtures module install
2 parents 6e600d1 + f9d6633 commit a2e1384

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@ When specifying the repo source of the fixture you have a few options as to whic
211211
puppet_version: '>= 6.0.0'
212212
```
213213

214+
Using Forge Authorization
215+
==============
216+
217+
In order to perform forge operations which required authorization, such as installing premium modules, you can export your forge api key as an environment variable in your terminal.
218+
219+
```bash
220+
FORGE_API_KEY='your_api_key'
221+
```
222+
223+
puppetlabs_spec_helper will then automatically append this key to all `puppet module install` requests when running `rake spec_prep`.
224+
214225
**Notes:**
215226

216227
* `ref` and `branch` can be used together to get a specific revision on a specific branch

lib/puppetlabs_spec_helper/tasks/fixtures.rb

+3
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ def download_module(remote, opts)
379379
flags = " #{opts['flags']}" if opts['flags']
380380
end
381381

382+
forge_token = ENV.fetch('FORGE_API_KEY', nil)
383+
flags += " --forge_authorization \"Bearer #{forge_token}\"" if forge_token
384+
382385
return false if File.directory?(target) && (ref.empty? || opts['ref'] == module_version(target))
383386

384387
# The PMT cannot handle multi threaded runs due to cache directory collisons

spec/unit/puppetlabs_spec_helper/tasks/fixture_helpers_spec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,29 @@
165165
end
166166
end
167167

168+
context 'when forge_api_key env variable is set' do
169+
before do
170+
# required to prevent unwanted output on stub of $CHILD_STATUS
171+
RSpec::Mocks.configuration.allow_message_expectations_on_nil = true
172+
end
173+
174+
after do
175+
RSpec::Mocks.configuration.allow_message_expectations_on_nil = false
176+
end
177+
178+
it 'correctly sets --forge_authorization' do
179+
allow(ENV).to receive(:fetch).with('FORGE_API_KEY', nil).and_return('myforgeapikey')
180+
# Mock the system call to prevent actual execution
181+
allow_any_instance_of(Kernel).to receive(:system) do |command| # rubocop:disable RSpec/AnyInstance
182+
expect(command).to include('--forge_authorization "Bearer myforgeapikey"')
183+
# Simulate setting $CHILD_STATUS to a successful status
184+
allow($CHILD_STATUS).to receive(:success?).and_return(true)
185+
true
186+
end
187+
helper.download_module('puppetlabs-stdlib', 'target' => 'spec/fixtures/modules/stdlib')
188+
end
189+
end
190+
168191
context 'when file specifies repository fixtures' do
169192
before do
170193
allow(File).to receive(:exist?).with('.fixtures.yml').and_return true

0 commit comments

Comments
 (0)