Add suite for akka persistence when upgrading ODL
[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 *** Keywords ***
22 Open_Connection_To_ODL_System
23     [Documentation]    Open a connection to the ODL system and return its identifier.
24     ...    On clustered systems this opens the connection to the first node.
25     ${odl}=    SSHLibrary.Open_Connection    ${ODL_SYSTEM_IP}    prompt=${ODL_SYSTEM_PROMPT}    timeout=10s
26     Utils.Flexible_Controller_Login
27     [Return]    ${odl}
28
29 Open_Connection_To_Tools_System
30     [Documentation]    Open a connection to the tools system and return its identifier.
31     ${tools}=    SSHLibrary.Open_Connection    ${TOOLS_SYSTEM_IP}    prompt=${TOOLS_SYSTEM_PROMPT}
32     Utils.Flexible_Mininet_Login
33     [Return]    ${tools}
34
35 Execute_Command_Passes
36     [Arguments]    ${command}    ${return_success_only}=True    ${log_on_success}=False    ${log_on_failure}=True    ${stderr_must_be_empty}=False
37     [Documentation]    Execute command via the active SSH connection. For success, rc has to be zero and optionally stderr has to be empty.
38     ...    Log everything, depending on arguments and success. Retrun either success string or stdout.
39     ...    TODO: Do we want to support customizing return values the same way as SSHLibrary.Execute_Command does?
40     ${stdout}    ${stderr}    ${rc} =    SSHLibrary.Execute_Command    ${command}    return_stderr=True    return_rc=True
41     ${emptiness_status}    ${result} =    BuiltIn.Run_Keyword_And_Ignore_Error    BuiltIn.Should_Be_Empty    ${stderr}
42     ${success} =    BuiltIn.Set_Variable_If    (${rc} == 0) and (("${emptiness_status}" == "PASS") or not ${stderr_must_be_empty})    True    False
43     BuiltIn.Run_Keyword_If    (${log_on_success} and ${success}) or (${log_on_failure} and not ${success})    Log_Command_Results    ${stdout}    ${stderr}    ${rc}
44     BuiltIn.Return_From_Keyword_If    ${return_success_only}    ${success}
45     BuiltIn.Return_From_Keyword_If    ${success}    ${stdout}
46     BuiltIn.Fail    Got rc: ${rc} or stdout was not empty: ${stdout}
47
48 Execute_Command_Should_Pass
49     [Arguments]    ${command}    ${log_on_success}=True    ${log_on_failure}=True    ${stderr_must_be_empty}=False
50     [Documentation]    A wrapper for Execute_Command_Passes with return_success_only=False
51     ...    Also, log_on_success defaults to True (but is customizable, unlike return_success_only)..
52     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}
53
54 Log_Command_Results
55     [Arguments]    ${stdout}    ${stderr}    ${rc}
56     [Documentation]    Log everything returned by SSHLibrary.Execute_Command
57     BuiltIn.Log    ${stdout}
58     BuiltIn.Log    ${stderr}
59     BuiltIn.Log    ${rc}
60
61 Require_Python
62     [Documentation]    Verify current SSH connection leads to machine with python working. Fatal fail otherwise.
63     ${passed} =    Execute_Command_Passes    python --help
64     BuiltIn.Return_From_Keyword_If    ${passed}
65     BuiltIn.Fatal_Error    Python is not installed!
66
67 Assure_Library_Ipaddr
68     [Arguments]    ${target_dir}=.
69     [Documentation]    Tests whether ipaddr module is present on ssh-connected machine, Puts ipaddr.py to target_dir if not.
70     ${passed} =    Execute_Command_Passes    bash -c 'cd "${target_dir}" && python -c "import ipaddr"'
71     BuiltIn.Return_From_Keyword_If    ${passed}
72     SSHLibrary.Put_File    ${CURDIR}/ipaddr.py    ${target_dir}/
73
74 Assure_Library_Counter
75     [Arguments]    ${target_dir}=.
76     [Documentation]    Tests whether Counter is present in collections on ssh-connected machine, Puts Counter.py to workspace if not.
77     ${passed} =    Execute_Command_Passes    bash -c 'cd "${target_dir}" && python -c "from collections import Counter"'
78     # TODO: Move the bash-cd wrapper to separate keyword?
79     BuiltIn.Return_From_Keyword_If    ${passed}
80     SSHLibrary.Put_File    ${CURDIR}/Counter.py    ${target_dir}/
81
82 Count_Port_Occurences
83     [Arguments]    ${port}    ${state}    ${name}
84     [Documentation]    Run 'netstat' on the remote machine and count occurences of given port in the given state connected to process with the given name.
85     ${output}=    SSHLibrary.Execute_Command    netstat -natp 2> /dev/null | grep -E ":${port} .+ ${state} .+${name}" | wc -l
86     [Return]    ${output}