Moved the karaf logging keywords to KarafKeywords
[integration/test.git] / csit / libraries / KarafKeywords.robot
1 *** Settings ***
2 Library           SSHLibrary
3 Library           OperatingSystem
4 Variables         ../variables/Variables.py
5
6 *** Variables ***
7 ${WORKSPACE}      /tmp
8 ${BUNDLEFOLDER}    distribution-karaf-0.3.0-SNAPSHOT
9 ${KarafKeywords__karaf_connection_index}    -1
10
11 *** Keywords ***
12 Check Karaf Log File Does Not Have Messages
13     [Arguments]    ${ip}    ${message}    ${user}=${CONTROLLER_USER}    ${password}=${CONTROLLER_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${log_file}=${WORKSPACE}/${BUNDLEFOLDER}/data/log/karaf.log
14     [Documentation]    Fails if the provided ${message} is found in the karaf.log file. Uses grep to search. The
15     ...    karaf.log file can be overridden with ${log_file} to be any file on the given system @ ${ip}
16     ${output}=    Run Command On Controller    ${ip}    grep ${message} ${log_file}    user=${user}    password=${password}    prompt=${prompt}
17     Should Not Contain    ${output}    ${message}
18
19 Verify Feature Is Installed
20     [Arguments]    ${feature_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}
21     [Documentation]    Will Succeed if the given ${feature_name} is found in the output of "feature:list -i"
22     ${output}=    Issue Command On Karaf Console    feature:list -i | grep ${feature_name}    ${controller}    ${karaf_port}
23     Should Contain    ${output}    ${feature_name}
24     [Return]    ${output}
25
26 Verify Feature Is Not Installed
27     [Arguments]    ${feature_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}
28     [Documentation]    Will Succeed if the given ${feature_name} is NOT found in the output of "feature:list -i"
29     ${output}=    Issue Command On Karaf Console    feature:list -i | grep ${feature_name}    ${controller}    ${karaf_port}
30     Should Not Contain    ${output}    ${feature_name}
31     [Return]    ${output}
32
33 Issue Command On Karaf Console
34     [Arguments]    ${cmd}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=5
35     [Documentation]    Will execute the given ${cmd} by ssh'ing to the karaf console running on ${CONTROLLER}
36     ...   Note that this keyword will open&close new SSH connection, without switching back to previously current session.
37     Open Connection    ${controller}    port=${karaf_port}    prompt=${KARAF_PROMPT}    timeout=${timeout}
38     Login    ${KARAF_USER}    ${KARAF_PASSWORD}
39     Write    ${cmd}
40     ${output}    Read Until    ${KARAF_PROMPT}
41     Close Connection
42     Log    ${output}
43     [Return]    ${output}
44
45 Verify Bundle Is Installed
46     [Arguments]    ${bundle_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}
47     [Documentation]    Will succeed if the given ${bundle name} is present in the output of "bundle:list -s "
48     ${output}=    Issue Command On Karaf Console    bundle:list -s | grep ${bundle_name}    ${controller}    ${karaf_port}
49     Should Contain    ${output}    ${bundle_name}
50     [Return]    ${output}
51
52 Verify Bundle Is Not Installed
53     [Arguments]    ${bundle_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}
54     [Documentation]    Will succeed if the given ${bundle_name} is NOT found in the output of "bundle:list -s"
55     ${output}=    Issue Command On Karaf Console    bundle:list -i | grep ${bundle_name}    ${controller}    ${karaf_port}
56     Should Not Contain    ${output}    ${bundle_name}
57     [Return]    ${output}
58
59 Check Karaf Log Has Messages
60     [Arguments]    ${filter_string}    @{message_list}
61     [Documentation]    Will succeed if the @{messages} are found in \ the output of "log:display"
62     ${output}=    Issue Command On Karaf Console    log:display | grep ${filter_string}
63     : FOR    ${message}    IN    @{message_list}
64     \    Should Contain    ${output}    ${message}
65     [Return]    ${output}
66
67 Install a Feature
68     [Arguments]    ${feature_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=15
69     [Documentation]    Will Install the given ${feature_name}
70     Log    ${timeout}
71     ${output}=    Issue Command On Karaf Console    feature:install ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
72     Log    ${output}
73     [Return]    ${output}
74
75 Uninstall a Feature
76     [Arguments]    ${feature_name}    ${controller}=${CONTROLLER}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=15
77     [Documentation]    Will UnInstall the given ${feature_name}
78     ${output}=    Issue Command On Karaf Console    feature:uninstall ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
79     Log    ${output}
80     [Return]    ${output}
81
82 Restore Current SSH Connection From Index
83     [Arguments]    ${connection_index}
84     [Documentation]    Restore active SSH connection in SSHLibrary to given index.
85     ...
86     ...    Restore the currently active connection state in
87     ...    SSHLibrary to match the state returned by "Switch
88     ...    Connection" or "Get Connection". More specifically makes
89     ...    sure that there will be no active connection when the
90     ...    \${connection_index} reported by these means is None.
91     ...
92     ...    There is a misfeature in SSHLibrary: Invoking "SSHLibrary.Switch_Connection"
93     ...    and passing None as the "index_or_alias" argument to it has exactly the
94     ...    same effect as invoking "Close Connection".
95     ...    https://github.com/robotframework/SSHLibrary/blob/master/src/SSHLibrary/library.py#L560
96     ...
97     ...    We want to have Keyword which will "switch out" to previous
98     ...    "no connection active" state without killing the background one.
99     ...
100     ...    As some suites may hypothetically rely on non-writability of active connection,
101     ...    workaround is applied by opening and closing temporary connection.
102     ...    Unfortunately this will fail if run on Jython and there is no SSH server
103     ...    running on localhost, port 22 but there is nothing easy that can be done about it.
104     BuiltIn.Run Keyword And Return If    ${connection_index} is not None    SSHLibrary.Switch Connection    ${connection_index}
105     # The background connection is still current, bury it.
106     SSHLibrary.Open Connection    127.0.0.1
107     SSHLibrary.Close Connection
108
109 Open Controller Karaf Console On Background
110     [Documentation]    Connect to the controller's karaf console, but do not switch to it.
111     ${current_ssh_connection}=    SSHLibrary.Get Connection
112     SSHLibrary.Open Connection    ${CONTROLLER}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_DETAILED_PROMPT}
113     ${karaf_connection}=    SSHLibrary.Get Connection
114     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}
115     BuiltIn.Set Suite Variable    ${KarafKeywords__karaf_connection_index}    ${karaf_connection.index}
116     Restore Current SSH Connection From Index    ${current_ssh_connection.index}
117
118 Execute Controller Karaf Command On Background
119     [Arguments]    ${command}
120     [Documentation]    Send command to karaf without affecting current SSH connection. Read, log and return response.
121     ...    This assumes Karaf connection has index saved and correct prompt set.
122     BuiltIn.Run Keyword If    ${KarafKeywords__karaf_connection_index} == -1    Fail    Need to connect to a Karaf Console first
123     ${current_connection_index}=    SSHLibrary.Switch Connection    ${KarafKeywords__karaf_connection_index}
124     ${status_write}    ${message_write}=    BuiltIn.Run Keyword And Ignore Error    SSHLibrary.Write    ${command}
125     ${status_wait}    ${message_wait}=    BuiltIn.Run Keyword And Ignore Error    SSHLibrary.Read Until Prompt
126     Restore Current SSH Connection From Index    ${current_connection_index}
127     BuiltIn.Run Keyword If    '${status_write}' != 'PASS'    BuiltIn.Fail    Failed to send the command: ${command}
128     BuiltIn.Log    ${message_wait}
129     BuiltIn.Run Keyword If    '${status_wait}' != 'PASS'    BuiltIn.Fail    Failed to see prompt after sending the command: ${command}
130     [Return]    ${message_wait}
131
132 Log Message To Controller Karaf
133     [Arguments]    ${message}
134     [Documentation]    Send a message into the controller's karaf log file. Do not change current SSH connection.
135     ${reply}=    Execute Controller Karaf Command On Background    log:log "ROBOT MESSAGE: ${message}"
136     [Return]    ${reply}
137
138 Log Test Suite Start To Controller Karaf
139     [Documentation]    Log suite name to karaf log, useful in suite setup.
140     Log Message To Controller Karaf    Starting suite ${SUITE_SOURCE}
141
142 Log Testcase Start To Controller Karaf
143     [Documentation]    Log test case name to karaf log, useful in test case setup.
144     Log Message To Controller Karaf    Starting test ${TEST_NAME}