Compare indexes as integers in Get_Owner_And_Candidates_For_Device
[integration/test.git] / csit / libraries / Utils.robot
index 13b1dd1b29db890d5f4c5de847c1a6e231ef6446..d9a750b3db391c7e273acc63fdc855b95c912778 100644 (file)
@@ -6,16 +6,17 @@ Library           DateTime
 Library           Process
 Library           Collections
 Library           RequestsLibrary
-Library           ./UtilLibrary.py
-Resource          KarafKeywords.robot
-Variables         ../variables/Variables.py
+Library           ${CURDIR}/UtilLibrary.py
+Resource          ${CURDIR}/SSHKeywords.robot
+Resource          ${CURDIR}/TemplatedRequests.robot
+Variables         ${CURDIR}/../variables/Variables.py
 
 *** Variables ***
 # TODO: Introduce ${tree_size} and use instead of 1 in the next line.
 ${start}          sudo mn --controller=remote,ip=${ODL_SYSTEM_IP} --topo tree,1 --switch ovsk,protocols=OpenFlow13
 
 *** Keywords ***
-Start Suite
+Start Mininet
     [Arguments]    ${system}=${TOOLS_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${timeout}=30s
     [Documentation]    Basic setup/cleanup work that can be done safely before any system
     ...    is run.
@@ -28,29 +29,7 @@ Start Suite
     Write    ${start}
     Read Until    mininet>
 
-Start Mininet
-    [Arguments]    ${system}=${TOOLS_SYSTEM_IP}    ${cmd}=${start}    ${custom}=    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}
-    ...    ${prompt_timeout}=30s
-    [Documentation]    Basic setup to start mininet with custom topology
-    Log    Start the test on the base edition
-    Clean Mininet System
-    ${mininet_conn_id}=    Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
-    Set Suite Variable    ${mininet_conn_id}
-    Flexible Mininet Login    user=${user}    password=${password}
-    Put File    ${custom}
-    Write    ${cmd}
-    Read Until    mininet>
-    [Return]    ${mininet_conn_id}
-
 Stop Mininet
-    [Arguments]    ${mininet_conn_id}    ${prompt}=${DEFAULT_LINUX_PROMPT}
-    [Documentation]    Basic setup to stop/clean mininet
-    Switch Connection    ${mininet_conn_id}
-    SSHLibrary.Write    exit
-    Read Until    ${prompt}
-    Close Connection
-
-Stop Suite
     [Arguments]    ${prompt}=${DEFAULT_LINUX_PROMPT}
     [Documentation]    Cleanup/Shutdown work that should be done at the completion of all
     ...    tests
@@ -97,17 +76,6 @@ Report_Failure_And_Point_To_Linked_Bugs
     BuiltIn.Set Test Message    ${msg}${newline}${bugs}${newline}${newline}${TEST_MESSAGE}
     BuiltIn.Log    ${msg}${newline}${bugs}
 
-Ensure All Nodes Are In Response
-    [Arguments]    ${URI}    ${node_list}
-    [Documentation]    A GET is made to the supplied ${URI} and every item in the ${node_list}
-    ...    is verified to exist in the repsonse. This keyword currently implies that it's node
-    ...    specific but any list of strings can be given in ${node_list}. Refactoring of this
-    ...    to make it more generic should be done. (see keyword "Check For Elements At URI")
-    : FOR    ${node}    IN    @{node_list}
-    \    ${resp}    RequestsLibrary.Get Request    session    ${URI}
-    \    Should Be Equal As Strings    ${resp.status_code}    200
-    \    Should Contain    ${resp.content}    ${node}
-
 Check Nodes Stats
     [Arguments]    ${node}
     [Documentation]    A GET on the /node/${node} API is made and specific flow stat
@@ -117,16 +85,6 @@ Check Nodes Stats
     Should Contain    ${resp.content}    flow-capable-node-connector-statistics
     Should Contain    ${resp.content}    flow-table-statistics
 
-Check That Port Count Is Ok
-    [Arguments]    ${node}    ${count}
-    [Documentation]    A GET on the /port API is made and the specified port ${count} is
-    ...    verified. A more generic Keyword "Check For Specific Number Of Elements At URI"
-    ...    also does this work and further consolidation should be done.
-    ${resp}    RequestsLibrary.Get Request    session    ${REST_CONTEXT}/${CONTAINER}/port
-    Log    ${resp.content}
-    Should Be Equal As Strings    ${resp.status_code}    200
-    Should Contain X Times    ${resp.content}    ${node}    ${count}
-
 Check For Specific Number Of Elements At URI
     [Arguments]    ${uri}    ${element}    ${expected_count}
     [Documentation]    A GET is made to the specified ${URI} and the specific count of a
@@ -233,13 +191,14 @@ Run Command On Remote System
     ...    robot keyword, taking care to log in with a public key and. The command given is written
     ...    and the output returned. No test conditions are checked.
     ${current_ssh_connection}=    SSHLibrary.Get Connection
-    Log    Attempting to execute command "${cmd}" on remote system "${system}" by user "${user}" with keyfile pass "${keyfile_pass}" and prompt "${prompt}"
+    BuiltIn.Log    Attempting to execute command "${cmd}" on remote system "${system}" by user "${user}" with keyfile pass "${keyfile_pass}" and prompt "${prompt}"
+    BuiltIn.Log    ${password}
     ${conn_id}=    SSHLibrary.Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
     Flexible SSH Login    ${user}    ${password}
     ${stdout}    ${stderr}    SSHLibrary.Execute Command    ${cmd}    return_stderr=True
     SSHLibrary.Close Connection
     Log    ${stderr}
-    [Teardown]    KarafKeywords.Restore_Current_SSH_Connection_From_Index    ${current_ssh_connection.index}
+    [Teardown]    SSHKeywords.Restore_Current_SSH_Connection_From_Index    ${current_ssh_connection.index}
     [Return]    ${stdout}
 
 Write_Bare_Ctrl_C
@@ -262,6 +221,7 @@ Run Command On Mininet
 Run Command On Controller
     [Arguments]    ${system}=${ODL_SYSTEM_IP}    ${cmd}=echo    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
     [Documentation]    Call Run Comand On Remote System, but with default values suitable for Controller machine.
+    BuiltIn.Log    ${password}
     BuiltIn.Run Keyword And Return    Run Command On Remote System    ${system}    ${cmd}    ${user}    ${password}    prompt=${prompt}
 
 Verify File Exists On Remote System
@@ -273,6 +233,13 @@ Verify File Exists On Remote System
     SSHLibrary.File Should Exist    ${file}
     Close Connection
 
+Check Karaf Log File Does Not Have Messages
+    [Arguments]    ${ip}    ${message}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}    ${log_file}=${WORKSPACE}/${BUNDLEFOLDER}/data/log/karaf.log
+    [Documentation]    Fails if the provided ${message} is found in the karaf.log file. Uses grep to search. The
+    ...    karaf.log file can be overridden with ${log_file} to be any file on the given system @ ${ip}
+    ${output}=    Run Command On Controller    ${ip}    grep -c '${message}' ${log_file}    user=${user}    password=${password}    prompt=${prompt}
+    Should Be Equal As Strings    ${output}    0
+
 Verify Controller Is Not Dead
     [Arguments]    ${controller_ip}=${ODL_SYSTEM_IP}
     [Documentation]    Will execute any tests to verify the controller is not dead. Some checks are
@@ -315,41 +282,48 @@ Concatenate the String
     [Return]    ${output}
 
 Post Elements To URI
-    [Arguments]    ${rest_uri}    ${data}    ${headers}=${headers}
+    [Arguments]    ${rest_uri}    ${data}    ${headers}=${headers}    ${session}=session
     [Documentation]    Perform a POST rest operation, using the URL and data provided
-    ${resp} =    RequestsLibrary.Post Request    session    ${rest_uri}    data=${data}    headers=${headers}
-    Should Be Equal As Strings    ${resp.status_code}    200
+    ${resp} =    RequestsLibrary.Post Request    ${session}    ${rest_uri}    data=${data}    headers=${headers}
+    Log    ${resp.content}
+    Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
 
 Remove All Elements At URI
     [Arguments]    ${uri}
     ${resp}    RequestsLibrary.Delete Request    session    ${uri}
-    Should Be Equal As Strings    ${resp.status_code}    200
+    Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
 
 Remove All Elements At URI And Verify
     [Arguments]    ${uri}
     ${resp}    RequestsLibrary.Delete Request    session    ${uri}
-    Should Be Equal As Strings    ${resp.status_code}    200
+    Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
     ${resp}    RequestsLibrary.Get Request    session    ${uri}
     Should Be Equal As Strings    ${resp.status_code}    404
 
+Remove All Elements If Exist
+    [Arguments]    ${uri}
+    [Documentation]    Delete all elements from an URI if the configuration was not empty
+    ${resp}    RequestsLibrary.Get Request    session    ${uri}
+    Run Keyword If    '${resp.status_code}'!='404'    Remove All Elements At URI    ${uri}
+
 Add Elements To URI From File
     [Arguments]    ${dest_uri}    ${data_file}    ${headers}=${headers}
     ${body}    OperatingSystem.Get File    ${data_file}
     ${resp}    RequestsLibrary.Put Request    session    ${dest_uri}    data=${body}    headers=${headers}
-    Should Be Equal As Strings    ${resp.status_code}    200
+    Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
 
 Add Elements To URI From File And Verify
     [Arguments]    ${dest_uri}    ${data_file}    ${headers}=${headers}
     ${body}    OperatingSystem.Get File    ${data_file}
     ${resp}    RequestsLibrary.Put Request    session    ${dest_uri}    data=${body}    headers=${headers}
-    Should Be Equal As Strings    ${resp.status_code}    200
+    Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
     ${resp}    RequestsLibrary.Get Request    session    ${dest_uri}
     Should Not Be Equal    ${resp.status_code}    404
 
 Add Elements To URI And Verify
     [Arguments]    ${dest_uri}    ${data_file}    ${headers}=${headers}
     ${resp}    RequestsLibrary.Put Request    session    ${dest_uri}    ${data_file}    headers=${headers}
-    Should Be Equal As Strings    ${resp.status_code}    200
+    Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
     ${resp}    RequestsLibrary.Get Request    session    ${dest_uri}
     Should Not Be Equal    ${resp.status_code}    404
 
@@ -357,7 +331,7 @@ Post Elements To URI From File
     [Arguments]    ${dest_uri}    ${data_file}    ${headers}=${headers}
     ${body}    OperatingSystem.Get File    ${data_file}
     ${resp}    RequestsLibrary.Post Request    session    ${dest_uri}    data=${body}    headers=${headers}
-    Should Be Equal As Strings    ${resp.status_code}    200
+    Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
 
 Run Process With Logging And Status Check
     [Arguments]    @{proc_args}
@@ -397,7 +371,7 @@ Get Index From List Of Dictionaries
     ${index}=    Set Variable    -1
     : FOR    ${i}    IN RANGE    ${length}
     \    ${dictionary}=    Get From List    ${dictionary_list}    ${i}
-    \    Run Keyword If    '&{dictionary}[${key}]' == '${value}'    Set Test Variable    ${index}    ${i}
+    \    Run Keyword If    """&{dictionary}[${key}]""" == """${value}"""    Set Test Variable    ${index}    ${i}
     [Return]    ${index}
 
 Check Item Occurrence