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, KarafKeywords.Restore_Current_SSH_Connection_From_Index and similar.
19 Resource ${CURDIR}/Utils.robot
22 ${SSHKeywords__current_remote_working_directory} .
23 ${SSHKeywords__current_venv_path} /tmp/defaultvenv
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
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
40 [Arguments] ${stdout} ${stderr} ${rc}
41 [Documentation] Log everything returned by SSHLibrary.Execute_Command
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}
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}
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}
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}
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}
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!
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}/
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}/
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
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}
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
121 [Documentation] Deletes a directory with virtual env.
122 Execute_Command_At_Cwd_Should_Pass rm -rf ${SSHKeywords__current_venv_path}
124 Virtual_Env_Run_Cmd_At_Cwd
125 [Arguments] ${cmd} ${log_on_success}=True ${log_on_failure}=True ${stderr_must_be_empty}=True
126 [Documentation] Runs given command within activated virtual env and returns stdout.
127 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}
129 Virtual_Env_Install_Package
130 [Arguments] ${package}
131 [Documentation] Installs python package into virtual env. Use with version if needed (e.g. exabgp==3.4.16). Returns stdout.
132 BuiltIn.Run_Keyword_And_Return Virtual_Env_Run_Cmd_At_Cwd pip install ${package} stderr_must_be_empty=False
134 Virtual_Env_Uninstall_Package
135 [Arguments] ${package}
136 [Documentation] Uninstalls python package from virtual env and returns stdout.
137 BuiltIn.Run_Keyword_And_Return Virtual_Env_Run_Cmd_At_Cwd pip uninstall -y ${package}
140 [Documentation] Shows installed packages within the returned stdout.
141 BuiltIn.Run_Keyword_And_Return Virtual_Env_Run_Cmd_At_Cwd pip freeze --all
143 Virtual_Env_Activate_On_Current_Session
144 [Arguments] ${log_output}=${False}
145 [Documentation] Activates virtual environment. To run anything in the env activated this way you should use SSHLibrary.Write and Read commands.
146 SSHLibrary.Write source ${SSHKeywords__current_venv_path}/bin/activate
147 ${output}= SSHLibrary.Read_Until_Prompt
148 BuiltIn.Run_Keyword_If ${log_output}==${True} BuiltIn.Log ${output}
150 Virtual_Env_Deactivate_On_Current_Session
151 [Arguments] ${log_output}=${False}
152 [Documentation] Deactivates virtual environment.
153 SSHLibrary.Write deactivate
154 ${output}= SSHLibrary.Read_Until_Prompt
155 BuiltIn.Run_Keyword_If ${log_output}==${True} BuiltIn.Log ${output}