

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# pytest フレームワーク AWS Glue を使用して で Python ETL ジョブのユニットテストを実行する
<a name="run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework"></a>

*Praveen Kumar Jeyarajan と Vaidy Sankaran、Amazon Web Services*

## 概要
<a name="run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework-summary"></a>

[ローカル開発環境で](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-libraries.html) の Python 抽出、変換、ロード (ETL) ジョブ AWS Glue のユニットテストを実行できますが、DevOps パイプラインでこれらのテストをレプリケートするのは困難で時間がかかる場合があります。 AWS テクノロジースタックでメインフレーム ETL プロセスをモダナイズする場合、ユニットテストは特に難しい場合があります。このパターンは、既存の機能をそのまま維持し、新機能をリリースしたときに主要なアプリケーション機能の中断を回避し、高品質のソフトウェアを維持しながら、ユニットテストを簡素化する方法を示しています。このパターンのステップとコードサンプルを使用して、pytest フレームワーク AWS Glue を使用して で Python ETL ジョブのユニットテストを実行できます AWS CodePipeline。このパターンを使用して、複数の AWS Glue ジョブをテストおよびデプロイすることもできます。

## 前提条件と制限
<a name="run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework-prereqs"></a>

**前提条件**
+ アクティブな AWS アカウント
+ Amazon ECR Public Gallery からダウンロードした AWS Glue ライブラリの Amazon Elastic Container Registry (Amazon ECR) イメージ URI [https://gallery.ecr.aws/glue/aws-glue-libs](https://gallery.ecr.aws/glue/aws-glue-libs)
+ ターゲット AWS アカウント と のプロファイルを持つ Bash ターミナル (任意のオペレーティングシステム上) AWS リージョン
+ [Python 3.10](https://www.python.org/downloads/) 以降
+ [Pytest](https://github.com/pytest-dev/pytest)
+ テスト用の [Moto](https://github.com/getmoto/moto) Python ライブラリ AWS のサービス

## アーキテクチャ
<a name="run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework-architecture"></a>

次の図は、Python に基づく AWS Glue ETL プロセスのユニットテストを一般的なエンタープライズスケールの AWS DevOps パイプラインに組み込む方法を示しています。

![\[AWS Glue ETL プロセスのユニットテスト。\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/images/pattern-img/82781ca8-4da0-4df0-bf23-32992fece231/images/6286dafc-f1e0-4967-beed-4dedc6047c10.png)


この図表は、次のワークフローを示しています:

1. ソースステージでは、 AWS CodePipeline はバージョニングされた Amazon Simple Storage Service (Amazon S3) バケットを使用して、ソースコードアセットを保存および管理します。これらのアセットには、サンプル Python ETL ジョブ (`sample.py`)、ユニットテストファイル (`test_sample.py`)、 AWS CloudFormation テンプレートが含まれます。次に、CodePipeline は最新のコードをメインブランチから AWS CodeBuild プロジェクトに転送し、さらに処理します。

1. ビルドおよび公開ステージでは、前のソースステージの最新のコードは、 AWS Glue パブリック Amazon ECR イメージを使用してユニットテストされます。次に、テストレポートが CodeBuild レポートグループに公開されます。 AWS Glue ライブラリのパブリック Amazon ECR リポジトリのコンテナイメージには、 で [PySpark ベースの](https://spark.apache.org/docs/latest/api/python/) ETL タスクを AWS Glue ローカルで実行およびユニットテストするために必要なすべてのバイナリが含まれています。パブリックコンテナリポジトリには、 AWS Glueがサポートするバージョンごとに 1 つずつ、合計 3 つのイメージタグがあります。デモンストレーションの目的から、このパターンでは `glue_libs_4.0.0_image_01` image タグを使用しています。このコンテナイメージを CodeBuild のランタイムイメージとして使用するには、使用する予定のイメージタグに対応するイメージ URI をコピーし、`TestBuild` リソースの GitHub リポジトリ内の `pipeline.yml` ファイルを更新します。

1. デプロイ段階では、CodeBuild プロジェクトが起動され、すべてのテストに合格すると Amazon S3 バケットにコードが公開されます。

1. ユーザーは、 `deploy`フォルダの CloudFormation テンプレートを使用して AWS Glue タスクをデプロイします。

## ツール
<a name="run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework-tools"></a>

**AWS のサービス**
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) は完全マネージド型の構築サービスです。ソースコードのコンパイル、ユニットテストの実行、すぐにデプロイできるアーティファクトの生成を行います。
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) は、ソフトウェアリリースのさまざまな段階を迅速にモデル化および設定し、ソフトウェアの変更を継続的にリリースするために必要なステップを自動化するのに役立ちます。
+ [Amazon Elastic Container Registry (Amazon ECR)](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html) は、セキュリティ、スケーラビリティ、信頼性を備えたマネージドコンテナイメージレジストリサービスです。
+ [AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html) はフルマネージド型の ETL サービスです。これにより、データストアとデータストリーム間でのデータの分類、整理、強化、移動を確実に行うことができます。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) は、業界をリードするスケーラビリティ、データ可用性、セキュリティ、およびパフォーマンスを提供するオブジェクトストレージサービスです。

**その他のツール**
+ [Python](https://www.python.org/) は、高水準のインタープリター型汎用プログラミング言語です。
+ [Moto](https://github.com/getmoto/moto) はテスト用の Python ライブラリです AWS のサービス。
+ [Pytest](https://github.com/pytest-dev/pytest) は、アプリケーションやライブラリの複雑な機能テストをサポートするように拡張できる小規模なユニットテストを作成するためのフレームワークです。
+ の [Python ETL ライブラリ](https://github.com/awslabs/aws-glue-libs) AWS Glue は、PySpark バッチジョブのローカル開発で使用される Python ライブラリのリポジトリです AWS Glue。

**コードリポジトリ**

このパターンのコードは、GitHub 内の「[aws-glue-jobs-unit-testing](https://github.com/aws-samples/aws-glue-jobs-unit-testing)」リポジトリで利用できます。リポジトリには次のリソースが含まれています。
+ `src` フォルダ内のサンプル Python ベースの AWS Glue ジョブ
+ `tests` フォルダ内の関連ユニットテストケース (pytest フレームワークを使用して構築)
+ `deploy` フォルダ内の CloudFormation テンプレート (YAML で記述)

## ベストプラクティス
<a name="run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework-best-practices"></a>

**CodePipeline リソースのセキュリティ**

CodePipeline のパイプラインに接続するソースリポジトリに暗号化と認証を使用することがベストプラクティスです。詳細については、CodePipeline ドキュメントの「[Security best practices](https://docs.aws.amazon.com/codepipeline/latest/userguide/security-best-practices.html)」を参照してください。

**CodePipeline リソースで使用するモニタリングとログ記録**

 AWS ログ記録機能を使用して、ユーザーがアカウントで実行するアクションと使用するリソースを決定するのがベストプラクティスです。ログファイルには次の内容が表示されます。
+ アクションが実行された日時
+ アクション送信元 IP アドレス
+ 不適切なアクセス権限が理由で失敗したアクション

ログ記録機能は、 AWS CloudTrail および Amazon CloudWatch Events で使用できます。CloudTrail を使用して、 によって、または に代わって行われた AWS API コールおよび関連イベントを記録できます AWS アカウント。詳細については、CodePipeline ドキュメントの「[Logging CodePipeline API calls with AWS CloudTrail](https://docs.aws.amazon.com/codepipeline/latest/userguide/monitoring-cloudtrail-logs.html)」を参照してください。

CloudWatch Events を使用して、 で実行されている AWS クラウド リソースとアプリケーションをモニタリングできます AWS。CloudWatch イベントでアラートを作成することもできます。詳細については、CodePipeline ドキュメントの「[CodePipeline イベントのモニタリング](https://docs.aws.amazon.com/codepipeline/latest/userguide/detect-state-changes-cloudwatch-events.html)」を参照してください。

## エピック
<a name="run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework-epics"></a>

### ソースコードをデプロイするには
<a name="deploy-the-source-code"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| コードアーカイブをデプロイ用に準備する。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework.html) | DevOps エンジニア | 
| CloudFormation スタックを更新する。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework.html)スタックは、Amazon S3 をソースとして使用して CodePipeline ビューを作成します。上記のステップでは、パイプラインは **aws-glue-unit-test-pipeline** です。 | AWS DevOps、DevOps エンジニア | 

### ユニットテストを実行する
<a name="run-the-unit-tests"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| パイプラインでユニットテストを実行する。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework.html) | AWS DevOps、DevOps エンジニア | 

### すべての AWS リソースをクリーンアップする
<a name="clean-up-all-aws-resources"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| 環境内のリソースをクリーンアップする。 | 追加のインフラストラクチャコストを避けるため、このパターンで提供されている例を試した後は、必ずスタックを削除してください。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework.html) | AWS DevOps、DevOps エンジニア | 

## トラブルシューティング
<a name="run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework-troubleshooting"></a>


| 問題 | ソリューション | 
| --- | --- | 
| CodePipeline サービスロールは Amazon S3 バケットにアクセスできません。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework.html) | 
| CodePipeline は、Amazon S3 バケットがバージョニングされていないというエラーを返します。 | CodePipeline では、ソース Amazon S3 バケットがバージョニングされている必要があります。Amazon S3 バケットのバージョニングを有効にします。手順については、「[バケットでのバージョニングの有効化](https://docs.aws.amazon.com/AmazonS3/latest/userguide/manage-versioning-examples.html)」を参照してください。 | 

## 関連リソース
<a name="run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework-resources"></a>
+ [AWS Glue](https://aws.amazon.com/glue/)
+ [ローカルでの AWS Glue ジョブの開発とテスト](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-libraries.html)
+ [の AWS CloudFormation AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/populate-with-cloudformation-templates.html)

## 追加情報
<a name="run-unit-tests-for-python-etl-jobs-in-aws-glue-using-the-pytest-framework-additional"></a>

さらに、 AWS Command Line Interface () を使用して AWS CloudFormation テンプレートをデプロイできますAWS CLI。詳細については、CloudFormation ドキュメントの「[変換が含まれるテンプレートの迅速なデプロイ](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-cli-deploy.html)」を参照してください。