class TestNetDevicesWithAcls(unittest.TestCase): """ Test NetDevices with ``settings.WITH_ACLs set`` to ``True``. """ def setUp(self): self.nd = NetDevices() self.nodename = self.nd.keys()[0] self.device = self.nd.values()[0] self.device.explicit_acls = set(['test1-abc-only']) def test_basics(self): """Basic test of NetDevices functionality.""" self.assertEqual(len(self.nd), 1) self.assertEqual(self.device.nodeName, self.nodename) self.assertEqual(self.device.manufacturer, 'JUNIPER') def test_aclsdb(self): """Test acls.db handling.""" self.assertTrue('test1-abc-only' in self.device.explicit_acls) def test_autoacls(self): """Test autoacls.py handling.""" self.assertTrue('router-protect.core' in self.device.implicit_acls) def test_find(self): """Test the find() method.""" self.assertEqual(self.nd.find(self.nodename), self.device) nodebasename = self.nodename[:self.nodename.index('.')] self.assertEqual(self.nd.find(nodebasename), self.device) self.assertRaises(KeyError, lambda: self.nd.find(self.nodename[0:3])) def test_all(self): """Test the all() method.""" expected = [self.device] self.assertEqual(expected, self.nd.all()) def test_search(self): """Test the search() method.""" expected = [self.device] self.assertEqual(expected, self.nd.search(self.nodename)) self.assertEqual(expected, self.nd.search('17', field='onCallID')) self.assertEqual(expected, self.nd.search('juniper', field='vendor')) def test_match(self): """Test the match() method.""" expected = [self.device] self.assertEqual(expected, self.nd.match(nodename=self.nodename)) self.assertEqual(expected, self.nd.match(vendor='juniper')) self.assertNotEqual(expected, self.nd.match(vendor='cisco')) def tearDown(self): NetDevices._Singleton = None
class TestNetDevicesWithAcls(unittest.TestCase): """ Test NetDevices with ``settings.WITH_ACLs set`` to ``True``. """ def setUp(self): self.nd = NetDevices() self.device = self.nd[DEVICE_NAME] self.device2 = self.nd[DEVICE2_NAME] self.nodename = self.device.nodeName self.device.explicit_acls = set(['test1-abc-only']) def test_basics(self): """Basic test of NetDevices functionality.""" self.assertEqual(len(self.nd), 2) self.assertEqual(self.device.nodeName, self.nodename) self.assertEqual(self.device.manufacturer, 'JUNIPER') def test_aclsdb(self): """Test acls.db handling.""" self.assertTrue('test1-abc-only' in self.device.explicit_acls) def test_autoacls(self): """Test autoacls.py handling.""" self.assertTrue('router-protect.core' in self.device.implicit_acls) def test_find(self): """Test the find() method.""" self.assertEqual(self.nd.find(self.nodename), self.device) nodebasename = self.nodename[:self.nodename.index('.')] self.assertEqual(self.nd.find(nodebasename), self.device) self.assertRaises(KeyError, lambda: self.nd.find(self.nodename[0:3])) def test_all(self): """Test the all() method.""" expected = [self.device, self.device2] self.assertEqual(sorted(expected), sorted(self.nd.all())) def test_search(self): """Test the search() method.""" expected = [self.device] self.assertEqual([self.device], self.nd.search(self.nodename)) self.assertEqual(self.nd.all(), self.nd.search('17', field='onCallID')) def test_match(self): """Test the match() method.""" self.assertEqual([self.device], self.nd.match(nodename=self.nodename)) self.assertEqual(self.nd.all(), self.nd.match(vendor='juniper')) self.assertEqual([], self.nd.match(vendor='cisco')) def test_multiple_filter_match(self): """Test that passing multiple kwargs filters properly.""" # There should be only one Juniper router. self.assertEqual(self.nd.match(nodename='test1-abc'), self.nd.match(vendor='juniper', devicetype='router')) # And only one Juniper switch. self.assertEqual(self.nd.match(nodename='test2-abc'), self.nd.match(vendor='juniper', devicetype='switch')) def test_match_with_null_value(self): """Test the match() method when attr value is ``None``.""" self.device.site = None # Zero it out! expected = [self.device] # None raw self.assertEqual(expected, self.nd.match(site=None)) # "None" string self.assertEqual(expected, self.nd.match(site='None')) # Case-insensitive attr *and* value self.assertEqual(expected, self.nd.match(SITE='NONE')) def test_reload(self): """Test the .reload() method.""" nd = self.nd nd.reload() self.assertEqual(nd, self.nd) def tearDown(self): _reset_netdevices()
class TestNetDevicesWithAcls(unittest.TestCase): """ Test NetDevices with ``settings.WITH_ACLs set`` to ``True``. """ def setUp(self): self.nd = NetDevices() self.device = self.nd[DEVICE_NAME] self.device2 = self.nd[DEVICE2_NAME] self.nodename = self.device.nodeName self.device.explicit_acls = set(['test1-abc-only']) def test_basics(self): """Basic test of NetDevices functionality.""" self.assertEqual(len(self.nd), 2) self.assertEqual(self.device.nodeName, self.nodename) self.assertEqual(self.device.manufacturer, 'JUNIPER') def test_aclsdb(self): """Test acls.db handling.""" self.assertTrue('test1-abc-only' in self.device.explicit_acls) def test_autoacls(self): """Test autoacls.py handling.""" self.assertTrue('router-protect.core' in self.device.implicit_acls) def test_find(self): """Test the find() method.""" self.assertEqual(self.nd.find(self.nodename), self.device) nodebasename = self.nodename[:self.nodename.index('.')] self.assertEqual(self.nd.find(nodebasename), self.device) self.assertRaises(KeyError, lambda: self.nd.find(self.nodename[0:3])) def test_all(self): """Test the all() method.""" expected = [self.device, self.device2] self.assertEqual(sorted(expected), sorted(self.nd.all())) def test_search(self): """Test the search() method.""" expected = [self.device] self.assertEqual([self.device], self.nd.search(self.nodename)) self.assertEqual(self.nd.all(), self.nd.search('17', field='onCallID')) def test_match(self): """Test the match() method.""" self.assertEqual([self.device], self.nd.match(nodename=self.nodename)) self.assertEqual(self.nd.all(), self.nd.match(vendor='juniper')) self.assertEqual([], self.nd.match(vendor='cisco')) def test_multiple_filter_match(self): """Test that passing multiple kwargs filters properly.""" # There should be only one Juniper router. self.assertEqual( self.nd.match(nodename='test1-abc'), self.nd.match(vendor='juniper', devicetype='router') ) # And only one Juniper switch. self.assertEqual( self.nd.match(nodename='test2-abc'), self.nd.match(vendor='juniper', devicetype='switch') ) def test_match_with_null_value(self): """Test the match() method when attr value is ``None``.""" self.device.site = None # Zero it out! expected = [self.device] # None raw self.assertEqual(expected, self.nd.match(site=None)) # "None" string self.assertEqual(expected, self.nd.match(site='None')) # Case-insensitive attr *and* value self.assertEqual(expected, self.nd.match(SITE='NONE')) def tearDown(self): _reset_netdevices()
#!/Users/rlaney/.virtualenvs/NetEngineerONE/bin/python from __future__ import absolute_import, division, print_function import json from trigger.cmds import Commando from trigger.netdevices import NetDevices nd = NetDevices() device_list = nd.match(deviceTags='neteng1') class ShowMeTheMoney(Commando): """Execute the following on a list of Cisco devices: 'terminal length 0' 'show ip int brief' 'show interface status' 'show cdp neighbors' 'show ip arp' 'show mac address-table' vendors = ['cisco'] """ commands = [ 'terminal length 0', 'show ip int brief', 'show interface status', 'show ip arp', 'show mac address-table', 'show cdp neighbors', 'show cdp neighbors detail' ] if __name__ == '__main__': showstuff = ShowMeTheMoney(devices=device_list)