Create binding-parent.robot suite
[integration/test.git] / csit / libraries / SSHKeywords.robot
1 *** Settings ***
2 Documentation     Resource enhancing SSHLibrary with Keywords used in multiple suites.
3 ...
4 ...               Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
5 ...
6 ...               This program and the accompanying materials are made available under the
7 ...               terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 ...               and is available at http://www.eclipse.org/legal/epl-v10.html
9 ...
10 ...
11 ...               Some suites evolved utility Keywords re-usable with other suites.
12 ...               When the Keywords assume a SSH session is active,
13 ...               and if the Keywords do not fit into a more specific Resource,
14 ...               you can place them here.
15 ...
16 ...               TODO: Migrate Keywords related to handling SSH here.
17 ...               That may include Utils.Flexible_SSH_Login, KarafKeywords.Restore_Current_SSH_Connection_From_Index and similar.
18 Library           SSHLibrary
19 Resource          ${CURDIR}/Utils.robot
20
21 *** Variables ***
22 ${SSHKeywords__current_remote_working_directory}    .
23
24 *** Keywords ***
25 Open_Connection_To_ODL_System
26     [Documentation]    Open a connection to the ODL system and return its identifier.
27     ...    On clustered systems this opens the connection to the first node.
28     ${odl} =    SSHLibrary.Open_Connection    ${ODL_SYSTEM_IP}    prompt=${ODL_SYSTEM_PROMPT}    timeout=10s
29     Utils.Flexible_Controller_Login
30     [Return]    ${odl}
31
32 Open_Connection_To_Tools_System
33     [Documentation]    Open a connection to the tools system and return its identifier.
34     ${tools} =    SSHLibrary.Open_Connection    ${TOOLS_SYSTEM_IP}    prompt=${TOOLS_SYSTEM_PROMPT}
35     Utils.Flexible_Mininet_Login
36     [Return]    ${tools}
37
38 Log_Command_Results
39     [Arguments]    ${stdout}    ${stderr}    ${rc}
40     [Documentation]    Log everything returned by SSHLibrary.Execute_Command
41     BuiltIn.Log    ${stdout}
42     BuiltIn.Log    ${stderr}
43     BuiltIn.Log    ${rc}
44
45 Execute_Command_Passes
46     [Arguments]    ${command}    ${return_success_only}=True    ${log_on_success}=False    ${log_on_failure}=True    ${stderr_must_be_empty}=False
47     [Documentation]    Execute command via the active SSH connection. For success, rc has to be zero and optionally stderr has to be empty.
48     ...    Log everything, depending on arguments and success. Retrun either success string or stdout.
49     ...    TODO: Do we want to support customizing return values the same way as SSHLibrary.Execute_Command does?
50     ${stdout}    ${stderr}    ${rc} =    SSHLibrary.Execute_Command    ${command}    return_stderr=True    return_rc=True
51     ${emptiness_status}    ${result} =    BuiltIn.Run_Keyword_And_Ignore_Error    BuiltIn.Should_Be_Empty    ${stderr}
52     ${success} =    BuiltIn.Set_Variable_If    (${rc} == 0) and (("${emptiness_status}" == "PASS") or not ${stderr_must_be_empty})    True    False
53     BuiltIn.Run_Keyword_If    (${log_on_success} and ${success}) or (${log_on_failure} and not ${success})    Log_Command_Results    ${stdout}    ${stderr}    ${rc}
54     BuiltIn.Return_From_Keyword_If    ${return_success_only}    ${success}
55     BuiltIn.Return_From_Keyword_If    ${success}    ${stdout}
56     BuiltIn.Fail    Got rc: ${rc} or stderr was not empty: ${stderr}
57
58 Execute_Command_Should_Pass
59     [Arguments]    ${command}    ${log_on_success}=True    ${log_on_failure}=True    ${stderr_must_be_empty}=False
60     [Documentation]    A wrapper for Execute_Command_Passes with return_success_only=False
61     ...    Also, log_on_success defaults to True (but is customizable, unlike return_success_only)..
62     BuiltIn.Run_Keyword_And_Return    Execute_Command_Passes    ${command}    return_success_only=False    log_on_success=${log_on_success}    log_on_failure=${log_on_failure}    stderr_must_be_empty=${stderr_must_be_empty}
63
64 Execute_Command_At_Path_Should_Pass
65     [Arguments]    ${command}    ${path}=None    ${log_on_success}=True    ${log_on_failure}=True    ${stderr_must_be_empty}=False
66     [Documentation]    A keyword similar to Execute_Command_Should_Pass which performs "cd" to ${path} before executing the ${command}.
67     ...    This is useful when rewriting bash scripts, as series of SSHLibrary.Execute_Command do not share current working directory.
68     ...    TODO: Perhaps a Keyword which sets up environment variables would be useful as well.
69     ${cd_and_command} =    BuiltIn.Set_Variable    cd '${path}' && ${command}
70     BuiltIn.Run_Keyword_And_Return    Execute_Command_Passes    ${cd_and_command}    return_success_only=False    log_on_success=${log_on_success}    log_on_failure=${log_on_failure}    stderr_must_be_empty=${stderr_must_be_empty}
71
72 Set_Cwd
73     [Arguments]    ${path}
74     [Documentation]    Set \${SSHKeywords__current_remote_working_directory} variable to ${path}. If SSH default is desired, use dot.
75     BuiltIn.Set_Suite_Variable    \${SSHKeywords__current_remote_working_directory}    ${path}
76
77 Execute_Command_At_Cwd_Should_Pass
78     [Arguments]    ${command}    ${log_on_success}=True    ${log_on_failure}=True    ${stderr_must_be_empty}=True
79     [Documentation]    Run Execute_Command_At_Path_Should_Pass with previously set CWD as path.
80     BuiltIn.Run_Keyword_And_Return    Execute_Command_At_Path_Should_Pass    command=${command}    path=${SSHKeywords__current_remote_working_directory}    log_on_success=${log_on_success}    log_on_failure=${log_on_failure}    stderr_must_be_empty=${stderr_must_be_empty}
81
82 Require_Python
83     [Documentation]    Verify current SSH connection leads to machine with python working. Fatal fail otherwise.
84     ${passed} =    Execute_Command_Passes    python --help
85     BuiltIn.Return_From_Keyword_If    ${passed}
86     BuiltIn.Fatal_Error    Python is not installed!
87
88 Assure_Library_Ipaddr
89     [Arguments]    ${target_dir}=.
90     [Documentation]    Tests whether ipaddr module is present on ssh-connected machine, Puts ipaddr.py to target_dir if not.
91     ${passed} =    Execute_Command_Passes    bash -c 'cd "${target_dir}" && python -c "import ipaddr"'
92     BuiltIn.Return_From_Keyword_If    ${passed}
93     SSHLibrary.Put_File    ${CURDIR}/ipaddr.py    ${target_dir}/
94
95 Assure_Library_Counter
96     [Arguments]    ${target_dir}=.
97     [Documentation]    Tests whether Counter is present in collections on ssh-connected machine, Puts Counter.py to workspace if not.
98     ${passed} =    Execute_Command_Passes    bash -c 'cd "${target_dir}" && python -c "from collections import Counter"'
99     # TODO: Move the bash-cd wrapper to separate keyword?
100     BuiltIn.Return_From_Keyword_If    ${passed}
101     SSHLibrary.Put_File    ${CURDIR}/Counter.py    ${target_dir}/
102
103 Count_Port_Occurences
104     [Arguments]    ${port}    ${state}    ${name}
105     [Documentation]    Run 'netstat' on the remote machine and count occurences of given port in the given state connected to process with the given name.
106     ${output} =    SSHLibrary.Execute_Command    netstat -natp 2> /dev/null | grep -E ":${port} .+ ${state} .+${name}" | wc -l
107     [Return]    ${output}