Example #1
0
print(d.database)
print("Mode : " + d.mode)
print("Distribute : " + str(d.distribute))
print("Locked : " + str(d.locked))

old = d.database
d.clear_database()

# Adding new device alias
new = {
    "device1": "21:00:00:0e:1e:30:34:a5",
    "device2": "21:00:00:0e:1e:30:3c:c5"
}
d.create(new)

prnt("Clearing database\nDatabase after adding new entry")
print(d.database)

# Renaming the device alias
d.rename("device1", "device_new_name")

print("Database after renaming device alias device1 as device_new_name")
print(d.database)

# Deleting device alias
d.delete("device_new_name")
d.delete("device2")

# Recreating original database
d.create(old)
class TestDeviceAliasRename(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.d = DeviceAlias(self.switch)
        self.olddb = self.d.database
        if self.olddb is None:
            self.name = get_random_string()
            self.pwwn = get_random_pwwn()
        else:
            while True:
                self.name = get_random_string()
                self.pwwn = get_random_pwwn()
                if (
                    self.name not in self.olddb.keys()
                    and self.pwwn not in self.olddb.values()
                ):
                    break
        log.debug({self.name: self.pwwn})

    def test_rename(self):
        self.d.create({self.name: self.pwwn})
        newdb = self.d.database
        self.assertIn(self.name, newdb.keys())
        self.assertEqual(self.pwwn, newdb[self.name])
        while True:
            self.newname_to_replace = get_random_string()
            if self.newname_to_replace not in newdb.keys():
                break
        self.d.rename(oldname=self.name, newname=self.newname_to_replace)
        chkdb = self.d.database
        self.assertIn(self.newname_to_replace, chkdb.keys())
        self.assertEqual(self.pwwn, chkdb[self.newname_to_replace])
        self.assertNotIn(self.name, chkdb.keys())
        self.d.delete(self.newname_to_replace)
        chkdb = self.d.database
        if chkdb is not None:
            self.assertNotIn(self.newname_to_replace, chkdb.keys())
            self.assertNotIn(self.pwwn, chkdb.values())
            self.assertNotIn(self.name, chkdb.keys())

    def test_rename_not_present(self):
        self.d.create({self.name: self.pwwn})
        newdb = self.d.database
        self.assertIn(self.name, newdb.keys())
        self.assertEqual(self.pwwn, newdb[self.name])
        while True:
            self.newname_to_replace = get_random_string()
            if self.newname_to_replace not in newdb.keys():
                break
        with self.assertRaises(CLIError) as e:
            self.d.rename(oldname=self.newname_to_replace, newname=self.name)
        self.assertIn("Device Alias not present", str(e.exception))

        self.d.delete(self.name)
        chkdb = self.d.database
        if chkdb is not None:
            self.assertNotIn(self.newname_to_replace, chkdb.keys())
            self.assertNotIn(self.pwwn, chkdb.values())
            self.assertNotIn(self.name, chkdb.keys())

    def test_rename_already_present(self):
        self.d.create({self.name: self.pwwn})
        newdb = self.d.database
        self.assertIn(self.name, newdb.keys())
        self.assertEqual(self.pwwn, newdb[self.name])

        with self.assertRaises(CLIError) as e:
            self.d.rename(oldname=self.name, newname=self.name)
        self.assertIn(
            "Target device-alias name already in use. Please specify a new name.",
            str(e.exception),
        )

        self.d.delete(self.name)
        chkdb = self.d.database
        if chkdb is not None:
            self.assertNotIn(self.pwwn, chkdb.values())
            self.assertNotIn(self.name, chkdb.keys())

    def test_rename_invalid(self):
        self.d.create({self.name: self.pwwn})
        newdb = self.d.database
        self.assertIn(self.name, newdb.keys())
        self.assertEqual(self.pwwn, newdb[self.name])

        invalid_name = "da1&"  # da name a-zA-Z1-9 - _ $ ^    64chars max
        with self.assertRaises(CLIError) as e:
            self.d.rename(oldname=self.name, newname=invalid_name)
        self.assertIn("Illegal character present in the name", str(e.exception))
        self.d.clear_lock()

        self.d.delete(self.name)
        chkdb = self.d.database
        if chkdb is not None:
            self.assertNotIn(self.pwwn, chkdb.values())
            self.assertNotIn(self.name, chkdb.keys())

    def tearDown(self) -> None:
        try:
            self.d.delete(self.name)
        except CLIError as c:
            if "Device Alias not present" not in c.message:
                self.fail("Tear down failed: " + c.message)
        enddb = self.d.database
        if enddb is not None:
            self.assertEqual(enddb, self.olddb)