Many library improvements.
[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 Resource          ${CURDIR}/SSHKeywords.robot
6 Variables         ${CURDIR}/../variables/Variables.py
7
8 *** Variables ***
9 ${WORKSPACE}      /tmp
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    ${loglevel}=INFO
22     [Documentation]    Will execute the given ${cmd} by ssh'ing to the karaf console running on ${controller}
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}    loglevel=${loglevel}
26     Write    ${cmd}
27     ${output}    Read Until    ${KARAF_PROMPT}
28     Close Connection
29     Log    ${output}
30     [Return]    ${output}
31
32 Safe_Issue_Command_On_Karaf_Console
33     [Arguments]    ${cmd}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=5    ${loglevel}=INFO
34     [Documentation]    Run Issue_Command_On_Karaf_Console but restore previous connection afterwards.
35     BuiltIn.Run_Keyword_And_Return    SSHKeywords.Run_Keyword_Preserve_Connection    Issue_Command_On_Karaf_Console    ${cmd}    ${controller}    ${karaf_port}    ${timeout}
36     ...    ${loglevel}
37
38 Check For Elements On Karaf Command Output Message
39     [Arguments]    ${cmd}    ${elements}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=5
40     [Documentation]    Will execute the command using Issue Command On Karaf Console then check for the given elements
41     ...    in the command output message
42     ${output}    Issue Command On Karaf Console    ${cmd}    ${controller}    ${karaf_port}    ${timeout}
43     : FOR    ${i}    IN    @{elements}
44     \    Should Contain    ${output}    ${i}
45
46 Verify Bundle Is Installed
47     [Arguments]    ${bundle_name}    ${controller}=${ODL_SYSTEM_IP}    ${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}=${ODL_SYSTEM_IP}    ${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 Check Karaf Log File Does Not Have Messages
69     [Arguments]    ${ip}    ${message}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}    ${log_file}=${WORKSPACE}/${BUNDLEFOLDER}/data/log/karaf.log
70     [Documentation]    Fails if the provided ${message} is found in the karaf.log file. Uses grep to search. The
71     ...    karaf.log file can be overridden with ${log_file} to be any file on the given system @ ${ip}
72     ${output}=    Run Command On Controller    ${ip}    grep -c '${message}' ${log_file}    user=${user}    password=${password}    prompt=${prompt}
73     Should Be Equal As Strings    ${output}    0
74
75 Install a Feature
76     [Arguments]    ${feature_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=180
77     [Documentation]    Will Install the given ${feature_name}
78     Log    ${timeout}
79     ${output}=    Issue Command On Karaf Console    feature:install ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
80     Log    ${output}
81     [Return]    ${output}
82
83 Uninstall a Feature
84     [Arguments]    ${feature_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=180
85     [Documentation]    Will UnInstall the given ${feature_name}
86     ${output}=    Issue Command On Karaf Console    feature:uninstall ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
87     Log    ${output}
88     [Return]    ${output}
89
90 Open Controller Karaf Console On Background
91     [Documentation]    Connect to the controller's karaf console, but do not switch to it.
92     ${current_ssh_connection}=    SSHLibrary.Get Connection
93     SSHLibrary.Open Connection    ${ODL_SYSTEM_IP}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_DETAILED_PROMPT}
94     ${karaf_connection}=    SSHLibrary.Get Connection
95     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}
96     BuiltIn.Set Suite Variable    ${KarafKeywords__karaf_connection_index}    ${karaf_connection.index}
97     [Teardown]    SSHKeywords.Restore Current SSH Connection From Index    ${current_ssh_connection.index}
98
99 Configure Timeout For Karaf Console
100     [Arguments]    ${timeout}
101     [Documentation]    Configure a different timeout for the Karaf console
102     BuiltIn.Run Keyword If    ${KarafKeywords__karaf_connection_index} == -1    Fail    Need to connect to a Karaf Console first
103     ${current_connection_index}=    SSHLibrary.Switch Connection    ${KarafKeywords__karaf_connection_index}
104     SSHLibrary.Set_Client_Configuration    timeout=${timeout}
105     [Teardown]    SshKeywords.Restore Current SSH Connection From Index    ${current_connection_index}
106
107 Execute Controller Karaf Command On Background
108     [Arguments]    ${command}
109     [Documentation]    Send command to karaf without affecting current SSH connection. Read, log and return response.
110     ...    This assumes Karaf connection has index saved and correct prompt set.
111     BuiltIn.Run Keyword If    ${KarafKeywords__karaf_connection_index} == -1    Fail    Need to connect to a Karaf Console first
112     ${current_connection_index}=    SSHLibrary.Switch Connection    ${KarafKeywords__karaf_connection_index}
113     ${status_write}    ${message_write}=    BuiltIn.Run Keyword And Ignore Error    SSHLibrary.Write    ${command}
114     ${status_wait}    ${message_wait}=    BuiltIn.Run Keyword And Ignore Error    SSHLibrary.Read Until Prompt
115     BuiltIn.Run Keyword If    '${status_write}' != 'PASS'    BuiltIn.Fail    Failed to send the command: ${command}
116     BuiltIn.Log    ${message_wait}
117     BuiltIn.Run Keyword If    '${status_wait}' != 'PASS'    BuiltIn.Fail    Failed to see prompt after sending the command: ${command}
118     [Teardown]    SshKeywords.Restore Current SSH Connection From Index    ${current_connection_index}
119     [Return]    ${message_wait}
120
121 Execute Controller Karaf Command With Retry On Background
122     [Arguments]    ${command}
123     [Documentation]    Attemp to send command to karaf, if fail then open connection and try again.
124     # As an attempt to debug intermittent SSH failures in the karaf logging command, we want to know the avail entropy on the controller system
125     Run Command On Controller    cmd=cat /proc/sys/kernel/random/entropy_avail
126     ${status}    ${message}=    BuiltIn.Run Keyword And Ignore Error    Execute Controller Karaf Command On Background    ${command}
127     BuiltIn.Return_From_Keyword_If    '${status}' == 'PASS'    ${message}
128     # TODO: Verify this does not leak connections indices.
129     Open Controller Karaf Console On Background
130     ${message}=    Execute Controller Karaf Command On Background    ${command}
131     [Return]    ${message}
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 With Retry 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
153
154 Wait For Karaf Log
155     [Arguments]    ${message}    ${timeout}=60
156     [Documentation]    Read karaf logs until message appear
157     Log    Waiting for '${message}' in karaf log
158     Open Connection    ${ODL_SYSTEM_IP}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_PROMPT}    timeout=${timeout}
159     Flexible SSH Login    ${KARAF_USER}    ${KARAF_PASSWORD}
160     Write    log:tail
161     Read Until    ${message}
162     Close Connection