LoginSignup
5
3

More than 3 years have passed since last update.

Robot Framework 使ってみる①

Last updated at Posted at 2019-09-04

背景

ネットワーク機器の試験自動化に向けてやらざるを得なくなった。
robotframeworkを選んだのは日本語ドキュメントが多そうなのと、レポートが綺麗だから。

環境

VPS001.png
VPS上にcentosとvyosのDockerコンテナを作成し、centosにrobotframeworkをインストール。

やってみたいこと

  1. vyosのshowコマンドの出力結果を取得する
  2. vyos内のファイルを取得する
  3. vyos→vyos2へのpingを実行し100%成功する事を確認する

はじめの一歩

とりあえず、ssh接続しないと何もできなさそうなので色々見ながらやってみる。

SSH接続で基本的な記述を確認する

test00.robot
*** Settings ***
Library  SSHLibrary

*** Test Cases ***
ssh test
    Open Connection  172.17.0.2
    Login   vyos  vyos
    ${output} =  Execute Command  echo SSH is succeeded!
    Log To Console  ${\n}${output}
    Should Be Equal  ${output}  SSH is succeeded!
    Close All Connections
result of test00.robot
# robot test00.robot
==============================================================================
Test00
==============================================================================
ssh test                                                              ...
SSH is succeeded!
ssh test                                                              | PASS |
------------------------------------------------------------------------------
Test00                                                                | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  /root/output.xml
Log:     /root/log.html
Report:  /root/report.html
#

10行程度で期待動作をしてくれた。

変数テーブルの定義はしておいた方がよさげ

test01.robot
*** Settings ***
Library  SSHLibrary

*** Variables ***
${user}       vyos
${password}   vyos
${ip1}  172.17.0.2
${ip2}  172.17.0.3

*** Test Cases ***
ssh test of vyos
    Open Connection  ${ip1}
    Login  ${user}  ${password}
    ${output} =  Execute Command  echo SSH to vyos is OK!
    Log To Console  ${\n}${output}
    Should Be Equal  ${output}  SSH to vyos is OK!
    Close All Connections

ssh test of vyos2
    Open Connection  ${ip2}
    Login  ${user}  ${password}
    ${output} =  Execute Command  echo SSH to vyos2 is OK!
    Log To Console  ${\n}${output}
    Should Be Equal  ${output}  SSH to vyos2 is OK!
    Close All Connections
result of test01.robot
# robot test01.robot
==============================================================================
Test01
==============================================================================
ssh test of vyos                                                      ...
SSH to vyos is OK!
ssh test of vyos                                                      | PASS |
------------------------------------------------------------------------------
ssh test of vyos2                                                     ...
SSH to vyos2 is OK!
ssh test of vyos2                                                     | PASS |
------------------------------------------------------------------------------
Test01                                                                | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Output:  /root/output.xml
Log:     /root/log.html
Report:  /root/report.html

*** Variables *** でスカラ変数を定義するだけでOK。
log.htmlも見やすい。
cap1.png

Keywordを定義することもできるようだ

test02.robot
*** Settings ***
Library  SSHLibrary

*** Variables ***
${user}       vyos
${password}   vyos
${ip1}  172.17.0.2
${ip2}  172.17.0.3

*** Test Cases ***
ssh test of vyos
    vyosに接続
    vyosにログイン
    ${output} =  Execute Command  echo SSH to vyos is OK!
    Log To Console  ${\n}${output}
    Should Be Equal  ${output}  SSH to vyos is OK!
    Close All Connections

ssh test of vyos2
    Open Connection  ${ip2}
    Login  ${user}  ${password}
    ${output} =  Execute Command  echo SSH to vyos2 is OK!
    Log To Console  ${\n}${output}
    Should Be Equal  ${output}  SSH to vyos2 is OK!
    Close All Connections

*** Keywords ***
vyosに接続
    Open Connection  ${ip1}
vyosにログイン
    Login  ${user}  ${password}
result of test02.robot
# robot test02.robot
==============================================================================
Test02
==============================================================================
ssh test of vyos                                                      ...
SSH to vyos is OK!
ssh test of vyos                                                      | PASS |
------------------------------------------------------------------------------
ssh test of vyos2                                                     ...
SSH to vyos2 is OK!
ssh test of vyos2                                                     | PASS |
------------------------------------------------------------------------------
Test02                                                                | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Output:  /root/output.xml
Log:     /root/log.html
Report:  /root/report.html
#

次回はリモートホストでのコマンド実行をやってみよう

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3