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} .
25 Open_Connection_To_ODL_System
26 [Documentation] Open a connection to the ODL system and return its identifier.
27 ... On clustered systems this opens the connection to the first node.
28 ${odl} = SSHLibrary.Open_Connection ${ODL_SYSTEM_IP} prompt=${ODL_SYSTEM_PROMPT} timeout=10s
29 Utils.Flexible_Controller_Login
32 Open_Connection_To_Tools_System
33 [Documentation] Open a connection to the tools system and return its identifier.
34 ${tools} = SSHLibrary.Open_Connection ${TOOLS_SYSTEM_IP} prompt=${TOOLS_SYSTEM_PROMPT}
35 Utils.Flexible_Mininet_Login
39 [Arguments] ${stdout} ${stderr} ${rc}
40 [Documentation] Log everything returned by SSHLibrary.Execute_Command
45 Execute_Command_Passes
46 [Arguments] ${command} ${return_success_only}=True ${log_on_success}=False ${log_on_failure}=True ${stderr_must_be_empty}=False
47 [Documentation] Execute command via the active SSH connection. For success, rc has to be zero and optionally stderr has to be empty.
48 ... Log everything, depending on arguments and success. Retrun either success string or stdout.
49 ... TODO: Do we want to support customizing return values the same way as SSHLibrary.Execute_Command does?
50 ${stdout} ${stderr} ${rc} = SSHLibrary.Execute_Command ${command} return_stderr=True return_rc=True
51 ${emptiness_status} ${result} = BuiltIn.Run_Keyword_And_Ignore_Error BuiltIn.Should_Be_Empty ${stderr}
52 ${success} = BuiltIn.Set_Variable_If (${rc} == 0) and (("${emptiness_status}" == "PASS") or not ${stderr_must_be_empty}) True False
53 BuiltIn.Run_Keyword_If (${log_on_success} and ${success}) or (${log_on_failure} and not ${success}) Log_Command_Results ${stdout} ${stderr} ${rc}
54 BuiltIn.Return_From_Keyword_If ${return_success_only} ${success}
55 BuiltIn.Return_From_Keyword_If ${success} ${stdout}
56 BuiltIn.Fail Got rc: ${rc} or stderr was not empty: ${stderr}
58 Execute_Command_Should_Pass
59 [Arguments] ${command} ${log_on_success}=True ${log_on_failure}=True ${stderr_must_be_empty}=False
60 [Documentation] A wrapper for Execute_Command_Passes with return_success_only=False
61 ... Also, log_on_success defaults to True (but is customizable, unlike return_success_only)..
62 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 Execute_Command_At_Path_Should_Pass
65 [Arguments] ${command} ${path}=None ${log_on_success}=True ${log_on_failure}=True ${stderr_must_be_empty}=False
66 [Documentation] A keyword similar to Execute_Command_Should_Pass which performs "cd" to ${path} before executing the ${command}.
67 ... This is useful when rewriting bash scripts, as series of SSHLibrary.Execute_Command do not share current working directory.
68 ... TODO: Perhaps a Keyword which sets up environment variables would be useful as well.
69 ${cd_and_command} = BuiltIn.Set_Variable cd '${path}' && ${command}
70 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}
74 [Documentation] Set \${SSHKeywords__current_remote_working_directory} variable to ${path}. If SSH default is desired, use dot.
75 BuiltIn.Set_Suite_Variable \${SSHKeywords__current_remote_working_directory} ${path}
77 Execute_Command_At_Cwd_Should_Pass
78 [Arguments] ${command} ${log_on_success}=True ${log_on_failure}=True ${stderr_must_be_empty}=True
79 [Documentation] Run Execute_Command_At_Path_Should_Pass with previously set CWD as path.
80 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}
83 [Documentation] Verify current SSH connection leads to machine with python working. Fatal fail otherwise.
84 ${passed} = Execute_Command_Passes python --help
85 BuiltIn.Return_From_Keyword_If ${passed}
86 BuiltIn.Fatal_Error Python is not installed!
89 [Arguments] ${target_dir}=.
90 [Documentation] Tests whether ipaddr module is present on ssh-connected machine, Puts ipaddr.py to target_dir if not.
91 ${passed} = Execute_Command_Passes bash -c 'cd "${target_dir}" && python -c "import ipaddr"'
92 BuiltIn.Return_From_Keyword_If ${passed}
93 SSHLibrary.Put_File ${CURDIR}/ipaddr.py ${target_dir}/
95 Assure_Library_Counter
96 [Arguments] ${target_dir}=.
97 [Documentation] Tests whether Counter is present in collections on ssh-connected machine, Puts Counter.py to workspace if not.
98 ${passed} = Execute_Command_Passes bash -c 'cd "${target_dir}" && python -c "from collections import Counter"'
99 # TODO: Move the bash-cd wrapper to separate keyword?
100 BuiltIn.Return_From_Keyword_If ${passed}
101 SSHLibrary.Put_File ${CURDIR}/Counter.py ${target_dir}/
103 Count_Port_Occurences
104 [Arguments] ${port} ${state} ${name}
105 [Documentation] Run 'netstat' on the remote machine and count occurences of given port in the given state connected to process with the given name.
106 ${output} = SSHLibrary.Execute_Command netstat -natp 2> /dev/null | grep -E ":${port} .+ ${state} .+${name}" | wc -l