Set http_timeout=30 for several controller suites
[integration/test.git] / csit / libraries / KarafKeywords.robot
1 *** Settings ***
2 Documentation     Karaf library. This library is useful to deal with controller Karaf console for ssh sessions in cluster.
3 ...               Running Setup Karaf Keywords is necessary. If SetupUtils initialization is called, this gets initialized as well.
4 ...               If this gets initialized, ClusterManagement gets initialized as well.
5 Library           SSHLibrary
6 Library           OperatingSystem
7 Resource          ${CURDIR}/ClusterManagement.robot
8 Resource          ${CURDIR}/SSHKeywords.robot
9 Variables         ${CURDIR}/../variables/Variables.py
10
11 *** Variables ***
12 ${WORKSPACE}      /tmp
13 ${connection_index_dict}    &{EMPTY}
14
15 *** Keywords ***
16 Setup Karaf Keywords
17     [Arguments]    ${http_timeout}=1
18     [Documentation]    Initialize ClusterManagement. Open ssh karaf connections to each ODL.
19     ClusterManagement.ClusterManagement_Setup    http_timeout=${http_timeout}
20     BuiltIn.Comment    First connections to Karaf console may fail, so WUKS is used. TODO: Track as a Bug.
21     : FOR    ${index}    IN    @{ClusterManagement__member_index_list}
22     \    BuiltIn.Wait_Until_Keyword_Succeeds    10x    0.2s    Open Controller Karaf Console On Background    member_index=${index}
23
24 Verify Feature Is Installed
25     [Arguments]    ${feature_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}
26     [Documentation]    Will Succeed if the given ${feature_name} is found in the output of "feature:list -i"
27     ${output}=    Issue Command On Karaf Console    feature:list -i | grep ${feature_name}    ${controller}    ${karaf_port}
28     Should Contain    ${output}    ${feature_name}
29     [Return]    ${output}
30
31 Issue Command On Karaf Console
32     [Arguments]    ${cmd}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=10    ${loglevel}=INFO
33     [Documentation]    Will execute the given ${cmd} by ssh'ing to the karaf console running on ${controller}
34     ...    Note that this keyword will open&close new SSH connection, without switching back to previously current session.
35     Open Connection    ${controller}    port=${karaf_port}    prompt=${KARAF_PROMPT}    timeout=${timeout}
36     Login    ${KARAF_USER}    ${KARAF_PASSWORD}    loglevel=${loglevel}
37     Write    ${cmd}
38     ${output}    Read Until    ${KARAF_PROMPT}
39     Close Connection
40     Log    ${output}
41     [Return]    ${output}
42
43 Safe_Issue_Command_On_Karaf_Console
44     [Arguments]    ${cmd}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=10    ${loglevel}=INFO
45     [Documentation]    Run Issue_Command_On_Karaf_Console but restore previous connection afterwards.
46     BuiltIn.Run_Keyword_And_Return    SSHKeywords.Run_Keyword_Preserve_Connection    Issue_Command_On_Karaf_Console    ${cmd}    ${controller}    ${karaf_port}    ${timeout}
47     ...    ${loglevel}
48
49 Check For Elements On Karaf Command Output Message
50     [Arguments]    ${cmd}    ${elements}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=5
51     [Documentation]    Will execute the command using Issue Command On Karaf Console then check for the given elements
52     ...    in the command output message
53     ${output}    Issue Command On Karaf Console    ${cmd}    ${controller}    ${karaf_port}    ${timeout}
54     : FOR    ${i}    IN    @{elements}
55     \    Should Contain    ${output}    ${i}
56
57 Verify Bundle Is Installed
58     [Arguments]    ${bundle_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}
59     [Documentation]    Will succeed if the given ${bundle name} is present in the output of "bundle:list -s "
60     ${output}=    Issue Command On Karaf Console    bundle:list -s | grep ${bundle_name}    ${controller}    ${karaf_port}
61     Should Contain    ${output}    ${bundle_name}
62     [Return]    ${output}
63
64 Verify Bundle Is Not Installed
65     [Arguments]    ${bundle_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}
66     [Documentation]    Will succeed if the given ${bundle_name} is NOT found in the output of "bundle:list -s"
67     ${output}=    Issue Command On Karaf Console    bundle:list -i | grep ${bundle_name}    ${controller}    ${karaf_port}
68     Should Not Contain    ${output}    ${bundle_name}
69     [Return]    ${output}
70
71 Check Karaf Log Has Messages
72     [Arguments]    ${filter_string}    @{message_list}
73     [Documentation]    Will succeed if the @{messages} are found in \ the output of "log:display"
74     ${output}=    Issue Command On Karaf Console    log:display | grep ${filter_string}
75     : FOR    ${message}    IN    @{message_list}
76     \    Should Contain    ${output}    ${message}
77     [Return]    ${output}
78
79 Install a Feature
80     [Arguments]    ${feature_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=180
81     [Documentation]    Will Install the given ${feature_name}
82     Log    ${timeout}
83     ${output}=    Issue Command On Karaf Console    feature:install ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
84     Log    ${output}
85     [Return]    ${output}
86
87 Install a Feature Using Active Connection
88     [Arguments]    ${feature_name}
89     [Documentation]    Will Install the given ${feature_name} using active connection
90     ${cmd}=    BuiltIn.Set_Variable    feature:install ${feature_name}
91     Write    ${cmd}
92     ${output}    SSHLibrary.Read_Until    ${KARAF_PROMPT}
93     Log    ${output}
94     [Return]    ${output}
95
96 Uninstall a Feature
97     [Arguments]    ${feature_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=180
98     [Documentation]    Will UnInstall the given ${feature_name}
99     ${output}=    Issue Command On Karaf Console    feature:uninstall ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
100     Log    ${output}
101     [Return]    ${output}
102
103 Open Controller Karaf Console On Background
104     [Arguments]    ${member_index}=${1}    ${timeout}=10    ${loglevel}=INFO
105     [Documentation]    If there is a stored ssh connection index of connection to the controller's karaf console for ${member_index},
106     ...    close the previous connection. In any case create a new connection
107     ...    to karaf console for ${member_index}, set correct prompt set and login to karaf console.
108     ...    Store connection index for ${member_index} and restore the previous active connection.
109     ${current_ssh_connection_object}=    SSHLibrary.Get Connection
110     BuiltIn.Log    ${connection_index_dict}
111     BuiltIn.Log    ${member_index}
112     ${status}    ${old_connection_index} =    BuiltIn.Run Keyword And Ignore Error    Get From Dictionary    ${connection_index_dict}    ${member_index}
113     BuiltIn.Run Keyword If    '${status}'=='PASS'    BuiltIn.Run Keywords    SSHLibrary.Switch Connection    ${old_connection_index}
114     ...    AND    SSHLibrary.Close Connection
115     ${odl_ip}=    ClusterManagement.Resolve_IP_Address_For_Member    ${member_index}
116     SSHLibrary.Open Connection    ${odl_ip}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_DETAILED_PROMPT}    timeout=${timeout}
117     ${karaf_connection_object}=    SSHLibrary.Get Connection
118     Collections.Set To Dictionary    ${connection_index_dict}    ${member_index}    ${karaf_connection_object.index}
119     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}    loglevel=${loglevel}
120     [Teardown]    SSHKeywords.Restore Current SSH Connection From Index    ${current_ssh_connection_object.index}
121
122 Open Controller Karaf Console With Timeout
123     [Arguments]    ${member_index}=${1}    ${timeout}=3s
124     [Documentation]    Open new connection to karaf console for member index with specified timeout.
125     BuiltIn.Log    ${member_index}
126     ${odl_ip}=    ClusterManagement.Resolve_IP_Address_For_Member    ${member_index}
127     SSHLibrary.Open Connection    ${odl_ip}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_DETAILED_PROMPT}    timeout=${timeout}
128     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}
129
130 Configure Timeout For Karaf Console
131     [Arguments]    ${timeout}    ${member_index_list}=${EMPTY}
132     [Documentation]    Configure a different timeout for each Karaf console.
133     ${index_list} =    ClusterManagement.List Indices Or All    given_list=${member_index_list}
134     ${current_connection_object}=    SSHLibrary.Get Connection
135     : FOR    ${member_index}    IN    @{index_list}    # usually: 1, 2, 3
136     \    ${karaf_connection_index}=    Collections.Get From Dictionary    ${connection_index_dict}    ${member_index}
137     \    SSHLibrary.Switch Connection    ${karaf_connection_index}
138     \    SSHLibrary.Set_Client_Configuration    timeout=${timeout}
139     [Teardown]    SSHKeywords.Restore Current SSH Connection From Index    ${current_connection_object.index}
140
141 Execute Controller Karaf Command On Background
142     [Arguments]    ${command}    ${member_index}=${1}
143     [Documentation]    Send command to karaf without affecting current SSH connection. Read, log and return response.
144     ${karaf_connection_index}=    Collections.Get From Dictionary    ${connection_index_dict}    ${member_index}
145     ${current_connection_index}=    SSHLibrary.Switch Connection    ${karaf_connection_index}
146     ${status_write}    ${message_write}=    BuiltIn.Run Keyword And Ignore Error    SSHLibrary.Write    ${command}
147     ${status_wait}    ${message_wait}=    BuiltIn.Run Keyword And Ignore Error    SSHLibrary.Read Until Prompt
148     BuiltIn.Run Keyword If    '${status_write}' != 'PASS'    BuiltIn.Fail    Failed to send the command: ${command}
149     BuiltIn.Log    ${message_wait}
150     BuiltIn.Run Keyword If    '${status_wait}' != 'PASS'    BuiltIn.Fail    Failed to see prompt after sending the command: ${command}
151     [Teardown]    SSHKeywords.Restore Current SSH Connection From Index    ${current_connection_index}
152     [Return]    ${message_wait}
153
154 Execute Controller Karaf Command With Retry On Background
155     [Arguments]    ${command}    ${member_index}=${1}
156     [Documentation]    Attemp to send command to karaf for ${member_index}, if fail then open connection and try again.
157     ${status}    ${message}=    BuiltIn.Run Keyword And Ignore Error    Execute Controller Karaf Command On Background    ${command}    ${member_index}
158     BuiltIn.Return_From_Keyword_If    '${status}' == 'PASS'    ${message}
159     # TODO: Verify this does not leak connections indices.
160     Open Controller Karaf Console On Background    ${member_index}
161     ${message}=    Execute Controller Karaf Command On Background    ${command}    ${member_index}
162     [Return]    ${message}
163
164 Log Message To Controller Karaf
165     [Arguments]    ${message}    ${member_index_list}=${EMPTY}    ${tolerate_failure}=True
166     [Documentation]    Make sure this resource is initialized. Send a message into the controller's karaf log file on every node listed (or all).
167     ...    By default, failure while processing a node is silently ignored, unless ${tolerate_failure} is False.
168     ${index_list} =    ClusterManagement.List Indices Or All    given_list=${member_index_list}
169     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
170     \    ${status}    ${output}=    BuiltIn.Run Keyword And Ignore Error    Execute Controller Karaf Command With Retry On Background    log:log "ROBOT MESSAGE: ${message}"    member_index=${index}
171     \    BuiltIn.Run_Keyword_Unless    ${tolerate_failure} or "${status}" == "PASS"    BuiltIn.Fail    ${output}
172
173 Log Test Suite Start To Controller Karaf
174     [Arguments]    ${member_index_list}=${EMPTY}
175     [Documentation]    Log suite name to karaf log, useful in suite setup.
176     Log Message To Controller Karaf    Starting suite ${SUITE_SOURCE}    ${member_index_list}
177
178 Log Testcase Start To Controller Karaf
179     [Arguments]    ${member_index_list}=${EMPTY}
180     [Documentation]    Log test case name to karaf log, useful in test case setup.
181     Log Message To Controller Karaf    Starting test ${TEST_NAME}    ${member_index_list}
182
183 Set Bgpcep Log Levels
184     [Arguments]    ${bgpcep_level}=${DEFAULT_BGPCEP_LOG_LEVEL}    ${protocol_level}=${DEFAULT_PROTOCOL_LOG_LEVEL}    ${member_index_list}=${EMPTY}
185     [Documentation]    Assuming OCKCOB was used, set logging level on bgpcep and protocol loggers without affecting current SSH session.
186     # FIXME: Move to appropriate Resource
187     ${index_list} =    ClusterManagement.List Indices Or All    given_list=${member_index_list}
188     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
189     \    Execute Controller Karaf Command On Background    log:set ${bgpcep_level} org.opendaylight.bgpcep    member_index=${index}
190     \    Execute Controller Karaf Command On Background    log:set ${protocol_level} org.opendaylight.protocol    member_index=${index}
191
192 Wait For Karaf Log
193     [Arguments]    ${message}    ${timeout}=60    ${member_index}=${1}
194     [Documentation]    Read karaf logs until message appear
195     # TODO: refactor this keyword to use the new workflow to account for multiple controllers.    Initial work was done
196     # in this patch https://git.opendaylight.org/gerrit/#/c/45596/
197     # however, the consumers of this keyword were breaking after that change.    Initial theory is that a previous
198     # keyword used before this "Wait For Karaf Log" keyword was closing the karaf console connection, so the
199     # "Flexible SSH Login" keyword from the patch above (45596) was failing.
200     Log    Waiting for '${message}' in karaf log
201     Open Connection    ${ODL_SYSTEM_IP}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_PROMPT}    timeout=${timeout}
202     Login    ${KARAF_USER}    ${KARAF_PASSWORD}    loglevel=${loglevel}
203     Write    log:tail
204     Read Until    ${message}
205     Close Connection
206
207 Restart Bundle
208     [Arguments]    ${bundle_id}
209     [Documentation]    Restarts bundle passed as argument. Note this operation is only for testing and not production environments
210     # TODO: prepare this for cluster environment and multiple controllers
211     Safe_Issue_Command_On_Karaf_Console    bundle:restart -f $(bundle:id '${bundle_id}')
212
213 Restart Karaf
214     [Documentation]    Restarts Karaf and polls log to detect when Karaf is up and running again
215     # TODO: prepare this for cluster environment and multiple controllers
216     Safe_Issue_Command_On_Karaf_Console    log:clear
217     Issue_Command_On_Karaf_Console    shutdown -r -f
218     Run Keyword And Return Status    Wait Until Keyword Succeeds    240s    60s    Wait For Karaf Log    Karaf started in
219
220 Restart Jetty
221     [Documentation]    Restarts jetty bundle (to reload certificates or key/truststore information)
222     Safe_Issue_Command_On_Karaf_Console    log:clear
223     Restart Bundle    OPS4J Pax Web - Jetty
224     Wait For Karaf Log    Instantiated the Application class org.opendaylight.restconf.RestconfApplication
225     Wait For Karaf Log    Instantiated the Application class org.opendaylight.netconf.sal.rest.impl.RestconfApplication
226     Wait For Karaf Log    Instantiated the Application class org.opendaylight.aaa.idm.IdmLightApplication