dd7efed84279cfa3a5626a2f0d751bbb67b16dcb
[integration/test.git] / csit / libraries / Utils.robot
1 *** Settings ***
2 Library           SSHLibrary
3 Library           String
4 Library           DateTime
5 Library           Process
6 Library           ./UtilLibrary.py
7 Resource          KarafKeywords.robot
8 Variables           ../variables/Variables.py
9
10 *** Variables ***
11 # TODO: Introduce ${tree_size} and use instead of 1 in the next line.
12 ${start}          sudo mn --controller=remote,ip=${CONTROLLER} --topo tree,1 --switch ovsk,protocols=OpenFlow13
13 ${controller_index}    -1
14
15 *** Keywords ***
16 Start Suite
17     [Documentation]    Basic setup/cleanup work that can be done safely before any system
18     ...    is run.
19     [Arguments]    ${system}=${MININET}    ${user}=${MININET_USER}    ${password}=${MININET_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${timeout}=30s
20     Log    Start the test on the base edition
21     Clean Mininet System
22     ${mininet_conn_id}=    Open Connection    ${system}    prompt=${DEFAULT_LINUX_PROMPT}    timeout=${timeout}
23     Set Suite Variable    ${mininet_conn_id}
24     Flexible Mininet Login    user=${user}    password=${password}
25     Execute Command    sudo ovs-vsctl set-manager ptcp:6644
26     Write    ${start}
27     Read Until    mininet>
28
29 Start Mininet
30     [Arguments]    ${system}=${MININET}    ${cmd}=${start}    ${custom}=    ${user}=${MININET_USER}    ${password}=${MININET_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=30s
31     [Documentation]    Basic setup to start mininet with custom topology
32     Log    Start the test on the base edition
33     Clean Mininet System
34     ${mininet_conn_id}=    Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
35     Set Suite Variable    ${mininet_conn_id}
36     Flexible Mininet Login    user=${user}    password=${password}
37     Put File    ${custom}
38     Write    ${cmd}
39     Read Until    mininet>
40     [Return]    ${mininet_conn_id}
41
42 Connect To Controller Karaf
43     [Documentation]    Connect to the controller's karaf console.
44     ${esc}=    BuiltIn.Evaluate    chr(int(27))
45     ${prompt}=    Builtin.Set Variable    @${esc}[0m${esc}[34mroot${esc}[0m>
46     ${connection}=    SSHLibrary.Open_Connection    ${CONTROLLER}    port=${KARAF_SHELL_PORT}    prompt=${prompt}
47     Set Suite Variable    ${controller_index}    ${connection}
48     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}
49
50 Log Message To Controller Karaf
51     [Arguments]    ${message}
52     [Documentation]    Send a message into the controller's karaf log file.
53     # Background info: If there was no previous SSH connection, the "Get
54     # Connection" returns an information structure whose "index" field
55     # resolves to "None", and the "Switch Connection" below does not
56     # complain.
57     ${current}=    Get_Connection
58     ${connection}=    Set Variable    ${current.index}
59     BuiltIn.Run Keyword If    ${controller_index} <> -1    Switch Connection    ${controller_index}
60     BuiltIn.Run Keyword If    ${controller_index} == -1    Connect to Controller Karaf
61     SSHLibrary.Write    log:log "ROBOT MESSAGE: ${message}"
62     SSHLibrary.Read_Until_Prompt
63     Switch Connection    ${connection}
64
65 Stop Mininet
66     [Arguments]    ${mininet_conn_id}    ${prompt}=${DEFAULT_LINUX_PROMPT}
67     [Documentation]    Basic setup to stop/clean mininet
68     Switch Connection    ${mininet_conn_id}
69     SSHLibrary.Write    exit
70     Read Until    ${prompt}
71     Close Connection
72
73 Stop Suite
74     [Arguments]    ${prompt}=${DEFAULT_LINUX_PROMPT}
75     [Documentation]    Cleanup/Shutdown work that should be done at the completion of all
76     ...    tests
77     Log    Stop the test on the base edition
78     Switch Connection    ${mininet_conn_id}
79     Read
80     Write    exit
81     Read Until    ${prompt}
82     Close Connection
83
84 Ensure All Nodes Are In Response
85     [Arguments]    ${URI}    ${node_list}
86     [Documentation]    A GET is made to the supplied ${URI} and every item in the ${node_list}
87     ...    is verified to exist in the repsonse. This keyword currently implies that it's node
88     ...    specific but any list of strings can be given in ${node_list}. Refactoring of this
89     ...    to make it more generic should be done. (see keyword "Check For Elements At URI")
90     : FOR    ${node}    IN    @{node_list}
91     \    ${resp}    RequestsLibrary.Get    session    ${URI}
92     \    Should Be Equal As Strings    ${resp.status_code}    200
93     \    Should Contain    ${resp.content}    ${node}
94
95 Check Nodes Stats
96     [Arguments]    ${node}
97     [Documentation]    A GET on the /node/${node} API is made and specific flow stat
98     ...    strings are checked for existence.
99     ${resp}    RequestsLibrary.Get    session    ${OPERATIONAL_NODES_API}/node/${node}
100     Should Be Equal As Strings    ${resp.status_code}    200
101     Should Contain    ${resp.content}    flow-capable-node-connector-statistics
102     Should Contain    ${resp.content}    flow-table-statistics
103
104 Check That Port Count Is Ok
105     [Arguments]    ${node}    ${count}
106     [Documentation]    A GET on the /port API is made and the specified port ${count} is
107     ...    verified. A more generic Keyword "Check For Specific Number Of Elements At URI"
108     ...    also does this work and further consolidation should be done.
109     ${resp}    RequestsLibrary.Get    session    ${REST_CONTEXT}/${CONTAINER}/port
110     Log    ${resp.content}
111     Should Be Equal As Strings    ${resp.status_code}    200
112     Should Contain X Times    ${resp.content}    ${node}    ${count}
113
114 Check For Specific Number Of Elements At URI
115     [Arguments]    ${uri}    ${element}    ${expected_count}
116     [Documentation]    A GET is made to the specified ${URI} and the specific count of a
117     ...    given element is done (as supplied by ${element} and ${expected_count})
118     ${resp}    RequestsLibrary.Get    session    ${uri}
119     Log    ${resp.content}
120     Should Be Equal As Strings    ${resp.status_code}    200
121     Should Contain X Times    ${resp.content}    ${element}    ${expected_count}
122
123 Check For Elements At URI
124     [Arguments]    ${uri}    ${elements}
125     [Documentation]    A GET is made at the supplied ${URI} and every item in the list of
126     ...    ${elements} is verified to exist in the response
127     ${resp}    RequestsLibrary.Get    session    ${uri}
128     Log    ${resp.content}
129     Should Be Equal As Strings    ${resp.status_code}    200
130     : FOR    ${i}    IN    @{elements}
131     \    Should Contain    ${resp.content}    ${i}
132
133 Check For Elements Not At URI
134     [Arguments]    ${uri}    ${elements}
135     [Documentation]    A GET is made at the supplied ${URI} and every item in the list of
136     ...    ${elements} is verified to NOT exist in the response
137     ${resp}    RequestsLibrary.Get    session    ${uri}
138     Log    ${resp.content}
139     Should Be Equal As Strings    ${resp.status_code}    200
140     : FOR    ${i}    IN    @{elements}
141     \    Should Not Contain    ${resp.content}    ${i}
142
143 Clean Mininet System
144     [Arguments]    ${system}=${MININET}
145     Run Command On Mininet    ${system}    sudo mn -c
146     Run Command On Mininet    ${system}    sudo ps -elf | egrep 'usr/local/bin/mn' | egrep python | awk '{print "sudo kill -9",$4}' | sh
147
148 Clean Up Ovs
149     [Arguments]    ${system}=${MININET}
150     [Documentation]    Cleans up the OVS instance and remove any existing common known bridges.
151     ${output}=    Run Command On Mininet    ${system}    sudo ovs-vsctl list-br
152     Log    ${output}
153     : FOR    ${i}    IN    ${output}
154     \    Run Command On Mininet    ${system}    sudo ovs-vsctl --if-exists del-br ${i}
155     Run Command On Mininet    ${system}    sudo ovs-vsctl del-manager
156
157 Extract Value From Content
158     [Arguments]    ${content}    ${index}    ${strip}=nostrip
159     [Documentation]    Will take the given response content and return the value at the given index as a string
160     ${value}=    Get Json Value    ${content}    ${index}
161     ${value}=    Convert To String    ${value}
162     ${value}=    Run Keyword If    '${strip}' == 'strip'    Strip Quotes    ${value}
163     [Return]    ${value}
164
165 Get Process ID Based On Regex On Remote System
166     [Arguments]    ${system}    ${regex_string_to_match_on}    ${user}=${MININET_USER}    ${password}=${EMPTY}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=30s
167     [Documentation]    Uses ps to find a process that matches the supplied regex. Returns the PID of that process
168     ...    The ${regex_string_to_match_on} should produce a unique process otherwise the PID returned may not be
169     ...    the expected PID
170     # doing the extra -v grep in this command to exclude the grep process itself from the output
171     ${cmd}=    Set Variable    ps -elf | grep -v grep | grep ${regex_string_to_match_on} | awk '{print $4}'
172     ${output}=    Run Command On Remote System    ${system}    ${cmd}    user=${user}    password=${password}    prompt=${prompt}    prompt_timeout=${prompt_timeout}
173     # ${output} contains the system prompt and all we want is the value of the number
174     ${pid}=    Fetch From Left    ${output}    \r
175     [Return]    ${pid}
176     # TODO: Get Process * keywords have perhaps non-standard default credentials.
177     # ...    Should there be * On Mininet and * On Controller specializations?
178
179 Get Process Thread Count On Remote System
180     [Arguments]    ${system}    ${pid}    ${user}=${MININET_USER}    ${password}=${EMPTY}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=30s
181     [Documentation]    Executes the ps command to retrieve the lightweight process (aka thread) count.
182     ${cmd}=    Set Variable    ps --no-headers -o nlwp ${pid}
183     ${output}=    Run Command On Remote System    ${system}    ${cmd}    user=${user}    password=${password}    prompt=${prompt}    prompt_timeout=${prompt_timeout}
184     # ${output} contains the system prompt and all we want is the value of the number
185     ${thread_count}=    Fetch From Left    ${output}    \r
186     [Return]    ${thread_count}
187
188 Strip Quotes
189     [Arguments]    ${string_to_strip}
190     [Documentation]    Will strip ALL quotes from given string and return the new string
191     ${string_to_return}=    Replace String    ${string_to_strip}    "    \    count=-1
192     [Return]    ${string_to_return}
193
194 Flexible SSH Login
195     [Arguments]    ${user}    ${password}=${EMPTY}    ${delay}=0.5s
196     [Documentation]    On active SSH session: if given non-empty password, do Login, else do Login With Public Key.
197     ${pwd_length} =    BuiltIn.Get Length    ${password}
198     # ${pwd_length} is guaranteed to be an integer, so we are safe to evaluate it as Python expression.
199     BuiltIn.Run Keyword And Return If    ${pwd_length} > 0    SSHLibrary.Login    ${user}    ${password}    delay=${delay}
200     BuiltIn.Run Keyword And Return    SSHLibrary.Login With Public Key    ${user}    ${USER_HOME}/.ssh/${SSH_KEY}    ${KEYFILE_PASS}   delay=${delay}
201
202 Flexible Mininet Login
203     [Arguments]    ${user}=${MININET_USER}    ${password}=${MININET_PASSWORD}    ${delay}=0.5s
204     [Documentation]    Call Flexible SSH Login, but with default values suitable for Mininet machine.
205     BuiltIn.Run Keyword And Return    Flexible SSH Login    user=${user}    password=${password}    delay=${delay}
206
207 Flexible Controller Login
208     [Arguments]    ${user}=${CONTROLLER_USER}    ${password}=${CONTROLLER_PASSWORD}    ${delay}=0.5s
209     [Documentation]    Call Flexible SSH Login, but with default values suitable for Controller machine.
210     BuiltIn.Run Keyword And Return    Flexible SSH Login    user=${user}    password=${password}    delay=${delay}
211
212 Run Command On Remote System
213     [Arguments]    ${system}    ${cmd}    ${user}=${MININET_USER}    ${password}=${EMPTY}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=30s
214     [Documentation]    Reduces the common work of running a command on a remote system to a single higher level
215     ...    robot keyword, taking care to log in with a public key and. The command given is written
216     ...    and the output returned. No test conditions are checked.
217     Log    Attempting to execute ${cmd} on ${system} by ${user} with ${keyfile_pass} and ${prompt}
218     ${conn_id}=    SSHLibrary.Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
219     Flexible SSH Login    ${user}    ${password}
220     SSHLibrary.Write    ${cmd}
221     ${output}=    SSHLibrary.Read Until    ${prompt}
222     SSHLibrary.Close Connection
223     Log    ${output}
224     [Return]    ${output}
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 Run Command On Mininet
233     [Arguments]    ${system}=${MININET}    ${cmd}=echo    ${user}=${MININET_USER}    ${password}=${MININET_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=30s
234     [Documentation]    Call Run Comand On Remote System, but with default values suitable for Mininet machine.
235     BuiltIn.Run Keyword And Return    Run Command On Remote System    ${system}    ${cmd}    user=${user}    password=${password}    prompt=${prompt}    prompt_timeout=${prompt_timeout}
236
237 Run Command On Controller
238     [Arguments]    ${system}=${CONTROLLER}    ${cmd}=echo    ${user}=${CONTROLLER_USER}    ${password}=${CONTROLLER_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=30s
239     [Documentation]    Call Run Comand On Remote System, but with default values suitable for Controller machine.
240     BuiltIn.Run Keyword And Return    Run Command On Remote System    ${system}    ${cmd}    user=${user}    password=${password}    prompt=${prompt}    prompt_timeout=${prompt_timeout}
241
242 Verify File Exists On Remote System
243     [Arguments]    ${system}    ${file}    ${user}=${MININET_USER}    ${password}=${MININET_PASSWORD}    ${prompt}=${DEFAULT_LINUX_PROMPT}    ${prompt_timeout}=5s
244     [Documentation]    Will create connection with public key and will PASS if the given ${file} exists,
245     ...    otherwise will FAIL
246     ${conn_id}=    Open Connection    ${system}    prompt=${prompt}    timeout=${prompt_timeout}
247     Flexible SSH Login    ${user}    ${password}
248     SSHLibrary.File Should Exist    ${file}
249     Close Connection
250
251 Verify Controller Is Not Dead
252     [Arguments]    ${controller_ip}=${CONTROLLER}
253     [Documentation]    Will execute any tests to verify the controller is not dead. Some checks are
254     ...    Out Of Memory Execptions.
255     Check Karaf Log File Does Not Have Messages    ${controller_ip}    java.lang.OutOfMemoryError
256     # TODO: Should Verify Controller * keywords also accept user, password, prompt and karaf_log arguments?
257
258 Verify Controller Has No Null Pointer Exceptions
259     [Arguments]    ${controller_ip}=${CONTROLLER}
260     [Documentation]    Will execute any tests to verify the controller is not having any null pointer eceptions.
261     Check Karaf Log File Does Not Have Messages    ${controller_ip}    java.lang.NullPointerException
262
263 Get Epoch Time
264     [Arguments]    ${time}
265     [Documentation]    Get the Epoc time from MM/DD/YYYY HH:MM:SS
266     ${epoch_time}=    Convert Date    ${time}    epoch    exclude_milles=True    date_format=%m/%d/%Y %H:%M:%S
267     ${epoch_time}=    Convert To Integer    ${epoch_time}
268     [Return]    ${epoch_time}
269
270 Remove Space on String
271     [Arguments]    ${str}    ${count}=-1
272     [Documentation]    Remove the empty space from given string.count is optional,if its given
273     ...    that many occurence of space will be removed from left
274     ${x}=    Convert To String    ${str}
275     ${x}=    Replace String    ${str}    ${SPACE}    ${EMPTY}    count=${count}
276     [Return]    ${x}
277
278 Split Value from String
279     [Arguments]    ${str}    ${splitter}
280     [Documentation]    Split the String based on given splitter and return as list
281     @{x}=    Split String    ${str}    ${splitter}
282     [Return]    @{x}
283
284 Concatenate the String
285     [Arguments]    ${str1}    ${str2}
286     [Documentation]    Catenate the two non-string objects and return as String
287     ${str1}=    Convert to String    ${str1}
288     ${str2}=    Convert to String    ${str2}
289     ${output}=    Catenate    ${str1}    ${str2}
290     [Return]    ${output}
291
292 Remove All Elements At URI
293     [Arguments]    ${uri}
294     ${resp}    RequestsLibrary.Delete    session    ${uri}
295     Should Be Equal As Strings    ${resp.status_code}    200
296
297 Add Elements To URI From File
298     [Arguments]    ${dest_uri}    ${data_file}
299     ${body}    OperatingSystem.Get File    ${data_file}
300     ${resp}    RequestsLibrary.Put    session    ${dest_uri}    data=${body}    headers=${headers}
301     Should Be Equal As Strings    ${resp.status_code}    200
302
303 Post Elements To URI From File
304     [Arguments]    ${dest_uri}    ${data_file}
305     ${body}    OperatingSystem.Get File    ${data_file}
306     ${resp}    RequestsLibrary.Post    session    ${dest_uri}    data=${body}    headers=${headers}
307     Should Be Equal As Strings    ${resp.status_code}    200
308
309 Run Process With Logging And Status Check
310     [Arguments]    @{proc_args}
311     [Documentation]    Execute an OS command, log STDOUT and STDERR output and check exit code to be 0
312     ${result}=    Run Process    @{proc_args}
313     Log    ${result.stdout}
314     Log    ${result.stderr}
315     Should Be Equal As Integers    ${result.rc}    0
316     [Return]    ${result}