Repair OF test
[integration/test.git] / csit / libraries / SSHKeywords.robot
index 870f90494101d259a5b0107b1135b13e3b9ff753..eea009d98cb718b2a1daf4308a641d7884844ff0 100644 (file)
@@ -12,11 +12,9 @@ Documentation     Resource enhancing SSHLibrary with Keywords used in multiple s
 ...               When the Keywords assume a SSH session is active,
 ...               and if the Keywords do not fit into a more specific Resource,
 ...               you can place them here.
-...
-...               TODO: Migrate Keywords related to handling SSH here.
-...               That may include Utils.Flexible_SSH_Login, and similar.
 Library           SSHLibrary
 Resource          ${CURDIR}/Utils.robot
+Resource          ../variables/Variables.robot
 
 *** Variables ***
 ${SSHKeywords__current_remote_working_directory}    .
@@ -27,14 +25,14 @@ Open_Connection_To_ODL_System
     [Arguments]    ${ip_address}=${ODL_SYSTEM_IP}    ${timeout}=10s
     [Documentation]    Open a connection to the ODL system at ${ip_address} and return its identifier.
     ${odl_connection} =    SSHLibrary.Open_Connection    ${ip_address}    prompt=${ODL_SYSTEM_PROMPT}    timeout=${timeout}
-    Utils.Flexible_Controller_Login
+    Flexible_Controller_Login
     [Return]    ${odl_connection}
 
 Open_Connection_To_Tools_System
     [Arguments]    ${ip_address}=${TOOLS_SYSTEM_IP}    ${timeout}=10s
     [Documentation]    Open a connection to the tools system at ${ip_address} and return its identifier.
     ${tools_connection} =    SSHLibrary.Open_Connection    ${ip_address}    prompt=${TOOLS_SYSTEM_PROMPT}    timeout=${timeout}
-    Utils.Flexible_Mininet_Login
+    Flexible_Mininet_Login
     [Return]    ${tools_connection}
 
 Restore_Current_Ssh_Connection_From_Index
@@ -73,6 +71,22 @@ Run_Keyword_Preserve_Connection
     # Resource name has to be prepended, as KarafKeywords still contains a redirect.
     [Teardown]    SSHKeywords.Restore_Current_SSH_Connection_From_Index    ${current_connection.index}
 
+Run_Keyword_With_Ssh
+    [Arguments]    ${ip_address}    ${keyword_name}    @{args}    &{kwargs}
+    [Documentation]    Open temporary connection to given IP address, run keyword, close connection, restore previously active connection, return result.
+    Run_Keyword_Preserve_Connection    Run_Unsafely_Keyword_Over_Temporary_Odl_Session    ${ip_address}    ${keyword_name}    @{args}    &{kwargs}
+
+Run_Unsafely_Keyword_Over_Temporary_Odl_Session
+    [Arguments]    ${ip_address}    ${keyword_name}    @{args}    &{kwargs}
+    [Documentation]    Open connection to given IP address, run keyword, close connection, return result.
+    ...    This is unsafe in the sense that previously active session will be switched out off, but safe in the sense only the temporary connection is closed.
+    Open_Connection_To_ODL_System    ${ip_address}
+    # Not using Teardown, to avoid a call to close if the previous line fails.
+    ${status}    ${result} =    BuiltIn.Run_Keyword_And_Ignore_Error    ${keyword_name}    @{args}    &{kwargs}
+    SSHLibrary.Close_Connection
+    BuiltIn.Return_From_Keyword_If    "${status}" == "PASS"    ${result}
+    BuiltIn.Fail    ${result}
+
 Log_Command_Results
     [Arguments]    ${stdout}    ${stderr}    ${rc}
     [Documentation]    Log everything returned by SSHLibrary.Execute_Command
@@ -191,11 +205,48 @@ Virtual_Env_Deactivate_On_Current_Session
     ${output}=    SSHLibrary.Read_Until_Prompt
     BuiltIn.Run_Keyword_If    ${log_output}==${True}    BuiltIn.Log    ${output}
 
-Copy File To Remote System
-    [Arguments]    ${system}    ${source}    ${destination}    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${prompt}=${TOOLS_SYSTEM_PROMPT}
+Unsafe_Copy_File_To_Remote_System
+    [Arguments]    ${system}    ${source}    ${destination}=./    ${user}=${DEFAULT_USER}    ${password}=${DEFAULT_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}
+    ...    ${prompt_timeout}=5s
+    [Documentation]    Copy the ${source} file to the ${destination} file on the remote ${system}. The keyword opens and closes a single
+    ...    ssh connection and does not rely on any existing ssh connection that may be open.
+    SSHLibrary.Open_Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
+    Flexible_SSH_Login    ${user}    ${password}
+    SSHLibrary.Put_File    ${source}    ${destination}
+    SSHLibrary.Close Connection
+
+Copy_File_To_Remote_System
+    [Arguments]    ${system}    ${source}    ${destination}=./    ${user}=${DEFAULT_USER}    ${password}=${DEFAULT_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}
     ...    ${prompt_timeout}=5s
-    [Documentation]    Simplifies copy file operations to remote system
-    ${conn_id}=    Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
-    Flexible SSH Login    ${user}    ${password}
-    SSHLibrary.Put File    ${source}    ${destination}
-    Close Connection
+    [Documentation]    Copy the ${source} file to the ${destination} file on the remote ${system}. Any pre-existing active
+    ...    ssh connection will be retained.
+    SSHKeywords.Run_Keyword_Preserve_Connection    SSHKeywords.Unsafe_Copy_File_To_Remote_System    ${system}    ${source}    ${destination}    ${user}    ${password}
+    ...    ${prompt}    ${prompt_timeout}
+
+Copy_File_To_Odl_System
+    [Arguments]    ${system}    ${source}    ${destination}=./
+    [Documentation]    Wrapper keyword to make it easier to copy a file to an ODL specific system
+    SSHKeywords.Copy_File_To_Remote_System    ${system}    ${source}    ${destination}    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}    ${ODL_SYSTEM_PROMPT}
+
+Copy_File_To_Tools_System
+    [Arguments]    ${system}    ${source}    ${destination}=./
+    [Documentation]    Wrapper keyword to make it easier to copy a file to an Tools specific system
+    SSHKeywords.Copy_File_To_Remote_System    ${system}    ${source}    ${destination}    ${TOOLS_SYSTEM_USER}    ${TOOLS_SYSTEM_PASSWORD}    ${TOOLS_SYSTEM_PROMPT}
+
+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}