教程:开发简单的 IDT 测试套件 - Amazon IoT Greengrass
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

Amazon IoT Greengrass Version 12023 年 6 月 30 日进入延长寿命阶段。有关更多信息,请参阅Amazon IoT Greengrass V1维护政策。在此日期之后,Amazon IoT Greengrass V1不会发布提供功能、增强功能、错误修复或安全补丁的更新。在上运行的设备Amazon IoT Greengrass V1不会受到干扰,将继续运行并连接到云端。我们强烈建议你迁移到Amazon IoT Greengrass Version 2,它补充说重要的新功能支持其他平台

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

教程:开发简单的 IDT 测试套件

测试套件组合了以下内容:

  • 测试包含测试逻辑的可执行文件

  • 描述测试套件的 JSON 配置文件

本教程介绍如何将 IDT 用于Amazon IoT Greengrass开发包含单个测试用例的 Python 测试套件。在本教程中,您将完成以下步骤:

先决条件

要完成本教程,您需要:

  • 主机要求
    • 最新版本的Amazon IoTDevice Tester

    • Python3.7 或更高版本

      要检查计算机上安装的 Python 版本,请运行以下命令:

      python3 --version

      在 Windows 上,如果使用此命令返回错误,则使用python --version相反。如果返回的版本号为 3.7 或更高,则在 Powershell 终端中运行以下命令以设置python3作为您的别名python命令。

      Set-Alias -Name "python3" -Value "python"

      如果没有返回版本信息或者版本号小于 3.7,请按照中的说明进行操作下载 Python安装 Python 3.7+。有关更多信息,请参阅 。Python 文档.

    • urllib3

      验证urllib3正确安装,请运行以下命令:

      python3 -c 'import urllib3'

      如果urllib3未安装,请运行以下命令进行安装:

      python3 -m pip install urllib3
  • 设备要求
    • 具有 Linux 操作系统且与主机相同网络的网络连接的设备。

      建议使用Raspberry Pi使用树莓 Pi OS。确保设置SSH在 Raspberry Pi 上远程连接到它。

创建测试套件目录

IDT 在逻辑上将测试用例分成每个测试套件中的测试组。每个测试案例必须位于测试组内。在本教程中,创建一个名为的文件夹MyTestSuite_1.0.0然后在此文件夹中创建以下目录树:

MyTestSuite_1.0.0 └── suite └── myTestGroup └── myTestCase

创建 JSON 配置文件

您的测试套件必须包含以下内容:JSON 配置文件

所需的 JSON 文件
suite.json

包含有关测试套件的信息。请参阅 配置套件 .JSON

group.json

包含有关测试组的信息。您必须创建group.json测试套件中每个测试组的文件。请参阅 配置组 .JSON

test.json

包含有关测试案例的信息。您必须创建test.json为测试套件中的每个测试用例提交文件。请参阅 配置 test.JSON

  1. MyTestSuite_1.0.0/suite如何创建suite.json文件具有以下结构:

    { "id": "MyTestSuite_1.0.0", "title": "My Test Suite", "details": "This is my test suite.", "userDataRequired": false }
  2. MyTestSuite_1.0.0/myTestGroup如何创建group.json文件具有以下结构:

    { "id": "MyTestGroup", "title": "My Test Group", "details": "This is my test group.", "optional": false }
  3. MyTestSuite_1.0.0/myTestGroup/myTestCase如何创建test.json文件具有以下结构:

    { "id": "MyTestCase", "title": "My Test Case", "details": "This is my test case.", "execution": { "timeout": 300000, "linux": { "cmd": "python3", "args": [ "myTestCase.py" ] }, "mac": { "cmd": "python3", "args": [ "myTestCase.py" ] }, "win": { "cmd": "python3", "args": [ "myTestCase.py" ] } } }

你的目录树MyTestSuite_1.0.0现在文件夹应该呈现以下状态:

MyTestSuite_1.0.0 └── suite ├── suite.json └── myTestGroup ├── group.json └── myTestCase └── test.json

获取 IDT 客户端 SDK

您将IDT 客户端 SDK使 IDT 能够与被测设备进行交互并报告测试结果。在本教程中,您将使用 Python 版本的 SDK。

<device-tester-extract-location>/sdks/python/文件夹中,复制idt_client文件夹到你的MyTestSuite_1.0.0/suite/myTestGroup/myTestCasefolder。

要验证 SDK 是否已成功复制,请运行以下命令。

cd MyTestSuite_1.0.0/suite/myTestGroup/myTestCase python3 -c 'import idt_client'

创建测试用例可执行文件

测试用例可执行文件包含要运行的测试逻辑。测试套件可以包含多个测试用例可执行文件。在本教程中,您将仅创建一个测试用例可执行文件。

  1. 创建测试套件文件。

    MyTestSuite_1.0.0/suite/myTestGroup/myTestCase如何创建myTestCase.py包含以下内容:

    from idt_client import * def main(): # Use the client SDK to communicate with IDT client = Client() if __name__ == "__main__": main()
  2. 使用客户端 SDK 函数将以下测试逻辑添加到您的myTestCase.pyfile:

    1. 在所测试设备上运行 SSH 命令。

      from idt_client import * def main(): # Use the client SDK to communicate with IDT client = Client() # Create an execute on device request exec_req = ExecuteOnDeviceRequest(ExecuteOnDeviceCommand("echo 'hello world'")) # Run the command exec_resp = client.execute_on_device(exec_req) # Print the standard output print(exec_resp.stdout) if __name__ == "__main__": main()
    2. 将测试结果发送给 IDT。

      from idt_client import * def main(): # Use the client SDK to communicate with IDT client = Client() # Create an execute on device request exec_req = ExecuteOnDeviceRequest(ExecuteOnDeviceCommand("echo 'hello world'")) # Run the command exec_resp = client.execute_on_device(exec_req) # Print the standard output print(exec_resp.stdout) # Create a send result request sr_req = SendResultRequest(TestResult(passed=True)) # Send the result client.send_result(sr_req) if __name__ == "__main__": main()

为 IDT 配置设备信息

为 IDT 配置设备信息以运行测试。您必须更新device.json模板位于<device-tester-extract-location>/configs包含以下信息的文件夹。

[ { "id": "pool", "sku": "N/A", "devices": [ { "id": "<device-id>", "connectivity": { "protocol": "ssh", "ip": "<ip-address>", "port": "<port>", "auth": { "method": "pki | password", "credentials": { "user": "<user-name>", "privKeyPath": "/path/to/private/key", "password": "<password>" } } } } ] } ]

devices对象,请提供以下信息:

id

用户定义的设备的唯一标识符。

connectivity.ip

您的设备的 IP 地址。

connectivity.port

可选。用于与设备的 SSH 连接的端口号。

connectivity.auth

连接的身份验证信息。

此属性仅在 connectivity.protocol 设置为 ssh 时适用。

connectivity.auth.method

用于通过给定的连接协议访问设备的身份验证方法。

支持的值为:

  • pki

  • password

connectivity.auth.credentials

用于身份验证的凭证。

connectivity.auth.credentials.user

用于登录设备的用户名。

connectivity.auth.credentials.privKeyPath

用于登录设备的私有密钥的完整路径。

此值仅在 connectivity.auth.method 设置为 pki 时适用。

devices.connectivity.auth.credentials.password

用于登录设备的密码。

此值仅在 connectivity.auth.method 设置为 password 时适用。

注意

只有当 method 设置为 pki 时才指定 privKeyPath

只有当 method 设置为 password 时才指定 password

运行测试套件

创建测试套件后,您希望确保其按预期运行。完成以下步骤以使用现有设备池运行测试套件以执行此操作。

  1. 复制你的MyTestSuite_1.0.0中的文件夹<device-tester-extract-location>/tests.

  2. 运行以下命令:

    cd <device-tester-extract-location>/bin ./devicetester_[linux | mac | win_x86-64] run-suite --suite-id MyTestSuite

IDT 运行测试套件并将结果流式传输到控制台。测试结束运行后,您会看到以下信息:

time="2020-10-19T09:24:47-07:00" level=info msg=Using pool: pool time="2020-10-19T09:24:47-07:00" level=info msg=Using test suite "MyTestSuite_1.0.0" for execution time="2020-10-19T09:24:47-07:00" level=info msg=b'hello world\n' suiteId=MyTestSuite groupId=myTestGroup testCaseId=myTestCase deviceId=my-device executionId=9a52f362-1227-11eb-86c9-8c8590419f30 time="2020-10-19T09:24:47-07:00" level=info msg=All tests finished. executionId=9a52f362-1227-11eb-86c9-8c8590419f30 time="2020-10-19T09:24:48-07:00" level=info msg= ========== Test Summary ========== Execution Time: 1s Tests Completed: 1 Tests Passed: 1 Tests Failed: 0 Tests Skipped: 0 ---------------------------------- Test Groups: myTestGroup: PASSED ---------------------------------- Path to IoT Device Tester Report: /path/to/devicetester/results/9a52f362-1227-11eb-86c9-8c8590419f30/awsiotdevicetester_report.xml Path to Test Execution Logs: /path/to/devicetester/results/9a52f362-1227-11eb-86c9-8c8590419f30/logs Path to Aggregated JUnit Report: /path/to/devicetester/results/9a52f362-1227-11eb-86c9-8c8590419f30/MyTestSuite_Report.xml

问题排查

在完成本教程中,请使用以下信息帮助解决所有问题。

测试用例未成功运行

如果测试未成功运行,IDT 会将错误日志流式传输到控制台,以帮助您对测试运行进行故障排除。在检查错误日志之前,请验证以下几点:

  • IDT 客户端 SDK 位于正确的文件夹中,如中所述本步骤.

  • 你遇见了所有先决条件在本教程中。

无法连接到所测试设备

请验证以下内容:

  • 您的device.json文件包含正确的 IP 地址、端口和身份验证信息。

  • 您可以从主机通过 SSH 连接到设备。