Migrate Get Requests invocations(libraries)
[integration/test.git] / csit / libraries / KillPythonTool.robot
1 *** Settings ***
2 Documentation       Robot keyword library (Resource) for killing possibly left-over Python utilities.
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 ...                 This library assumes that a SSH connections exists (and is switched to)
12 ...                 to a Linux machine (usualy TOOLS_SYSTEM_IP) where the Python process should be killed.
13 ...                 TODO: The Utils.robot library has a "Run Command On Remote System" if we didn't want to make the assumption that an SSH connection was already open.
14 ...                 alternative TODO: Explain that it is not worth to perform separate SSH logins.
15 ...
16 ...                 The argument ${filter} should hold what you would type to grep command in bash:
17 ...                 enclosed in single quotes, dots escaped by backslash and so on.
18 ...                 Note that single quote inside cannot be escaped, but may be typed as this: '"'"'
19
20
21 *** Keywords ***
22 Search_And_Kill_Remote_Python
23     [Documentation]    The main keyword. Search for processes, Log the list of them, kill them.
24     [Arguments]    ${filter}
25     ${processes}=    Search_For_Remote_Python_Processes    ${filter}
26     BuiltIn.Log    ${processes}
27     Kill_Remote_Processes    ${processes}
28
29 Search_For_Remote_Python_Processes
30     [Documentation]    Only searches for the list of processes, in case something else than kill has to be done with them.
31     [Arguments]    ${filter}
32     ${processes}=    SSHLibrary.Execute_Command    ps -elf | egrep python | egrep ${filter} | egrep -v grep
33     RETURN    ${processes}
34
35     # TODO: Is "python" worth such a special treatment to have it mentioned in keyword name?
36
37 Kill_Remote_Processes
38     [Documentation]    Kill processes by PIDs from given list (no-op if the list is empty), using specified signal. Log the kill commands used.
39     [Arguments]    ${pself_lines}    ${signal}=9
40     ${arg_length}=    BuiltIn.Get_Length    ${pself_lines}
41     # nothing to kill here
42     IF    ${arg_length} == 0    RETURN
43     ${commands}=    SSHLibrary.Execute_Command    echo '${pself_lines}' | awk '{print "kill -${signal}",$4}'
44     BuiltIn.Log    ${commands}
45     ${stdout}    ${stderr}=    SSHLibrary.Execute_Command
46     ...    echo 'set -exu; ${commands}' | sudo sh
47     ...    return_stderr=True
48     # TODO: Is -exu needed? Should we Log ${std*}?