RobotFrameWorkで簡単な判定をさせてみた
モチベが高い今こそ自分を叩いて学習しようという安易な気持ちでタイトルをつけました
タイトルからは想像が一切つかない内容ですみません、
今回は短めの記事になります。
前回のおさらい
持てる力をすべて使いpyats動かしてみた
詳細は以下
https://qiita.com/snamon/items/78eb3e314bf8e5787719
構成/対象機器
サーバは前回と同じくCentos8になります。
インストール手順などは前回の記事を参考ください。
予約無しでいつでも使うことができます(便利)
今回の対象機器はIOSXRになります
Cisco IOS XR Software, Version 6.5.3
Testbedの作成
今回はpyatsのprojectは作成せずにRobotファイルを作成して、showコマンドの判定をしてみます。
Testbedは今回も必要になるので作成していきます。
[root@localhost ~]# source pyATS/bin/activate
(pyATS) [root@localhost ~]#
(pyATS) [root@localhost ~]# mkdir Robot
(pyATS) [root@localhost ~]#
(pyATS) [root@localhost ~]# cd Robot/
(pyATS) [root@localhost Robot]# vi testbed.yml
そしてTestbed作成ですが、
その前に一度対象機器にTeratermなどでLoginしておきます
SandBoxに入りUser/passとportを確認してSSH
このときプロンプトに表示されるHostnameも確認しておきましょう
では今度こそ作成
---
testbed:
name: RobotFrameWork_Testbed
tacacs:
username: admin
passwords:
tacacs: C1sco12345
devices:
cisco.test:
connections:
defaults:
class: 'unicon.Unicon'
vty:
protocol: ssh
ip: sbx-iosxr-mgmt.cisco.com
port: 8181
os: iosxr
type: iosxr
ここまで来たらtestbedが正しいのか接続確認を実施します。
方法はいくつかあるようですが、今回はgenie shellコマンドでConnectionCheckします。
(pyATS) [root@localhost Robot]# genie shell --testbed testbed.yml
Welcome to Genie Interactive Shell
==================================
Python 3.6.8 (default, Oct 7 2019, 17:58:22)
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)]
>>> from genie.testbed import load
>>> testbed = load('testbed.yml')
-------------------------------------------------------------------------------
>>> test = testbed.devices['cisco.test']
>>> test.connect()
[2019-10-23 12:06:17,190] +++ cisco.test logfile /tmp/cisco.test-cli-20191023T120617188.log +++
[2019-10-23 12:06:17,192] +++ Unicon plugin iosxr +++
The authenticity of host '[64.103.37.3]:8181 ([64.103.37.3]:8181)' can't be established.
RSA key fingerprint is SHA256:11wzj9NmCIPoZpMxGcUtYYYkvTozWnFnIF8eKaMIWoI.
Are you sure you want to continue connecting (yes/no)?
[2019-10-23 12:06:18,714] +++ connection to spawn: ssh -l admin 64.103.37.3 -p 8181, id: 139739676523936 +++
[2019-10-23 12:06:18,715] connection to cisco.test
yes
Warning: Permanently added '[64.103.37.3]:8181' (RSA) to the list of known hosts.
Password:
RP/0/RP0/CPU0:cisco.test#
[2019-10-23 12:06:21,778] +++ initializing handle +++
[2019-10-23 12:06:21,782] +++ cisco.test: executing command 'terminal length 0' +++
terminal length 0
Wed Oct 23 16:10:35.058 UTC
RP/0/RP0/CPU0:cisco.test#
[2019-10-23 12:06:22,424] +++ cisco.test: executing command 'terminal width 0' +++
terminal width 0
Wed Oct 23 16:10:35.683 UTC
RP/0/RP0/CPU0:cisco.test#
[2019-10-23 12:06:23,052] +++ cisco.test: config +++
configure terminal
Wed Oct 23 16:10:36.320 UTC
RP/0/RP0/CPU0:cisco.test(config)#no logging console
RP/0/RP0/CPU0:cisco.test(config)#line console
RP/0/RP0/CPU0:cisco.test(config-line)#exec-timeout 0 0
RP/0/RP0/CPU0:cisco.test(config-line)#absolute-timeout 0
RP/0/RP0/CPU0:cisco.test(config-line)#session-timeout 0
RP/0/RP0/CPU0:cisco.test(config-line)#line default
RP/0/RP0/CPU0:cisco.test(config-line)#exec-timeout 0 0
RP/0/RP0/CPU0:cisco.test(config-line)#absolute-timeout 0
RP/0/RP0/CPU0:cisco.test(config-line)#session-timeout 0
RP/0/RP0/CPU0:cisco.test(config-line)#commit
Wed Oct 23 16:10:42.953 UTC
RP/0/RP0/CPU0:cisco.test(config-line)#end
RP/0/RP0/CPU0:cisco.test#
"Are you sure you want to continue connecting (yes/no)? yes\r\nWarning: Permanently added '[64.103.37.3]:8181' (RSA) to the list of known hosts.\r\r\nPassword: \r\n\r\n\r\nRP/0/RP0/CPU0:cisco.test#"
>>>
問題なさそうですね。
今回のテスト内容
RobotFrameWorkを使って以下をチェックします。
- 10.10.20.0/24のルーティングテーブルに登録されていること
- デフォルトルートがルーティングテーブルに登録されていること
- Gigabit0/0/0/0がAdminDownであること
さっそくテスト用のファイルを作成します
**** Settings ***
Library ats.robot.pyATSRobot
Library genie.libs.robot.GenieRobot
Library unicon.robot.UniconRobot
Suite setup Setup
*** Variables ***
${testbed} testbed.yml
${ip1} 10.10.20.0/24
*** Test Cases ***
Verify Route
${cmd}= parse "show route ipv4" on device "cisco.test"
should contain ${cmd}[vrf][default][address_family][ipv4][routes] ${ip1}
should contain ${cmd}[vrf][default][address_family][ipv4][routes] 0.0.0.0/0
Verify Interface
${cmd}= parse "show interface brief" on device "cisco.test"
should contain ${cmd}[interface][ethernet][GigabitEthernet0/0/0/0][status] admin-down
*** Keywords ***
Setup
use genie testbed "${testbed}"
パッと見た感じで多少無理がある感がでているがご愛嬌
コマンドは下記のgenieのドキュメントから選んできました。
https://developer.cisco.com/docs/genie-docs/
なーんかイケてない書き方なきがしてならないです。。。
実行ログ
------------------------------------------------------------------------------
NWtest | FAIL |
Suite setup failed:
ConnectionError: failed to connect to cisco.test
Failed while bringing device to "any" state
2 critical tests, 0 passed, 2 failed
2 tests total, 0 passed, 2 failed
==============================================================================
Output: /root/Robot/output.xml
Log: /root/Robot/log.html
Report: /root/Robot/report.html
(pyATS) [root@localhost Robot]#
あれれ、、失敗した。。
ConnectionErrorですね、、何度か接続確認はできたので妙だなと思いつつも実機へ再度SSHしてみると。。。
ええぇ、、hostname変わってる。。
こういうところも自動化の注意点ですね、手で触るユーザがのこるとこんなリスクもあるのか。
今回はフリーの検証環境なのでしょうがないですね
hostnameを入れ替えて再度実行!
実行ログ
(pyATS) [root@localhost Robot]# robot NWtest.robot
==============================================================================
NWtest
==============================================================================
[ WARN ] Could not load the Datafile correctly
Verify Route | PASS |
------------------------------------------------------------------------------
Verify Interface | PASS |
------------------------------------------------------------------------------
NWtest | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Output: /root/Robot/output.xml
Log: /root/Robot/log.html
Report: /root/Robot/report.html
(pyATS) [root@localhost Robot]#
無事成功!
実行結果として以下が作成されています(failのときも作成されてますね)
- output.xml
- log.html
- report.html
こんな感じです。
何やら確認をしてくれているようなんですが、、いまいちわからない。。
まとめ
うまくいってんだかよくわからねぇ
が、確認項目をミリミリかければ正常性確認はできそうな気がしました。
単体試験や結合試験でどうやって使っていくか?についてはこれからも考えていこうと思います。