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