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