Ejemplo n.º 1
0
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())
Ejemplo n.º 2
0
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())
Ejemplo n.º 3
0
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())