def test1(self): "parse_uri2(''), parse_uri3('')" res = samutils.parse_uri2('') self.failUnlessEqual(res, ['', None], "Expected ['', None], got %s" % res) res = samutils.parse_uri3('') self.failUnlessEqual(res, [None, '', None], "Expected [None, '', None], got %s" % res)
def get_working_ldap(ldaps, net_timeout=LDAP_TIMEOUT_NETWORK): """Test given list of LDAP servers and return a first working one as IP address. Depending on availability uses either LDAP API or CLI. @param ldaps: list of LDAP endpoints (ldap://<hostname>:[<port>]). @type ldaps: L{list} @param net_timeout: connection timeout (default: L{LDAP_TIMEOUT_NETWORK}). @type net_timeout: L{int} @return: - on success: - C{endpoint} - first working LDAP endpoint as IP address @rtype: L{str} @raises LookupError,TypeError,ValueError: - LookupError - if no working endpoints found. - TypeError - L{ldaps} must be a list object. - ValueError - list of empty endpoints or empty list is given. """ if not isinstance(ldaps, list): raise TypeError('ldaps should be a list object.') l = len(ldaps) if l == 0: raise ValueError('Empty LDAP endpoints list given (%s()).' % \ sys._getframe(0).f_code.co_name) else: i = 0 for v in ldaps: if not v: i += 1 if i == l: raise ValueError('List of empty LDAP endpoints given (%s()).' % \ sys._getframe(0).f_code.co_name) failed_ldaps = {} for ldap_url in ldaps: proto, hostname, port = samutils.parse_uri3(ldap_url) try: ips = samutils.dns_lookup_forward(hostname) except IOError, e: # Forward DNS resolution failed. Continue with the next host. failed_ldaps[ldap_url] = str(e) continue else: for ip in ips: ldap_url_ip = '%s%s:%s' %(proto or '', ip, port) if LDAP_LIB: rc, error = __ldap_bind_API(ldap_url_ip, net_timeout) if rc: return ldap_url_ip else: rc, error = __ldap_bind_CLI(ldap_url_ip, net_timeout) if rc: return ldap_url_ip host_ip = samutils.ldap_url2hostname_ip(ldap_url_ip) failed_ldaps[host_ip] = error
def get_working_ldap(ldaps, net_timeout=LDAP_TIMEOUT_NETWORK): """Test given list of LDAP servers and return a first working one as IP address. Depending on availability uses either LDAP API or CLI. @param ldaps: list of LDAP endpoints (ldap://<hostname>:[<port>]). @type ldaps: L{list} @param net_timeout: connection timeout (default: L{LDAP_TIMEOUT_NETWORK}). @type net_timeout: L{int} @return: - on success: - C{endpoint} - first working LDAP endpoint as IP address @rtype: L{str} @raises LookupError,TypeError,ValueError: - LookupError - if no working endpoints found. - TypeError - L{ldaps} must be a list object. - ValueError - list of empty endpoints or empty list is given. """ if not isinstance(ldaps, list): raise TypeError('ldaps should be a list object.') l = len(ldaps) if l == 0: raise ValueError('Empty LDAP endpoints list given (%s()).' % \ sys._getframe(0).f_code.co_name) else: i = 0 for v in ldaps: if not v: i += 1 if i == l: raise ValueError('List of empty LDAP endpoints given (%s()).' % \ sys._getframe(0).f_code.co_name) failed_ldaps = {} for ldap_url in ldaps: proto, hostname, port = samutils.parse_uri3(ldap_url) try: ips = samutils.dns_lookup_forward(hostname) except IOError, e: # Forward DNS resolution failed. Continue with the next host. failed_ldaps[ldap_url] = str(e) continue else: for ip in ips: ldap_url_ip = '%s%s:%s' % (proto or '', ip, port) if LDAP_LIB: rc, error = __ldap_bind_API(ldap_url_ip, net_timeout) if rc: return ldap_url_ip else: rc, error = __ldap_bind_CLI(ldap_url_ip, net_timeout) if rc: return ldap_url_ip host_ip = samutils.ldap_url2hostname_ip(ldap_url_ip) failed_ldaps[host_ip] = error
def test2(self): "parse_uri2(valid_uri), parse_uri3(valid_uri)" proto = 'http://'; host = 'www.test.host'; port = '80' uri = '%s%s:%s' % (proto, host, port) res = samutils.parse_uri2(uri) self.failUnlessEqual(res, [host, port], "Expected [%s, %s], got %s" % (host, port, res)) res = samutils.parse_uri3(uri) self.failUnlessEqual(res, [proto, host, port], "Expected [%s, %s, %s], got %s" % (proto, host, port, res))
def to_full_ldap_url(url, port='2170'): """Given LDAP url return full LDAP uri. Keyword argument C{port} is used if url:port wasn't found in the given url. @return: ldap://<url:hostname>:<url:port>. """ hp = samutils.parse_uri3(url) if not hp[0]: hp[0] = 'ldap://' if not hp[2]: hp[2] = port return '%s%s:%s' % (hp[0], hp[1], hp[2])
def test2(self): "parse_uri2(valid_uri), parse_uri3(valid_uri)" proto = 'http://' host = 'www.test.host' port = '80' uri = '%s%s:%s' % (proto, host, port) res = samutils.parse_uri2(uri) self.failUnlessEqual(res, [host, port], "Expected [%s, %s], got %s" % (host, port, res)) res = samutils.parse_uri3(uri) self.failUnlessEqual( res, [proto, host, port], "Expected [%s, %s, %s], got %s" % (proto, host, port, res))