def testCaseInsensitivePartialMatch(self): """Test for the case_insensitive keyword""" mlist = ["test1.example.com", "test2.example.net"] self.assertEqual( utils.MatchNameComponent("test2", mlist, case_sensitive=False), "test2.example.net") self.assertEqual( utils.MatchNameComponent("Test2", mlist, case_sensitive=False), "test2.example.net") self.assertEqual( utils.MatchNameComponent("teSt2", mlist, case_sensitive=False), "test2.example.net") self.assertEqual( utils.MatchNameComponent("TeSt2", mlist, case_sensitive=False), "test2.example.net")
def CheckHostnameSane(lu, name): """Ensures that a given hostname resolves to a 'sane' name. The given name is required to be a prefix of the resolved hostname, to prevent accidental mismatches. @param lu: the logical unit on behalf of which we're checking @param name: the name we should resolve and check @return: the resolved hostname object """ hostname = netutils.GetHostname(name=name) if hostname.name != name: lu.LogInfo("Resolved given name '%s' to '%s'", name, hostname.name) if not utils.MatchNameComponent(name, [hostname.name]): raise errors.OpPrereqError( ("Resolved hostname '%s' does not look the" " same as given hostname '%s'") % (hostname.name, name), errors.ECODE_INVAL) return hostname
def testCaseInsensitiveFullMatch(self): mlist = ["ts1.ex", "ts1.ex.org", "ts2.ex", "Ts2.ex"] # Between the two ts1 a full string match non-case insensitive should work self.assertEqual( utils.MatchNameComponent("Ts1", mlist, case_sensitive=False), None) self.assertEqual( utils.MatchNameComponent("Ts1.ex", mlist, case_sensitive=False), "ts1.ex") self.assertEqual( utils.MatchNameComponent("ts1.ex", mlist, case_sensitive=False), "ts1.ex") # Between the two ts2 only case differs, so only case-match works self.assertEqual( utils.MatchNameComponent("ts2.ex", mlist, case_sensitive=False), "ts2.ex") self.assertEqual( utils.MatchNameComponent("Ts2.ex", mlist, case_sensitive=False), "Ts2.ex") self.assertEqual( utils.MatchNameComponent("TS2.ex", mlist, case_sensitive=False), None)
def testMultipleMatches(self): """Test that a multiple match is returned as None""" mlist = ["test1.example.com", "test1.example.org", "test1.example.net"] for key in "test1", "test1.example": self.assertEqual(utils.MatchNameComponent(key, mlist), None)
def testSingleMatch(self): """Test that a single match is performed correctly""" mlist = ["test1.example.com", "test2.example.com", "test3.example.com"] for key in "test2", "test2.example", "test2.example.com": self.assertEqual(utils.MatchNameComponent(key, mlist), mlist[1])
def testEmptyList(self): """Test that there is no match against an empty list""" self.assertEqual(utils.MatchNameComponent("", []), None) self.assertEqual(utils.MatchNameComponent("test", []), None)