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