Refactor flow service test
[integration/test.git] / csit / libraries / Utils.robot
1 *** Settings ***
2 Documentation     General Utils library. This library has broad scope, it can be used by any robot system tests.
3 Library           SSHLibrary
4 Library           String
5 Library           DateTime
6 Library           Process
7 Library           Collections
8 Library           RequestsLibrary
9 Library           ${CURDIR}/UtilLibrary.py
10 Resource          ${CURDIR}/SSHKeywords.robot
11 Resource          ${CURDIR}/TemplatedRequests.robot
12 Variables         ${CURDIR}/../variables/Variables.py
13
14 *** Variables ***
15 # TODO: Introduce ${tree_size} and use instead of 1 in the next line.
16 ${start}          sudo mn --controller=remote,ip=${ODL_SYSTEM_IP} --topo tree,1 --switch ovsk,protocols=OpenFlow13
17
18 *** Keywords ***
19 Start Suite
20     [Arguments]    ${system}=${TOOLS_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${timeout}=30s
21     [Documentation]    Basic setup/cleanup work that can be done safely before any system
22     ...    is run.
23     Log    Start the test on the base edition
24     Clean Mininet System
25     ${mininet_conn_id}=    Open Connection    ${system}    prompt=${prompt}    timeout=${timeout}
26     Set Suite Variable    ${mininet_conn_id}
27     Flexible Mininet Login    user=${user}    password=${password}
28     Execute Command    sudo ovs-vsctl set-manager ptcp:6644
29     Write    ${start}
30     Read Until    mininet>
31
32 Start Mininet
33     [Arguments]    ${system}=${TOOLS_SYSTEM_IP}    ${cmd}=${start}    ${custom}=    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}
34     ...    ${prompt_timeout}=30s
35     [Documentation]    Basic setup to start mininet with custom topology
36     Log    Start the test on the base edition
37     Clean Mininet System
38     ${mininet_conn_id}=    Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
39     Set Suite Variable    ${mininet_conn_id}
40     Flexible Mininet Login    user=${user}    password=${password}
41     Put File    ${custom}
42     Write    ${cmd}
43     Read Until    mininet>
44     [Return]    ${mininet_conn_id}
45
46 Stop Mininet
47     [Arguments]    ${mininet_conn_id}    ${prompt}=${DEFAULT_LINUX_PROMPT}
48     [Documentation]    Basic setup to stop/clean mininet
49     Switch Connection    ${mininet_conn_id}
50     SSHLibrary.Write    exit
51     Read Until    ${prompt}
52     Close Connection
53
54 Stop Suite
55     [Arguments]    ${prompt}=${DEFAULT_LINUX_PROMPT}
56     [Documentation]    Cleanup/Shutdown work that should be done at the completion of all
57     ...    tests
58     Log    Stop the test on the base edition
59     Switch Connection    ${mininet_conn_id}
60     Read
61     Write    exit
62     Read Until    ${prompt}
63     Close Connection
64
65 Report_Failure_Due_To_Bug
66     [Arguments]    ${number}
67     [Documentation]    Report that a test failed due to a known Bugzilla bug whose
68     ...    number is provided as an argument.
69     ...    Not FAILED (incl. SKIPPED) test are not reported.
70     ...    This keyword must be used in the [Teardown] setting of the affected test
71     ...    or as the first line of the test if FastFail module is not being
72     ...    used. It reports the URL of the bug on console and also puts it
73     ...    into the Robot log file.
74     ${test_skipped}=    BuiltIn.Evaluate    len(re.findall('SKIPPED', """${TEST_MESSAGE}""")) > 0    modules=re
75     BuiltIn.Return From Keyword If    ('${TEST_STATUS}' != 'FAIL') or ${test_skipped}
76     ${newline}=    BuiltIn.Evaluate    chr(10)
77     ${msg}=    BuiltIn.Set_Variable    This test fails due to https://bugs.opendaylight.org/show_bug.cgi?id=${number}
78     BuiltIn.Set Test Message    ${msg}${newline}${newline}${TEST_MESSAGE}
79     BuiltIn.Log    ${msg}
80
81 Report_Failure_And_Point_To_Linked_Bugs
82     [Documentation]    Report that a test failed and point to linked Bugzilla bug(s).
83     ...    Linked bugs must contain the ${reference} inside comments (workaround
84     ...    becasue of currently missing suitable field for external references and
85     ...    not correctly working the CONTENT MATCHES filter).
86     ...    Not FAILED (incl. SKIPPED) test are not reported.
87     ...    This keyword must be used in the [Teardown] setting of the affected test
88     ...    or as the first line of the test if FastFail module is not being
89     ...    used. It reports the URL of the bug on console and also puts it
90     ...    into the Robot log file.
91     ${test_skipped}=    BuiltIn.Evaluate    len(re.findall('SKIPPED', """${TEST_MESSAGE}""")) > 0    modules=re
92     BuiltIn.Return From Keyword If    ('${TEST_STATUS}' != 'FAIL') or ${test_skipped}
93     ${newline}=    BuiltIn.Evaluate    chr(10)
94     ${reference}=    String.Replace_String_Using_Regexp    ${SUITE_NAME}_${TEST_NAME}    [ /\.-]    _
95     ${reference}=    String.Convert_To_Lowercase    ${reference}
96     ${msg}=    BuiltIn.Set_Variable    ... click for list of related bugs or create a new one if needed (with the${newline}"${reference}"${newline}reference somewhere inside)
97     ${bugs}=    BuiltIn.Set_Variable    "https://bugs.opendaylight.org/buglist.cgi?f1=cf_external_ref&o1=substring&v1=${reference}&order=bug_status"
98     BuiltIn.Set Test Message    ${msg}${newline}${bugs}${newline}${newline}${TEST_MESSAGE}
99     BuiltIn.Log    ${msg}${newline}${bugs}
100
101 Check Nodes Stats
102     [Arguments]    ${node}
103     [Documentation]    A GET on the /node/${node} API is made and specific flow stat
104     ...    strings are checked for existence.
105     ${resp}    RequestsLibrary.Get Request    session    ${OPERATIONAL_NODES_API}/node/${node}
106     Should Be Equal As Strings    ${resp.status_code}    200
107     Should Contain    ${resp.content}    flow-capable-node-connector-statistics
108     Should Contain    ${resp.content}    flow-table-statistics
109
110 Check For Specific Number Of Elements At URI
111     [Arguments]    ${uri}    ${element}    ${expected_count}
112     [Documentation]    A GET is made to the specified ${URI} and the specific count of a
113     ...    given element is done (as supplied by ${element} and ${expected_count})
114     ${resp}    RequestsLibrary.Get Request    session    ${uri}
115     Log    ${resp.content}
116     Should Be Equal As Strings    ${resp.status_code}    200
117     Should Contain X Times    ${resp.content}    ${element}    ${expected_count}
118
119 Check For Elements At URI
120     [Arguments]    ${uri}    ${elements}
121     [Documentation]    A GET is made at the supplied ${URI} and every item in the list of
122     ...    ${elements} is verified to exist in the response
123     ${resp}    RequestsLibrary.Get Request    session    ${uri}
124     Log    ${resp.content}
125     Should Be Equal As Strings    ${resp.status_code}    200
126     : FOR    ${i}    IN    @{elements}
127     \    Should Contain    ${resp.content}    ${i}
128
129 Check For Elements Not At URI
130     [Arguments]    ${uri}    ${elements}
131     [Documentation]    A GET is made at the supplied ${URI} and every item in the list of
132     ...    ${elements} is verified to NOT exist in the response
133     ${resp}    RequestsLibrary.Get Request    session    ${uri}
134     Log    ${resp.content}
135     Should Be Equal As Strings    ${resp.status_code}    200
136     : FOR    ${i}    IN    @{elements}
137     \    Should Not Contain    ${resp.content}    ${i}
138
139 Clean Mininet System
140     [Arguments]    ${system}=${TOOLS_SYSTEM_IP}
141     Run Command On Mininet    ${system}    sudo mn -c
142     Run Command On Mininet    ${system}    sudo ps -elf | egrep 'usr/local/bin/mn' | egrep python | awk '{print "sudo kill -9",$4}' | sh
143
144 Clean Up Ovs
145     [Arguments]    ${system}=${TOOLS_SYSTEM_IP}
146     [Documentation]    Cleans up the OVS instance and remove any existing common known bridges.
147     ${output}=    Run Command On Mininet    ${system}    sudo ovs-vsctl list-br
148     Log    ${output}
149     : FOR    ${i}    IN    ${output}
150     \    Run Command On Mininet    ${system}    sudo ovs-vsctl --if-exists del-br ${i}
151     Run Command On Mininet    ${system}    sudo ovs-vsctl del-manager
152
153 Extract Value From Content
154     [Arguments]    ${content}    ${index}    ${strip}=nostrip
155     [Documentation]    Will take the given response content and return the value at the given index as a string
156     ${value}=    Get Json Value    ${content}    ${index}
157     ${value}=    Convert To String    ${value}
158     ${value}=    Run Keyword If    '${strip}' == 'strip'    Strip Quotes    ${value}
159     [Return]    ${value}
160
161 Get Process ID Based On Regex On Remote System
162     [Arguments]    ${system}    ${regex_string_to_match_on}    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${EMPTY}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=30s
163     [Documentation]    Uses ps to find a process that matches the supplied regex. Returns the PID of that process
164     ...    The ${regex_string_to_match_on} should produce a unique process otherwise the PID returned may not be
165     ...    the expected PID
166     # doing the extra -v grep in this command to exclude the grep process itself from the output
167     ${cmd}=    Set Variable    ps -elf | grep -v grep | grep ${regex_string_to_match_on} | awk '{print $4}'
168     ${output}=    Run Command On Remote System    ${system}    ${cmd}    user=${user}    password=${password}    prompt=${prompt}
169     ...    prompt_timeout=${prompt_timeout}
170     # ${output} contains the system prompt and all we want is the value of the number
171     ${pid}=    Fetch From Left    ${output}    \r
172     # TODO: Get Process * keywords have perhaps non-standard default credentials.
173     # ...    Should there be * On Mininet and * On Controller specializations?
174     [Return]    ${pid}
175
176 Get Process Thread Count On Remote System
177     [Arguments]    ${system}    ${pid}    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${EMPTY}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=30s
178     [Documentation]    Executes the ps command to retrieve the lightweight process (aka thread) count.
179     ${cmd}=    Set Variable    ps --no-headers -o nlwp ${pid}
180     ${output}=    Run Command On Remote System    ${system}    ${cmd}    user=${user}    password=${password}    prompt=${prompt}
181     ...    prompt_timeout=${prompt_timeout}
182     # ${output} contains the system prompt and all we want is the value of the number
183     ${thread_count}=    Fetch From Left    ${output}    \r
184     [Return]    ${thread_count}
185
186 Strip Quotes
187     [Arguments]    ${string_to_strip}
188     [Documentation]    Will strip ALL quotes from given string and return the new string
189     ${string_to_return}=    Replace String    ${string_to_strip}    "    \    count=-1
190     [Return]    ${string_to_return}
191
192 Flexible SSH Login
193     [Arguments]    ${user}    ${password}=${EMPTY}    ${delay}=0.5s
194     [Documentation]    On active SSH session: if given non-empty password, do Login, else do Login With Public Key.
195     ${pwd_length} =    BuiltIn.Get Length    ${password}
196     # ${pwd_length} is guaranteed to be an integer, so we are safe to evaluate it as Python expression.
197     BuiltIn.Run Keyword And Return If    ${pwd_length} > 0    SSHLibrary.Login    ${user}    ${password}    delay=${delay}
198     BuiltIn.Run Keyword And Return    SSHLibrary.Login With Public Key    ${user}    ${USER_HOME}/.ssh/${SSH_KEY}    ${KEYFILE_PASS}    delay=${delay}
199
200 Flexible Mininet Login
201     [Arguments]    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${delay}=0.5s
202     [Documentation]    Call Flexible SSH Login, but with default values suitable for Mininet machine.
203     BuiltIn.Run Keyword And Return    Flexible SSH Login    user=${user}    password=${password}    delay=${delay}
204
205 Flexible Controller Login
206     [Arguments]    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${delay}=0.5s
207     [Documentation]    Call Flexible SSH Login, but with default values suitable for Controller machine.
208     BuiltIn.Run Keyword And Return    Flexible SSH Login    user=${user}    password=${password}    delay=${delay}
209
210 Run Command On Remote System
211     [Arguments]    ${system}    ${cmd}    ${user}=${DEFAULT_USER}    ${password}=${EMPTY}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=${DEFAULT_TIMEOUT}
212     [Documentation]    Reduces the common work of running a command on a remote system to a single higher level
213     ...    robot keyword, taking care to log in with a public key and. The command given is written
214     ...    and the output returned. No test conditions are checked.
215     ${current_ssh_connection}=    SSHLibrary.Get Connection
216     BuiltIn.Log    Attempting to execute command "${cmd}" on remote system "${system}" by user "${user}" with keyfile pass "${keyfile_pass}" and prompt "${prompt}"
217     BuiltIn.Log    ${password}
218     ${conn_id}=    SSHLibrary.Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
219     Flexible SSH Login    ${user}    ${password}
220     ${stdout}    ${stderr}    SSHLibrary.Execute Command    ${cmd}    return_stderr=True
221     SSHLibrary.Close Connection
222     Log    ${stderr}
223     [Teardown]    SSHKeywords.Restore_Current_SSH_Connection_From_Index    ${current_ssh_connection.index}
224     [Return]    ${stdout}
225
226 Write_Bare_Ctrl_C
227     [Documentation]    Construct ctrl+c character and SSH-write it (without endline) to the current SSH connection.
228     ...    Do not read anything yet.
229     ${ctrl_c}=    BuiltIn.Evaluate    chr(int(3))
230     SSHLibrary.Write_Bare    ${ctrl_c}
231
232 Write Bare Ctrl D
233     [Documentation]    Construct ctrl+d character and SSH-write it (without endline) to the current SSH connection.
234     ...    Do not read anything yet.
235     ${ctrl_d}=    BuiltIn.Evaluate    chr(int(4))
236     SSHLibrary.Write Bare    ${ctrl_d}
237
238 Run Command On Mininet
239     [Arguments]    ${system}=${TOOLS_SYSTEM_IP}    ${cmd}=echo    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${prompt}=${TOOLS_SYSTEM_PROMPT}
240     [Documentation]    Call Run Comand On Remote System, but with default values suitable for Mininet machine.
241     BuiltIn.Run Keyword And Return    Run Command On Remote System    ${system}    ${cmd}    ${user}    ${password}    prompt=${prompt}
242
243 Run Command On Controller
244     [Arguments]    ${system}=${ODL_SYSTEM_IP}    ${cmd}=echo    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
245     [Documentation]    Call Run Comand On Remote System, but with default values suitable for Controller machine.
246     BuiltIn.Log    ${password}
247     BuiltIn.Run Keyword And Return    Run Command On Remote System    ${system}    ${cmd}    ${user}    ${password}    prompt=${prompt}
248
249 Verify File Exists On Remote System
250     [Arguments]    ${system}    ${file}    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=5s
251     [Documentation]    Will create connection with public key and will PASS if the given ${file} exists,
252     ...    otherwise will FAIL
253     ${conn_id}=    Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
254     Flexible SSH Login    ${user}    ${password}
255     SSHLibrary.File Should Exist    ${file}
256     Close Connection
257
258 Check Karaf Log File Does Not Have Messages
259     [Arguments]    ${ip}    ${message}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}    ${log_file}=${WORKSPACE}/${BUNDLEFOLDER}/data/log/karaf.log
260     [Documentation]    Fails if the provided ${message} is found in the karaf.log file. Uses grep to search. The
261     ...    karaf.log file can be overridden with ${log_file} to be any file on the given system @ ${ip}
262     ${output}=    Run Command On Controller    ${ip}    grep -c '${message}' ${log_file}    user=${user}    password=${password}    prompt=${prompt}
263     Should Be Equal As Strings    ${output}    0
264
265 Verify Controller Is Not Dead
266     [Arguments]    ${controller_ip}=${ODL_SYSTEM_IP}
267     [Documentation]    Will execute any tests to verify the controller is not dead. Some checks are
268     ...    Out Of Memory Execptions.
269     Check Karaf Log File Does Not Have Messages    ${controller_ip}    java.lang.OutOfMemoryError
270     # TODO: Should Verify Controller * keywords also accept user, password, prompt and karaf_log arguments?
271
272 Verify Controller Has No Null Pointer Exceptions
273     [Arguments]    ${controller_ip}=${ODL_SYSTEM_IP}
274     [Documentation]    Will execute any tests to verify the controller is not having any null pointer eceptions.
275     Check Karaf Log File Does Not Have Messages    ${controller_ip}    java.lang.NullPointerException
276
277 Get Epoch Time
278     [Arguments]    ${time}
279     [Documentation]    Get the Epoc time from MM/DD/YYYY HH:MM:SS
280     ${epoch_time}=    Convert Date    ${time}    epoch    exclude_milles=True    date_format=%m/%d/%Y %H:%M:%S
281     ${epoch_time}=    Convert To Integer    ${epoch_time}
282     [Return]    ${epoch_time}
283
284 Remove Space on String
285     [Arguments]    ${str}    ${count}=-1
286     [Documentation]    Remove the empty space from given string.count is optional,if its given
287     ...    that many occurence of space will be removed from left
288     ${x}=    Convert To String    ${str}
289     ${x}=    Replace String    ${str}    ${SPACE}    ${EMPTY}    count=${count}
290     [Return]    ${x}
291
292 Split Value from String
293     [Arguments]    ${str}    ${splitter}
294     [Documentation]    Split the String based on given splitter and return as list
295     @{x}=    Split String    ${str}    ${splitter}
296     [Return]    @{x}
297
298 Concatenate the String
299     [Arguments]    ${str1}    ${str2}
300     [Documentation]    Catenate the two non-string objects and return as String
301     ${str1}=    Convert to String    ${str1}
302     ${str2}=    Convert to String    ${str2}
303     ${output}=    Catenate    ${str1}    ${str2}
304     [Return]    ${output}
305
306 Post Elements To URI
307     [Arguments]    ${rest_uri}    ${data}    ${headers}=${headers}    ${session}=session
308     [Documentation]    Perform a POST rest operation, using the URL and data provided
309     ${resp} =    RequestsLibrary.Post Request    ${session}    ${rest_uri}    data=${data}    headers=${headers}
310     Log    ${resp.content}
311     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
312
313 Remove All Elements At URI
314     [Arguments]    ${uri}
315     ${resp}    RequestsLibrary.Delete Request    session    ${uri}
316     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
317
318 Remove All Elements At URI And Verify
319     [Arguments]    ${uri}
320     ${resp}    RequestsLibrary.Delete Request    session    ${uri}
321     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
322     ${resp}    RequestsLibrary.Get Request    session    ${uri}
323     Should Be Equal As Strings    ${resp.status_code}    404
324
325 Remove All Elements If Exist
326     [Arguments]    ${uri}
327     [Documentation]    Delete all elements from an URI if the configuration was not empty
328     ${resp}    RequestsLibrary.Get Request    session    ${uri}
329     Run Keyword If    '${resp.status_code}'!='404'    Remove All Elements At URI    ${uri}
330
331 Add Elements To URI From File
332     [Arguments]    ${dest_uri}    ${data_file}    ${headers}=${headers}
333     ${body}    OperatingSystem.Get File    ${data_file}
334     ${resp}    RequestsLibrary.Put Request    session    ${dest_uri}    data=${body}    headers=${headers}
335     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
336
337 Add Elements To URI From File And Verify
338     [Arguments]    ${dest_uri}    ${data_file}    ${headers}=${headers}
339     ${body}    OperatingSystem.Get File    ${data_file}
340     ${resp}    RequestsLibrary.Put Request    session    ${dest_uri}    data=${body}    headers=${headers}
341     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
342     ${resp}    RequestsLibrary.Get Request    session    ${dest_uri}
343     Should Not Be Equal    ${resp.status_code}    404
344
345 Add Elements To URI And Verify
346     [Arguments]    ${dest_uri}    ${data_file}    ${headers}=${headers}
347     ${resp}    RequestsLibrary.Put Request    session    ${dest_uri}    ${data_file}    headers=${headers}
348     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
349     ${resp}    RequestsLibrary.Get Request    session    ${dest_uri}
350     Should Not Be Equal    ${resp.status_code}    404
351
352 Post Elements To URI From File
353     [Arguments]    ${dest_uri}    ${data_file}    ${headers}=${headers}
354     ${body}    OperatingSystem.Get File    ${data_file}
355     ${resp}    RequestsLibrary.Post Request    session    ${dest_uri}    data=${body}    headers=${headers}
356     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
357
358 Run Process With Logging And Status Check
359     [Arguments]    @{proc_args}
360     [Documentation]    Execute an OS command, log STDOUT and STDERR output and check exit code to be 0
361     ${result}=    Run Process    @{proc_args}
362     Log    ${result.stdout}
363     Log    ${result.stderr}
364     Should Be Equal As Integers    ${result.rc}    0
365     [Return]    ${result}
366
367 Get Data From URI
368     [Arguments]    ${session}    ${uri}    ${headers}=${NONE}
369     [Documentation]    Issue a GET request and return the data obtained or on error log the error and fail.
370     ...    Issues a GET request for ${uri} in ${session} using headers from
371     ...    ${headers}. If the request returns a HTTP error, fails. Otherwise
372     ...    returns the data obtained by the request.
373     ${response}=    RequestsLibrary.Get Request    ${session}    ${uri}    ${headers}
374     Builtin.Return_From_Keyword_If    ${response.status_code} == 200    ${response.text}
375     Builtin.Log    ${response.text}
376     Builtin.Fail    The request failed with code ${response.status_code}
377
378 No Content From URI
379     [Arguments]    ${session}    ${uri}    ${headers}=${NONE}
380     [Documentation]    Issue a GET request and return on error 404 (No content) or will fail and log the content.
381     ...    Issues a GET request for ${uri} in ${session} using headers from
382     ...    ${headers}. If the request returns a HTTP error, fails. Otherwise
383     ...    returns the data obtained by the request.
384     ${response}=    RequestsLibrary.Get Request    ${session}    ${uri}    ${headers}
385     Builtin.Return_From_Keyword_If    ${response.status_code} == 404
386     Builtin.Log    ${response.text}
387     Builtin.Fail    The request failed with code ${response.status_code}
388
389 Get Index From List Of Dictionaries
390     [Arguments]    ${dictionary_list}    ${key}    ${value}
391     [Documentation]    Extract index for the dictionary in a list that contains a key-value pair. Returns -1 if key-value is not found.
392     ${length}=    Get Length    ${dictionary_list}
393     ${index}=    Set Variable    -1
394     : FOR    ${i}    IN RANGE    ${length}
395     \    ${dictionary}=    Get From List    ${dictionary_list}    ${i}
396     \    Run Keyword If    """&{dictionary}[${key}]""" == """${value}"""    Set Test Variable    ${index}    ${i}
397     [Return]    ${index}
398
399 Check Item Occurrence
400     [Arguments]    ${string}    ${dictionary_item_occurrence}
401     [Documentation]    Check string for occurrences of items expressed in a list of dictionaries {item=occurrences}. 0 occurences means item is not present.
402     : FOR    ${item}    IN    @{dictionary_item_occurrence}
403     \    Should Contain X Times    ${string}    ${item}    &{dictionary_item_occurrence}[${item}]
404
405 Post Log Check
406     [Arguments]    ${uri}    ${body}    ${status_code}=200
407     [Documentation]    Post body to ${uri}, log response content, and check status
408     ${resp}=    RequestsLibrary.Post Request    session    ${uri}    ${body}
409     Log    ${resp.content}
410     Should Be Equal As Strings    ${resp.status_code}    ${status_code}
411     [Return]    ${resp}
412
413 Get Log File Name
414     [Arguments]    ${testtool}    ${testcase}=${EMPTY}
415     [Documentation]    Get the name of the suite sanitized to be usable as a part of filename.
416     ...    These names are used to constructs names of the log files produced
417     ...    by the testing tools so two suites using a tool wont overwrite the
418     ...    log files if they happen to run in one job.
419     ${name}=    BuiltIn.Evaluate    """${SUITE_NAME}""".replace(" ","-").replace("/","-").replace(".","-")
420     ${suffix}=    BuiltIn.Set_Variable_If    '${testcase}' != ''    --${testcase}    ${EMPTY}
421     [Return]    ${testtool}--${name}${suffix}.log
422
423 Set_User_Configurable_Variable_Default
424     [Arguments]    ${name}    ${value}
425     [Documentation]    Set a default value for an user configurable variable.
426     ...    This keyword is needed if your default value is calculated using
427     ...    a complex expression which needs BuiltIn.Evaluate or even more
428     ...    complex keywords. It sets the variable ${name} (the name of the
429     ...    variable MUST be specified WITHOUT the ${} syntactic sugar due
430     ...    to limitations of Robot Framework) to ${value} but only if the
431     ...    variable ${name} was not set previously. This keyword is intended
432     ...    for user configurable variables which are supposed to be set only
433     ...    with pybot -v; calling this keyword on a variable that was already
434     ...    set by another keyword will silently turn the call into a NOP and
435     ...    thus is a bug in the suite or resource trying to call this
436     ...    keyword.
437     # TODO: Figure out how to make the ${value} evaluation "lazy" (meaning
438     #    evaluating it only when the user did not set anything and thus the
439     #    default is needed). This might be needed to avoid potentially costly
440     #    keyword invocations when they are not needed. Currently no need for
441     #    this was identified, thus leaving it here as a TODO. Based on
442     #    comments the best approach would be to create another keyword that
443     #    expects a ScalarClosure in the place of ${value} and calls the
444     #    closure to get the value but only if the value is needed).
445     #    The best idea how to implement this "laziness" would be to have the
446     #    used to define another keyword that will be responsible for getting
447     #    the default value and then passing the name of this getter keyword
448     #    to this keyword. Then this keyword would call the getter (to obtain
449     #    the expensive default value) only if it discovers that this value
450     #    is really needed (because the variable is not set yet).
451     # TODO: Is the above TODO really necessary? Right now we don't have any
452     #    examples of "expensive default values" where to obtain the default
453     #    value is so expensive on resources (e.g. need to SSH somewhere to
454     #    check something) that we would want to skip the calculation if the
455     #    variable for which it is needed has a value already provided by the
456     #    user using "pybot -v" or something. One example would be
457     #    JAVA_HOME if it would be designed as user-configurable variable
458     #    (currently it is not; users can specify "use jdk7" or "use jdk8"
459     #    but not "use the jdk over there"; and there actually is no JAVA_HOME
460     #    present in the resource, rather the Java invocation command uses the
461     #    Java invocation with a full path). The default value of JAVA_HOME
462     #    has to be obtained by issuing commands on the SSH connection where
463     #    the resulting Java invocation command will be used (to check
464     #    multiple candidate paths until one that fits is found) and we could
465     #    skip all this checking if a JAVA_HOME was supplied by the user using
466     #    "pybot -v".
467     ${value}=    BuiltIn.Get_Variable_Value    \${${name}}    ${value}
468     BuiltIn.Set_Suite_Variable    \${${name}}    ${value}
469
470 Convert_To_Minutes
471     [Arguments]    ${time}
472     [Documentation]    Convert a Robot time string to an integer expressing the time in minutes, rounded up
473     ...    This is a wrapper around DateTime.Convert_Time which does not
474     ...    provide this functionality directly nor is even able to produce
475     ...    an integer directly. It is needed for RestPerfClient which
476     ...    cannot accept floats for its --timeout parameter and interprets
477     ...    the value supplied in this parameter in minutes.
478     ${seconds}=    DateTime.Convert_Time    ${time}    result_format=number
479     ${minutes}=    BuiltIn.Evaluate    int(math.ceil(${seconds}/60.0))    modules=math
480     [Return]    ${minutes}
481
482 Write Commands Until Expected Prompt
483     [Arguments]    ${cmd}    ${prompt}    ${timeout}=30s
484     [Documentation]    quick wrapper for Write and Read Until Prompt Keywords to make test cases more readable
485     SSHLibrary.Set Client Configuration    timeout=${timeout}
486     SSHLibrary.Write    ${cmd}
487     ${output}=    SSHLibrary.Read Until    ${prompt}
488     [Return]    ${output}
489
490 Install Package On Ubuntu System
491     [Arguments]    ${package_name}    ${system}=${TOOLS_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${password}=${TOOLS_SYSTEM_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=30s
492     [Documentation]    Keyword to install packages for testing to Ubuntu Mininet VM
493     Log    Keyword to install package to Mininet Ubuntu VM
494     Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
495     Flexible Mininet Login    user=${user}    password=${password}
496     Write    sudo apt-get install -y ${package_name}
497     Read Until    ${prompt}