コントラクト テスト ツール「API Hub for Contract Testing」を使ってみよう!パート 2: コンシューマー側の操作

この記事では、API Hub for Contract Testing を使って、コントラクト (契約) テストを開発ワークフローに統合して、デザイン ファーストの API 開発を実現する方法を全 3 パートのシリーズとして紹介します。

パート 1 では、プロバイダー (API 提供者) 側の操作について説明しました。パート 2 となる本記事では、コンシューマー (API 利用者) 側の操作について説明します。

本記事では、パート 1 で作成して公開した API を使用してコンシューマー側の操作を説明します。パート 1 をまだお読みになっていない場合は、先にパート 1 をご覧になることを推奨します。

example-consumer プロジェクトのセットアップとトリガー

  1. example-consumer プロジェクトを自分の GitHub アカウントにフォークします (右上の [Fork] ボタンをクリックします)。
  2. フォークした example-consumer プロジェクトを開きます (https://github.com/<ユーザー名>/example-consumer)。
  3. API Hub for Contract Testing の API トークンを保存する GitHub Secret を作成します。
    1. API Hub for Contract Testing で以下の操作を行います。
      1. API Hub for Contract Testing アカウント (https://<サブドメイン>.pactflow.io) にログインし、[Settings] > [API Tokens] に移動します。ドキュメントはこちらを参照してください。
      2. 読み書き可能な CI トークンの [Copy] ボタンをクリックします (読み取り専用ではなく、読み書き可能なトークンであることを確認してください)。
    2. Github で以下の操作を行います。
      1. フォークした example-consumer プロジェクトを開きます (https://github.com/<ユーザー名>/example-consumer)。
      2. [Settings] タブをクリックします。
      3. サイドメニューから [Secrets] を選択します。
      4. [New repository secret] ([Actions secrets] 見出しの右側のボタン) をクリックします。
      5. シークレットの名前を PACTFLOW_TOKEN_FOR_CI_CD_WORKSHOP に設定します。
      6. 前の手順でコピーした API Hub for Contract Testing の API トークンの値をペーストします。
      7. [Actions] タブをクリックします。
      8. [I understand my workflows, go ahead and enable them] ボタンをクリックします。
      9. .github/workflows/Build.yml を開きます。
      10. ファイル ビューの右上にある 🖊️ をクリックして、ファイル エディターを開きます。
      11. PACT_BROKER_BASE_URL の値を、API Hub for Contract Testing アカウントのベース URL に更新します。ベース URL は、API Hub for Contract Testing の [API Tokens] ページにある [COPY PACTFLOW BASE URL] ボタンをクリックして取得できます。
      12. 緑色の [Commit changes] ボタンをクリックします。

ビルド

コンシューマーのビルドがトリガーされます。

cancel-workflow.png

Pact コントラクト テスト

コンシューマー ユニット テストにパスするはずです。

build-yml.png

ビルド ジョブをクリックすると、出力を確認できます。

deploy.png

Pact コントラクト ファイルのアップロード

Pact ファイルは API Hub for Contract Testing Broker にアップロードされます。

complete-job.png
Pact successfully published for pactflow-example-consumer version 253c165958d15624ce7245d5739689860439d24b and provider pactflow-example-provider.
  View the published pact at https://saflow.pactflow.io/pacts/provider/pactflow-example-provider/consumer/pactflow-example-consumer/version/253c165958d15624ce7245d5739689860439d24b
  Events detected: contract_published, contract_content_changed (first time untagged pact published)
  No enabled webhooks found for the detected events
  Next steps:
    * Add Pact verification tests to the pactflow-example-provider build. See https://docs.pact.io/go/provider_verification

コンシューマーの can-i-deploy チェック

デプロイする前に can-i-deploy コマンドを呼び出します。コンシューマー コントラクトは OpenAPI 定義の有効なサブセットであるため、パスするはずです。

can-i-deploy.png
========== STAGE: can-i-deploy? ==========

Computer says yes \o/ 

CONSUMER                  | C.VERSION  | PROVIDER                  | P.VERSION               | SUCCESS? | RESULT#
--------------------------|------------|---------------------------|-------------------------|----------|--------
pactflow-example-consumer | 253c165... | pactflow-example-provider | 1.0.0-21b1f7f...-design | true     | 1      

VERIFICATION RESULTS
--------------------
1. https://saflow.pactflow.io/contracts/bi-directional/provider/pactflow-example-provider/version/1.0.0-21b1f7fdf6428bfb0f583e151d9893c230a1c555-design/consumer/pactflow-example-consumer/version/253c165958d15624ce7245d5739689860439d24b/cross-contract-verification-results (success)

All required verification results are published and successful

コンシューマー レコードのデプロイ

can-i-deploy にパスしたら、コンシューマーをデプロイできます。デプロイが完了すると、コンシューマーのアプリケーション バージョンは本番環境にデプロイ済みとして記録されます。

record-deployment.png
========== STAGE: deploy ==========

Deploying to production
touch .env
Recorded deployment of pactflow-example-consumer version 253c165958d15624ce7245d5739689860439d24b to production environment in API Hub for Contract Testing.

これまでの手順で、コンシューマーとプロバイダーの両方のビルドがパスしているはずです。

PF_Screenshot_ExampleConsumer_01
PF_Screenshot_ConsumerAndProvider_02

パート 2 では、コンシューマー (API 利用者) 側の操作について説明しました。

パート 3 では、互換性を損なう API 変更の例について説明します。


この資料は、SmartBear の Web サイトで公開されている API Hub for Contract Testing University ドキュメントの「Design First With API Hub for Design」にある「Quick Start Guide with Github Actions」を参考に作成しました。

タイトルとURLをコピーしました