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