Cleanup ovsdb suites
[integration/test.git] / csit / libraries / Utils.robot
index bd4ecaf64393bd63b85d9fbab5bd5445485a1747..b30a37a025b9773327cb29e03cf560cb11ff5264 100644 (file)
@@ -26,7 +26,7 @@ Start Mininet
     Clean Mininet System
     ${mininet_conn_id}=    Open Connection    ${system}    prompt=${prompt}    timeout=${timeout}
     Set Suite Variable    ${mininet_conn_id}
-    Flexible Mininet Login    user=${user}    password=${password}
+    SSHKeywords.Flexible Mininet Login    user=${user}    password=${password}
     Execute Command    sudo ovs-vsctl set-manager ptcp:6644
     Write    ${start}
     Read Until    mininet>
@@ -43,7 +43,7 @@ Stop Mininet
     Close Connection
 
 Report_Failure_Due_To_Bug
-    [Arguments]    ${number}
+    [Arguments]    ${number}    ${include_bug_in_tags}=True
     [Documentation]    Report that a test failed due to a known Bugzilla bug whose
     ...    number is provided as an argument.
     ...    Not FAILED (incl. SKIPPED) test are not reported.
@@ -53,10 +53,12 @@ Report_Failure_Due_To_Bug
     ...    into the Robot log file.
     ${test_skipped}=    BuiltIn.Evaluate    len(re.findall('SKIPPED', """${TEST_MESSAGE}""")) > 0    modules=re
     BuiltIn.Return From Keyword If    ('${TEST_STATUS}' != 'FAIL') or ${test_skipped}
+    ${bug_url}=    BuiltIn.Set_Variable    https://bugs.opendaylight.org/show_bug.cgi?id=${number}
+    ${msg}=    BuiltIn.Set_Variable    This test fails due to ${bug_url}
     ${newline}=    BuiltIn.Evaluate    chr(10)
-    ${msg}=    BuiltIn.Set_Variable    This test fails due to https://bugs.opendaylight.org/show_bug.cgi?id=${number}
     BuiltIn.Set Test Message    ${msg}${newline}${newline}${TEST_MESSAGE}
     BuiltIn.Log    ${msg}
+    BuiltIn.Run_Keyword_If    "${include_bug_in_tags}"=="True"    Set Tags    ${bug_url}
 
 Report_Failure_And_Point_To_Linked_Bugs
     [Documentation]    Report that a test failed and point to linked Bugzilla bug(s).
@@ -96,22 +98,31 @@ Check For Specific Number Of Elements At URI
     Should Be Equal As Strings    ${resp.status_code}    200
     Should Contain X Times    ${resp.content}    ${element}    ${expected_count}
 
+Log Content
+    [Arguments]    ${resp_content}
+    ${resp_json} =    BuiltIn.Run Keyword If    '''${resp_content}''' != '${EMPTY}'    RequestsLibrary.To Json    ${resp_content}    pretty_print=True
+    ...    ELSE    BuiltIn.Set Variable    ${EMPTY}
+    BuiltIn.Log    ${resp_json}
+    [Return]    ${resp_json}
+
 Check For Elements At URI
-    [Arguments]    ${uri}    ${elements}    ${session}=session
+    [Arguments]    ${uri}    ${elements}    ${session}=session    ${pretty_print_json}=False
     [Documentation]    A GET is made at the supplied ${URI} and every item in the list of
     ...    ${elements} is verified to exist in the response
     ${resp}    RequestsLibrary.Get Request    ${session}    ${uri}
-    Log    ${resp.content}
+    BuiltIn.Run Keyword If    "${pretty_print_json}" == "True"    Log Content    ${resp.content}
+    ...    ELSE    BuiltIn.Log    ${resp.content}
     Should Be Equal As Strings    ${resp.status_code}    200
     : FOR    ${i}    IN    @{elements}
     \    Should Contain    ${resp.content}    ${i}
 
 Check For Elements Not At URI
-    [Arguments]    ${uri}    ${elements}    ${session}=session
+    [Arguments]    ${uri}    ${elements}    ${session}=session    ${pretty_print_json}=False
     [Documentation]    A GET is made at the supplied ${URI} and every item in the list of
     ...    ${elements} is verified to NOT exist in the response
     ${resp}    RequestsLibrary.Get Request    ${session}    ${uri}
-    Log    ${resp.content}
+    BuiltIn.Run Keyword If    "${pretty_print_json}" == "True"    Log Content    ${resp.content}
+    ...    ELSE    BuiltIn.Log    ${resp.content}
     Should Be Equal As Strings    ${resp.status_code}    200
     : FOR    ${i}    IN    @{elements}
     \    Should Not Contain    ${resp.content}    ${i}
@@ -169,53 +180,37 @@ Strip Quotes
     ${string_to_return}=    Replace String    ${string_to_strip}    "    \    count=-1
     [Return]    ${string_to_return}
 
-Flexible SSH Login
-    [Arguments]    ${user}    ${password}=${EMPTY}    ${delay}=0.5s
-    [Documentation]    On active SSH session: if given non-empty password, do Login, else do Login With Public Key.
-    ${pwd_length} =    BuiltIn.Get Length    ${password}
-    # ${pwd_length} is guaranteed to be an integer, so we are safe to evaluate it as Python expression.
-    BuiltIn.Run Keyword And Return If    ${pwd_length} > 0    SSHLibrary.Login    ${user}    ${password}    delay=${delay}
-    BuiltIn.Run Keyword And Return    SSHLibrary.Login With Public Key    ${user}    ${USER_HOME}/.ssh/${SSH_KEY}    ${KEYFILE_PASS}    delay=${delay}
-
-Flexible Mininet Login
-    [Arguments]    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${delay}=0.5s
-    [Documentation]    Call Flexible SSH Login, but with default values suitable for Mininet machine.
-    BuiltIn.Run Keyword And Return    Flexible SSH Login    user=${user}    password=${password}    delay=${delay}
-
-Flexible Controller Login
-    [Arguments]    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${delay}=0.5s
-    [Documentation]    Call Flexible SSH Login, but with default values suitable for Controller machine.
-    BuiltIn.Run Keyword And Return    Flexible SSH Login    user=${user}    password=${password}    delay=${delay}
-
 Run Command On Remote System
     [Arguments]    ${system}    ${cmd}    ${user}=${DEFAULT_USER}    ${password}=${EMPTY}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=${DEFAULT_TIMEOUT}
+    ...    ${return_stdout}=True    ${return_stderr}=False
     [Documentation]    Reduces the common work of running a command on a remote system to a single higher level
-    ...    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.
+    ...    robot keyword, taking care to log in with a public key and. The command given is written and the return value
+    ...    depends on the passed argument values of return_stdout (default: True) and return_stderr (default: False).
+    ...    At least one should be True, or the keyword will exit and FAIL. If both are True, the resulting return value
+    ...    will be a two element list containing both. Otherwise the resulting return value is a string.
+    ...    No test conditions are checked.
+    Run Keyword If    "${return_stdout}"!="True" and "${return_stderr}"!="True"    Fail    At least one of {return_stdout} or {return_stderr} args should be set to True
     ${current_ssh_connection}=    SSHLibrary.Get Connection
-    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}
+    BuiltIn.Log    Attempting to execute command "${cmd}" on remote system "${system}" by user "${user}" with keyfile pass "${keyfile_pass}" and prompt "${prompt}" and password "${password}"
     ${conn_id}=    SSHLibrary.Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
-    Flexible SSH Login    ${user}    ${password}
+    SSHKeywords.Flexible SSH Login    ${user}    ${password}
     ${stdout}    ${stderr}    SSHLibrary.Execute Command    ${cmd}    return_stderr=True
     SSHLibrary.Close Connection
     Log    ${stderr}
+    Run Keyword If    "${return_stdout}"!="True"    Return From Keyword    ${stderr}
+    Run Keyword If    "${return_stderr}"!="True"    Return From Keyword    ${stdout}
     [Teardown]    SSHKeywords.Restore_Current_SSH_Connection_From_Index    ${current_ssh_connection.index}
-    [Return]    ${stdout}
-
-Write_Bare_Ctrl_C
-    [Documentation]    Construct ctrl+c character and SSH-write it (without endline) to the current SSH connection.
-    ...    Do not read anything yet.
-    BuiltIn.Comment    Use RemoteBash.Write_Bare_Ctrl_C instead.
-    ${ctrl_c}=    BuiltIn.Evaluate    chr(int(3))
-    SSHLibrary.Write_Bare    ${ctrl_c}
-
-Write Bare Ctrl D
-    [Documentation]    Construct ctrl+d character and SSH-write it (without endline) to the current SSH connection.
-    ...    Do not read anything yet.
-    BuiltIn.Comment    Use RemoteBash.Write_Bare_Ctrl_D instead.
-    ${ctrl_d}=    BuiltIn.Evaluate    chr(int(4))
-    SSHLibrary.Write Bare    ${ctrl_d}
+    [Return]    ${stdout}    ${stderr}
+
+Run Command On Remote System And Log
+    [Arguments]    ${system}    ${cmd}    ${user}=${DEFAULT_USER}    ${password}=${EMPTY}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=${DEFAULT_TIMEOUT}
+    [Documentation]    Reduces the common work of running a command on a remote system to a single higher level
+    ...    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.
+    ${output} =    Run Command On Remote System    ${system}    ${cmd}    ${user}    ${password}    ${prompt}
+    ...    ${prompt_timeout}
+    Log    ${output}
+    [Return]    ${output}
 
 Run Command On Mininet
     [Arguments]    ${system}=${TOOLS_SYSTEM_IP}    ${cmd}=echo    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${prompt}=${TOOLS_SYSTEM_PROMPT}
@@ -225,7 +220,6 @@ 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
@@ -233,7 +227,7 @@ Verify File Exists On Remote System
     [Documentation]    Will create connection with public key and will PASS if the given ${file} exists,
     ...    otherwise will FAIL
     ${conn_id}=    Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
-    Flexible SSH Login    ${user}    ${password}
+    SSHKeywords.Flexible SSH Login    ${user}    ${password}
     SSHLibrary.File Should Exist    ${file}
     Close Connection
 
@@ -333,10 +327,21 @@ Add Elements To URI And Verify
 
 Add Elements To URI From File And Check Validation Error
     [Arguments]    ${dest_uri}    ${data_file}    ${headers}=${headers}    ${session}=session
+    [Documentation]    Shorthand for PUTting data from file and expecting status code 400.
+    BuiltIn.Comment    TODO: Does this have any benefits, considering TemplatedRequests can also do this in one line?
     ${body}    OperatingSystem.Get File    ${data_file}
     ${resp}    RequestsLibrary.Put Request    ${session}    ${dest_uri}    data=${body}    headers=${headers}
     Should Contain    ${DATA_VALIDATION_ERROR}    ${resp.status_code}
 
+Add Elements To URI From File And Check Server Error
+    [Arguments]    ${dest_uri}    ${data_file}    ${headers}=${headers}    ${session}=session
+    [Documentation]    Shorthand for PUTting data from file and expecting status code 500.
+    ...    Consider opening a Bug against ODL, as in most test cases, 400 is the http code to expect.
+    BuiltIn.Comment    TODO: Does this have any benefits, considering TemplatedRequests can also do this in one line?
+    ${body}    OperatingSystem.Get File    ${data_file}
+    ${resp}    RequestsLibrary.Put Request    ${session}    ${dest_uri}    data=${body}    headers=${headers}
+    Should Contain    ${INTERNAL_SERVER_ERROR}    ${resp.status_code}
+
 Post Elements To URI From File
     [Arguments]    ${dest_uri}    ${data_file}    ${headers}=${headers}    ${session}=session
     ${body}    OperatingSystem.Get File    ${data_file}
@@ -481,7 +486,7 @@ Install Package On Ubuntu System
     [Documentation]    Keyword to install packages for testing to Ubuntu Mininet VM
     Log    Keyword to install package to Mininet Ubuntu VM
     Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
-    Flexible Mininet Login    user=${user}    password=${password}
+    SSHKeywords.Flexible Mininet Login    user=${user}    password=${password}
     Write    sudo apt-get install -y ${package_name}
     Read Until    ${prompt}
 
@@ -494,6 +499,17 @@ Json Parse From String
 Json Parse From File
     [Arguments]    ${json_file}
     [Documentation]    Parse given file content into json (dictionary)
-    ${json_plain_string}    Get file    ${json_file}
+    ${json_plain_string}    OperatingSystem.Get file    ${json_file}
     ${json_data}    Json Parse From String    ${json_plain_string}
     [Return]    ${json_data}
+
+Modify Iptables On Remote System
+    [Arguments]    ${remote_system_ip}    ${iptables_rule}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
+    [Documentation]    Wrapper keyword to run iptables with any given ${iptables_rule} string on the remote system given
+    ...    by ${remote_system_ip}. The iptables listing will be output before and after the command is run
+    ${list_iptables_command} =    BuiltIn.Set Variable    sudo /sbin/iptables -L -n
+    ${output} =    Utils.Run Command On Remote System    ${remote_system_ip}    ${list_iptables_command}    ${user}    ${password}    prompt=${prompt}
+    BuiltIn.Log    ${output}
+    Utils.Run Command On Remote System    ${remote_system_ip}    sudo /sbin/iptables ${iptables_rule}    ${user}    ${password}    prompt=${prompt}
+    ${output} =    Utils.Run Command On Remote System    ${remote_system_ip}    ${list_iptables_command}    ${user}    ${password}    prompt=${prompt}
+    BuiltIn.Log    ${output}