Python – Robot Framework + SSHLibrary

Loading

測試環境為 CentOS 8 x86_64 (虛擬機)

透過 Robot Framework 做自動化跟一般透過 Python 套件來做的差別在於不懂 Python 程式的人也可以使用,只要會編寫 Robot Framework 設定檔就可以跑自動化測試.

下面範例是透過 Python 的 paramiko 套件來進行 SSH 連線並執行遠端指令.

Python paramiko

[root@localhost ~]# python3
Python 3.6.8 (default, Mar 25 2022, 11:15:52)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-10)] on linux
Type "help", "copyright", "credits" or "license" for more information.

import paramiko
username = "root"
password = "111111"
hostname = "localhost"
port = 22
 
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, port, username, password)
stdin, stdout, stderr = client.exec_command('echo Hello SSHLibrary!')
result = stdout.readlines()
print (result)
['Hello SSHLibrary!\n']
>>>
[1]+  Stopped                 python3

但不懂 Python 程式的人就無法使用,我們可以透過 Robot Framework 的功能,使用者只須編寫 Robot Framework 設定檔就可以跑自動化測試.

Robot Framework

但 Robot Framework 有支援哪種類的自動化測試 LIBRARIES 可以查看 – https://robotframework.org/#resources 其中的.

  • SeleniumLibrary 用於測試網頁
  • SSHLibrary 用於可以透過 SSH 連線進行測試,下面來說明.

所需套件 (需先安裝 Python + pip – https://benjr.tw/104226)

[root@localhost ~]# pip install robotframework
[root@localhost ~]# pip install robotframework-sshlibrary

其中 robotframework-sshlibrary – https://github.com/robotframework/SSHLibrary 會安裝 scp, paramiko – https://benjr.tw/105529 與 Cryptography modules (如 paramiko 版本小於 2.0 時則安裝 PyCrypto ).

編寫 Robot Framework 設定檔

[root@localhost ~]# vi ssh-sample.robot
*** Settings ***
Documentation          This example demonstrates executing a command on a remote machine
...                    and getting its output.
...
...                    Notice how connections are handled as part of the suite setup and
...                    teardown. This saves some time when executing several test cases.

Library                SSHLibrary
Suite Setup            Open Connection And Log In
Suite Teardown         Close All Connections

*** Variables ***
${HOST}                localhost
${USERNAME}            root
${PASSWORD}            111111

*** Test Cases ***
Execute Command And Verify Output
    [Documentation]    Execute Command can be used to run commands on the remote machine.
    ...                The keyword returns the standard output by default.
    ${output}=         Execute Command    echo Hello SSHLibrary!
    Should Be Equal    ${output}          Hello SSHLibrary!

*** Keywords ***
Open Connection And Log In
   Open Connection     ${HOST}
   Login               ${USERNAME}        ${PASSWORD}

程式說明:
Robot Framework 使用預先定義好的 Keywords 關鍵字來執行各種行為 除了 build-in 內建之外還有 LIBRARIES 的 Keywords.

  • Build-in keyword , 詳細請參考 https://robotframework.org/robotframework/latest/libraries/BuiltIn.html 說明.
  • SSHLibrary Keywords , 詳細請參考 http://robotframework.org/SSHLibrary/SSHLibrary.html 說明.

Settings

*** Settings ***
Documentation          This example demonstrates executing a command on a remote machine
...                    and getting its output.
...
...                    Notice how connections are handled as part of the suite setup and
...                    teardown. This saves some time when executing several test cases.

Library                SSHLibrary
Suite Setup            Open Connection And Log In
Suite Teardown         Close All Connections
  1. Documentation
    文件說明
  2. Library
    使用那些 Librarys
  3. Suite Setup
    在 Test Cases 開始前要做的動作. 動作為 Open Connection And Log In 定義在自訂的 Keywords 裡面,其中的 Open Connection 與 Login 是 SSHLibrary 預先定義好的 Keywords.

    *** Keywords ***
    Open Connection And Log In
       Open Connection     ${HOST}
       Login               ${USERNAME}        ${PASSWORD}
    
  4. Suite Teardown
  5. 在 Test Cases 結束後要做的動作. Close All Connections 一樣是 SSHLibrary 預先定義好的 Keywords

Variables

*** Variables ***
${HOST}                localhost
${USERNAME}            root
${PASSWORD}            111111

變數,請自行填入相對應要登錄的 SSH 資料.
這邊使用 localhost 就是本機 127.0.0.1 與其帳號密碼.

Test Cases

*** Test Cases ***
Execute Command And Verify Output
    [Documentation]    Execute Command can be used to run commands on the remote machine.
    ...                The keyword returns the standard output by default.
    ${output}=         Execute Command    echo Hello SSHLibrary!
    Should Be Equal    ${output}          Hello SSHLibrary!
  1. Execute Command And Verify Output
    這個是這一個測試的名稱,可自訂.
  2. ${output}= Execute Command echo Hello SSHLibrary!
    透過 SSHLibrary 預設關鍵字 Execute Command 執行(並等待回傳值) echo Hello SSHLibrary! 指令. 最後會把結果儲存在 ${output} 變數裡.
  3. Should Be Equal ${output} Hello SSHLibrary!
    透過 Build-in 預設關鍵字 Should Be Equal 檢查 {output} 變數與後面緊接著的預期結果.
    除了 Should Be Equal 外還有以以下 Should Contain , Should Be Empty , Should Be Equal As Integers 等..

    其中的字串比對 Pattern matching 使用 fnmatch 來定義,請參考 – https://docs.python.org/3/library/fnmatch.html

    • *
      matches everything
    • ?
      matches any single character
    • [seq]
      matches any character in seq
    • [!seq]
      matches any character not in seq

    Keywords

    這邊就是可自行定義關鍵字可供呼叫,自訂 Open Connection And Log In 關鍵字裡面用的 Open Connection 與 Login 都是 SSHLibrary 預先定義好的 Keywords.

    這個 Open Connection And Log In 在 Settings 的 Suite Setup 呼叫過.

    *** Keywords ***
    Open Connection And Log In
       Open Connection     ${HOST}
       Login               ${USERNAME}        ${PASSWORD}
    

    執行結果.

    [root@localhost ~]# robot ssh-sample.robot
    /usr/local/lib/python3.6/site-packages/paramiko/transport.py:32: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography (40.0) will be the last to support Python 3.6.
      from cryptography.hazmat.backends import default_backend
    ==============================================================================
    Ssh-Sample :: This example demonstrates executing a command on a remote mac...
    ==============================================================================
    Execute Command And Verify Output :: Execute Command can be used t... | PASS |
    ------------------------------------------------------------------------------
    Ssh-Sample :: This example demonstrates executing a command on a r... | PASS |
    1 test, 1 passed, 0 failed
    ==============================================================================
    Output:  /root/output.xml
    Log:     /root/log.html
    Report:  /root/report.html
    

    上面會有簡單的執行結果,或是檢視 Robot Framework 所產生的 output.xml , log.html 與 report.html 來看詳細結果.

    沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料