Search code examples
pythonrobotframeworkpython-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:

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:

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.

What is the issue here?

The issue here is the scope level which cannot be passed as above from 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 :

*** 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. :)


Solution

  • Presuming the exception is raised in the my_search method - by default the arguments to methods in RF are casted to string. 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" - i.e. with the quotation marks included, thus probably not what you're aiming for; remove them
    • the same for the searchFilter - remove the quotes in the call
    • the searchScope, which probably is your problem, will receive the value ldap.SCOPE_ONELEVEL - a string with this content. This most probably is a constant defined in your ldap module; 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. Perhaps you could export it and the other constants in the COMMON/Libraries/pythonldap.py library, and use it in the test cases
    • finally, the retrieveAttributes argument will receive the string literal "None", not the None datatype you probably want; to get it, use this RF builtin variable - ${None}

    HTH, and again - provide more details to receive on-the-spot answers.