Robot Framework 使ってみる①

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

#環境
![VPS001.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/284963/fb35b75f-f657-ced8-628f-9008200214b8.png)
VPS上にcentosとvyosのDockerコンテナを作成し、centosにrobotframeworkをインストール。

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

#はじめの一歩 
とりあえず、ssh接続しないと何もできなさそうなので色々見ながらやってみる。
###SSH接続で基本的な記述を確認する

```text: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
```


```text:result&nbsp;of&nbsp;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行程度で期待動作をしてくれた。

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

```text: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
```

```text:result&nbsp;of&nbsp;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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/284963/6499c346-095e-6337-4f37-a0152d731016.png)

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

```text: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}
```

```text:result&nbsp;of&nbsp;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
#
```

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