Skip to content

Commit

Permalink
Merge pull request #473 from puppetlabs/feat-add_forge_api_key_fixtures
Browse files Browse the repository at this point in the history
(CAT-1984) - Add forge auth to fixtures module install
  • Loading branch information
jordanbreen28 authored Sep 16, 2024
2 parents 6e600d1 + f9d6633 commit a2e1384
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ When specifying the repo source of the fixture you have a few options as to whic
puppet_version: '>= 6.0.0'
```

Using Forge Authorization
==============

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.

```bash
FORGE_API_KEY='your_api_key'
```

puppetlabs_spec_helper will then automatically append this key to all `puppet module install` requests when running `rake spec_prep`.

**Notes:**

* `ref` and `branch` can be used together to get a specific revision on a specific branch
Expand Down
3 changes: 3 additions & 0 deletions lib/puppetlabs_spec_helper/tasks/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ def download_module(remote, opts)
flags = " #{opts['flags']}" if opts['flags']
end

forge_token = ENV.fetch('FORGE_API_KEY', nil)
flags += " --forge_authorization \"Bearer #{forge_token}\"" if forge_token

return false if File.directory?(target) && (ref.empty? || opts['ref'] == module_version(target))

# The PMT cannot handle multi threaded runs due to cache directory collisons
Expand Down
23 changes: 23 additions & 0 deletions spec/unit/puppetlabs_spec_helper/tasks/fixture_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,29 @@
end
end

context 'when forge_api_key env variable is set' do
before do
# required to prevent unwanted output on stub of $CHILD_STATUS
RSpec::Mocks.configuration.allow_message_expectations_on_nil = true
end

after do
RSpec::Mocks.configuration.allow_message_expectations_on_nil = false
end

it 'correctly sets --forge_authorization' do
allow(ENV).to receive(:fetch).with('FORGE_API_KEY', nil).and_return('myforgeapikey')
# Mock the system call to prevent actual execution
allow_any_instance_of(Kernel).to receive(:system) do |command| # rubocop:disable RSpec/AnyInstance
expect(command).to include('--forge_authorization "Bearer myforgeapikey"')
# Simulate setting $CHILD_STATUS to a successful status
allow($CHILD_STATUS).to receive(:success?).and_return(true)
true
end
helper.download_module('puppetlabs-stdlib', 'target' => 'spec/fixtures/modules/stdlib')
end
end

context 'when file specifies repository fixtures' do
before do
allow(File).to receive(:exist?).with('.fixtures.yml').and_return true
Expand Down

0 comments on commit a2e1384

Please # to comment.