6a95514b5f64fd048ddb1c546251717cb22fcd63
[integration/test.git] / test / csit / libraries / Utils.txt
1 *** Settings ***
2 Library           SSHLibrary
3 Library           String
4 Library           DateTime
5 Library           ./UtilLibrary.py
6 Resource          KarafKeywords.txt
7
8 *** Variables ***
9 ${start}          sudo mn --controller=remote,ip=${CONTROLLER} --topo tree,1 --switch ovsk,protocols=OpenFlow13
10
11 *** Keywords ***
12 Start Suite
13     [Documentation]    Basic setup/cleanup work that can be done safely before any system
14     ...    is run.
15     Log    Start the test on the base edition
16     Clean Mininet System
17     ${mininet_conn_id}=    Open Connection    ${MININET}    prompt=${LINUX_PROMPT}    timeout=30s
18     Set Suite Variable    ${mininet_conn_id}
19     Login With Public Key    ${MININET_USER}    ${USER_HOME}/.ssh/${SSH_KEY}    any
20     Execute Command    sudo ovs-vsctl set-manager ptcp:6644
21     Write    ${start}
22     Read Until    mininet>
23
24 Start Mininet
25     [Arguments]    ${MININET}    ${cmd}=${start}    ${custom}=${OVSDB_CONFIG_DIR}/ovsdb.py    ${user}=${MININET_USER}    ${prompt}=${LINUX_PROMPT}    ${prompt_timeout}=30s
26     [Documentation]    Basic setup to start mininet with custom topology
27     Log    Start the test on the base edition
28     Clean Mininet System
29     ${mininet_conn_id}=    Open Connection    ${MININET}    prompt=${LINUX_PROMPT}    timeout=30s
30     Set Suite Variable    ${mininet_conn_id}
31     Login With Public Key    ${MININET_USER}    ${USER_HOME}/.ssh/id_rsa    any
32     Put File    ${custom}
33     Write    ${cmd}
34     Read Until    mininet>
35     [Return]    ${mininet_conn_id}
36
37 Stop Mininet
38     [Arguments]    ${mininet_conn_id}    ${prompt}=${LINUX_PROMPT}
39     [Documentation]    Basic setup to stop/clean mininet
40     Switch Connection    ${mininet_conn_id}
41     SSHLibrary.Write    exit
42     Read Until    ${LINUX_PROMPT}
43     Close Connection
44
45 Stop Suite
46     [Documentation]    Cleanup/Shutdown work that should be done at the completion of all
47     ...    tests
48     Log    Stop the test on the base edition
49     Switch Connection    ${mininet_conn_id}
50     Read
51     Write    exit
52     Read Until    ${LINUX_PROMPT}
53     Close Connection
54
55 Ensure All Nodes Are In Response
56     [Arguments]    ${URI}    ${node_list}
57     [Documentation]    A GET is made to the supplied ${URI} and every item in the ${node_list}
58     ...    is verified to exist in the repsonse. This keyword currently implies that it's node
59     ...    specific but any list of strings can be given in ${node_list}. Refactoring of this
60     ...    to make it more generic should be done. (see keyword "Check For Elements At URI")
61     : FOR    ${node}    IN    @{node_list}
62     \    ${resp}    RequestsLibrary.Get    session    ${URI}
63     \    Should Be Equal As Strings    ${resp.status_code}    200
64     \    Should Contain    ${resp.content}    ${node}
65
66 Check Nodes Stats
67     [Arguments]    ${node}
68     [Documentation]    A GET on the /node/${node} API is made and specific flow stat
69     ...    strings are checked for existence.
70     ${resp}    RequestsLibrary.Get    session    ${OPERATIONAL_NODES_API}/node/${node}
71     Should Be Equal As Strings    ${resp.status_code}    200
72     Should Contain    ${resp.content}    flow-capable-node-connector-statistics
73     Should Contain    ${resp.content}    flow-table-statistics
74
75 Check That Port Count Is Ok
76     [Arguments]    ${node}    ${count}
77     [Documentation]    A GET on the /port API is made and the specified port ${count} is
78     ...    verified. A more generic Keyword "Check For Specific Number Of Elements At URI"
79     ...    also does this work and further consolidation should be done.
80     ${resp}    RequestsLibrary.Get    session    ${REST_CONTEXT}/${CONTAINER}/port
81     Log    ${resp.content}
82     Should Be Equal As Strings    ${resp.status_code}    200
83     Should Contain X Times    ${resp.content}    ${node}    ${count}
84
85 Check For Specific Number Of Elements At URI
86     [Arguments]    ${uri}    ${element}    ${expected_count}
87     [Documentation]    A GET is made to the specified ${URI} and the specific count of a
88     ...    given element is done (as supplied by ${element} and ${expected_count})
89     ${resp}    RequestsLibrary.Get    session    ${uri}
90     Log    ${resp.content}
91     Should Be Equal As Strings    ${resp.status_code}    200
92     Should Contain X Times    ${resp.content}    ${element}    ${expected_count}
93
94 Check For Elements At URI
95     [Arguments]    ${uri}    ${elements}
96     [Documentation]    A GET is made at the supplied ${URI} and every item in the list of
97     ...    ${elements} is verified to exist in the response
98     ${resp}    RequestsLibrary.Get    session    ${uri}
99     Log    ${resp.content}
100     Should Be Equal As Strings    ${resp.status_code}    200
101     : FOR    ${i}    IN    @{elements}
102     \    Should Contain    ${resp.content}    ${i}
103
104 Check For Elements Not At URI
105     [Arguments]    ${uri}    ${elements}
106     [Documentation]    A GET is made at the supplied ${URI} and every item in the list of
107     ...    ${elements} is verified to NOT exist in the response
108     ${resp}    RequestsLibrary.Get    session    ${uri}
109     Log    ${resp.content}
110     Should Be Equal As Strings    ${resp.status_code}    200
111     : FOR    ${i}    IN    @{elements}
112     \    Should Not Contain    ${resp.content}    ${i}
113
114 Clean Mininet System
115     [Arguments]    ${mininet_system}=${MININET}
116     Run Command On Remote System    ${mininet_system}    sudo mn -c
117     Run Command On Remote System    ${mininet_system}    sudo ps -elf | egrep 'usr/local/bin/mn' | egrep python | awk '{print "sudo kill -9",$4}' | sh
118
119 Clean Up Ovs
120     [Arguments]     ${mininet_system}=${MININET}
121     [Documentation]    Cleans up the OVS instance and remove any existing common known bridges.
122     ${output}=    Run Command On Remote System    ${mininet_system}    sudo ovs-vsctl list-br
123     Log    ${output}
124     : FOR    ${i}    IN    ${output}
125     \    Run Command On Remote System    ${mininet_system}    sudo ovs-vsctl --if-exists del-br ${i}
126     Run Command On Remote System    ${mininet_system}    sudo ovs-vsctl del-manager
127
128 Extract Value From Content
129     [Arguments]    ${content}    ${index}    ${strip}=nostrip
130     [Documentation]    Will take the given response content and return the value at the given index as a string
131     ${value}=    Get Json Value    ${content}    ${index}
132     ${value}=    Convert To String    ${value}
133     ${value}=    Run Keyword If    '${strip}' == 'strip'    Strip Quotes    ${value}
134     [Return]    ${value}
135
136 Get Process ID Based On Regex On Remote System
137     [Arguments]    ${remote_system}    ${regex_string_to_match_on}
138     [Documentation]    Uses ps to find a process that matches the supplied regex. Returns the PID of that process
139     ...    The ${regex_string_to_match_on} should produce a unique process otherwise the PID returned may not be
140     ...    the expected PID
141     # doing the extra -v grep in this command to exclude the grep process itself from the output
142     ${output}=    Run Command On Remote System    ${remote_system}    ps -elf | grep -v grep | grep ${regex_string_to_match_on} | awk '{print $4}'
143     # ${output} contains the system prompt and all we want is the value of the number
144     ${pid}=    Fetch From Left    ${output}    \r
145     [Return]    ${pid}
146
147 Get Process Thread Count On Remote System
148     [Arguments]    ${remote_system}    ${pid}
149     [Documentation]    Executes the ps command to retrieve the lightweight process (aka thread) count.
150     ${output}=    Run Command On Remote System    ${remote_system}    ps --no-headers -o nlwp ${pid}
151     # ${output} contains the system prompt and all we want is the value of the number
152     ${thread_count}=    Fetch From Left    ${output}    \r
153     [Return]    ${thread_count}
154
155 Strip Quotes
156     [Arguments]    ${string_to_strip}
157     [Documentation]    Will strip ALL quotes from given string and return the new string
158     ${string_to_return}=    Replace String    ${string_to_strip}    "    \    count=-1
159     [Return]    ${string_to_return}
160
161 Run Command On Remote System
162     [Arguments]    ${remote_system}    ${cmd}    ${user}=${MININET_USER}    ${prompt}=${LINUX_PROMPT}    ${prompt_timeout}=30s
163     [Documentation]    Reduces the common work of running a command on a remote system to a single higher level robot keyword,
164     ...    taking care to log in with a public key and. The command given is written and the output returned. No test conditions
165     ...    are checked.
166     Log    Attempting to execute ${cmd} on ${remote_system} by ${user} with ${keyfile_pass} and ${prompt}
167     ${conn_id}=    SSHLibrary.Open Connection    ${remote_system}    prompt=${prompt}    timeout=${prompt_timeout}
168     Login With Public Key    ${user}    ${USER_HOME}/.ssh/${SSH_KEY}    ${KEYFILE_PASS}
169     SSHLibrary.Write    ${cmd}
170     ${output}=    SSHLibrary.Read Until    ${prompt}
171     SSHLibrary.Close Connection
172     Log    ${output}
173     [Return]    ${output}
174
175 Verify File Exists On Remote System
176     [Arguments]    ${remote_system}    ${file}    ${user}=${MININET_USER}    ${prompt}=${LINUX_PROMPT}    ${prompt_timeout}=5s
177     [Documentation]    Will create connection with public key and will PASS if the given ${file} exists, otherwise will FAIL
178     ${conn_id}=    Open Connection    ${remote_system}    prompt=${prompt}    timeout=${prompt_timeout}
179     Login With Public Key    ${user}    ${USER_HOME}/.ssh/${SSH_KEY}    any
180     SSHLibrary.File Should Exist    ${file}
181     Close Connection
182
183 Verify Controller Is Not Dead
184     [Arguments]    ${controller_ip}=${CONTROLLER}
185     [Documentation]    Will execute any tests to verify the controller is not dead. Some checks are
186     ...    Out Of Memory Execptions.
187     Check Karaf Log File Does Not Have Messages    ${controller_ip}    java.lang.OutOfMemoryError
188
189 Verify Controller Has No Null Pointer Exceptions
190     [Arguments]    ${controller_ip}=${CONTROLLER}
191     [Documentation]    Will execute any tests to verify the controller is not having any null pointer eceptions.
192     Check Karaf Log File Does Not Have Messages    ${controller_ip}    java.lang.NullPointerException
193
194 Get Epoch Time
195     [Arguments]    ${time}
196     [Documentation]    Get the Epoc time from MM/DD/YYYY HH:MM:SS
197     ${epoch_time}=    Convert Date    ${time}    epoch    exclude_milles=True    date_format=%m/%d/%Y %H:%M:%S
198     ${epoch_time}=    Convert To Integer    ${epoch_time}
199     [Return]    ${epoch_time}
200
201 Remove Space on String
202     [Arguments]    ${str}    ${count}=-1
203     [Documentation]    Remove the empty space from given string.count is optional,if its given
204     ...    that many occurence of space will be removed from left
205     ${x}=    Convert To String    ${str}
206     ${x}=    Replace String    ${str}    ${SPACE}    ${EMPTY}    count=${count}
207     [Return]    ${x}
208
209 Split Value from String
210     [Arguments]    ${str}    ${splitter}
211     [Documentation]    Split the String based on given splitter and return as list
212     @{x}=    Split String    ${str}    ${splitter}
213     [Return]    @{x}
214
215 Concatenate the String
216     [Arguments]    ${str1}    ${str2}
217     [Documentation]    Catenate the two non-string objects and return as String
218     ${str1}=    Convert to String    ${str1}
219     ${str2}=    Convert to String    ${str2}
220     ${output}=    Catenate    ${str1}    ${str2}
221     [Return]    ${output}
222
223 Remove All Elements At URI
224     [Arguments]    ${uri}
225     ${resp}    RequestsLibrary.Delete    session    ${uri}
226     Should Be Equal As Strings    ${resp.status_code}    200
227
228 Add Elements To URI From File
229     [Arguments]    ${dest_uri}    ${data_file}
230     ${body}    OperatingSystem.Get File    ${data_file}
231     ${resp}    RequestsLibrary.Put    session    ${dest_uri}    data=${body}    headers=${headers}
232     Should Be Equal As Strings    ${resp.status_code}    200
233
234 Post Elements To URI From File
235     [Arguments]    ${dest_uri}    ${data_file}
236     ${body}    OperatingSystem.Get File    ${data_file}
237     ${resp}    RequestsLibrary.Post    session    ${dest_uri}    data=${body}    headers=${headers}
238     Should Be Equal As Strings    ${resp.status_code}    200
239
240 Stop One Or More Controllers
241     [Arguments]    @{controllers}
242     [Documentation]    Give this keyword a scalar or list of controllers to be stopped.
243     ${cmd} =    Set Variable    ${KARAF_HOME}/bin/stop
244     : FOR    ${ip}    IN    @{controllers}
245     \    Run Command On Remote System    ${ip}    ${cmd}
246     : FOR    ${ip}    IN    @{controllers}
247     \    Wait Until Keyword Succeeds    60 s    3 s    Controller Down Check    ${ip}
248
249 Start One Or More Controllers
250     [Arguments]    @{controllers}
251     [Documentation]    Give this keyword a scalar or list of controllers to be started.
252     ${cmd} =    Set Variable    ${KARAF_HOME}/bin/start
253     : FOR    ${ip}    IN    @{controllers}
254     \    Run Command On Remote System    ${ip}    ${cmd}
255     # TODO: This should throw an error if controller never comes up.
256     : FOR    ${ip}    IN    @{controllers}
257     \    UtilLibrary.Wait For Controller Up    ${ip}    ${RESTCONFPORT}
258
259 Kill One Or More Controllers
260     [Arguments]    @{controllers}
261     [Documentation]    Give this keyword a scalar or list of controllers to be stopped.
262     ${cmd} =    Set Variable    ps axf | grep karaf | grep -v grep | awk '{print \"kill -9 \" $1}' | sh
263     log     ${cmd}
264     ${controller_pid}=    Get Process ID Based On Regex On Remote System    ${CONTROLLER}    karaf
265     : FOR    ${ip}    IN    @{controllers}
266     \    Run Command On Remote System    ${ip}    ${cmd}
267     : FOR    ${ip}    IN    @{controllers}
268     \    Wait Until Keyword Succeeds    60 s    3 s    Controller Down Check    ${ip}
269
270 Controller Down Check
271     [Arguments]    ${ip}
272     [Documentation]    Checks to see if a controller is down by verifying that the karaf process isn't present.
273     ${cmd} =    Set Variable    ps axf | grep karaf | grep -v grep | wc -l
274     ${response}    Run Command On Remote System    ${ip}    ${cmd}
275     Log    Number of controller instances running: ${response}
276     Should Start With    ${response}    0    Controller process found or there may be extra instances of
277     ...    karaf running on the host machine.
278
279 Clean One Or More Journals
280     [Arguments]    @{controllers}
281     [Documentation]    Give this keyword a scalar or list of controllers on which to clean journals.
282     ${del_cmd} =    Set Variable    rm -rf ${KARAF_HOME}/journal
283     : FOR    ${ip}    IN    @{controllers}
284     \    Run Command On Remote System    ${ip}    ${del_cmd}
285
286 Show Cluster Configuation Files
287     [Arguments]    @{controllers}
288     [Documentation]    Prints out the cluster configuration files for one or more controllers.
289     Log    controllers: @{controllers}
290     ${cmd} =    Set Variable    cat ${KARAF_HOME}/configuration/initial/akka.conf
291     : FOR    ${ip}    IN    @{controllers}
292     \    Run Command On Remote System    ${ip}    ${cmd}
293     ${cmd} =    Set Variable    cat ${KARAF_HOME}/configuration/initial/modules.conf
294     : FOR    ${ip}    IN    @{controllers}
295     \    Run Command On Remote System    ${ip}    ${cmd}
296     ${cmd} =    Set Variable    cat ${KARAF_HOME}/configuration/initial/module-shards.conf
297     : FOR    ${ip}    IN    @{controllers}
298     \    Run Command On Remote System    ${ip}    ${cmd}
299     ${cmd} =    Set Variable    cat ${KARAF_HOME}/configuration/initial/jolokia.xml
300     : FOR    ${ip}    IN    @{controllers}
301     \    Run Command On Remote System    ${ip}    ${cmd}
302     ${cmd} =    Set Variable    cat ${KARAF_HOME}/etc/initial/org.apache.karaf.management.cfg
303     : FOR    ${ip}    IN    @{controllers}
304     \    Run Command On Remote System    ${ip}    ${cmd}
305     ${cmd} =    Set Variable    cat ${KARAF_HOME}/etc/org.apache.karaf.features.cfg
306     : FOR    ${ip}    IN    @{controllers}
307     \    Run Command On Remote System    ${ip}    ${cmd}