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