Add keyword for watching TCP ports on remote machine
[integration/test.git] / csit / libraries / SSHKeywords.robot
1 Documentation     Resource enhancing SSHLibrary with Keywords used in multiple suites.
2 ...
3 ...               Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 ...
5 ...               This program and the accompanying materials are made available under the
6 ...               terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 ...               and is available at http://www.eclipse.org/legal/epl-v10.html
8 ...
9 ...
10 ...               Some suites evolved utility Keywords re-usable with other suites.
11 ...               When the Keywords assume a SSH session is active,
12 ...               and if the Keywords do not fit into a more specific Resource,
13 ...               you can place them here.
14 Library           SSHLibrary
15
16 *** Keywords ***
17 Execute_Command_Passes
18     [Arguments]    ${command}
19     [Documentation]    Execute command via SSH. If RC is nonzero, log everything. Retrun bool string of command success.
20     ${stdout}    ${stderr}    ${rc} =    SSHLibrary.Execute_Command    ${command}    return_stderr=True    return_rc=True
21     BuiltIn.Return_From_Keyword_If    ${rc} == 0    True
22     BuiltIn.Log    ${stdout}
23     BuiltIn.Log    ${stderr}
24     BuiltIn.Log    ${rc}
25     [Return]    False
26
27 Require_Python
28     [Documentation]    Verify current SSH connection leads to machine with python working. Fatal fail otherwise.
29     ${passed} =    Execute_Command_Passes    python --help
30     BuiltIn.Return_From_Keyword_If    ${passed}
31     BuiltIn.Fatal_Error    Python is not installed!
32
33 Assure_Library_Ipaddr
34     [Arguments]    ${target_dir}=.
35     [Documentation]    Tests whether ipaddr module is present on ssh-connected machine, Puts ipaddr.py to target_dir if not.
36     ${passed} =    Execute_Command_Passes    bash -c 'cd "${target_dir}" && python -c "import ipaddr"'
37     BuiltIn.Return_From_Keyword_If    ${passed}
38     SSHLibrary.Put_File    ${CURDIR}/ipaddr.py    ${target_dir}/
39
40 Assure_Library_Counter
41     [Arguments]    ${target_dir}=.
42     [Documentation]    Tests whether Counter is present in collections on ssh-connected machine, Puts Counter.py to workspace if not.
43     ${passed} =    Execute_Command_Passes    bash -c 'cd "${target_dir}" && python -c "from collections import Counter"'
44     # TODO: Move the bash-cd wrapper to separate keyword?
45     BuiltIn.Return_From_Keyword_If    ${passed}
46     SSHLibrary.Put_File    ${CURDIR}/Counter.py    ${target_dir}/
47
48 Count_Port_Occurences
49     [Arguments]    ${port}    ${state}    ${name}
50     [Documentation]    Run 'netstat' on the remote machine and count occurences of given port in the given state connected to process with the given name.
51     ${output}=    SSHLibrary.Execute_Command    netstat -natp 2> /dev/null | grep -E ":${port} .+ ${state} .+${name}" | wc -l
52     [Return]    ${output}