Patch for basic HA cluster test (2/2)
[integration/test.git] / csit / libraries / KarafKeywords.robot
1 *** Settings ***
2 Documentation     Karaf library. This library is useful to deal with controller Karaf console.
3 Library           SSHLibrary
4 Library           OperatingSystem
5 Variables         ../variables/Variables.py
6
7 *** Variables ***
8 ${WORKSPACE}      /tmp
9 ${BUNDLEFOLDER}    distribution-karaf-0.3.0-SNAPSHOT
10 ${KarafKeywords__karaf_connection_index}    -1
11
12 *** Keywords ***
13 Check Karaf Log File Does Not Have Messages
14     [Arguments]    ${ip}    ${message}    ${user}=${CONTROLLER_USER}    ${password}=${CONTROLLER_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${log_file}=${WORKSPACE}/${BUNDLEFOLDER}/data/log/karaf.log
15     [Documentation]    Fails if the provided ${message} is found in the karaf.log file. Uses grep to search. The
16     ...    karaf.log file can be overridden with ${log_file} to be any file on the given system @ ${ip}
17     ${output}=    Run Command On Controller    ${ip}    grep ${message} ${log_file}    user=${user}    password=${password}    prompt=${prompt}
18     Should Not Contain    ${output}    ${message}
19
20 Verify Feature Is Installed
21     [Arguments]    ${feature_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}
22     [Documentation]    Will Succeed if the given ${feature_name} is found in the output of "feature:list -i"
23     ${output}=    Issue Command On Karaf Console    feature:list -i | grep ${feature_name}    ${controller}    ${karaf_port}
24     Should Contain    ${output}    ${feature_name}
25     [Return]    ${output}
26
27 Verify Feature Is Not Installed
28     [Arguments]    ${feature_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}
29     [Documentation]    Will Succeed if the given ${feature_name} is NOT found in the output of "feature:list -i"
30     ${output}=    Issue Command On Karaf Console    feature:list -i | grep ${feature_name}    ${controller}    ${karaf_port}
31     Should Not Contain    ${output}    ${feature_name}
32     [Return]    ${output}
33
34 Issue Command On Karaf Console
35     [Arguments]    ${cmd}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=5
36     [Documentation]    Will execute the given ${cmd} by ssh'ing to the karaf console running on ${CONTROLLER}
37     ...    Note that this keyword will open&close new SSH connection, without switching back to previously current session.
38     Open Connection    ${controller}    port=${karaf_port}    prompt=${KARAF_PROMPT}    timeout=${timeout}
39     Login    ${KARAF_USER}    ${KARAF_PASSWORD}
40     Write    ${cmd}
41     ${output}    Read Until    ${KARAF_PROMPT}
42     Close Connection
43     Log    ${output}
44     [Return]    ${output}
45
46 Verify Bundle Is Installed
47     [Arguments]    ${bundle_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}
48     [Documentation]    Will succeed if the given ${bundle name} is present in the output of "bundle:list -s "
49     ${output}=    Issue Command On Karaf Console    bundle:list -s | grep ${bundle_name}    ${controller}    ${karaf_port}
50     Should Contain    ${output}    ${bundle_name}
51     [Return]    ${output}
52
53 Verify Bundle Is Not Installed
54     [Arguments]    ${bundle_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}
55     [Documentation]    Will succeed if the given ${bundle_name} is NOT found in the output of "bundle:list -s"
56     ${output}=    Issue Command On Karaf Console    bundle:list -i | grep ${bundle_name}    ${controller}    ${karaf_port}
57     Should Not Contain    ${output}    ${bundle_name}
58     [Return]    ${output}
59
60 Check Karaf Log Has Messages
61     [Arguments]    ${filter_string}    @{message_list}
62     [Documentation]    Will succeed if the @{messages} are found in \ the output of "log:display"
63     ${output}=    Issue Command On Karaf Console    log:display | grep ${filter_string}
64     : FOR    ${message}    IN    @{message_list}
65     \    Should Contain    ${output}    ${message}
66     [Return]    ${output}
67
68 Install a Feature
69     [Arguments]    ${feature_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=15
70     [Documentation]    Will Install the given ${feature_name}
71     Log    ${timeout}
72     ${output}=    Issue Command On Karaf Console    feature:install ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
73     Log    ${output}
74     [Return]    ${output}
75
76 Uninstall a Feature
77     [Arguments]    ${feature_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=15
78     [Documentation]    Will UnInstall the given ${feature_name}
79     ${output}=    Issue Command On Karaf Console    feature:uninstall ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
80     Log    ${output}
81     [Return]    ${output}
82
83 Restore Current SSH Connection From Index
84     [Arguments]    ${connection_index}
85     [Documentation]    Restore active SSH connection in SSHLibrary to given index.
86     ...
87     ...    Restore the currently active connection state in
88     ...    SSHLibrary to match the state returned by "Switch
89     ...    Connection" or "Get Connection". More specifically makes
90     ...    sure that there will be no active connection when the
91     ...    \${connection_index} reported by these means is None.
92     ...
93     ...    There is a misfeature in SSHLibrary: Invoking "SSHLibrary.Switch_Connection"
94     ...    and passing None as the "index_or_alias" argument to it has exactly the
95     ...    same effect as invoking "Close Connection".
96     ...    https://github.com/robotframework/SSHLibrary/blob/master/src/SSHLibrary/library.py#L560
97     ...
98     ...    We want to have Keyword which will "switch out" to previous
99     ...    "no connection active" state without killing the background one.
100     ...
101     ...    As some suites may hypothetically rely on non-writability of active connection,
102     ...    workaround is applied by opening and closing temporary connection.
103     ...    Unfortunately this will fail if run on Jython and there is no SSH server
104     ...    running on localhost, port 22 but there is nothing easy that can be done about it.
105     BuiltIn.Run Keyword And Return If    ${connection_index} is not None    SSHLibrary.Switch Connection    ${connection_index}
106     # The background connection is still current, bury it.
107     SSHLibrary.Open Connection    127.0.0.1
108     SSHLibrary.Close Connection
109
110 Open Controller Karaf Console On Background
111     [Documentation]    Connect to the controller's karaf console, but do not switch to it.
112     ${current_ssh_connection}=    SSHLibrary.Get Connection
113     SSHLibrary.Open Connection    ${CONTROLLER}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_DETAILED_PROMPT}
114     ${karaf_connection}=    SSHLibrary.Get Connection
115     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}
116     BuiltIn.Set Suite Variable    ${KarafKeywords__karaf_connection_index}    ${karaf_connection.index}
117     Restore Current SSH Connection From Index    ${current_ssh_connection.index}
118
119 Execute Controller Karaf Command On Background
120     [Arguments]    ${command}
121     [Documentation]    Send command to karaf without affecting current SSH connection. Read, log and return response.
122     ...    This assumes Karaf connection has index saved and correct prompt set.
123     BuiltIn.Run Keyword If    ${KarafKeywords__karaf_connection_index} == -1    Fail    Need to connect to a Karaf Console first
124     ${current_connection_index}=    SSHLibrary.Switch Connection    ${KarafKeywords__karaf_connection_index}
125     ${status_write}    ${message_write}=    BuiltIn.Run Keyword And Ignore Error    SSHLibrary.Write    ${command}
126     ${status_wait}    ${message_wait}=    BuiltIn.Run Keyword And Ignore Error    SSHLibrary.Read Until Prompt
127     Restore Current SSH Connection From Index    ${current_connection_index}
128     BuiltIn.Run Keyword If    '${status_write}' != 'PASS'    BuiltIn.Fail    Failed to send the command: ${command}
129     BuiltIn.Log    ${message_wait}
130     BuiltIn.Run Keyword If    '${status_wait}' != 'PASS'    BuiltIn.Fail    Failed to see prompt after sending the command: ${command}
131     [Return]    ${message_wait}
132
133 Log Message To Controller Karaf
134     [Arguments]    ${message}
135     [Documentation]    Send a message into the controller's karaf log file. Do not change current SSH connection.
136     ${reply}=    Execute Controller Karaf Command On Background    log:log "ROBOT MESSAGE: ${message}"
137     [Return]    ${reply}
138
139 Log Test Suite Start To Controller Karaf
140     [Documentation]    Log suite name to karaf log, useful in suite setup.
141     Log Message To Controller Karaf    Starting suite ${SUITE_SOURCE}
142
143 Log Testcase Start To Controller Karaf
144     [Documentation]    Log test case name to karaf log, useful in test case setup.
145     Log Message To Controller Karaf    Starting test ${TEST_NAME}
146
147 Set Bgpcep Log Levels
148     [Arguments]    ${bgpcep_level}=${DEFAULT_BGPCEP_LOG_LEVEL}    ${protocol_level}=${DEFAULT_PROTOCOL_LOG_LEVEL}
149     [Documentation]    Assuming OCKCOB was used, set logging level on bgpcep and protocol loggers without affecting current SSH session.
150     # FIXME: Move to appropriate Resource
151     Execute Controller Karaf Command On Background    log:set ${bgpcep_level} org.opendaylight.bgpcep
152     Execute Controller Karaf Command On Background    log:set ${protocol_level} org.opendaylight.protocol