Test multiple nxos versions at once (#25595)

* Extend tests to have multiple device representations

* Move filepath munging to nxos_module

* Device needs to be kwarg so we can leave it off

* Update other nxos tests

* Update tests that fell through
This commit is contained in:
Nathaniel Case 2017-06-27 12:45:36 -04:00 committed by GitHub
parent 79b2897462
commit 3d6d428bbc
30 changed files with 86 additions and 72 deletions

View file

@ -0,0 +1,2 @@
nxapi enabled
HTTP Listen on port 80

View file

@ -36,8 +36,10 @@ fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def load_fixture(name):
path = os.path.join(fixture_path, name)
def load_fixture(module_name, name, device=''):
path = os.path.join(fixture_path, module_name, device, name)
if not os.path.exists(path):
path = os.path.join(fixture_path, module_name, name)
if path in fixture_data:
return fixture_data[path]
@ -64,9 +66,27 @@ class AnsibleFailJson(Exception):
class TestNxosModule(unittest.TestCase):
def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False):
def execute_module_devices(self, failed=False, changed=False, commands=None, sort=True, defaults=False):
module_name = self.module.__name__.rsplit('.', 1)[1]
local_fixture_path = os.path.join(fixture_path, module_name)
self.load_fixtures(commands)
models = []
for path in os.listdir(local_fixture_path):
path = os.path.join(local_fixture_path, path)
if os.path.isdir(path):
models.append(os.path.basename(path))
if not models:
models = ['']
retvals = {}
for model in models:
retvals[model] = self.execute_module(failed, changed, commands, sort, device=model)
return retvals
def execute_module(self, failed=False, changed=False, commands=None, sort=True, device=''):
self.load_fixtures(commands, device=device)
if failed:
result = self.failed()
@ -110,5 +130,5 @@ class TestNxosModule(unittest.TestCase):
self.assertEqual(result['changed'], changed, result)
return result
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
pass

View file

@ -41,7 +41,7 @@ class TestNxosAclModule(TestNxosModule):
self.mock_run_commands.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
@ -52,9 +52,8 @@ class TestNxosAclModule(TestNxosModule):
command = obj['command']
except ValueError:
command = item
filename = str(command).split(' | ')[0].replace(' ', '_')
filename = 'nxos_acl/%s.txt' % filename
output.append(load_fixture(filename))
filename = '%s.txt' % str(command).split(' | ')[0].replace(' ', '_')
output.append(load_fixture('nxos_acl', filename))
return output
self.run_commands.side_effect = load_from_file

View file

@ -41,7 +41,7 @@ class TestNxosAclInterfaceModule(TestNxosModule):
self.mock_run_commands.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
@ -52,9 +52,8 @@ class TestNxosAclInterfaceModule(TestNxosModule):
command = obj['command']
except ValueError:
command = item
filename = str(command).split(' | ')[0].replace(' ', '_')
filename = 'nxos_acl_interface/%s.txt' % filename
output.append(load_fixture(filename))
filename = '%s.txt' % str(command).split(' | ')[0].replace(' ', '_')
output.append(load_fixture('nxos_acl_interface', filename))
return output
self.run_commands.side_effect = load_from_file

View file

@ -39,7 +39,7 @@ class TestNxosBannerModule(TestNxosModule):
self.mock_run_commands.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
self.load_config.return_value = dict(diff=None, session='session')
def test_nxos_banner_create(self):

View file

@ -41,8 +41,8 @@ class TestNxosBgpModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_get_config.stop()
def load_fixtures(self, commands=None):
self.get_config.return_value = load_fixture('nxos_bgp_config.cfg')
def load_fixtures(self, commands=None, device=''):
self.get_config.return_value = load_fixture('', 'nxos_bgp_config.cfg')
self.load_config.return_value = None
def test_nxos_bgp(self):

View file

@ -41,8 +41,8 @@ class TestNxosBgpAfModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_get_config.stop()
def load_fixtures(self, commands=None):
self.get_config.return_value = load_fixture('nxos_bgp_config.cfg')
def load_fixtures(self, commands=None, device=''):
self.get_config.return_value = load_fixture('', 'nxos_bgp_config.cfg')
self.load_config.return_value = None
def test_nxos_bgp_af(self):

View file

@ -41,8 +41,8 @@ class TestNxosBgpNeighborModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_get_config.stop()
def load_fixtures(self, commands=None):
self.get_config.return_value = load_fixture('nxos_bgp_config.cfg')
def load_fixtures(self, commands=None, device=''):
self.get_config.return_value = load_fixture('', 'nxos_bgp_config.cfg')
self.load_config.return_value = None
def test_nxos_bgp_neighbor(self):

View file

@ -41,8 +41,8 @@ class TestNxosBgpNeighborAfModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_get_config.stop()
def load_fixtures(self, commands=None):
self.get_config.return_value = load_fixture('nxos_bgp_config.cfg')
def load_fixtures(self, commands=None, device=''):
self.get_config.return_value = load_fixture('', 'nxos_bgp_config.cfg')
self.load_config.return_value = None
def test_nxos_bgp_neighbor_af(self):

View file

@ -37,7 +37,7 @@ class TestNxosCommandModule(TestNxosModule):
def tearDown(self):
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
@ -48,9 +48,8 @@ class TestNxosCommandModule(TestNxosModule):
command = obj['command']
except ValueError:
command = item['command']
filename = str(command).replace(' ', '_')
filename = 'nxos_command/%s.txt' % filename
output.append(load_fixture(filename))
filename = '%s.txt' % str(command).replace(' ', '_')
output.append(load_fixture('nxos_command', filename))
return output
self.run_commands.side_effect = load_from_file

View file

@ -43,8 +43,8 @@ class TestNxosConfigModule(TestNxosModule):
self.mock_get_config.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
self.get_config.return_value = load_fixture('nxos_config/config.cfg')
def load_fixtures(self, commands=None, device=''):
self.get_config.return_value = load_fixture('nxos_config', 'config.cfg')
self.load_config.return_value = None
def test_nxos_config_no_change(self):
@ -53,7 +53,7 @@ class TestNxosConfigModule(TestNxosModule):
result = self.execute_module()
def test_nxos_config_src(self):
args = dict(src=load_fixture('nxos_config/candidate.cfg'))
args = dict(src=load_fixture('nxos_config', 'candidate.cfg'))
set_module_args(args)
result = self.execute_module(changed=True)

View file

@ -41,15 +41,15 @@ class TestNxosEvpnGlobalModule(TestNxosModule):
self.mock_get_config.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
self.load_config.return_value = None
def start_configured(self, *args, **kwargs):
self.get_config.return_value = load_fixture('nxos_evpn_global/configured.cfg')
self.get_config.return_value = load_fixture('nxos_evpn_global', 'configured.cfg')
return self.execute_module(*args, **kwargs)
def start_unconfigured(self, *args, **kwargs):
self.get_config.return_value = load_fixture('nxos_evpn_global/unconfigured.cfg')
self.get_config.return_value = load_fixture('nxos_evpn_global', 'unconfigured.cfg')
return self.execute_module(*args, **kwargs)
def test_nxos_evpn_global_enable(self):

View file

@ -45,8 +45,8 @@ class TestNxosEvpnVniModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_get_config.stop()
def load_fixtures(self, commands=None):
self.get_config.return_value = load_fixture('nxos_evpn_vni_config.cfg')
def load_fixtures(self, commands=None, device=''):
self.get_config.return_value = load_fixture('', 'nxos_evpn_vni_config.cfg')
self.load_config.return_value = None
def test_nxos_evpn_vni_present(self):

View file

@ -41,7 +41,7 @@ class TestNxosFeatureModule(TestNxosModule):
self.mock_run_commands.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
@ -52,9 +52,8 @@ class TestNxosFeatureModule(TestNxosModule):
command = obj['command']
except ValueError:
command = item['command']
filename = str(command).replace(' ', '_')
filename = 'nxos_feature/%s.txt' % filename
output.append(load_fixture(filename))
filename = '%s.txt' % str(command).replace(' ', '_')
output.append(load_fixture('nxos_feature', filename))
return output
self.run_commands.side_effect = load_from_file

View file

@ -41,7 +41,7 @@ class TestNxosHsrpModule(TestNxosModule):
self.mock_run_commands.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
self.load_config.return_value = None
def test_nxos_hsrp(self):

View file

@ -45,7 +45,7 @@ class TestNxosInterfaceModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_get_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
self.load_config.return_value = None
def test_nxos_interface_up(self):

View file

@ -47,9 +47,9 @@ class TestNxosIPInterfaceModule(TestNxosModule):
self.mock_send_show_command.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
self.get_interface_mode.return_value = 'layer3'
self.send_show_command.return_value = [load_fixture('nxos_ip_interface.cfg')]
self.send_show_command.return_value = [load_fixture('', 'nxos_ip_interface.cfg')]
self.load_config.return_value = None
def test_nxos_ip_interface_ip_present(self):

View file

@ -42,15 +42,15 @@ class TestNxosNxapiModule(TestNxosModule):
self.mock_run_commands.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
module_name = self.module.__name__.rsplit('.', 1)[1]
output = list()
for command in commands:
filename = str(command).replace(' ', '_')
filename = os.path.join('nxos_nxapi', filename)
output.append(load_fixture(filename))
filename = str(command).split(' | ')[0].replace(' ', '_')
output.append(load_fixture(module_name, filename, device))
return output
self.run_commands.side_effect = load_from_file
@ -58,12 +58,12 @@ class TestNxosNxapiModule(TestNxosModule):
def test_nxos_nxapi_no_change(self):
set_module_args(dict(http=True, https=False, http_port=80, https_port=443, sandbox=False))
self.execute_module(changed=False, commands=[])
self.execute_module_devices(changed=False, commands=[])
def test_nxos_nxapi_disable(self):
set_module_args(dict(state='absent'))
self.execute_module(changed=True, commands=['no feature nxapi'])
self.execute_module_devices(changed=True, commands=['no feature nxapi'])
def test_nxos_nxapi_no_http(self):
set_module_args(dict(https=True, http=False, https_port=8443))
self.execute_module(changed=True, commands=['no nxapi http', 'nxapi https port 8443'])
self.execute_module_devices(changed=True, commands=['no nxapi http', 'nxapi https port 8443'])

View file

@ -41,7 +41,7 @@ class TestNxosOspfModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_get_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
self.load_config.return_value = None
def test_nxos_ospf_present(self):

View file

@ -41,7 +41,7 @@ class TestNxosOspfVrfModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_get_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
self.load_config.return_value = None
def test_nxos_ospf_vrf_present(self):

View file

@ -45,7 +45,7 @@ class TestNxosPortchannelModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_get_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
self.load_config.return_value = None
def test_nxos_portchannel(self):

View file

@ -41,8 +41,8 @@ class TestNxosStaticRouteModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_get_config.stop()
def load_fixtures(self, commands=None):
self.get_config.return_value = load_fixture('nxos_static_route.cfg')
def load_fixtures(self, commands=None, device=''):
self.get_config.return_value = load_fixture('', 'nxos_static_route.cfg')
self.load_config.return_value = None
def test_nxos_static_route_present(self):

View file

@ -41,8 +41,8 @@ class TestNxosSystemModule(TestNxosModule):
self.mock_get_config.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
self.get_config.return_value = load_fixture('nxos_system_config.cfg')
def load_fixtures(self, commands=None, device=''):
self.get_config.return_value = load_fixture('', 'nxos_system_config.cfg')
self.load_config.return_value = None
def test_nxos_system_hostname_changed(self):

View file

@ -44,7 +44,7 @@ class TestNxosVlanModule(TestNxosModule):
self.mock_run_commands.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
@ -55,9 +55,8 @@ class TestNxosVlanModule(TestNxosModule):
command = obj['command']
except ValueError:
command = item
filename = str(command).split(' | ')[0].replace(' ', '_')
filename = 'nxos_vlan/%s.txt' % filename
output.append(load_fixture(filename))
filename = '%s.txt' % str(command).split(' | ')[0].replace(' ', '_')
output.append(load_fixture('nxos_vlan', filename))
return output
self.run_commands.side_effect = load_from_file

View file

@ -41,15 +41,14 @@ class TestNxosVpcModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
for command in commands:
filename = str(command).split(' | ')[0].replace(' ', '_')
filename = os.path.join('nxos_vpc', filename)
output.append(load_fixture(filename))
output.append(load_fixture('nxos_vpc', filename))
return output
self.load_config.return_value = None

View file

@ -45,14 +45,13 @@ class TestNxosVpcModule(TestNxosModule):
self.mock_get_config.stop()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
for command in commands:
filename = str(command).split(' | ')[0].replace(' ', '_')
filename = os.path.join('nxos_vpc_interface', filename)
output.append(load_fixture(filename))
output.append(load_fixture('nxos_vpc_interface', filename))
return output
self.run_commands.side_effect = load_from_file

View file

@ -41,15 +41,14 @@ class TestNxosVrfModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
for command in commands:
filename = str(command).split(' | ')[0].replace(' ', '_')
filename = os.path.join('nxos_vrf', filename)
output.append(load_fixture(filename))
output.append(load_fixture('nxos_vrf', filename))
return output
self.load_config.return_value = None

View file

@ -44,7 +44,7 @@ class TestNxosVrfafModule(TestNxosModule):
self.mock_load_config.stop()
self.mock_get_config.stop()
def load_fixtures(self, commands=None):
def load_fixtures(self, commands=None, device=''):
self.load_config.return_value = None
def test_nxos_vrf_af_present(self):

View file

@ -42,7 +42,7 @@ class TestNxosVxlanVtepVniModule(TestNxosModule):
self.mock_load_config.stop()
def load_fixtures(self, commands=None, device=''):
self.get_config.return_value = load_fixture('nxos_vxlan_vtep/config.cfg')
self.get_config.return_value = load_fixture('nxos_vxlan_vtep', 'config.cfg')
self.load_config.return_value = None
def test_nxos_vxlan_vtep(self):