Skip to content

Commit

Permalink
TESTS: Update test framework (#402)
Browse files Browse the repository at this point in the history
* Test with pytest from tox instead of using Ansible

* Update tests to run with python3.6

* Update netiron show monitor commands to use a single template

* Remove redundant test, ``test_that_all_entries_dicts_match``

* Change to using yaml's safe_load method

* Group tests together in order to load files and call TextFSM once, which creates faster test runs

* Fix spacing and styling

* Add failure messages to tests
  • Loading branch information
jmcgill298 authored Apr 24, 2019
1 parent 29c693d commit 80b5171
Show file tree
Hide file tree
Showing 28 changed files with 153 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ dist/
downloads/
eggs/
.eggs/
lib/
#lib/
lib64/
parts/
sdist/
Expand Down
12 changes: 3 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
language: python

python:
- "2.7"
- "3.6"

sudo: false

install:
- pip install netmiko
- pip install textfsm
- export PYTHONPATH=$PYTHONPATH:/home/travis/build/networktocode/ntc-templates/gtextfsm-0.2.1/textfsm
- pip install ansible==1.9.2
- pip install terminal
- git clone https://github.com/networktocode/ntc-ansible.git

- pip install tox

script:
- python test-templates.py
- tox
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions lib/ntc_templates/templates
1 change: 0 additions & 1 deletion ntc_templates/templates

This file was deleted.

9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import re
from codecs import open
from setuptools import setup
from setuptools import setup, find_packages

version = ''
with open('ntc_templates/__init__.py', 'r') as fd:
with open('lib/ntc_templates/__init__.py', 'r') as fd:
version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
fd.read(), re.MULTILINE).group(1)

Expand All @@ -20,7 +20,8 @@

config = {
'name': 'ntc_templates',
'packages': ['ntc_templates'],
'package_dir': {'': 'lib'},
'packages': find_packages('lib'),
'version': version,
'package_data': {'ntc_templates': ['templates/*']},
'description': 'Package to return structured data from the output of network devices.',
Expand All @@ -35,7 +36,7 @@
'classifiers': ['Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'Programming Language :: Python :: 2.7'],
'Programming Language :: Python :: 3'],
'zip_safe': False
}

Expand Down
2 changes: 1 addition & 1 deletion templates/index
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ brocade_netiron_show_running-config_interface.template, .*, brocade_netiron, sh[
brocade_netiron_show_lldp_neighbors_detail.template, .*, brocade_netiron, sh[[ow]] ll[[dp]] n[[eighbors]] d[[etail]]
brocade_netiron_show_running-config_vlan.template, .*, brocade_netiron, sh[[ow]] ru[[nning-config]] v[[lan]]
brocade_netiron_show_interfaces_brief.template, .*, brocade_netiron, sh[[ow]] in[[terfaces]] b[[rief]]
brocade_netiron_show_monitor_actual.template, .*, brocade_netiron, sh[[ow]] mon[[itor]] (?:ac|co)
brocade_netiron_show_interfaces.template, .*, brocade_netiron, sh[[ow]] in[[terfaces]]
brocade_netiron_show_lag_brief.template, .*, brocade_netiron, sh[[ow]] lag b[[rief]]
brocade_netiron_show_monitor.template, .*, brocade_netiron, sh[[ow]] mon[[itor]] [actual|config]
brocade_netiron_show_metro.template, .*, brocade_netiron, sh[[ow]] met[[ro-ring]]
brocade_netiron_show_span.template, .*, brocade_netiron, sh[[ow]] sp[[anning-tree]]
brocade_netiron_show_topo.template, .*, brocade_netiron, sh[[ow]] to[[pology-group]]
Expand Down
17 changes: 17 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""tests."""

import os
import csv

from ntc_templates.parse import _get_template_dir


def load_index_data():
"""Load data from index file."""
index_data = []
with open('%s%sindex' % (_get_template_dir(), os.sep)) as indexfs:
data = csv.reader(indexfs)
for row in data:
if len(row) > 2 and row[0] != 'Template':
index_data.append(row)
return index_data

This file was deleted.

This file was deleted.

30 changes: 30 additions & 0 deletions tests/cisco_ios/show_ip_ospf_neighbor/four_neighbors.parsed
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
parsed_sample:

- neighbor_id: '10.190.30.2'
priority: '1'
state: 'FULL/BDR'
dead_time: '00:00:04'
address: '10.190.16.11'
interface: 'GigabitEthernet0/2.101'

- neighbor_id: '10.190.30.3'
priority: '1'
state: 'FULL/DR'
dead_time: '00:00:04'
address: '10.190.16.12'
interface: 'GigabitEthernet0/2.101'

- neighbor_id: '10.190.30.2'
priority: '1'
state: 'FULL/BDR'
dead_time: '00:00:04'
address: '10.190.16.3'
interface: 'GigabitEthernet0/2.100'

- neighbor_id: '10.190.30.3'
priority: '1'
state: 'FULL/DR'
dead_time: '00:00:04'
address: '10.190.16.4'
interface: 'GigabitEthernet0/2.100'
5 changes: 5 additions & 0 deletions tests/cisco_ios/show_ip_ospf_neighbor/four_neighbors.raw
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Neighbor ID Pri State Dead Time Address Interface
10.190.30.2 1 FULL/BDR 00:00:04 10.190.16.11 GigabitEthernet0/2.101
10.190.30.3 1 FULL/DR 00:00:04 10.190.16.12 GigabitEthernet0/2.101
10.190.30.2 1 FULL/BDR 00:00:04 10.190.16.3 GigabitEthernet0/2.100
10.190.30.3 1 FULL/DR 00:00:04 10.190.16.4 GigabitEthernet0/2.100
54 changes: 23 additions & 31 deletions tests/test_index_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@
import glob
import csv

from tests import load_index_data


def load_indexdata():
"""Load data from index file."""
index_data = []
with open('./templates/index') as indexfs:
data = csv.reader(indexfs)
for row in data:
if len(row) > 2 and row[0] != 'Template':
index_data.append(row)
return index_data

def check_order(current_os, prior_os,cmd_len, prior_len, os_choices, used_os, cmd, prior_cmd):
def check_order(current_os, prior_os, cmd_len, prior_len, os_choices, used_os, cmd, prior_cmd):
add_os_check = []

if current_os not in used_os and used_os is not None:
Expand All @@ -27,36 +18,41 @@ def check_order(current_os, prior_os,cmd_len, prior_len, os_choices, used_os, cm

if used_os != sorted(used_os):
msg = "OS's are not in alpabetical order, current order: '{}'".format(used_os)
return False , msg
return False, msg
elif add_os_check != sorted(add_os_check):
msg = "OS's are not in alpabetical order, current order: '{}'".format(add_os_check)
return False , msg
return False, msg

if current_os not in os_choices:
msg = "'{}' is not one of the valid options '{}'".format(current_os, used_os)
return False, msg

if not prior_os and prior_len == 0:
# Starting Point
return True , ''
return True, ''
elif current_os == prior_os and cmd_len == prior_len and cmd == min(prior_cmd, cmd):
msg = "OS is the same and command same length, please set {} before {} to be in alphabetical order".format(cmd, prior_cmd)
msg = (
"OS is the same and command same length, "
"please set {} before {} to be in alphabetical order".format(cmd, prior_cmd)
)
return False, msg
elif current_os == prior_os and cmd_len <= prior_len:
# OS is the same as previous, and cmd_len is smaller or equal to prior so good
return True , ''
return True, ''
elif current_os != prior_os and current_os not in used_os:
# prior OS has changed, do not need to check for length yet
return True , ''
return True, ''
elif current_os == prior_os and cmd_len > prior_len:
msg = "Current Command len '{}' larger then previous '{}', for command '{}'".format(cmd_len, prior_len, cmd )
return False , msg
msg = "Current Command len '{}' larger then previous '{}', for command '{}'".format(
cmd_len, prior_len, cmd
)
return False, msg
elif current_os != prior_os and current_os in used_os:
msg = "'{}' OS was already used in list on command '{}'".format(current_os, cmd)
return False , msg
return False, msg
else:
msg = "Failed for unknown reason"
return False , msg
return False, msg

def test_index_ordering():

Expand All @@ -67,34 +63,30 @@ def test_index_ordering():
'cisco_xe', 'cisco_xr', 'dell_force10', 'enterasys', 'extreme', 'f5_ltm', 'fortinet',
'hp_comware', 'hp_procurve', 'huawei', 'juniper', 'juniper_junos', 'juniper_screenos',
'alcatel_sros', 'linux', 'ovs_linux', 'paloalto_panos', 'quanta_mesh',
'ubiquiti_edgeswitch', 'vmware_nsxv', 'vyatta_vyos', 'vyos'
'ubiquiti_edgeswitch', 'vmware_nsxv', 'vyatta_vyos', 'vyos',
]

prior_os = ""
prior_len = 0
prior_cmd = ""
used_os = []

index = load_indexdata()
index = load_index_data()
for row in index:
template = row[0].strip()
os = '_'.join(template.split('_')[:2])
cmd = '_'.join(template.split('_')[2:])
cmd_len = len(cmd)
check_val, check_msg = check_order(os, prior_os, cmd_len, prior_len, os_choices, used_os, cmd, prior_cmd)
check_val, check_msg = check_order(
os, prior_os, cmd_len, prior_len, os_choices, used_os, cmd, prior_cmd
)
if not check_val:
#assertFalse(check_val, msg=check_msg)
print("Error on line: {}".format(row))
print("Error Message: {}".format(check_msg))
assert check_val != False
assert check_val
if os not in used_os:
used_os.append(os)
prior_len = cmd_len
prior_cmd = cmd
prior_os = os

def main():
test_index_ordering()

if __name__ == "__main__":
main()
Loading

0 comments on commit 80b5171

Please sign in to comment.