class TestPortChannelCreate(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) def test_create(self): self.pc.create() self.assertIsNotNone(self.pc.channel_mode) self.pc.delete() def test_create_invalid(self): for i in [0, 257]: with self.assertRaises(InvalidPortChannelRange) as e: pc = PortChannel(self.switch, i) self.assertEqual( "InvalidPortChannelRange: Port channel id " + str(i) + " is invalid, id should range from 1 to 256", str(e.exception), ) def tearDown(self) -> None: self.pc.delete() self.assertEqual(self.interfaces.keys(), self.switch.interfaces.keys())
class TestPortChannelAttrName(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) def test_name_read(self): self.pc.create() self.assertEqual("port-channel" + str(self.pc_id), self.pc.name) self.pc.delete() def test_name_write_error(self): with self.assertRaises(AttributeError) as e: self.pc.name = "asdf" self.assertEqual("can't set attribute", str(e.exception)) def tearDown(self) -> None: self.pc.delete() self.assertEqual(self.interfaces.keys(), self.switch.interfaces.keys())
class TestPortChannelDelete(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) def test_delete_nonexisting(self): self.assertIsNone(self.pc.channel_mode) self.pc.delete() # no error self.assertIsNone(self.pc.channel_mode) def test_delete(self): self.pc.create() self.assertIsNotNone(self.pc.channel_mode) self.pc.delete() self.assertIsNone(self.pc.channel_mode) def tearDown(self) -> None: self.pc.delete() self.assertEqual(self.interfaces.keys(), self.switch.interfaces.keys())
class TestPortChannelAttrCounters(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) def test_counters_read_nonexisting(self): c = self.pc.counters self.assertIsNone(c.brief) # with self.assertRaises(CLIError) as e: # log.debug(self.pc.counters.__getattribute__("brief")) # self.assertIn("Invalid range", str(e.exception)) # self.pc.delete() def test_counters_read(self): self.skipTest( "Need to check why sometimes its None and test is failing") self.pc.create() self.assertIsNotNone(self.pc.counters, "pc.counters did not get counter objects") dir_counters = [ x for x in dir(self.pc.counters) if not x.startswith("_") ] for t in dir_counters: val = self.pc.counters.__getattribute__(t) log.debug(str(t) + " " + str(val)) # print(t) ##print(val) print(self.pc.name) self.assertIsNotNone(val) if t not in ["other_stats", "tcp_conn"]: self.assertIsNotNone(val) else: self.assertIsNone(val) self.pc.delete() def test_counters_write_error(self): with self.assertRaises(AttributeError) as e: self.pc.counters = "mds" self.assertEqual("can't set attribute", str(e.exception)) def test_counters_clear(self): self.skipTest("Needs to be fixed") self.pc.counters.clear() def tearDown(self) -> None: self.pc.delete() self.assertEqual(self.interfaces.keys(), self.switch.interfaces.keys())
def test_addinterfaces_repeated(self): v = Vsan(switch=self.switch, id=self.vsan_id[8]) v.create() fc1 = Fc(switch=self.switch, name=self.fc_name[7]) pc = PortChannel(self.switch, self.pc_id[5]) pc.create() v.add_interfaces([fc1, fc1, fc1, fc1]) self.assertEqual(fc1.name, v.interfaces[0].name) pc.delete() v.delete()
class TestPortChannelAttrMode(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) self.mode_values = mode_values def test_mode_read(self): self.pc.create() self.assertIn(self.pc.mode, self.mode_values + ["--"]) self.pc.delete() def test_mode_read_nonexisting(self): self.assertIsNone(self.pc.mode) def test_mode_write(self): self.skipTest("Needs to be fixed") self.pc.create() oldmode = self.pc.mode for mode in self.mode_values: self.pc.mode = mode self.assertEqual(mode, self.pc.mode) if "--" in oldmode: oldmode = "auto" self.pc.mode = oldmode self.pc.delete() def test_mode_write_invalid(self): mode = "asdf" with self.assertRaises(CLIError) as e: self.pc.mode = mode self.assertEqual( 'The command " interface port-channel' + str(self.pc_id) + " ; switchport mode " + str(mode) + ' " gave the error " % Invalid command ".', str(e.exception), ) def tearDown(self) -> None: self.pc.delete() self.assertEqual(self.interfaces.keys(), self.switch.interfaces.keys())
class TestPortChannelAttrTrunk(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) self.trunk_values = trunk_values def test_trunk_read(self): self.pc.create() self.assertIn(self.pc.trunk, self.trunk_values) self.pc.delete() def test_trunk_read_nonexisting(self): self.assertIsNone(self.pc.trunk) def test_trunk_write(self): self.pc.create() oldtrunk = self.pc.trunk for trunk in self.trunk_values: self.pc.trunk = trunk self.assertEqual(trunk, self.pc.trunk) self.pc.trunk = oldtrunk self.pc.delete() def test_trunk_write_invalid(self): trunk = "asdf" with self.assertRaises(CLIError) as e: self.pc.trunk = trunk self.assertEqual( 'The command " interface port-channel' + str(self.pc_id) + " ; switchport trunk mode " + str(trunk) + ' " gave the error " % Invalid command ".', str(e.exception), ) def tearDown(self) -> None: self.pc.delete() self.assertEqual(self.interfaces.keys(), self.switch.interfaces.keys())
def test_addinterfaces_splitlist(self): v = Vsan(switch=self.switch, id=self.vsan_id[3]) v.create() fc1 = Fc(switch=self.switch, name=self.fc_name[2]) fc2 = Fc(switch=self.switch, name=self.fc_name[3]) pc = PortChannel(self.switch, self.pc_id[2]) pc.create() v.add_interfaces([fc1]) v.add_interfaces([fc2]) v.add_interfaces([pc]) self.assertEqual(fc1.name, v.interfaces[0].name) self.assertEqual(fc2.name, v.interfaces[1].name) self.assertEqual(pc.name, v.interfaces[2].name) pc.delete() v.delete()
def test_addinterfaces(self): v = Vsan(switch=self.switch, id=self.vsan_id[1]) v.create() fc = Fc(switch=self.switch, name=self.fc_name[1]) v.add_interfaces([fc]) self.assertEqual(fc.name, v.interfaces[0].name) v.delete() v = Vsan(switch=self.switch, id=self.vsan_id[2]) v.create() pc = PortChannel(self.switch, self.pc_id[1]) pc.create() v.add_interfaces([pc]) self.assertEqual(pc.name, v.interfaces[0].name) pc.delete() v.delete()
class TestPortChannelAttrStatus(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) def test_status_read(self): self.pc.create() self.assertEqual("noOperMembers", self.pc.status) self.pc.delete() def test_status_read_nonexisting(self): self.assertIsNone(self.pc.status) def test_status_write(self): self.pc.create() status = "shutdown" self.pc.status = status self.assertEqual("down", self.pc.status) status1 = "no shutdown" self.pc.status = status1 self.assertEqual("noOperMembers", self.pc.status) self.pc.delete() def test_status_write_invalid(self): status = "asdf" with self.assertRaises(CLIError) as e: self.pc.status = status self.assertEqual( 'The command " terminal dont-ask ; interface port-channel' + str(self.pc_id) + " ; " + str(status) + ' ; no terminal dont-ask " gave the error " % Invalid command ".', str(e.exception), ) def tearDown(self) -> None: self.pc.delete() self.assertEqual(self.interfaces.keys(), self.switch.interfaces.keys())
def test_addinterfaces_list(self): v = Vsan(switch=self.switch, id=self.vsan_id[4]) v.create() fc1 = Fc(switch=self.switch, name=self.fc_name[4]) fc2 = Fc(switch=self.switch, name=self.fc_name[5]) pc4 = PortChannel(self.switch, self.pc_id[3]) pc5 = PortChannel(self.switch, self.pc_id[4]) pc4.create() pc5.create() l = [fc1, fc2, pc4, pc5] v.add_interfaces(l) self.assertEqual(fc1.name, v.interfaces[0].name) self.assertEqual(fc2.name, v.interfaces[1].name) self.assertEqual(pc4.name, v.interfaces[2].name) self.assertEqual(pc5.name, v.interfaces[3].name) pc4.delete() pc5.delete() v.delete()
class TestPortChannelAttrDescription(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) def test_description_read(self): self.pc.create() self.assertIsNotNone(self.pc.description) self.pc.delete() def test_description_read_nonexisting(self): with self.assertRaises(CLIError) as e: log.debug(self.pc.description) self.assertIn("Invalid range", str(e.exception)) def test_description_write_max254(self): self.pc.create() old = self.pc.description desc = "switch12345678912345678912345678switch12345678912345678912345678switch12345678912345678912345678switch12345678912345678912345678switch12345678912345678912345678switch12345678912345678912345678switch12345678912345678912345678switch123456789123456789123456" self.pc.description = desc self.assertEqual(desc, self.pc.description) self.pc.description = old self.pc.delete() def test_description_write_beyondmax(self): desc = "switch12345678912345678912345678switch12345678912345678912345678switch12345678912345678912345678switch12345678912345678912345678switch12345678912345678912345678switch12345678912345678912345678switch12345678912345678912345678switch12345678912345678912345678" with self.assertRaises(CLIError) as e: self.pc.description = desc self.assertIn("String exceeded max length of (254)", str(e.exception)) self.pc.delete() def tearDown(self) -> None: self.pc.delete() self.assertEqual(self.interfaces.keys(), self.switch.interfaces.keys())
class TestPortChannelAttrMembers(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) def test_members_read_nonexisting(self): self.assertIsNone(self.pc.members) def test_members_read(self): self.pc.create() while True: k, v = random.choice(list(self.interfaces.items())) if type(v) is Fc: fc = v log.debug(k) break try: self.pc.add_members([fc]) except CLIError as c: if "port not compatible" in c.message: self.skipTest( "Skipping test as as port not compatible. Please rerun the test cases" ) self.assertIn(fc.name, self.pc.members) self.pc.delete() def test_members_write_error(self): with self.assertRaises(AttributeError) as e: self.pc.members = [] self.assertEqual("can't set attribute", str(e.exception)) def tearDown(self) -> None: self.pc.delete() self.assertEqual(self.interfaces.keys(), self.switch.interfaces.keys())
class TestPortChannelAttrChannelMode(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) self.channel_mode_values = channel_mode_values def test_channel_mode_read_nonexisting(self): self.assertIsNone(self.pc.channel_mode) def test_channel_mode_read(self): self.pc.create() self.assertIn(self.pc.channel_mode, self.channel_mode_values) self.pc.delete() def test_channel_mode_write_nonexisting(self): with self.assertRaises(PortChannelNotPresent) as e: self.pc.channel_mode = self.channel_mode_values[0] self.assertEqual( "PortChannelNotPresent: Port channel " + str(self.pc_id) + " is not present on the switch, please create the PC first", str(e.exception), ) def test_channel_mode_write(self): self.pc.create() for mode in self.channel_mode_values: self.pc.channel_mode = mode self.assertEqual(mode, self.pc.channel_mode) self.pc.delete() def test_channel_mode_write_invalid(self): channel_mode = "asdf" self.pc.create() with self.assertRaises(InvalidChannelMode) as e: self.pc.channel_mode = channel_mode self.assertEqual( "InvalidChannelMode: Invalid channel mode (" + str(channel_mode) + "), Valid values are: on,active", str(e.exception), ) self.pc.delete() def tearDown(self) -> None: self.pc.delete() self.assertEqual(self.interfaces.keys(), self.switch.interfaces.keys())
class TestPortChannelAddMembers(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) while True: k, v = random.choice(list(self.interfaces.items())) if type(v) is Fc: self.fc = v log.debug(k) break def test_add_members_paramtype(self): self.pc.create() with self.assertRaises(TypeError) as e: self.pc.add_members(self.fc) self.assertEqual("'Fc' object is not iterable", str(e.exception)) self.pc.delete() def test_add_members_one(self): self.pc.create() try: self.pc.add_members([self.fc]) except CLIError as c: if "port not compatible" in c.message: self.skipTest( "Skipping test as as port not compatible. Please rerun the test cases" ) self.assertIn(self.fc.name, self.pc.members) self.pc.delete() def test_add_members_multiple(self): self.pc.create() while True: k, v = random.choice(list(self.interfaces.items())) if type(v) is Fc and k != self.fc.name: fc2 = v log.debug(k) break try: self.pc.add_members([self.fc, fc2]) except CLIError as c: if "port not compatible" in c.message: self.skipTest( "Skipping test as as port not compatible. Please rerun the test cases" ) self.assertIn(self.fc.name, self.pc.members) self.assertIn(fc2.name, self.pc.members) self.pc.delete() def test_add_members_nonexisting(self): with self.assertRaises(PortChannelNotPresent) as e: self.pc.add_members([self.fc]) self.assertEqual( "PortChannelNotPresent: Port channel " + str(self.pc_id) + " is not present on the switch, please create the PC first", str(e.exception), ) def test_add_members_invalidfc(self): invalidfc = "fc48/48" ### check fc1 = Fc(self.switch, invalidfc) self.pc.create() with self.assertRaises(CLIError) as e: self.pc.add_members([fc1]) self.assertEqual( 'The command " interface ' + str(invalidfc) + " ; channel-group " + str(self.pc_id) + ' force " gave the error " Invalid interface format ".', str(e.exception), ) self.pc.delete() def test_add_members_invalid(self): self.pc.create() with self.assertRaises(AttributeError) as e: self.pc.add_members(["fc1/1"]) self.assertEqual("'str' object has no attribute 'name'", str(e.exception)) self.pc.delete() def tearDown(self) -> None: self.pc.delete() self.assertCountEqual(self.interfaces.keys(), self.switch.interfaces.keys())
class TestVsanAddInterfaces(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.vsandb = self.switch.vsans while True: self.id = get_random_id() if self.id not in self.vsandb.keys(): break self.v = Vsan(switch=self.switch, id=self.id) self.fc = self.vsandb[1].interfaces[1] ## fc interface from vsan 1 self.interfaces = self.switch.interfaces while True: self.pc_id = get_random_id(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) self.invalid_fc = Fc( self.switch, "fc48/48") ## matches fc pattern but is not present on switch def test_addinterfaces_type_error(self): self.v.create() with self.assertRaises(TypeError) as e: self.v.add_interfaces(self.fc) self.assertIn("object is not iterable", str(e.exception)) self.v.delete() def test_addinterfaces(self): self.v.create() self.pc.create() self.v.add_interfaces([self.fc, self.pc]) self.assertEqual(self.fc.name, self.v.interfaces[0].name) self.assertEqual(self.pc.name, self.v.interfaces[1].name) self.pc.delete() self.v.delete() def test_addinterfaces_fc_invalid(self): self.v.create() with self.assertRaises(CLIError) as e: self.v.add_interfaces([self.invalid_fc]) self.assertIn("Invalid interface format", str(e.exception)) self.v.delete() def test_addinterfaces_invalid(self): self.v.create() with self.assertRaises(AttributeError) as e: self.v.add_interfaces("asdfg") self.assertEqual("'str' object has no attribute 'name'", str(e.exception)) self.v.delete() def test_addinterfaces_nonexistingvsan(self): with self.assertRaises(VsanNotPresent) as e: self.v.add_interfaces([self.fc]) self.assertEqual( "VsanNotPresent: Vsan " + str(self.id) + " is not present on the switch.", str(e.exception), ) def test_addinterfaces_repeated(self): self.v.create() self.pc.create() self.v.add_interfaces( [self.fc, self.fc, self.pc]) ## self.fc even though repeated will not be added self.assertEqual(self.fc.name, self.v.interfaces[0].name) self.assertEqual(2, len(self.v.interfaces)) self.pc.delete() self.v.delete() def test_addinterfaces_portchannelnotpresent(self): self.v.create() with self.assertRaises(CLIError) as e: self.v.add_interfaces([self.pc]) self.assertRegex( str(e.exception), ".*The command.*vsan database ; vsan " + str(self.id) + " interface port-channel" + str(self.pc.id) + ".*gave the error.*Invalid range .*", ) self.v.delete() def tearDown(self) -> None: if self.v.id is not None: self.v.delete() if self.pc.channel_mode is not None: self.pc.delete() self.vsandb[1].add_interfaces([self.fc]) self.assertEqual(self.vsandb.keys(), self.switch.vsans.keys())
class TestPortChannelRemoveMembers(unittest.TestCase): def __init__(self, testName, sw): super().__init__(testName) self.switch = sw def setUp(self) -> None: log.debug(self.switch.version) log.debug(self.switch.ipaddr) self.interfaces = self.switch.interfaces while True: self.pc_id = random.randint(1, 256) if "port-channel" + str(self.pc_id) not in self.interfaces.keys(): break self.pc = PortChannel(self.switch, self.pc_id) while True: k, v = random.choice(list(self.interfaces.items())) if type(v) is Fc and v.status not in ["up", "trunking"]: self.fc = v log.debug(k) break def test_remove_members_paramtype(self): self.pc.create() self.pc.add_members([self.fc]) with self.assertRaises(TypeError) as e: self.pc.remove_members(self.fc) self.assertEqual("'Fc' object is not iterable", str(e.exception)) self.pc.delete() def test_remove_members_one(self): self.pc.create() while True: k, v = random.choice(list(self.interfaces.items())) if (type(v) is Fc and k != self.fc.name and v.status not in ["up", "trunking"]): fc2 = v log.debug(k) break try: self.pc.add_members([self.fc, fc2]) except CLIError as c: if "port not compatible" in c.message: self.skipTest( "Skipping test as as port not compatible. Please rerun the test cases" ) self.pc.remove_members([self.fc]) # print(self.fc.name, self.pc.members) self.assertNotIn(self.fc.name, self.pc.members) self.assertIn(fc2.name, self.pc.members) self.pc.delete() def test_remove_members_multiple(self): self.pc.create() while True: k, v = random.choice(list(self.interfaces.items())) if (type(v) is Fc and k != self.fc.name and v.status not in ["up", "trunking"]): fc2 = v log.debug(k) break try: self.pc.add_members([self.fc, fc2]) except CLIError as c: if "port not compatible" in c.message: self.skipTest( "Skipping test as as port not compatible. Please rerun the test cases" ) self.pc.remove_members([self.fc, fc2]) self.assertEquals(self.pc.members, {}) self.pc.delete() def test_remove_members_nonexistingpc(self): with self.assertRaises(PortChannelNotPresent) as e: self.pc.remove_members([self.fc]) self.assertEqual( "PortChannelNotPresent: Port channel " + str(self.pc_id) + " is not present on the switch, please create the PC first", str(e.exception), ) def test_remove_members_nonexisting(self): self.pc.create() with self.assertRaises(CLIError) as e: self.pc.remove_members([self.fc]) self.assertEqual( 'The command " interface ' + str(self.fc.name) + " ; no channel-group " + str(self.pc_id) + ' " gave the error " ' + str(self.fc.name) + ': not part of port-channel ".', str(e.exception), ) self.pc.delete() def tearDown(self) -> None: self.pc.delete() self.assertEqual(self.interfaces.keys(), self.switch.interfaces.keys())
) # Instantiating Vsan object with id 2 v = Vsan(sw, 2) # Creating vsan v.create() # Creating Fc object for interface fc1/3 int13 = Fc(sw, "fc1/3") # Instantiating PortChannel object 1 pc1 = PortChannel(sw, 1) # Creating port channel pc1.create() # Adding interfaces to vsan 2 v.add_interfaces([int13, pc1]) # Instantiating DeviceAlias object d = DeviceAlias(sw) new = {"da1": "60:66:61:01:0e:00:01:ff"} # Adding new device alias d.create(new) # Instantiate zone object z = Zone(sw, "zone1", v.id) # Create new zone