Skip to content

Commit

Permalink
Push canvas-as-a-consumer http contracts to pact broker
Browse files Browse the repository at this point in the history
flag=none
closes OUT-3903

Note: Pact and FSC don't play nicely when generating contracts
since Pact thinks that a new FSC run of a request+state pair
is trying to override something unintentionally

FSC_REPEAT_FACTOR='1'

Test-plan
 - verify that contract tests run
 - (post-merge): verify that contracts are pushed to inseng broker

Change-Id: Ic856bf6b506a00a921e9b1dd6add6dc1bdd105bd
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/246265
Tested-by: Service Cloud Jenkins <[email protected]>
Reviewed-by: Aaron Ogata <[email protected]>
Reviewed-by: Augusto Callejas <[email protected]>
Reviewed-by: Michael Brewer-Davis <[email protected]>
QA-Review: Brian Watson <[email protected]>
Product-Review: Brian Watson <[email protected]>
  • Loading branch information
brianlwatson committed Aug 31, 2020
1 parent ab2ea25 commit 0cb4e64
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 18 deletions.
19 changes: 17 additions & 2 deletions Jenkinsfile.contract-tests
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def cleanupFn(status) {
reportFiles: htmlFiles.join(','),
reportName: 'Test Results'
])
} else {
if(configuration.isChangeMerged()) {
sh 'build/new-jenkins/pact/contracts-publish-api.sh'
}
}
} finally {
sh 'rm -vrf spec_results/'
Expand All @@ -55,8 +59,8 @@ pipeline {
}

environment {
COMPOSE_FILE = 'docker-compose.new-jenkins.yml'
DOCKER_PROCESSES = '6'
COMPOSE_FILE = 'docker-compose.new-jenkins.yml:docker-compose.new-jenkins-contract-tests.yml'
DOCKER_PROCESSES = '7'
POSTGRES_PASSWORD = 'sekret'
PACT_BROKER = credentials('PACT_BROKER')
PACT_BROKER_USERNAME="${env.PACT_BROKER_USR}"
Expand Down Expand Up @@ -150,6 +154,17 @@ pipeline {
}
}

stages['Outcomes'] = {
timeout(time: 5) {
withEnv([
"DATABASE_NAME=pact_test6"
]) {
sh 'build/new-jenkins/pact/contracts-generate-api.sh'
sh 'build/new-jenkins/docker-copy-files.sh /usr/src/app/log/results.xml tmp/spec_results/${DATABASE_NAME} ${DATABASE_NAME} --allow-error --clean-dir'
}
}
}

parallel(stages)
}
}
Expand Down
8 changes: 8 additions & 0 deletions build/new-jenkins/pact/contracts-generate-api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -o errexit -o errtrace -o pipefail -o xtrace

docker-compose run --name ${DATABASE_NAME} -T \
-e RAILS_ENV=test \
-e DATABASE_URL=postgres://postgres:${POSTGRES_PASSWORD}@postgres:5432/${DATABASE_NAME} \
canvas bundle exec rspec spec/contracts/service_providers/ \
--tag pact --format doc
9 changes: 9 additions & 0 deletions build/new-jenkins/pact/contracts-publish-api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -o errexit -o errtrace -o pipefail -o xtrace

sha="$(git rev-parse --short HEAD)"
docker-compose run --no-deps --rm -e SHA="${sha}" \
-e PACT_BROKER_HOST=inst-pact-broker.inseng.net \
-e PACT_BROKER_USERNAME="${PACT_BROKER_USERNAME}" \
-e PACT_BROKER_PASSWORD="${PACT_BROKER_PASSWORD}" \
canvas bundle exec rake broker:pact:publish:jenkins_post_merge
8 changes: 8 additions & 0 deletions docker-compose.new-jenkins-contract-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "2.3"
services:
canvas:
volumes:
- pacts:/usr/src/app/pacts

volumes:
pacts: {}
2 changes: 1 addition & 1 deletion lib/tasks/pact_broker.rake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace :broker do
end

def prepare_pact_files_for_publishing(task)
task.pattern = 'pacts/*.json'
task.pattern = 'pacts/**/*.json'
task.pact_broker_base_url = PactConfig.broker_host
task.pact_broker_basic_auth = {
username: PactConfig.broker_username,
Expand Down
8 changes: 4 additions & 4 deletions spec/contracts/service_consumers/pact_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ module PactConfig
# accidentally breaking the contract tests
module Providers
CANVAS_LMS_API = 'Canvas LMS API'.freeze
sha = `git rev-parse --short HEAD`.strip
CANVAS_API_VERSION = '1.0'
CANVAS_API_VERSION = '1.0'.freeze
CANVAS_LMS_LIVE_EVENTS = 'Canvas LMS Live Events'.freeze
OUTCOMES = 'Outcomes'.freeze
ALL = Providers.constants.map { |c| Providers.const_get(c) }.freeze
Expand All @@ -34,7 +33,7 @@ module Consumers
my_broker_host = ENV.fetch('PACT_BROKER_HOST', 'pact-broker.docker')
# common consumer
GENERIC_CONSUMER = 'Generic Consumer'.freeze
CANVAS_API_VERSION = '1.0'
CANVAS_API_VERSION = '1.0'.freeze
CANVAS_LMS_API = 'Canvas LMS API'.freeze
if my_broker_host.include?(EXTERNAL_BROKER_HOST)
# external consumers
Expand Down Expand Up @@ -81,7 +80,8 @@ def consumer_tag
end

def consumer_version
Consumers::CANVAS_API_VERSION
sha = ENV['SHA']
sha.blank? ? Consumers::CANVAS_API_VERSION : "#{Consumers::CANVAS_API_VERSION}+#{sha}"
end

def broker_password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
host: outcomes_host.split(':').first,
consumer_key: outcomes_key,
scope: 'content_migration.export',
exp: 100.years.from_now.to_i
exp: 100.years.from_now.to_i,
context_type: 'course',
context_id: '100',
id: '*'
}
end
let(:export_get_token) { JSON::JWT.new(export_get_payload).sign(outcomes_secret, :HS512) }
Expand Down Expand Up @@ -59,7 +62,7 @@
}
}
end
let(:export_data) do { "export_id": "1" } end
let(:export_data) { { "export_id": "1" } }

before do
outcomes.given('artifacts and an export to retrieve').
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
{
host: outcomes_host.split(':').first,
consumer_key: outcomes_key,
scope: 'content_imports.show',
exp: 100.years.from_now.to_i
scope: 'content_migration.import',
exp: 100.years.from_now.to_i,
context_type: 'course',
context_id: '100',
id: '*'
}
end
let(:import_get_token) { JSON::JWT.new(import_get_payload).sign(outcomes_secret, :HS512) }
Expand All @@ -44,11 +47,11 @@
"state": "completed"
}
end
let!(:course) {course_factory(active_course: true)}
let(:import_data) do { course: course, import_id: 1 } end
let!(:course) { course_factory(active_course: true) }
let(:import_data) { { course: course, import_id: 1 } }

before do
outcomes.given('a provisioned outcomes service account with existing outcomes').
outcomes.given('a provisioned outcomes service account with a completed content import').
upon_receiving('a request to return imported content').
with(
method: :get,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
{
host: outcomes_host.split(':').first,
consumer_key: outcomes_key,
scope: 'content_exports.create',
exp: 100.years.from_now.to_i
scope: 'content_migration.export',
exp: 100.years.from_now.to_i,
context_type: 'course',
context_id: '100',
id: '*'
}
end
let(:export_post_token) { JSON::JWT.new(export_post_payload).sign(outcomes_secret, :HS512) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
{
host: outcomes_host.split(':').first,
consumer_key: outcomes_key,
scope: 'content_imports.create',
exp: 100.years.from_now.to_i
scope: 'content_migration.import',
exp: 100.years.from_now.to_i,
context_type: 'course',
context_id: '100',
id: '*'
}
end
let(:import_post_token) { JSON::JWT.new(import_post_payload).sign(outcomes_secret, :HS512) }
Expand Down

0 comments on commit 0cb4e64

Please sign in to comment.