diff --git a/.github/workflows/verify_changed_contract.yml b/.github/workflows/verify_changed_contract.yml new file mode 100644 index 00000000..9e1eaf9e --- /dev/null +++ b/.github/workflows/verify_changed_contract.yml @@ -0,0 +1,43 @@ +name: Verify contract changed by a consumer + +on: + repository_dispatch: + types: + - contract_changed + +env: + CHANGED_PACT_URL: ${{ github.event.client_payload.pact_url }} + GIT_ENV: Production + PACT_BROKER_BASE_URL: https://edx.pactflow.io + PACT_BROKER_TOKEN: ${{ secrets.PACT_FLOW_ACCESS_TOKEN }} + PUBLISH_VERIFICATION_RESULTS: true + VERIFY_WITH_BROKER: false + +jobs: + changed-contract-verification: + name: Pact Provider Verification for a changed contract + runs-on: ubuntu-20.04 + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: setup python + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install pip + run: pip install -r requirements/pip.txt + + - name: Install Dependencies + run: | + pip install -r requirements/ci.txt + pip install -r requirements/test.txt + + - name: Verify Changed Contract + run: | + export PUBLISH_VERSION=`git rev-parse --short HEAD` + export PUBLISH_TAGS=${GITHUB_REF:11} + export GIT_ENV='production' + pytest -s edxval/pacts/verify_pact.py --ds=edxval.settings.pact \ No newline at end of file diff --git a/edxval/pacts/README.md b/edxval/pacts/README.md index 7550f928..fd04f87f 100644 --- a/edxval/pacts/README.md +++ b/edxval/pacts/README.md @@ -5,7 +5,9 @@ This readme contains the description of different files in the current directory * views.py: This will have provider state setup view which is hit during provider verification to setup appropriate DB state per interaction * utils.py: Different DB utilities used by views.py * video-encode-manager-edx-val.json: The only pact file is a duplicate of pact file from https://github.com/edx/video-encode-manager/blob/a4f4c12db0c3919858df1e2fa58ee74071bc264a/encode_manager/apps/core/tests/pact_tests/pacts/video-encode-manager-edx-val.json. This is temporarily needed until pact broker can be setup. Until then, any update in the original pact should be ported in the json in VAL. -* verify_pact.py: Runs the provider verification against the pact(either JSON file in the current directory or via pact broker). To run the verification, either of the following two commands can be used - * `DJANGO_SETTINGS_MODULE=edxval.settings.pact pytest -s edxval/pacts/verify_pact.py` - * `pytest -s edxval/pacts/verify_pact.py --ds=edxval.settings.pact` +* verify_pact.py: Runs the provider verification against the pact. Use either `DJANGO_SETTINGS_MODULE=edxval.settings.pact pytest -s edxval/pacts/verify_pact.py` or `pytest -s edxval/pacts/verify_pact.py --ds=edxval.settings.pact` commands to run the verification. + There are 3 instances of running the verification: + * **Verify with broker**: By default, the verification is done against the broker. + * **Changed Pact's Verification**: For this verification, `VERIFY_WITH_BROKER` environment variable should be set to False and `CHANGED_PACT_URL` should point to a valid pact. + * **Local JSON Pact**: Similar to Changed Pact Verification. Alternatively, if only `VERIFY_WITH_BROKER` is set to False, the pact named video-encode-manager-edx-val.json will be verified. \ No newline at end of file diff --git a/edxval/pacts/verify_pact.py b/edxval/pacts/verify_pact.py index ffaa30ab..b6ef852e 100644 --- a/edxval/pacts/verify_pact.py +++ b/edxval/pacts/verify_pact.py @@ -17,16 +17,23 @@ class ProviderVerificationServer(LiveServerTestCase): """ Execute the provider verification on the consumer pacts """ - PACT_CONFIG = { + PACT_DEFAULT_CONFIG = { + 'enable_pending': True, + 'headers': ['Pact-Authentication: AllowAny', ], + 'verbose': False, + 'publish_version': settings.PUBLISH_VERSION, + 'provider_tags': [settings.PUBLISH_TAGS, settings.GIT_ENV], + 'publish_verification_results': settings.PUBLISH_VERIFICATION_RESULTS, + 'verify_with_broker': settings.VERIFY_WITH_BROKER, + } + + PACT_BROKER_CONFIG = { 'broker_url': settings.PACT_BROKER_BASE_URL, 'consumer_version_selectors': [ {'tag': 'production', 'latest': True}, {"tag": "master", "latest": True}, ], - 'publish_version': settings.PUBLISH_VERSION, - 'provider_tags': [settings.PUBLISH_TAGS, settings.GIT_ENV], - 'publish_verification_results': settings.PUBLISH_VERIFICATION_RESULTS, - 'headers': ['Pact-Authentication: AllowAny', ], + **PACT_DEFAULT_CONFIG } @classmethod @@ -45,19 +52,23 @@ def tearDownClass(cls): def test_verify_pact(self): """ Run provider verification either using pact broker or local pact. + + * If VERIFY_WITH_BROKER environment variable is set, broker config will be used to run verification + of pacts on broker. + * If VERIFY_WITH_BROKER is not set, the pact specified by CHANGED_PACT_URL environment variable or the + local directory pact will be verified. VERIFY_WITH_BROKER is not set when the contract is changed by the + consumer and a provider verification build specific to the changed contract should be executed. """ - if self.PACT_CONFIG['publish_verification_results']: + if self.PACT_DEFAULT_CONFIG['verify_with_broker']: output, _ = self.verifier.verify_with_broker( - **self.PACT_CONFIG, - verbose=False, - enable_pending=True, + **self.PACT_BROKER_CONFIG, provider_states_setup_url=f"{self.live_server_url}{reverse('provider-state-view')}", ) else: output, _ = self.verifier.verify_pacts( - os.path.join(PACT_DIR, PACT_FILE), + settings.CHANGED_PACT_URL or os.path.join(PACT_DIR, PACT_FILE), provider_states_setup_url=f"{self.live_server_url}{reverse('provider-state-view')}", - headers=self.PACT_CONFIG['headers'], + **self.PACT_DEFAULT_CONFIG, ) assert output == 0 diff --git a/edxval/settings/pact.py b/edxval/settings/pact.py index 7d1307a0..08ace40b 100644 --- a/edxval/settings/pact.py +++ b/edxval/settings/pact.py @@ -8,11 +8,16 @@ from edxval.settings.test import * # pylint: disable=wildcard-import -PUBLISH_VERIFICATION_RESULTS = os.environ.get('PUBLISH_VERIFICATION_RESULTS', 'False').lower() in ('true', 'yes', '1') +CHANGED_PACT_URL = os.environ.get('CHANGED_PACT_URL') +GIT_ENV = os.environ.get('GIT_ENV', 'development') PROVIDER_STATES_SETUP_VIEW_URL = True PACT_BROKER_BASE_URL = os.environ.get('PACT_BROKER_BASE_URL', 'http://localhost:9292') -PUBLISH_VERSION = os.environ.get('PUBLISH_VERSION', '1') PUBLISH_TAGS = os.environ.get('PUBLISH_TAGS', 'master') -GIT_ENV = os.environ.get('GIT_ENV') +PUBLISH_VERSION = os.environ.get('PUBLISH_VERSION', '1') +PUBLISH_VERIFICATION_RESULTS = os.environ.get('PUBLISH_VERIFICATION_RESULTS', 'False').lower() in ('true', 'yes', '1') + +# By default, the verification will be done from broker. This option will need to be +# overridden when doing verification for a changed contract +VERIFY_WITH_BROKER = os.environ.get('VERIFY_WITH_BROKER', 'True').lower() in ('true', 'yes', '1') MIDDLEWARE = MIDDLEWARE + ('edxval.pacts.middleware.AuthenticationMiddleware',)