-
Notifications
You must be signed in to change notification settings - Fork 5
Dispatch integration tests when deploying to heroku test env #971
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
Merged
+167
−3
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| web: bundle exec puma -C config/puma.rb | ||
| release: bundle exec rails db:migrate && bundle exec rake projects:create_experience_cs_examples | ||
| release: bundle exec rails db:migrate && bundle exec rake projects:create_experience_cs_examples integration_tests:dispatch | ||
| worker: bundle exec good_job start --max-threads=8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| namespace :integration_tests do | ||
| desc 'Trigger the learner-experience-integration-tests GitHub Actions workflow against the deployed test environment' | ||
| task dispatch: :environment do | ||
| next unless on_test_app? | ||
|
|
||
| response = connection.post(dispatch_url, dispatch_payload) | ||
| raise "GitHub dispatch API returned status #{response.status}" unless response.status == 204 | ||
| rescue StandardError => e | ||
| Sentry.capture_exception(e) | ||
| end | ||
|
|
||
| def dispatch_url | ||
| 'https://api.github.com/repos/RaspberryPiFoundation/learner-experience-integration-tests/dispatches' | ||
| end | ||
|
|
||
| def on_test_app? | ||
| ENV.fetch('HEROKU_APP_NAME') == 'editor-api-test' | ||
| end | ||
|
|
||
| def connection | ||
| Faraday.new do |faraday| | ||
| faraday.request :json | ||
| faraday.headers = { | ||
| 'Accept' => 'application/vnd.github+json', | ||
| 'Authorization' => "Bearer #{ENV.fetch('LEARNER_EXPERIENCE_TESTS_DISPATCH_TOKEN')}" | ||
| } | ||
| end | ||
| end | ||
|
zetter-rpf marked this conversation as resolved.
|
||
|
|
||
| def dispatch_payload | ||
| sha = ENV.fetch('HEROKU_SLUG_COMMIT') | ||
|
|
||
| { | ||
| event_type: 'learner-experience-test', | ||
| client_payload: { | ||
| repo: 'editor-api', | ||
| sha:, | ||
| ref: 'main', | ||
| commit_message: "https://github.com/RaspberryPiFoundation/editor-api/commit/#{sha}", | ||
| deploy_url: ENV.fetch('HOST_URL'), | ||
| triggered_at: Time.now.utc.iso8601 | ||
| } | ||
| } | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
| require 'rake' | ||
|
|
||
| RSpec.describe 'integration_tests', type: :task do | ||
| describe ':dispatch' do | ||
| let(:task) { Rake::Task['integration_tests:dispatch'] } | ||
| let(:dispatch_url) { 'https://api.github.com/repos/RaspberryPiFoundation/learner-experience-integration-tests/dispatches' } | ||
| let(:heroku_app_name) { 'editor-api-test' } | ||
| let(:host_url) { 'https://test-editor-api.raspberrypi.org' } | ||
| let(:sha) { SecureRandom.hex(20) } | ||
| let(:token) { 'dispatch-token' } | ||
| let(:env) do | ||
| { | ||
| 'HEROKU_APP_NAME' => heroku_app_name, | ||
| 'HEROKU_SLUG_COMMIT' => sha, | ||
| 'HOST_URL' => host_url, | ||
| 'LEARNER_EXPERIENCE_TESTS_DISPATCH_TOKEN' => token | ||
| } | ||
| end | ||
|
|
||
| around { |example| ClimateControl.modify(env) { example.run } } | ||
|
|
||
| before do | ||
| stub_request(:post, dispatch_url).to_return(status: 204) | ||
| end | ||
|
|
||
| context 'when HEROKU_APP_NAME is editor-api-test' do | ||
| it 'dispatches the workflow with the expected payload and auth header' do | ||
| task.invoke | ||
|
|
||
| expect(WebMock).to have_requested(:post, dispatch_url) | ||
| .with( | ||
| headers: { | ||
| 'Authorization' => "Bearer #{token}", | ||
| 'Accept' => 'application/vnd.github+json' | ||
| }, | ||
| body: { | ||
| event_type: 'learner-experience-test', | ||
| client_payload: { | ||
| repo: 'editor-api', | ||
| sha:, | ||
| ref: 'main', | ||
| commit_message: "https://github.com/RaspberryPiFoundation/editor-api/commit/#{sha}", | ||
| deploy_url: host_url, | ||
| triggered_at: /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\z/ | ||
| } | ||
| } | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| context 'when HEROKU_APP_NAME is not editor-api-test' do | ||
| let(:heroku_app_name) { 'editor-api-staging' } | ||
|
|
||
| it 'does not dispatch the workflow' do | ||
| task.invoke | ||
|
|
||
| expect(WebMock).not_to have_requested(:post, dispatch_url) | ||
| end | ||
| end | ||
|
zetter-rpf marked this conversation as resolved.
|
||
|
|
||
| context 'when a required env var is missing' do | ||
| let(:env) { super().merge('HEROKU_SLUG_COMMIT' => nil) } | ||
|
|
||
| it 'does not raise' do | ||
| expect { task.invoke }.not_to raise_error | ||
| end | ||
|
|
||
| it 'reports the error to Sentry' do | ||
| allow(Sentry).to receive(:capture_exception) | ||
|
|
||
| task.invoke | ||
|
|
||
| expect(Sentry).to have_received(:capture_exception).with(an_instance_of(KeyError)) | ||
| end | ||
| end | ||
|
|
||
| context 'when the request fails' do | ||
| before do | ||
| stub_request(:post, dispatch_url).to_return(status: 500) | ||
| end | ||
|
|
||
| it 'does not raise' do | ||
| expect { task.invoke }.not_to raise_error | ||
| end | ||
|
|
||
| it 'reports the error to Sentry' do | ||
| allow(Sentry).to receive(:capture_exception) | ||
|
|
||
| task.invoke | ||
|
|
||
| expect(Sentry).to have_received(:capture_exception).with(an_instance_of(RuntimeError)) | ||
| end | ||
| end | ||
|
|
||
| context 'when an unexpected error is raised' do | ||
| before do | ||
| allow_any_instance_of(Faraday::Connection).to receive(:post).and_raise(Faraday::ConnectionFailed, 'boom') # rubocop:disable RSpec/AnyInstance | ||
| end | ||
|
|
||
| it 'does not raise' do | ||
| expect { task.invoke }.not_to raise_error | ||
| end | ||
|
|
||
| it 'reports the error to Sentry' do | ||
| allow(Sentry).to receive(:capture_exception) | ||
|
|
||
| task.invoke | ||
|
|
||
| expect(Sentry).to have_received(:capture_exception).with(an_instance_of(Faraday::ConnectionFailed)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.