Skip to content

Commit 4553bb2

Browse files
author
jordanbreen28
committed
(CAT-1984) - Add forge auth to fixtures module install
Prior to this commit, users were unable to perform the install of modules which required forge authorization. This change introduces the ability to know install modules behind authorization, by referencing an env variable set by the user, and appending this onto the `puppet module install` operation if present.
1 parent 6e600d1 commit 4553bb2

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,31 @@
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(File).to receive(:exist?).with('.fixtures.yml').and_return true
180+
allow(YAML).to receive(:load_file).with('.fixtures.yml').and_return('fixtures' => { 'forge_modules' => { 'stdlib' => 'puppetlabs-stdlib' } })
181+
allow(ENV).to receive(:fetch).with('FORGE_API_KEY', nil).and_return('myforgeapikey')
182+
# Mock the system call to prevent actual execution
183+
allow_any_instance_of(Kernel).to receive(:system) do |command| # rubocop:disable RSpec/AnyInstance
184+
expect(command).to include('--forge_authorization "Bearer myforgeapikey"')
185+
# Simulate setting $CHILD_STATUS to a successful status
186+
allow($CHILD_STATUS).to receive(:success?).and_return(true)
187+
true
188+
end
189+
helper.download_module('puppetlabs-stdlib', 'target' => 'spec/fixtures/modules/stdlib')
190+
end
191+
end
192+
168193
context 'when file specifies repository fixtures' do
169194
before do
170195
allow(File).to receive(:exist?).with('.fixtures.yml').and_return true

0 commit comments

Comments
 (0)