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

(CAT-1984) - Add forge auth to fixtures module install #473

Merged
merged 1 commit into from
Sep 16, 2024
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
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