2 Documentation Resource enhancing SSHLibrary with Keywords used in multiple suites.
4 ... Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
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.
16 ... TODO: Migrate Keywords related to handling SSH here.
17 ... That may include Utils.Flexible_SSH_Login, and similar.
19 Resource ${CURDIR}/Utils.robot
20 Resource ../variables/Variables.robot
23 ${SSHKeywords__current_remote_working_directory} .
24 ${SSHKeywords__current_venv_path} /tmp/defaultvenv
27 Open_Connection_To_ODL_System
28 [Arguments] ${ip_address}=${ODL_SYSTEM_IP} ${timeout}=10s
29 [Documentation] Open a connection to the ODL system at ${ip_address} and return its identifier.
30 ${odl_connection} = SSHLibrary.Open_Connection ${ip_address} prompt=${ODL_SYSTEM_PROMPT} timeout=${timeout}
31 Utils.Flexible_Controller_Login
32 [Return] ${odl_connection}
34 Open_Connection_To_Tools_System
35 [Arguments] ${ip_address}=${TOOLS_SYSTEM_IP} ${timeout}=10s
36 [Documentation] Open a connection to the tools system at ${ip_address} and return its identifier.
37 ${tools_connection} = SSHLibrary.Open_Connection ${ip_address} prompt=${TOOLS_SYSTEM_PROMPT} timeout=${timeout}
38 Utils.Flexible_Mininet_Login
39 [Return] ${tools_connection}
41 Restore_Current_Ssh_Connection_From_Index
42 [Arguments] ${connection_index}
43 [Documentation] Restore active SSH connection in SSHLibrary to given index.
45 ... Restore the currently active connection state in
46 ... SSHLibrary to match the state returned by "Switch
47 ... Connection" or "Get Connection". More specifically makes
48 ... sure that there will be no active connection when the
49 ... \${connection_index} reported by these means is None.
51 ... There is a misfeature in SSHLibrary: Invoking "SSHLibrary.Switch_Connection"
52 ... and passing None as the "index_or_alias" argument to it has exactly the
53 ... same effect as invoking "Close Connection".
54 ... https://github.com/robotframework/SSHLibrary/blob/master/src/SSHLibrary/library.py#L560
56 ... We want to have Keyword which will "switch out" to previous
57 ... "no connection active" state without killing the background one.
59 ... As some suites may hypothetically rely on non-writability of active connection,
60 ... workaround is applied by opening and closing temporary connection.
61 ... Unfortunately this will fail if run on Jython and there is no SSH server
62 ... running on localhost, port 22 but there is nothing easy that can be done about it.
63 BuiltIn.Run Keyword And Return If ${connection_index} is not None SSHLibrary.Switch Connection ${connection_index}
64 # The background connection is still current, bury it.
65 SSHLibrary.Open Connection 127.0.0.1
66 SSHLibrary.Close Connection
68 Run_Keyword_Preserve_Connection
69 [Arguments] ${keyword_name} @{args} &{kwargs}
70 [Documentation] Store current connection index, run keyword returning its result, restore connection in teardown.
71 ... Note that in order to avoid "got positional argument after named arguments", it is safer to use positional (not named) arguments on call.
72 ${current_connection}= SSHLibrary.Get_Connection
73 BuiltIn.Run_Keyword_And_Return ${keyword_name} @{args} &{kwargs}
74 # Resource name has to be prepended, as KarafKeywords still contains a redirect.
75 [Teardown] SSHKeywords.Restore_Current_SSH_Connection_From_Index ${current_connection.index}
78 [Arguments] ${stdout} ${stderr} ${rc}
79 [Documentation] Log everything returned by SSHLibrary.Execute_Command
84 Execute_Command_Passes
85 [Arguments] ${command} ${return_success_only}=True ${log_on_success}=False ${log_on_failure}=True ${stderr_must_be_empty}=False
86 [Documentation] Execute command via the active SSH connection. For success, rc has to be zero and optionally stderr has to be empty.
87 ... Log everything, depending on arguments and success. Return either success string or stdout.
88 ... TODO: Do we want to support customizing return values the same way as SSHLibrary.Execute_Command does?
89 ${stdout} ${stderr} ${rc} = SSHLibrary.Execute_Command ${command} return_stderr=True return_rc=True
90 ${emptiness_status} ${result} = BuiltIn.Run_Keyword_And_Ignore_Error BuiltIn.Should_Be_Empty ${stderr}
91 ${success} = BuiltIn.Set_Variable_If (${rc} == 0) and (("${emptiness_status}" == "PASS") or not ${stderr_must_be_empty}) True False
92 BuiltIn.Run_Keyword_If (${log_on_success} and ${success}) or (${log_on_failure} and not ${success}) Log_Command_Results ${stdout} ${stderr} ${rc}
93 BuiltIn.Return_From_Keyword_If ${return_success_only} ${success}
94 BuiltIn.Return_From_Keyword_If ${success} ${stdout}
95 BuiltIn.Fail Got rc: ${rc} or stderr was not empty: ${stderr}
97 Execute_Command_Should_Pass
98 [Arguments] ${command} ${log_on_success}=True ${log_on_failure}=True ${stderr_must_be_empty}=False
99 [Documentation] A wrapper for Execute_Command_Passes with return_success_only=False
100 ... Also, log_on_success defaults to True (but is customizable, unlike return_success_only)..
101 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}
103 Execute_Command_At_Path_Should_Pass
104 [Arguments] ${command} ${path}=None ${log_on_success}=True ${log_on_failure}=True ${stderr_must_be_empty}=False
105 [Documentation] A keyword similar to Execute_Command_Should_Pass which performs "cd" to ${path} before executing the ${command}.
106 ... This is useful when rewriting bash scripts, as series of SSHLibrary.Execute_Command do not share current working directory.
107 ... TODO: Perhaps a Keyword which sets up environment variables would be useful as well.
108 ${cd_and_command} = BuiltIn.Set_Variable cd '${path}' && ${command}
109 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}
113 [Documentation] Set \${SSHKeywords__current_remote_working_directory} variable to ${path}. If SSH default is desired, use dot.
114 BuiltIn.Set_Suite_Variable \${SSHKeywords__current_remote_working_directory} ${path}
116 Execute_Command_At_Cwd_Should_Pass
117 [Arguments] ${command} ${log_on_success}=True ${log_on_failure}=True ${stderr_must_be_empty}=True
118 [Documentation] Run Execute_Command_At_Path_Should_Pass with previously set CWD as path.
119 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}
122 [Documentation] Verify current SSH connection leads to machine with python working. Fatal fail otherwise.
123 ${passed} = Execute_Command_Passes python --help
124 BuiltIn.Return_From_Keyword_If ${passed}
125 BuiltIn.Fatal_Error Python is not installed!
127 Assure_Library_Ipaddr
128 [Arguments] ${target_dir}=.
129 [Documentation] Tests whether ipaddr module is present on ssh-connected machine, Puts ipaddr.py to target_dir if not.
130 ${passed} = Execute_Command_Passes bash -c 'cd "${target_dir}" && python -c "import ipaddr"'
131 BuiltIn.Return_From_Keyword_If ${passed}
132 SSHLibrary.Put_File ${CURDIR}/ipaddr.py ${target_dir}/
134 Assure_Library_Counter
135 [Arguments] ${target_dir}=.
136 [Documentation] Tests whether Counter is present in collections on ssh-connected machine, Puts Counter.py to workspace if not.
137 ${passed} = Execute_Command_Passes bash -c 'cd "${target_dir}" && python -c "from collections import Counter"'
138 # TODO: Move the bash-cd wrapper to separate keyword?
139 BuiltIn.Return_From_Keyword_If ${passed}
140 SSHLibrary.Put_File ${CURDIR}/Counter.py ${target_dir}/
142 Count_Port_Occurences
143 [Arguments] ${port} ${state} ${name}
144 [Documentation] Run 'netstat' on the remote machine and count occurences of given port in the given state connected to process with the given name.
145 ${output} = SSHLibrary.Execute_Command netstat -natp 2> /dev/null | grep -E ":${port} .+ ${state} .+${name}" | wc -l
149 [Arguments] ${venv_path}
150 [Documentation] Set \${SSHKeywords__current_venv_path} variable to ${venv_path}. Path should be absolute.
151 BuiltIn.Set_Global_Variable \${SSHKeywords__current_venv_path} ${venv_path}
154 [Documentation] Creates virtual env. If not to use the default name, use Virtual_Env_Set_Path kw. Returns stdout.
155 Execute_Command_At_Cwd_Should_Pass virtualenv ${SSHKeywords__current_venv_path}
156 BuiltIn.Run_Keyword_And_Return Virtual_Env_Run_Cmd_At_Cwd pip install --upgrade pip
159 [Documentation] Deletes a directory with virtual env.
160 Execute_Command_At_Cwd_Should_Pass rm -rf ${SSHKeywords__current_venv_path}
162 Virtual_Env_Run_Cmd_At_Cwd
163 [Arguments] ${cmd} ${log_on_success}=True ${log_on_failure}=True ${stderr_must_be_empty}=True
164 [Documentation] Runs given command within activated virtual env and returns stdout.
165 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}
167 Virtual_Env_Install_Package
168 [Arguments] ${package}
169 [Documentation] Installs python package into virtual env. Use with version if needed (e.g. exabgp==3.4.16). Returns stdout.
170 BuiltIn.Run_Keyword_And_Return Virtual_Env_Run_Cmd_At_Cwd pip install ${package} stderr_must_be_empty=False
172 Virtual_Env_Uninstall_Package
173 [Arguments] ${package}
174 [Documentation] Uninstalls python package from virtual env and returns stdout.
175 BuiltIn.Run_Keyword_And_Return Virtual_Env_Run_Cmd_At_Cwd pip uninstall -y ${package}
178 [Documentation] Shows installed packages within the returned stdout.
179 BuiltIn.Run_Keyword_And_Return Virtual_Env_Run_Cmd_At_Cwd pip freeze --all
181 Virtual_Env_Activate_On_Current_Session
182 [Arguments] ${log_output}=${False}
183 [Documentation] Activates virtual environment. To run anything in the env activated this way you should use SSHLibrary.Write and Read commands.
184 SSHLibrary.Write source ${SSHKeywords__current_venv_path}/bin/activate
185 ${output}= SSHLibrary.Read_Until_Prompt
186 BuiltIn.Run_Keyword_If ${log_output}==${True} BuiltIn.Log ${output}
188 Virtual_Env_Deactivate_On_Current_Session
189 [Arguments] ${log_output}=${False}
190 [Documentation] Deactivates virtual environment.
191 SSHLibrary.Write deactivate
192 ${output}= SSHLibrary.Read_Until_Prompt
193 BuiltIn.Run_Keyword_If ${log_output}==${True} BuiltIn.Log ${output}
195 Unsafe_Copy_File_To_Remote_System
196 [Arguments] ${system} ${source} ${destination}=./ ${user}=${DEFAULT_USER} ${password}=${DEFAULT_PASSWORD} ${prompt}=${DEFAULT_LINUX_PROMPT}
197 ... ${prompt_timeout}=5s
198 [Documentation] Copy the ${source} file to the ${destination} file on the remote ${system}. The keyword opens and closes a single
199 ... ssh connection and does not rely on any existing ssh connection that may be open.
200 SSHLibrary.Open_Connection ${system} prompt=${prompt} timeout=${prompt_timeout}
201 Utils.Flexible_SSH_Login ${user} ${password}
202 SSHLibrary.Put_File ${source} ${destination}
203 SSHLibrary.Close Connection
205 Copy_File_To_Remote_System
206 [Arguments] ${system} ${source} ${destination}=./ ${user}=${DEFAULT_USER} ${password}=${DEFAULT_PASSWORD} ${prompt}=${DEFAULT_LINUX_PROMPT}
207 ... ${prompt_timeout}=5s
208 [Documentation] Copy the ${source} file to the ${destination} file on the remote ${system}. Any pre-existing active
209 ... ssh connection will be retained.
210 SSHKeywords.Run_Keyword_Preserve_Connection SSHKeywords.Unsafe_Copy_File_To_Remote_System ${system} ${source} ${destination} ${user} ${password}
211 ... ${prompt} ${prompt_timeout}
213 Copy_File_To_Odl_System
214 [Arguments] ${system} ${source} ${destination}=./
215 [Documentation] Wrapper keyword to make it easier to copy a file to an ODL specific system
216 SSHKeywords.Copy_File_To_Remote_System ${system} ${source} ${destination} ${ODL_SYSTEM_USER} ${ODL_SYSTEM_PASSWORD} ${ODL_SYSTEM_PROMPT}
218 Copy_File_To_Tools_System
219 [Arguments] ${system} ${source} ${destination}=./
220 [Documentation] Wrapper keyword to make it easier to copy a file to an Tools specific system
221 SSHKeywords.Copy_File_To_Remote_System ${system} ${source} ${destination} ${TOOLS_SYSTEM_USER} ${TOOLS_SYSTEM_PASSWORD} ${TOOLS_SYSTEM_PROMPT}