go.ligato.io/vpp-agent/v3@v3.5.0/tests/robot/libraries/SshCommons.robot (about)

     1  *** Settings ***
     2  Documentation     This is a library for simple improvements over SSHLibrary for other robot libraries to use.
     3  Resource          ${CURDIR}/all_libs.robot
     4  
     5  *** Keywords ***
     6  Open_Ssh_Connection_Kube
     7      [Arguments]    ${name}    ${ip}    ${user}    ${pswd}
     8      [Documentation]    Create SSH connection to \{ip} aliased as \${name} and log in using \${user} and \${pswd} (or rsa).
     9      ...    Log to output file. The new connection is left active.
    10      BuiltIn.Log_Many    ${name}    ${ip}    ${user}    ${pswd}
    11      ${time} =    DateTime.Get_Current_Date
    12      ${connection}=    Open Connection    ${ip}    alias=${name}    timeout=${SSH_TIMEOUT}
    13      #${out} =    BuiltIn.Run_Keyword_If    """${pswd}""" != "rsa_id"    SSHLibrary.Login    ${user}    ${pswd}
    14      #${out2} =    BuiltIn.Run_Keyword_If    """${pswd}""" == "rsa_id"    SSHLibrary.Login_With_Public_Key    ${user}    %{HOME}/.ssh/id_rsa    any
    15      OperatingSystem.Append_To_File    ${RESULTS_FOLDER}/output_${name}.log    ${time}${\n}*** Command: Login${\n}
    16      ${out} =    BuiltIn.Run_Keyword_If    """${pswd}""" != "rsa_id"     Login    ${user}    ${pswd}
    17      ${out2} =    BuiltIn.Run_Keyword_If    """${pswd}""" == "rsa_id"    SSHLibrary.Login_With_Public_Key    ${user}    %{HOME}/.ssh/id_rsa    any
    18      ${time} =    DateTime.Get_Current_Date
    19      BuiltIn.Run_Keyword_If    """${out}""" != "None"    OperatingSystem.Append_To_File    ${RESULTS_FOLDER}/output_${name}.log    ${time}${\n}*** Response1: ${out}${\n}
    20      BuiltIn.Run_Keyword_If    """${out2}""" != "None"    OperatingSystem.Append_To_File    ${RESULTS_FOLDER}/output_${name}.log    ${time}${\n}*** Response2: {out2}${\n}
    21      [Return]    ${connection}
    22  
    23  Switch_And_Execute_With_Copied_File
    24      [Arguments]    ${ssh_session}    ${file_path}    ${command_prefix}    ${expected_rc}=0    ${ignore_stderr}=${False}    ${ignore_rc}=${False}
    25      [Documentation]    Switch to \${ssh_session} and continue with Execute_Command_With_Copied_File.
    26      BuiltIn.Log_Many    ${ssh_session}    ${file_path}    ${command_prefix}    ${expected_rc}    ${ignore_stderr}    ${ignore_rc}
    27      SSHLibrary.Switch_Connection    ${ssh_session}
    28      BuiltIn.Run_Keyword_And_Return    Execute_Command_With_Copied_File    ${file_path}    ${command_prefix}    expected_rc=${expected_rc}    ignore_stderr=${ignore_stderr}    ignore_rc=${ignore_rc}
    29  
    30  Execute_Command_With_Copied_File
    31      [Arguments]    ${file_path}    ${command_prefix}    ${expected_rc}=0    ${ignore_stderr}=${False}    ${ignore_rc}=${False}
    32      [Documentation]    Put file to current remote directory and execute command which takes computed file name as argument.
    33      BuiltIn.Log_Many    ${file_path}    ${command_prefix}    ${expected_rc}    ${ignore_stderr}    ${ignore_rc}
    34      Builtin.Comment    TODO: Do not pollute current remote directory.
    35      SSHLibrary.Put_File    ${file_path}    .
    36      ${splitted_path} =    String.Split_String    ${file_path}    separator=${/}
    37      BuiltIn.Run_Keyword_And_Return    Execute_Command_And_Log    ${command_prefix} @{splitted_path}[-1]    expected_rc=${expected_rc}    ignore_stderr=${ignore_stderr}    ignore_rc=${ignore_rc}
    38  
    39  Switch_Execute_And_Log_To_File
    40      [Arguments]    ${ssh_session}    ${command}    ${expected_rc}=0    ${ignore_stderr}=${False}    ${ignore_rc}=${False}    ${compress}=${False}
    41      [Documentation]    Call Switch_And_Execute_Command redirecting stdout to a remote file, download the file.
    42      ...    To distinguish separate invocations, suite name, test name, session alias
    43      ...    and full command are used to construct file name.
    44      BuiltIn.Log_Many    ${ssh_session}    ${command}    ${expected_rc}    ${ignore_stderr}    ${ignore_rc}    ${compress}
    45      SSHLibrary.Switch_Connection    ${ssh_session}
    46      ${connection} =    SSHLibrary.Get_Connection
    47      # In teardown, ${TEST_NAME} does not exist.
    48      ${testname} =    BuiltIn.Get_Variable_Value    ${TEST_NAME}    ${EMPTY}
    49      ${filename_with_spaces} =    BuiltIn.Set_Variable    ${testname}__${SUITE_NAME}__${connection.alias}__${command}.log
    50      ${filename} =    String.Replace_String    ${filename_with_spaces}    ${SPACE}    _
    51      BuiltIn.Log    ${filename}
    52      Execute_Command_And_Log    ${command} > ${filename}    expected_rc=${expected_rc}    ignore_stderr=${ignore_stderr}    ignore_rc=${ignore_rc}
    53      BuiltIn.Run_Keyword_If    ${compress}    Execute_Command_And_Log    xz -9e ${filename}
    54      ${filename} =    Builtin.Set_Variable_If    ${compress}    ${filename}.xz    ${filename}
    55      SSHLibrary.Get_File    ${filename}    ${RESULTS_FOLDER}/${filename}
    56      SSHLibrary.Get_File    ${filename}    ${RESULTS_FOLDER_SUITE}/${filename}
    57      [Teardown]    Execute_Command_And_Log    rm ${filename}
    58  
    59  Switch_And_Execute_Command
    60      [Arguments]    ${ssh_session}    ${command}    ${expected_rc}=0    ${ignore_stderr}=${False}    ${ignore_rc}=${False}
    61      [Documentation]    Switch to \${ssh_session}, and continue with Execute_Command_And_Log.
    62      BuiltIn.Log_Many    ${ssh_session}    ${command}    ${expected_rc}    ${ignore_stderr}    ${ignore_rc}
    63      SSHLibrary.Switch_Connection    ${ssh_session}
    64      ${out}=    Execute_Command_And_Log    ${command}    expected_rc=${expected_rc}    ignore_stderr=${ignore_stderr}    ignore_rc=${ignore_rc}
    65      BuiltIn.Log    ${out}
    66      [Return]    ${out}
    67  
    68  Execute_Command_And_Log
    69      [Arguments]    ${command}    ${expected_rc}=0    ${ignore_stderr}=${False}    ${ignore_rc}=${False}
    70      [Documentation]    Execute \${command} on current SSH session, log results, maybe fail on nonempty stderr, check \${expected_rc}, return stdout.
    71      BuiltIn.Log_Many    ${command}    ${expected_rc}    ${ignore_stderr}    ${ignore_rc}
    72      ${stdout}    ${stderr}    ${rc} =    SSHLibrary.Execute_Command    ${command}    return_stderr=True    return_rc=True
    73      BuiltIn.Log_Many    ${stdout}    ${stderr}    ${rc}
    74      Append_Command_Log    ${command}    ${stdout}    ${stderr}    ${rc}
    75      BuiltIn.Run_Keyword_Unless    ${ignore_stderr}    BuiltIn.Should_Be_Empty    ${stderr}
    76      BuiltIn.Run_Keyword_Unless    ${ignore_rc}    BuiltIn.Should_Be_Equal_As_Numbers    ${rc}    ${expected_rc}
    77      [Return]    ${stdout}
    78  
    79  Switch_And_Write_Command
    80      [Arguments]    ${ssh_session}    ${command}    ${prompt}=vpp#
    81      [Documentation]    Switch to \${ssh_session}, and continue with Write_Command_And_Log
    82      BuiltIn.Log_Many    ${ssh_session}    ${command}    ${prompt}
    83      SSHLibrary.Switch_Connection    ${ssh_session}
    84      BuiltIn.Run_Keyword_And_Return    Write_Command_And_Log    ${command}    ${prompt}
    85  
    86  Write_Command_And_Log
    87      [Arguments]    ${command}    ${prompt}=vpp#
    88      [Documentation]    Write \${command} on current SSH session, wait for prompt, log output, return output.
    89      BuiltIn.Log_Many    ${command}    ${prompt}
    90      SSHLibrary.Write    ${command}
    91      ${output} =    SSHLibrary.Read_Until    ${prompt}
    92      Append_Command_Log    ${command}    ${output}
    93      [Return]    ${output}
    94  
    95  Append_Command_Log
    96      [Arguments]    ${command}    ${output}=${EMPTY}    ${stderr}=${EMPTY}    ${rc}=${EMPTY}
    97      [Documentation]    Detect connection alias and time, append line with command and output to appropriate log file.
    98      Builtin.Log_Many    ${command}    ${output}    ${stderr}    ${rc}
    99      ${connection} =    SSHLibrary.Get_Connection
   100      ${time} =    DateTime.Get_Current_Date
   101      OperatingSystem.Append_To_File    ${RESULTS_FOLDER}/output_${connection.alias}.log    ${time}${\n}*** Command: ${command}${\n}
   102      OperatingSystem.Append_To_File    ${RESULTS_FOLDER_SUITE}/output_${connection.alias}.log    ${time}${\n}*** Command: ${command}${\n}
   103      ${output_length} =    BuiltIn.Get_Length    ${output}
   104      ${if_output} =    BuiltIn.Set_Variable_If    ${output_length}    ${output}${\n}    ${EMPTY}
   105      ${stderr_length} =    Builtin.Get_Length    ${stderr}
   106      ${if_stderr} =    BuiltIn.Set_Variable_If    ${stderr_length}    *** Stderr: ${stderr}${\n}    ${EMPTY}
   107      ${if_rc} =    BuiltIn.Set_Variable_If    """${rc}"""    *** Return code: ${rc}${\n}    ${EMPTY}
   108      ${time} =    DateTime.Get_Current_Date
   109      OperatingSystem.Append_To_File    ${RESULTS_FOLDER}/output_${connection.alias}.log    ${time}${\n}*** Response: ${if_stderr}${if_rc}${if_output}
   110      OperatingSystem.Append_To_File    ${RESULTS_FOLDER_SUITE}/output_${connection.alias}.log    ${time}${\n}*** Response: ${if_stderr}${if_rc}${if_output}