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