教學課程:開發簡單的 IDT 測試套件 - AWS IoT Greengrass

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

      驗證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 檔案
suite.json

包含關於測試套件的資訊。請參閱 設定套件。

group.json

包含關於測試組的資訊。您必須建立group.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 客户端軟件開發工具包

您會使用IDT 客户端開發套件,使 IDT 能夠與被測設備進行交互並報告測試結果。在本教程中,您將使用軟件開發工具包的 Python 版本。

<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.py文件:

    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 從主機連接到您的設備。