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