204330f4a1644f1cb77f87e55db2e1dffb5afea8
[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 Extract Value From Content
99     [Arguments]    ${content}    ${index}    ${strip}=nostrip
100     [Documentation]    Will take the given response content and return the value at the given index as a string
101     ${value}=    Get Json Value    ${content}    ${index}
102     ${value}=    Convert To String    ${value}
103     ${value}=    Run Keyword If    '${strip}' == 'strip'    Strip Quotes    ${value}
104     [Return]    ${value}
105
106 Get Process ID Based On Regex On Remote System
107     [Documentation]    Uses ps to find a process that matches the supplied regex.  Returns the PID of that process
108     ...    The ${regex_string_to_match_on} should produce a unique process otherwise the PID returned may not be
109     ...    the expected PID
110     [Arguments]    ${remote_system}    ${regex_string_to_match_on}
111     # doing the extra -v grep in this command to exclude the grep process itself from the output
112     ${output}=    Run Command On Remote System    ${remote_system}    ps -elf | grep -v grep | grep ${regex_string_to_match_on} | awk '{print $4}'
113     # ${output} contains the system prompt and all we want is the value of the number
114     ${pid}=    Fetch From Left    ${output}    \r
115     [Return]    ${pid}
116
117 Get Process Thread Count On Remote System
118     [Documentation]    Executes the ps command to retrieve the lightweight process (aka thread) count.
119     [Arguments]     ${remote_system}    ${pid}
120     ${output}=    Run Command On Remote System    ${remote_system}    ps --no-headers -o nlwp ${pid}
121     # ${output} contains the system prompt and all we want is the value of the number
122     ${thread_count}=    Fetch From Left    ${output}    \r
123     [Return]    ${thread_count}
124
125 Strip Quotes
126     [Arguments]    ${string_to_strip}
127     [Documentation]    Will strip ALL quotes from given string and return the new string
128     ${string_to_return}=    Replace String    ${string_to_strip}    "    \    count=-1
129     [Return]    ${string_to_return}
130
131 Run Command On Remote System
132     [Arguments]    ${remote_system}    ${cmd}    ${user}=${MININET_USER}    ${prompt}=${LINUX_PROMPT}    ${prompt_timeout}=30s
133     [Documentation]    Reduces the common work of running a command on a remote system to a single higher level robot keyword,
134     ...    taking care to log in with a public key and. The command given is written and the output returned. No test conditions
135     ...    are checked.
136     Log    Attempting to execute ${cmd} on ${remote_system}
137     ${conn_id}=    SSHLibrary.Open Connection    ${remote_system}    prompt=${prompt}    timeout=${prompt_timeout}
138     Login With Public Key    ${user}    ${USER_HOME}/.ssh/id_rsa    any
139     SSHLibrary.Write    ${cmd}
140     ${output}=    SSHLibrary.Read Until    ${LINUX_PROMPT}
141     SSHLibrary.Close Connection
142     Log    ${output}
143     [Return]    ${output}
144
145 Verify File Exists On Remote System
146     [Arguments]    ${remote_system}    ${file}    ${user}=${MININET_USER}    ${prompt}=${LINUX_PROMPT}    ${prompt_timeout}=5s
147     [Documentation]    Will create connection with public key and will PASS if the given ${file} exists, otherwise will FAIL
148     ${conn_id}=    Open Connection    ${remote_system}    prompt=${prompt}    timeout=${prompt_timeout}
149     Login With Public Key    ${user}    ${USER_HOME}/.ssh/id_rsa    any
150     SSHLibrary.File Should Exist    ${file}
151     Close Connection
152
153 Verify Controller Is Not Dead
154     [Arguments]    ${controller_ip}=${CONTROLLER}
155     [Documentation]    Will execute any tests to verify the controller is not dead. Some checks are
156     ...    Out Of Memory Execptions.
157     Check Karaf Log File Does Not Have Messages    ${controller_ip}    java.lang.OutOfMemoryError
158
159 Get Epoch Time
160     [Arguments]    ${time}
161     [Documentation]    Get the Epoc time from MM/DD/YYYY HH:MM:SS
162     ${epoch_time}=    Convert Date    ${time}    epoch    exclude_milles=True    date_format=%m/%d/%Y %H:%M:%S
163     ${epoch_time}=    Convert To Integer    ${epoch_time}
164     [Return]    ${epoch_time}
165
166 Remove Space on String
167     [Arguments]    ${str}    ${count}=-1
168     [Documentation]    Remove the empty space from given string.count is optional,if its given
169     ...    that many occurence of space will be removed from left
170     ${x}=    Convert To String    ${str}
171     ${x}=    Replace String    ${str}    ${SPACE}    ${EMPTY}    count=${count}
172     [Return]    ${x}
173
174 Split Value from String
175     [Arguments]    ${str}    ${splitter}
176     [Documentation]    Split the String based on given splitter and return as list
177     @{x}=    Split String    ${str}    ${splitter}
178     [Return]    @{result}
179
180 Concatenate the String
181     [Arguments]    ${str1}    ${str2}
182     [Documentation]    Catenate the two non-string objects and return as String
183     ${str1}=    Convert to String    ${str1}
184     ${str2}=    Convert to String    ${str2}
185     ${output}=    Catenate    ${str1}    ${str2}
186     [Return]    ${output}