d562a0c5c913b2842ec4f99959554b1eed4770a8
[integration/test.git] / csit / libraries / KarafKeywords.robot
1 *** Settings ***
2 Documentation     Karaf library. General utility keywords for interacting with the karaf environment, such as the
3 ...               karaf console, karaf.log, karaf features, and karaf config files.
4 ...
5 ...               This library is useful to deal with controller Karaf console for ssh sessions in cluster.
6 ...               Running Setup_Karaf_Keywords is necessary. If SetupUtils initialization is called, this gets initialized as well.
7 ...               If this gets initialized, ClusterManagement gets initialized as well.
8 Library           SSHLibrary
9 Library           OperatingSystem
10 Resource          ${CURDIR}/ClusterManagement.robot
11 Resource          ${CURDIR}/SSHKeywords.robot
12 Variables         ${CURDIR}/../variables/Variables.py
13
14 *** Variables ***
15 ${WORKSPACE}      /tmp
16 ${connection_index_dict}    &{EMPTY}
17
18 *** Keywords ***
19 Setup_Karaf_Keywords
20     [Arguments]    ${http_timeout}=${DEFAULT_TIMEOUT_HTTP}
21     [Documentation]    Initialize ClusterManagement. Open ssh karaf connections to each ODL.
22     ClusterManagement.ClusterManagement_Setup    http_timeout=${http_timeout}
23     ClusterManagement.Run_Bash_Command_On_List_Or_All    iptables -I INPUT -p tcp --dport ${KARAF_SHELL_PORT} -j ACCEPT; iptables-save
24     BuiltIn.Comment    First connections to Karaf console may fail, so WUKS is used. TODO: Track as a Bug.
25     : FOR    ${index}    IN    @{ClusterManagement__member_index_list}
26     \    BuiltIn.Run_Keyword_And_Ignore_Error    BuiltIn.Wait_Until_Keyword_Succeeds    3s    1s    Open_Controller_Karaf_Console_On_Background    member_index=${index}
27
28 Verify_Feature_Is_Installed
29     [Arguments]    ${feature_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}
30     [Documentation]    Will Succeed if the given ${feature_name} is found in the output of "feature:list -i"
31     ${output} =    Issue_Command_On_Karaf_Console    feature:list -i | grep ${feature_name}    ${controller}    ${karaf_port}
32     BuiltIn.Should_Contain    ${output}    ${feature_name}
33     [Return]    ${output}
34
35 Issue_Command_On_Karaf_Console
36     [Arguments]    ${cmd}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=10    ${loglevel}=INFO
37     [Documentation]    Will execute the given ${cmd} by ssh'ing to the karaf console running on ${controller}
38     ...    Note that this keyword will open&close new SSH connection, without switching back to previously current session.
39     SSHLibrary.Open_Connection    ${controller}    port=${karaf_port}    prompt=${KARAF_PROMPT_LOGIN}    timeout=${timeout}
40     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}    loglevel=${loglevel}
41     SSHLibrary.Write    ${cmd}
42     ${output}    SSHLibrary.Read_Until_Regexp    ${KARAF_PROMPT}
43     SSHLibrary.Close_Connection
44     BuiltIn.Log    ${output}
45     [Return]    ${output}
46
47 Safe_Issue_Command_On_Karaf_Console
48     [Arguments]    ${cmd}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=10    ${loglevel}=INFO
49     [Documentation]    Run Issue_Command_On_Karaf_Console but restore previous connection afterwards.
50     BuiltIn.Run_Keyword_And_Return    SSHKeywords.Run_Keyword_Preserve_Connection    Issue_Command_On_Karaf_Console    ${cmd}    ${controller}    ${karaf_port}    ${timeout}
51     ...    ${loglevel}
52
53 Check For Elements On Karaf Command Output Message
54     [Arguments]    ${cmd}    ${elements}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=5
55     [Documentation]    Will execute the command using Issue Command On Karaf Console then check for the given elements
56     ...    in the command output message
57     ${output}    Issue_Command_On_Karaf_Console    ${cmd}    ${controller}    ${karaf_port}    ${timeout}
58     : FOR    ${i}    IN    @{elements}
59     \    BuiltIn.Should_Contain    ${output}    ${i}
60
61 Verify_Bundle_Is_Installed
62     [Arguments]    ${bundle_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}
63     [Documentation]    Will succeed if the given ${bundle name} is present in the output of "bundle:list -s "
64     ${output} =    Issue_Command_On_Karaf_Console    bundle:list -s | grep ${bundle_name}    ${controller}    ${karaf_port}
65     BuiltIn.Should_Contain    ${output}    ${bundle_name}
66     [Return]    ${output}
67
68 Verify_Bundle_Is_Not_Installed
69     [Arguments]    ${bundle_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}
70     [Documentation]    Will succeed if the given ${bundle_name} is NOT found in the output of "bundle:list -s"
71     ${output} =    Issue_Command_On_Karaf_Console    bundle:list -i | grep ${bundle_name}    ${controller}    ${karaf_port}
72     BuiltIn.Should_Not_Contain    ${output}    ${bundle_name}
73     [Return]    ${output}
74
75 Check_Karaf_Log_Has_Messages
76     [Arguments]    ${filter_string}    @{message_list}
77     [Documentation]    Will succeed if the @{messages} are found in \ the output of "log:display"
78     ${output} =    Issue_Command_On_Karaf_Console    log:display | grep ${filter_string}
79     : FOR    ${message}    IN    @{message_list}
80     \    BuiltIn.Should_Contain    ${output}    ${message}
81     [Return]    ${output}
82
83 Check_Karaf_Log_Message_Count
84     [Arguments]    ${message}    ${count}    ${use_console}=False
85     [Documentation]    Verifies that the ${message} exists specified number of times in
86     ...    karaf console log or Karaf Log Folder based on the arg ${use_console}.
87     Run Keyword If    ${use_console} == False    Check_Karaf_Log_File    ${message}    ${count}
88     ...    ELSE    Check_Karaf_Log_From_Console    ${message}    ${count}
89
90 Check_Karaf_Log_From_Console
91     [Arguments]    ${message}    ${count}
92     [Documentation]    Verifies that the ${message} exists in the Karaf Console log:display and checks
93     ...    that it appears ${count} number of times
94     ${output} =    Issue_Command_On_Karaf_Console    log:display | grep ${message} | wc -l
95     ${line} =    Get Line    ${output}    0
96     ${stripped} =    Strip String    ${line}
97     Should Be Equal As Strings    ${stripped}    ${count}
98
99 Check_Karaf_Log_File
100     [Arguments]    ${message}    ${count}
101     [Documentation]    Verifies that the ${message} exists in the Karaf Log Folder and checks
102     ...    that it appears ${count} number of times
103     ${output}    Run Command On Controller    ${ODL_SYSTEM_IP}    grep -o ${message} ${WORKSPACE}/${BUNDLEFOLDER}/data/log/* | wc -l
104     Should Be Equal As Strings    ${output}    ${count}
105
106 Install_A_Feature
107     [Arguments]    ${feature_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=180
108     [Documentation]    Will Install the given ${feature_name}
109     BuiltIn.Log    ${timeout}
110     ${output} =    Issue_Command_On_Karaf_Console    feature:install ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
111     BuiltIn.Log    ${output}
112     [Return]    ${output}
113
114 Install_A_Feature_Using_Active_Connection
115     [Arguments]    ${feature_name}
116     [Documentation]    Will Install the given ${feature_name} using active connection
117     ${cmd} =    BuiltIn.Set_Variable    feature:install ${feature_name}
118     SSHLibrary.Write    ${cmd}
119     ${output}    SSHLibrary.Read_Until_Regexp    ${KARAF_PROMPT}
120     BuiltIn.Log    ${output}
121     [Return]    ${output}
122
123 Uninstall_A_Feature
124     [Arguments]    ${feature_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=180
125     [Documentation]    Will UnInstall the given ${feature_name}
126     ${output} =    Issue_Command_On_Karaf_Console    feature:uninstall ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
127     BuiltIn.Log    ${output}
128     [Return]    ${output}
129
130 Open_Controller_Karaf_Console_On_Background
131     [Arguments]    ${member_index}=${1}    ${timeout}=10    ${loglevel}=INFO
132     [Documentation]    If there is a stored ssh connection index of connection to the controller's karaf console for ${member_index},
133     ...    close the previous connection. In any case create a new connection
134     ...    to karaf console for ${member_index}, set correct prompt set and login to karaf console.
135     ...    Store connection index for ${member_index} and restore the previous active connection.
136     ${current_ssh_connection_object}=    SSHLibrary.Get_Connection
137     BuiltIn.Log    ${connection_index_dict}
138     BuiltIn.Log    ${member_index}
139     ${status}    ${old_connection_index} =    BuiltIn.Run_Keyword_And_Ignore_Error    Get From Dictionary    ${connection_index_dict}    ${member_index}
140     BuiltIn.Run_Keyword_If    '${status}'=='PASS'    BuiltIn.Run_Keywords    SSHLibrary.Switch_Connection    ${old_connection_index}
141     ...    AND    SSHLibrary.Close_Connection
142     ${odl_ip} =    ClusterManagement.Resolve_IP_Address_For_Member    ${member_index}
143     SSHLibrary.Open_Connection    ${odl_ip}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_PROMPT_LOGIN}    timeout=${timeout}
144     ${karaf_connection_object} =    SSHLibrary.Get_Connection
145     Collections.Set_To_Dictionary    ${connection_index_dict}    ${member_index}    ${karaf_connection_object.index}
146     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}    loglevel=${loglevel}
147     [Teardown]    SSHKeywords.Restore_Current_Ssh_Connection_From_Index    ${current_ssh_connection_object.index}
148
149 Open_Controller_Karaf_Console_With_Timeout
150     [Arguments]    ${member_index}=${1}    ${timeout}=3s
151     [Documentation]    Open new connection to karaf console for member index with specified timeout.
152     BuiltIn.Log    ${member_index}
153     ${odl_ip} =    ClusterManagement.Resolve_IP_Address_For_Member    ${member_index}
154     SSHLibrary.Open_Connection    ${odl_ip}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_PROMPT_LOGIN}    timeout=${timeout}
155     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}
156
157 Configure_Timeout_For_Karaf_Console
158     [Arguments]    ${timeout}    ${member_index_list}=${EMPTY}
159     [Documentation]    Configure a different timeout for each Karaf console.
160     ${index_list} =    ClusterManagement.List_Indices_Or_All    given_list=${member_index_list}
161     ${current_connection_object} =    SSHLibrary.Get_Connection
162     : FOR    ${member_index}    IN    @{index_list}    # usually: 1, 2, 3
163     \    ${karaf_connection_index} =    Collections.Get_From_Dictionary    ${connection_index_dict}    ${member_index}
164     \    SSHLibrary.Switch_Connection    ${karaf_connection_index}
165     \    SSHLibrary.Set_Client_Configuration    timeout=${timeout}
166     [Teardown]    SSHKeywords.Restore_Current_Ssh_Connection_From_Index    ${current_connection_object.index}
167
168 Execute_Controller_Karaf_Command_On_Background
169     [Arguments]    ${command}    ${member_index}=${1}
170     [Documentation]    Send command to karaf without affecting current SSH connection. Read, log and return response.
171     ${karaf_connection_index} =    Collections.Get_From_Dictionary    ${connection_index_dict}    ${member_index}
172     ${current_connection_index} =    SSHLibrary.Switch_Connection    ${karaf_connection_index}
173     ${status_write}    ${message_write} =    BuiltIn.Run_Keyword_And_Ignore_Error    SSHLibrary.Write    ${command}
174     ${status_wait}    ${message_wait} =    BuiltIn.Run_Keyword_And_Ignore_Error    SSHLibrary.Read_Until_Regexp    ${KARAF_PROMPT}
175     BuiltIn.Run Keyword If    '${status_write}' != 'PASS'    BuiltIn.Fail    Failed to send the command: ${command}
176     BuiltIn.Log    ${message_wait}
177     BuiltIn.Run_Keyword_If    '${status_wait}' != 'PASS'    BuiltIn.Fail    Failed to see prompt after sending the command: ${command}
178     [Teardown]    SSHKeywords.Restore_Current_Ssh_Connection_From_Index    ${current_connection_index}
179     [Return]    ${message_wait}
180
181 Execute_Controller_Karaf_Command_With_Retry_On_Background
182     [Arguments]    ${command}    ${member_index}=${1}
183     [Documentation]    Attemp to send command to karaf for ${member_index}, if fail then open connection and try again.
184     ${status}    ${message} =    BuiltIn.Run_Keyword_And_Ignore_Error    Execute_Controller_Karaf_Command_On_Background    ${command}    ${member_index}
185     BuiltIn.Return_From_Keyword_If    '${status}' == 'PASS'    ${message}
186     # TODO: Verify this does not leak connections indices.
187     Open_Controller_Karaf_Console_On_Background    ${member_index}
188     ${message} =    Execute_Controller_Karaf_Command_On_Background    ${command}    ${member_index}
189     [Return]    ${message}
190
191 Log_Message_To_Controller_Karaf
192     [Arguments]    ${message}    ${member_index_list}=${EMPTY}    ${tolerate_failure}=True
193     [Documentation]    Make sure this resource is initialized. Send a message into the controller's karaf log file on every node listed (or all).
194     ...    By default, failure while processing a node is silently ignored, unless ${tolerate_failure} is False.
195     ${index_list} =    ClusterManagement.List_Indices_Or_All    given_list=${member_index_list}
196     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
197     \    ${status}    ${output} =    BuiltIn.Run_Keyword_And_Ignore_Error    Execute_Controller_Karaf_Command_With_Retry_On_Background    log:log "ROBOT MESSAGE: ${message}"    member_index=${index}
198     \    BuiltIn.Run_Keyword_Unless    ${tolerate_failure} or "${status}" == "PASS"    BuiltIn.Fail    ${output}
199
200 Log_Test_Suite_Start_To_Controller_Karaf
201     [Arguments]    ${member_index_list}=${EMPTY}
202     [Documentation]    Log suite name to karaf log, useful in suite setup.
203     Log_Message_To_Controller_Karaf    Starting suite ${SUITE_SOURCE}    ${member_index_list}
204
205 Log_Testcase_Start_To_Controller_Karaf
206     [Arguments]    ${member_index_list}=${EMPTY}
207     [Documentation]    Log test case name to karaf log, useful in test case setup.
208     Log_Message_To_Controller_Karaf    Starting test ${SUITE_NAME}.${TEST_NAME}    ${member_index_list}
209
210 Set_Bgpcep_Log_Levels
211     [Arguments]    ${bgpcep_level}=${DEFAULT_BGPCEP_LOG_LEVEL}    ${protocol_level}=${DEFAULT_PROTOCOL_LOG_LEVEL}    ${member_index_list}=${EMPTY}
212     [Documentation]    Assuming OCKCOB was used, set logging level on bgpcep and protocol loggers without affecting current SSH session.
213     # FIXME: Move to appropriate Resource
214     ${index_list} =    ClusterManagement.List_Indices_Or_All    given_list=${member_index_list}
215     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
216     \    Execute_Controller_Karaf_Command_On_Background    log:set ${bgpcep_level} org.opendaylight.bgpcep    member_index=${index}
217     \    Execute_Controller_Karaf_Command_On_Background    log:set ${protocol_level} org.opendaylight.protocol    member_index=${index}
218
219 Get Karaf Log Type From Test Start
220     [Arguments]    ${ip}    ${test_name}    ${type}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
221     ...    ${log_file}=${KARAF_LOG}
222     [Documentation]    Scrapes all log messages that match regexp ${type} which fall after a point given by a log message that
223     ...    contains ${test_name}. This is useful if your test cases are marking karaf.log with a message indicating when
224     ...    that test case has started; such that you can easily pull out any extra log messsages to parse/log/etc in the
225     ...    test logic itself. For example, you can grab all ERRORS that occur during your test case.
226     ${cmd}    Set Variable    sed '1,/ROBOT MESSAGE: Starting test ${test_name}/d' ${log_file} | grep '${type}'
227     ${output}    Run Command On Controller    ${ip}    ${cmd}    ${user}    ${password}    ${prompt}
228     [Return]    ${output}
229
230 Get Karaf Log Types From Test Start
231     [Arguments]    ${ip}    ${test_name}    ${types}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
232     ...    ${log_file}=${KARAF_LOG}
233     [Documentation]    A wrapper keyword for "Get Karaf Log Type From Test Start" so that we can parse for multiple types
234     ...    of log messages. For example, we can grab all messages of type WARN and ERROR
235     : FOR    ${type}    IN    @{types}
236     \    Get Karaf Log Type From Test Start    ${ip}    ${test_name}    ${type}    ${user}    ${password}
237     \    ...    ${prompt}    ${log_file}
238
239 Get Karaf Log Events From Test Start
240     [Arguments]    ${test_name}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
241     [Documentation]    Wrapper for the wrapper "Get Karaf Log Types From Test Start" so that we can easily loop over
242     ...    any number of controllers to analyze karaf.log for ERROR, WARN and Exception log messages
243     ${log_types} =    Create List    ERROR    WARN    Exception
244     : FOR    ${i}    IN RANGE    1    ${NUM_ODL_SYSTEM} + 1
245     \    Get Karaf Log Types From Test Start    ${ODL_SYSTEM_${i}_IP}    ${test_name}    ${log_types}
246
247 Fail If Exceptions Found During Test
248     [Arguments]    ${test_name}    ${exceptions_white_list}=${EMPTY}
249     [Documentation]    Create a failure if an Exception is found in the karaf.log. Will work for single controller jobs
250     ...    as well as 3node cluster jobs
251     : FOR    ${i}    IN RANGE    1    ${NUM_ODL_SYSTEM} + 1
252     \    Verify Exception Logging In Controller    ${ODL_SYSTEM_${i}_IP}    ${test_name}    ${exceptions_white_list}
253
254 Verify Exception Logging In Controller
255     [Arguments]    ${controller_ip}    ${test_name}    ${exceptions_white_list}
256     [Documentation]    Local keyword to make it easier to loop through N controllers to pull Exceptions from the
257     ...    karaf.log file and validate with "Check Against White List"
258     ${exceptions}=    Get Karaf Log Type From Test Start    ${controller_ip}    ${test_name}    Exception
259     @{log_lines}=    Split String    ${exceptions}    ${\n}
260     ${num_log_entries}    Get Length    ${log_lines}
261     Return From Keyword If    ${num_log_entries} == ${0}    No Exceptions found.
262     : FOR    ${log_message}    IN    @{log_lines}
263     \    Check Against White List    ${log_message}    ${exceptions_white_list}
264
265 Check Against White List
266     [Arguments]    ${exception_line}    ${exceptions_white_list}
267     [Documentation]    As soon as the ${exceptions_line} is found in one of the elements of ${exceptions_white_list}
268     ...    this keyword will exit and give a Pass to the caller. If there is no match, this keyword will end up
269     ...    marking a failure. In the case that no exceptions are found, the caller could end up passing a single
270     ...    empty line as that is what is returned when a grep on karaf.log has no match, so we can safely return
271     ...    in that case as well.
272     Return From Keyword If    "${exception_line}" == ""
273     : FOR    ${exception}    IN    @{exceptions_white_list}
274     \    Return From Keyword If    "${exception}" in "${exception_line}"    Exceptions found, but whitelisted: ${\n}${exception_line}${\n}
275     Fail    Exceptions Found: ${\n}${exception_line}${\n}
276
277 Wait_For_Karaf_Log
278     [Arguments]    ${message}    ${timeout}=60    ${member_index}=${1}
279     [Documentation]    Read karaf logs until message appear
280     # TODO: refactor this keyword to use the new workflow to account for multiple controllers.    Initial work was done
281     # in this patch https://git.opendaylight.org/gerrit/#/c/45596/
282     # however, the consumers of this keyword were breaking after that change.    Initial theory is that a previous
283     # keyword used before this "Wait For Karaf Log" keyword was closing the karaf console connection, so the
284     # "Flexible SSH Login" keyword from the patch above (45596) was failing.
285     BuiltIn.Log    Waiting for '${message}' in karaf log
286     SSHLibrary.Open_Connection    ${ODL_SYSTEM_IP}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_PROMPT_LOGIN}    timeout=${timeout}
287     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}    loglevel=${loglevel}
288     SSHLibrary.Write    log:tail
289     SSHLibrary.Read_Until    ${message}
290     SSHLibrary.Close_Connection
291
292 Restart_Bundle
293     [Arguments]    ${bundle_id}
294     [Documentation]    Restarts bundle passed as argument. Note this operation is only for testing and not production environments
295     # TODO: prepare this for cluster environment and multiple controllers
296     Execute_Controller_Karaf_Command_With_Retry_On_Background    bundle:restart -f $(bundle:id '${bundle_id}')
297
298 Restart_Karaf
299     [Documentation]    Restarts Karaf and polls log to detect when Karaf is up and running again
300     # TODO: prepare this for cluster environment and multiple controllers
301     Execute_Controller_Karaf_Command_With_Retry_On_Background    log:clear
302     Execute_Controller_Karaf_Command_With_Retry_On_Background    shutdown -r -f
303     BuiltIn.Run_Keyword_And_Return_Status    BuiltIn.Wait_Until_Keyword_Succeeds    240s    60s    Wait_For_Karaf_Log    Karaf started in
304
305 Restart_Jetty
306     [Documentation]    Restarts jetty bundle (to reload certificates or key/truststore information)
307     Execute_Controller_Karaf_Command_With_Retry_On_Background    log:clear
308     Restart_Bundle    OPS4J Pax Web - Jetty
309     Wait_For_Karaf_Log    Instantiated the Application class org.opendaylight.restconf.RestconfApplication
310     Wait_For_Karaf_Log    Instantiated the Application class org.opendaylight.netconf.sal.rest.impl.RestconfApplication
311     Wait_For_Karaf_Log    Instantiated the Application class org.opendaylight.aaa.idm.IdmLightApplication