

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# AWS Glue 使用 pytest 架構在 中執行 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>

您可以在 AWS Glue [本機開發環境中](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-libraries.html)為 執行 Python 擷取、轉換和載入 (ETL) 任務的單元測試，但在 DevOps 管道中複寫這些測試可能既困難又耗時。當您在 AWS 技術堆疊上現代化大型主機 ETL 程序時，單元測試特別具挑戰性。此模式說明如何簡化單元測試，同時保持現有功能完整，避免在您發佈新功能時中斷關鍵應用程式功能，並維護高品質的軟體。您可以使用此模式中的步驟和程式碼範例， AWS Glue 在 中使用 pytest 架構來執行 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 帳戶
+ 您 AWS Glue 程式庫的 Amazon Elastic Container Registry (Amazon ECR) 映像 URI，從 [Amazon ECR Public Gallery](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 服務

## Architecture
<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/zh_tw/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 儲存庫中的容器映像包含在 AWS Glue 本機執行和單位測試 [PySpark 型](https://spark.apache.org/docs/latest/api/python/) ETL 任務所需的所有二進位檔。公有容器儲存庫有三個映像標籤，每個 支援的版本各一個 AWS Glue。基於示範目的，此模式會使用`glue_libs_4.0.0_image_01`映像標籤。若要在 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 是 Python 程式庫的儲存庫，用於 PySpark 批次任務的本機開發 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 文件中的[安全最佳實務](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 文件中的使用 記錄 CodePipeline API 呼叫 AWS CloudTrail](https://docs.aws.amazon.com/codepipeline/latest/userguide/monitoring-cloudtrail-logs.html)。 CodePipeline 

您可以使用 CloudWatch Events 來監控在 上執行 AWS 雲端 的資源和應用程式 AWS。您也可以在 CloudWatch Events 中建立提醒。如需詳細資訊，請參閱 [ CodePipeline 文件中的監控 CodePipeline 事件](https://docs.aws.amazon.com/codepipeline/latest/userguide/detect-state-changes-cloudwatch-events.html)。 CodePipeline 

## 史詩
<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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 準備程式碼封存以進行部署。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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/zh_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在管道中執行單元測試。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 清除環境中的資源。 | 為了避免額外的基礎設施成本，請務必在實驗此模式中提供的範例之後刪除堆疊。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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/zh_tw/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)。