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