

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 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*

## Summary
<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 您的库的亚马逊 Elastic Container Registry (Amazon ECR) 图片 URI，从[亚马逊](https://gallery.ecr.aws/glue/aws-glue-libs) ECR 公共画廊下载
+ 带有目标 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/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/82781ca8-4da0-4df0-bf23-32992fece231/images/6286dafc-f1e0-4967-beed-4dedc6047c10.png)


下图显示了如下工作流：

1. 在源代码阶段， AWS CodePipeline 使用版本控制的亚马逊简单存储服务 (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支持的每个版本各有一个映像标签。出于演示目的，此模式使用了 `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-](https://github.com/aws-samples/aws-glue-jobs-unit-testing) testing 存储库中找到。存储库包含以下资源：
+ 文件夹中基于 Python 的 AWS Glue 作业示例 `src`
+ `tests` 文件夹中的关联单元测试用例（使用 pytest 框架构建）
+ 文件夹中的 CloudFormation 模板（用 YAML 编写）`deploy`

## 最佳实践
<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 Ev CloudWatch ents 中提供了日志功能。您可以使用 CloudTrail 记录由您或代表您进行的 AWS API 调用和相关事件 AWS 账户。有关更多信息，请参阅 CodePipeline 文档 AWS CloudTrail中的[使用记录 CodePipeline API 调用](https://docs.aws.amazon.com/codepipeline/latest/userguide/monitoring-cloudtrail-logs.html)。

您可以使用 CloudWatch 事件来监控您的 AWS Cloud 资源和正在运行的应用程序 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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 准备代码存档以进行部署。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/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_cn/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-pipelin** e。 | AWS DevOps， DevOps 工程师 | 

### 运行单元测试
<a name="run-the-unit-tests"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 运行管线中的单元测试。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在环境中清理资源。 | 为避免额外的基础设施成本，请务必在尝试此模式中提供的示例后删除堆栈。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/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_cn/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 CLI) 部署 AWS CloudFormation 模板。有关更多信息，请参阅 CloudFormation 文档中的[使用转换快速部署模板](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-cli-deploy.html)。