AWS IoT Greengrass Version 1 於 2023 年 6 月 30 日進入延長生命週期階段。如需詳細資訊,請參閱 AWS IoT Greengrass V1 維護政策。在此日期之後, AWS IoT Greengrass V1 不會發佈提供功能、增強功能、錯誤修正或安全修補程式的更新。在 上執行的裝置 AWS IoT Greengrass V1 不會中斷,且會繼續運作並連線至雲端。我們強烈建議您遷移至 AWS IoT Greengrass Version 2 ,這會新增重要的新功能,並支援其他平台。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
教學課程:開發簡單的 IDT 測試套件
測試套件結合了以下內容:
-
測試包含測試邏輯的可執行文件
-
描述測試套件的 JSON 配置文件
本教學課程將告訴您如何使用 IDTAWS IoT Greengrass來開發一個包含單個測試用例的 Python 測試套件。在本教學課程中,您會完成下列步驟:
先決條件
為了完成本教學,您需要以下項目:
-
主機電腦要求
-
的最新版本AWS IoTDevice Tester
-
蟒蛇
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 在你的樹莓派遠程連接到它。
-
建立測試套件目錄
IDT 在每個測試套件中邏輯上將測試用例分為測試組。每個測試案例必須位於測試組內。在本教程中,創建一個名為MyTestSuite_1.0.0
並在此文件夾中創建以下目錄樹:
MyTestSuite_1.0.0 └── suite └── myTestGroup └── myTestCase
建立 JSON 組態檔案
您的測試套件必須包含以下所需JSON 組態檔案:
必要的 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 客户端軟件開發工具包
您會使用IDT 客户端開發套件,使 IDT 能夠與被測設備進行交互並報告測試結果。在本教程中,您將使用軟件開發工具包的 Python 版本。
從
文件夾中,複製<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
文件:-
在待測裝置上執行 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 從主機連接到您的設備。