def _next_hop(): next_hop = choice(NEXT_HOPS) if next_hop is 'Null0': return (next_hop, None) ip1 = random_int(0, 223) ip2 = random_int(0, 255) ip3 = random_int(0, 255) ip4 = random_int(0, 255) ip_addr = "%s.%s.%s.%s" % (ip1, ip2, ip3, ip4) if next_hop is 'IP': return (ip_addr, None) return (next_hop, ip_addr)
def test_delete_and_return_true(self): for dut in self.duts: vid = str(random_int(2, 4094)) dut.config('vlan %s' % vid) result = dut.api('vlans').delete(vid) self.assertTrue(result, 'dut=%s' % dut) config = dut.run_commands('show vlan') self.assertNotIn(vid, config[0]['vlans'])
def test_create_and_return_true(self): for dut in self.duts: dut.config('no vlan 2-4094') vid = str(random_int(2, 4094)) result = dut.api('vlans').create(vid) self.assertTrue(result, 'dut=%s' % dut) config = dut.run_commands('show vlan') self.assertIn(vid, config[0]['vlans'], 'dut=%s' % dut)
def test_config_multiple_commands(self): for dut in self.duts: commands = list() for i in range(1, random_int(10, 200)): commands.append('hostname %s' % random_string(5, 20)) result = dut.config(commands[:]) self.assertIsInstance(result, list, 'dut=%s' % dut) self.assertEqual(len(result), len(commands), 'dut=%s' % dut)
def test_enable_multiple_commands(self): for dut in self.duts: commands = list() for i in range(1, random_int(10, 200)): commands.append('show version') result = dut.run_commands(commands[:]) self.assertIsInstance(result, list, 'dut=%s' % dut) self.assertEqual(len(result), len(commands), 'dut=%s' % dut)
def test_set_minimum_links_with_value(self): minlinks = random_int(1, 16) cmds = [ 'interface Port-Channel1', 'port-channel min-links %s' % minlinks ] func = function('set_minimum_links', 'Port-Channel1', minlinks) self.eapi_positive_config_test(func, cmds)
def test_set_state_suspend(self): for dut in self.duts: vid = str(random_int(2, 4095)) dut.config(['no vlan %s' % vid, 'vlan %s' % vid, 'state active']) result = dut.api('vlans').set_state(vid, 'suspend') self.assertTrue(result, 'dut=%s' % dut) config = dut.run_commands('show vlan') self.assertEqual('suspended', config[0]['vlans'][vid]['status'], 'dut=%s' % dut)
def test_set_name(self): for dut in self.duts: name = random_string(maxchar=20) vid = str(random_int(2, 4095)) dut.config(['no vlan %s' % vid, 'vlan %s' % vid]) result = dut.api('vlans').set_name(vid, name) self.assertTrue(result, 'dut=%s' % dut) config = dut.run_commands('show vlan') self.assertEqual(name, config[0]['vlans'][vid]['name'], 'dut=%s' % dut)
def test_add_trunk_group(self): for dut in self.duts: tg = random_string(maxchar=32) vid = str(random_int(2, 4095)) dut.config(['no vlan %s' % vid, 'vlan %s' % vid, 'no trunk group']) result = dut.api('vlans').add_trunk_group(vid, tg) self.assertTrue(result, 'dut=%s' % dut) config = dut.run_commands('show vlan trunk group') self.assertIn(tg, config[0]['trunkGroups'][vid]['names'], 'dut=%s' % dut)
def test_default(self): for dut in self.duts: vid = str(random_int(2, 4095)) name = random_string(maxchar=20) dut.config(['no vlan %s' % vid, 'vlan %s' % vid, 'vlan %s' % vid, 'name %s' % name]) result = dut.api('vlans').default(vid) self.assertTrue(result, 'dut=%s' % dut) config = dut.run_commands('show vlan') self.assertNotIn(vid, config[0]['vlans'], 'dut=%s' % dut)
def test_set_trunk_groups_default(self): for dut in self.duts: vid = str(random_int(2, 4094)) tg = random_string() dut.config(['no vlan %s' % vid, 'vlan %s' % vid, 'trunk group %s' % tg]) result = dut.api('vlans').set_trunk_groups(vid, default=True) self.assertTrue(result, 'dut=%s' % dut) cmd = 'show running-config section vlan %s' % vid config = dut.run_commands(cmd, 'text') self.assertNotIn('trunk group', config[0]['output'])
def test_create(self): # Test passing in a full set of parameters to 'create' # Some parameters may be not set: None for ip_dest in IP_DESTS: # Get the parameters for the call (next_hop, next_hop_ip) = choice(NEXT_HOPS) distance = choice(DISTANCES) if distance: distance = random_int(0, 255) tag = choice(TAGS) if tag: tag = random_int(0, 255) route_name = choice(ROUTE_NAMES) if route_name: route_name = random_string(minchar=4, maxchar=10) func = function('create', ip_dest, next_hop, next_hop_ip=next_hop_ip, distance=distance, tag=tag, route_name=route_name) # Build the expected string for comparison # A value of None will default to an empty string, and # add the tag or name keywords where appropriate cmd_next_hop_ip = cmd_distance = cmd_tag = cmd_route_name = '' if next_hop_ip is not None: cmd_next_hop_ip = " %s" % next_hop_ip if distance is not None: cmd_distance = " %d" % distance if tag is not None: cmd_tag = " tag %d" % tag if route_name is not None: cmd_route_name = " name %s" % route_name cmds = "ip route %s %s%s%s%s%s" % \ (ip_dest, next_hop, cmd_next_hop_ip, cmd_distance, cmd_tag, cmd_route_name) self.eapi_positive_config_test(func, cmds)
def test_minimum_links_valid(self): for dut in self.duts: minlinks = random_int(1, 16) dut.config(['no interface Port-Channel1', 'interface Port-Channel1']) result = dut.api('interfaces').set_minimum_links('Port-Channel1', minlinks) self.assertTrue(result, 'dut=%s' % dut) commands = 'show running-config interfaces Port-Channel1' config = dut.run_commands(commands, 'text') self.assertIn('port-channel min-links %s' % minlinks, config[0]['output'], 'dut=%s' % dut)
def test_minimum_links_valid(self): for dut in self.duts: minlinks = random_int(1, 16) dut.config( ['no interface Port-Channel1', 'interface Port-Channel1']) result = dut.api('interfaces').set_minimum_links( 'Port-Channel1', minlinks) self.assertTrue(result, 'dut=%s' % dut) commands = 'show running-config interfaces Port-Channel1' config = dut.run_commands(commands, 'text') self.assertIn('port-channel min-links %s' % minlinks, config[0]['output'], 'dut=%s' % dut)
def test_set_trunk_groups(self): for dut in self.duts: vid = str(random_int(2, 4094)) tg1 = random_string(maxchar=10) tg2 = random_string(maxchar=10) tg3 = random_string(maxchar=10) dut.config(['no vlan %s' % vid, 'vlan %s' % vid, 'trunk group %s' % tg1, 'trunk group %s' % tg2]) result = dut.api('vlans').set_trunk_groups(vid, [tg1, tg3]) self.assertTrue(result, 'dut=%s' % dut) config = dut.run_commands('show vlan %s trunk group' % vid) config = sorted(config[0]['trunkGroups'][vid]['names']) self.assertEqual(sorted([tg1, tg3]), config)
def test_enable_with_multiple_commands(self): commands = list() for i in range(0, random_int(2, 5)): commands.append(random_string()) def execute_response(cmds, *args): return {'result': [x for x in cmds]} self.connection.execute.side_effect = execute_response responses = self.node.enable(commands) self.assertEqual(self.connection.execute.call_count, len(commands)) expected_calls = [call(['enable', cmd], 'json') for cmd in commands] self.assertEqual(self.connection.execute.mock_calls, expected_calls) for index, response in enumerate(responses): self.assertEqual(commands[index], response['result'])
def test_set_minimum_links_with_value(self): minlinks = random_int(1, 16) cmds = ['interface Port-Channel1', 'port-channel min-links %s' % minlinks] func = function('set_minimum_links', 'Port-Channel1', minlinks) self.eapi_positive_config_test(func, cmds)
def test_multiple_requests(self): for dut in self.duts: for i in range(1, random_int(10, 200)): result = dut.run_commands('show version') self.assertIsInstance(result, list, 'dut=%s' % dut) self.assertEqual(len(result), 1, 'dut=%s' % dut)
def _ip_addr(): ip1 = random_int(0, 223) ip2 = random_int(0, 255) ip3 = random_int(0, 255) return "%s.%s.%s.0/24" % (ip1, ip2, ip3)
def _distance(): return random_int(1, 255)
def test_set_mtu_with_values(self): for intf in self.INTERFACES: for value in [68, 65535, random_int(68, 65535)]: cmds = ['interface %s' % intf, 'mtu %s' % value] func = function('set_mtu', intf, value) self.eapi_positive_config_test(func, cmds)
def test_minimum_links_invalid_value(self): for dut in self.duts: minlinks = random_int(17, 128) result = dut.api('interfaces').set_minimum_links('Port-Channel1', minlinks) self.assertFalse(result)
def _tag(): return random_int(0, 255)
def test_minimum_links_invalid_value(self): for dut in self.duts: minlinks = random_int(17, 128) result = dut.api('interfaces').set_minimum_links( 'Port-Channel1', minlinks) self.assertFalse(result)
def test_set_lacp_timeout_with_value(self): timeout = random_int(1, 16) cmds = ['interface Port-Channel1', 'port-channel lacp fallback timeout %s' % timeout] func = function('set_lacp_timeout', 'Port-Channel1', timeout) self.eapi_positive_config_test(func, cmds)