简体   繁体   English

如何在python函数中将ldap对象转换为整数?

[英]How can I convert an ldap object to integer in a python function?

I have created python function to search through an ldap object as below: 我创建了python函数来搜索ldap对象,如下所示:

def my_search(l, baseDN, searchScope=ldap.SCOPE_ONELEVEL, searchFilter="objectClass=*", retrieveAttributes=None):
    logger.console("Reachedhere")
    try:
        logger.console("Reachedhereinsidetry\n")
        ldap_result_id =       l.search_s(baseDN,searchScope,searchFilter,retrieveAttributes)
        logger.console("Gotresult\n")

So I invoke this keyword now in a Robot Testcase as so: 因此,我现在在机器人测试用例中这样调用此关键字:

*** Settings ***
Documentation     This testsuite checks the LDAP functionalities of DB nodes.
Resource          ../../COMMON/Libraries/SDL-DB-COMMON-LIB.txt
Library           ../../COMMON/Libraries/pythonldap.py

*** Test Cases ***
Perform Ldap Operations
    ${ldapObj}    ldapopen    ${DB_1_EXT_APP_IP}
    Log to Console    ${ldapObj}
    ${SearchReturn}    my_search    ${ldapObj}    "uid=5000000,ds=CRIBER,o=D,dc=CN"    ldap.SCOPE_ONELEVEL    "objectClass=*"    None

When I run this TC, it throws me an error in the search like so: 当我运行此TC时,它将在搜索中引发错误,如下所示:

TypeError: an integer is required

The error is definitely in "ldap_result_id = l.search_s(baseDN,searchScope,searchFilter,retrieveAttributes)" line, since I'm able to print the earlier comments. 该错误绝对在“ ldap_result_id = l.search_s(baseDN,searchScope,searchFilter,retrieveAttributes)”行中,因为我能够打印之前的注释。

What is the issue here? 这是什么问题?

The issue here is the scope level which cannot be passed as above from Robot. 这里的问题是无法从Robot像上面那样传递范围级别。 The changes I did was : 我所做的更改是:

def my_search(l, baseDN, searchScopeLevel, searchFilter="objectClass=*", retrieveAttributes=None):
try:
    if searchScopeLevel == 'ONE':
        searchScope=ldap.SCOPE_ONELEVEL
    elif searchScopeLevel == 'BASE':
        searchScope=ldap.SCOPE_BASE
    elif searchScopeLevel == 'SUB':
        searchScope=ldap.SCOPE_SUBTREE
    ldap_result_id = l.search(baseDN,searchScope,searchFilter,retrieveAttributes)

Robot TC Changes : 机器人TC更改:

*** Test Cases ***
Perform Ldap Operations
    ${ldapObj}    ldapopen    ${DB_1_EXT_APP_IP}
    ${SearchReturn}    my_search    ${ldapObj}        uid=205000000,ds=CRIBER,o=DEFT,dc=C    ONE    objectClass=*

And the issue is resolved. 问题已解决。 :) :)

Presuming the exception is raised in the my_search method - by default the arguments to methods in RF are casted to string. 假定在my_search方法中引发了异常-默认情况下,RF中方法的参数被my_search转换为字符串。 Thus this call: 因此,此调用:

${SearchReturn}    my_search    ${ldapObj}    "uid=2620105000000,ds=SUBSCRIBER,o=DEFAULT,dc=C-NTDB"    ldap.SCOPE_ONELEVEL    "objectClass=*"    None

Has a number of issues: 有很多问题:

  • the baseDN argument will have an actual value "uid=2620105000000,ds=SUBSCRIBER,o=DEFAULT,dc=C-NTDB" - ie with the quotation marks included, thus probably not what you're aiming for; baseDN参数的实际值为"uid=2620105000000,ds=SUBSCRIBER,o=DEFAULT,dc=C-NTDB" -即带引号,因此可能不是您想要的目标; remove them 删除它们
  • the same for the searchFilter - remove the quotes in the call searchFilter相同-删除通话中的引号
  • the searchScope , which probably is your problem, will receive the value ldap.SCOPE_ONELEVEL - a string with this content. searchScope (可能是您的问题)将收到值ldap.SCOPE_ONELEVEL-具有此内容的字符串。 This most probably is a constant defined in your ldap module; 这很可能是您的ldap模块中定义的常数。 the safest bet that it'll work is to provide the integer value of that const - integers are given in the format ${1} , but that's hardly sustainable. 最安全的选择是提供const的整数值-整数以${1}格式给出,但这很难持续。 Perhaps you could export it and the other constants in the COMMON/Libraries/pythonldap.py library, and use it in the test cases 也许您可以将其和其他常量导出到COMMON/Libraries/pythonldap.py库中,并在测试用例中使用
  • finally, the retrieveAttributes argument will receive the string literal "None", not the None datatype you probably want; 最后, retrieveAttributes参数将接收字符串文字“ None”,而不是您可能想要的None数据类型; to get it, use this RF builtin variable - ${None} 要获得它,请使用此RF内置变量- ${None}

HTH, and again - provide more details to receive on-the-spot answers. HTH,再说一次-提供更多详细信息以接收现场答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM