activation/deactivation virtenv kws
[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 ${SSHKeywords__current_venv_path}    /tmp/defaultvenv
24
25 *** Keywords ***
26 Open_Connection_To_ODL_System
27     [Documentation]    Open a connection to the ODL system and return its identifier.
28     ...    On clustered systems this opens the connection to the first node.
29     ${odl} =    SSHLibrary.Open_Connection    ${ODL_SYSTEM_IP}    prompt=${ODL_SYSTEM_PROMPT}    timeout=10s
30     Utils.Flexible_Controller_Login
31     [Return]    ${odl}
32
33 Open_Connection_To_Tools_System
34     [Documentation]    Open a connection to the tools system and return its identifier.
35     ${tools} =    SSHLibrary.Open_Connection    ${TOOLS_SYSTEM_IP}    prompt=${TOOLS_SYSTEM_PROMPT}
36     Utils.Flexible_Mininet_Login
37     [Return]    ${tools}
38
39 Log_Command_Results
40     [Arguments]    ${stdout}    ${stderr}    ${rc}
41     [Documentation]    Log everything returned by SSHLibrary.Execute_Command
42     BuiltIn.Log    ${stdout}
43     BuiltIn.Log    ${stderr}
44     BuiltIn.Log    ${rc}
45
46 Execute_Command_Passes
47     [Arguments]    ${command}    ${return_success_only}=True    ${log_on_success}=False    ${log_on_failure}=True    ${stderr_must_be_empty}=False
48     [Documentation]    Execute command via the active SSH connection. For success, rc has to be zero and optionally stderr has to be empty.
49     ...    Log everything, depending on arguments and success. Retrun either success string or stdout.
50     ...    TODO: Do we want to support customizing return values the same way as SSHLibrary.Execute_Command does?
51     ${stdout}    ${stderr}    ${rc} =    SSHLibrary.Execute_Command    ${command}    return_stderr=True    return_rc=True
52     ${emptiness_status}    ${result} =    BuiltIn.Run_Keyword_And_Ignore_Error    BuiltIn.Should_Be_Empty    ${stderr}
53     ${success} =    BuiltIn.Set_Variable_If    (${rc} == 0) and (("${emptiness_status}" == "PASS") or not ${stderr_must_be_empty})    True    False
54     BuiltIn.Run_Keyword_If    (${log_on_success} and ${success}) or (${log_on_failure} and not ${success})    Log_Command_Results    ${stdout}    ${stderr}    ${rc}
55     BuiltIn.Return_From_Keyword_If    ${return_success_only}    ${success}
56     BuiltIn.Return_From_Keyword_If    ${success}    ${stdout}
57     BuiltIn.Fail    Got rc: ${rc} or stderr was not empty: ${stderr}
58
59 Execute_Command_Should_Pass
60     [Arguments]    ${command}    ${log_on_success}=True    ${log_on_failure}=True    ${stderr_must_be_empty}=False
61     [Documentation]    A wrapper for Execute_Command_Passes with return_success_only=False
62     ...    Also, log_on_success defaults to True (but is customizable, unlike return_success_only)..
63     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}
64
65 Execute_Command_At_Path_Should_Pass
66     [Arguments]    ${command}    ${path}=None    ${log_on_success}=True    ${log_on_failure}=True    ${stderr_must_be_empty}=False
67     [Documentation]    A keyword similar to Execute_Command_Should_Pass which performs "cd" to ${path} before executing the ${command}.
68     ...    This is useful when rewriting bash scripts, as series of SSHLibrary.Execute_Command do not share current working directory.
69     ...    TODO: Perhaps a Keyword which sets up environment variables would be useful as well.
70     ${cd_and_command} =    BuiltIn.Set_Variable    cd '${path}' && ${command}
71     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}
72
73 Set_Cwd
74     [Arguments]    ${path}
75     [Documentation]    Set \${SSHKeywords__current_remote_working_directory} variable to ${path}. If SSH default is desired, use dot.
76     BuiltIn.Set_Suite_Variable    \${SSHKeywords__current_remote_working_directory}    ${path}
77
78 Execute_Command_At_Cwd_Should_Pass
79     [Arguments]    ${command}    ${log_on_success}=True    ${log_on_failure}=True    ${stderr_must_be_empty}=True
80     [Documentation]    Run Execute_Command_At_Path_Should_Pass with previously set CWD as path.
81     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}
82
83 Require_Python
84     [Documentation]    Verify current SSH connection leads to machine with python working. Fatal fail otherwise.
85     ${passed} =    Execute_Command_Passes    python --help
86     BuiltIn.Return_From_Keyword_If    ${passed}
87     BuiltIn.Fatal_Error    Python is not installed!
88
89 Assure_Library_Ipaddr
90     [Arguments]    ${target_dir}=.
91     [Documentation]    Tests whether ipaddr module is present on ssh-connected machine, Puts ipaddr.py to target_dir if not.
92     ${passed} =    Execute_Command_Passes    bash -c 'cd "${target_dir}" && python -c "import ipaddr"'
93     BuiltIn.Return_From_Keyword_If    ${passed}
94     SSHLibrary.Put_File    ${CURDIR}/ipaddr.py    ${target_dir}/
95
96 Assure_Library_Counter
97     [Arguments]    ${target_dir}=.
98     [Documentation]    Tests whether Counter is present in collections on ssh-connected machine, Puts Counter.py to workspace if not.
99     ${passed} =    Execute_Command_Passes    bash -c 'cd "${target_dir}" && python -c "from collections import Counter"'
100     # TODO: Move the bash-cd wrapper to separate keyword?
101     BuiltIn.Return_From_Keyword_If    ${passed}
102     SSHLibrary.Put_File    ${CURDIR}/Counter.py    ${target_dir}/
103
104 Count_Port_Occurences
105     [Arguments]    ${port}    ${state}    ${name}
106     [Documentation]    Run 'netstat' on the remote machine and count occurences of given port in the given state connected to process with the given name.
107     ${output} =    SSHLibrary.Execute_Command    netstat -natp 2> /dev/null | grep -E ":${port} .+ ${state} .+${name}" | wc -l
108     [Return]    ${output}
109
110 Virtual_Env_Set_Path
111     [Arguments]    ${venv_path}
112     [Documentation]    Set \${SSHKeywords__current_venv_path} variable to ${venv_path}. Path should be absolute.
113     BuiltIn.Set_Global_Variable    \${SSHKeywords__current_venv_path}    ${venv_path}
114
115 Virtual_Env_Create
116     [Documentation]    Creates virtual env. If not to use the default name, use Virtual_Env_Set_Path kw. Returns stdout.
117     Execute_Command_At_Cwd_Should_Pass    virtualenv ${SSHKeywords__current_venv_path}
118     BuiltIn.Run_Keyword_And_Return    Virtual_Env_Run_Cmd_At_Cwd    pip install --upgrade pip
119
120 Virtual_Env_Run_Cmd_At_Cwd
121     [Arguments]    ${cmd}    ${log_on_success}=True    ${log_on_failure}=True    ${stderr_must_be_empty}=True
122     [Documentation]    Runs given command within activated virtual env and returns stdout.
123     BuiltIn.Run_Keyword_And_Return    Execute_Command_At_Cwd_Should_Pass    source ${SSHKeywords__current_venv_path}/bin/activate; ${cmd}; deactivate    log_on_success=${log_on_success}    log_on_failure=${log_on_failure}    stderr_must_be_empty=${stderr_must_be_empty}
124
125 Virtual_Env_Install_Package
126     [Arguments]    ${package}
127     [Documentation]    Installs python package into virtual env. Use with version if needed (e.g. exabgp==3.4.16). Returns stdout.
128     BuiltIn.Run_Keyword_And_Return    Virtual_Env_Run_Cmd_At_Cwd    pip install ${package}    stderr_must_be_empty=False
129
130 Virtual_Env_Uninstall_Package
131     [Arguments]    ${package}
132     [Documentation]    Uninstalls python package from virtual env and returns stdout.
133     BuiltIn.Run_Keyword_And_Return    Virtual_Env_Run_Cmd_At_Cwd    pip uninstall -y ${package}
134
135 Virtual_Env_Freeze
136     [Documentation]    Shows installed packages within the returned stdout.
137     BuiltIn.Run_Keyword_And_Return    Virtual_Env_Run_Cmd_At_Cwd    pip freeze --all
138
139 Virtual_Env_Activate_On_Current_Session
140     [Arguments]    ${log_output}=${False}
141     [Documentation]    Activates virtual environment. To run anything in the env activated this way you should use SSHLibrary.Write and Read commands.
142     SSHLibrary.Write    source ${SSHKeywords__current_venv_path}/bin/activate
143     ${output}=    SSHLibrary.Read_Until_Prompt
144     BuiltIn.Run_Keyword_If    ${log_output}==${True}    BuiltIn.Log    ${output}
145
146 Virtual_Env_Deactivate_On_Current_Session
147     [Arguments]    ${log_output}=${False}
148     [Documentation]    Deactivates virtual environment.
149     SSHLibrary.Write    deactivate
150     ${output}=    SSHLibrary.Read_Until_Prompt
151     BuiltIn.Run_Keyword_If    ${log_output}==${True}    BuiltIn.Log    ${output}