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
-
Python
3.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
正确安装,请运行以下命令: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。
-
在
MyTestSuite_1.0.0/suite
如何创建suite.json
文件具有以下结构:{ "id": "MyTestSuite_1.0.0", "title": "My Test Suite", "details": "This is my test suite.", "userDataRequired": false }
-
在
MyTestSuite_1.0.0/myTestGroup
如何创建group.json
文件具有以下结构:{ "id": "MyTestGroup", "title": "My Test Group", "details": "This is my test group.", "optional": false }
-
在
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/myTestCase
folder。
要验证 SDK 是否已成功复制,请运行以下命令。
cd MyTestSuite_1.0.0/suite/myTestGroup/myTestCase python3 -c 'import idt_client'
创建测试用例可执行文件
测试用例可执行文件包含要运行的测试逻辑。测试套件可以包含多个测试用例可执行文件。在本教程中,您将仅创建一个测试用例可执行文件。
-
创建测试套件文件。
在
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()
-
使用客户端 SDK 函数将以下测试逻辑添加到您的
myTestCase.py
file:-
在所测试设备上运行 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() -
将测试结果发送给 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
。
运行测试套件
创建测试套件后,您希望确保其按预期运行。完成以下步骤以使用现有设备池运行测试套件以执行此操作。
-
复制你的
MyTestSuite_1.0.0
中的文件夹
.<device-tester-extract-location>
/tests -
运行以下命令:
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 会将错误日志流式传输到控制台,以帮助您对测试运行进行故障排除。在检查错误日志之前,请验证以下几点:
无法连接到所测试设备
请验证以下内容:
-
您的
device.json
文件包含正确的 IP 地址、端口和身份验证信息。 -
您可以从主机通过 SSH 连接到设备。