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