COE-43: Check for stale veth ports
[integration/test.git] / csit / libraries / Coe.robot
1 *** Settings ***
2 Library           BuiltIn
3 Library           SSHLibrary
4 Library           String
5 Resource          DataModels.robot
6 Resource          Genius.robot
7 Resource          OVSDB.robot
8 Resource          SSHKeywords.robot
9 Resource          Utils.robot
10 Resource          ../variables/netvirt/Variables.robot
11 Resource          ../variables/Variables.robot
12 Resource          VpnOperations.robot
13 Variables         ../variables/coe/Modules.py
14 Variables         ../variables/netvirt/Modules.py
15 Resource          ToolsSystem.robot
16
17 *** Variables ***
18 ${BUSY_BOX}       ${CURDIR}/../variables/coe/busy-box.yaml
19 ${CNI_BINARY_FILE}    /opt/cni/bin/odlovs-cni
20 ${CONFIG_FILE}    /etc/cni/net.d/odlovs-cni.conf
21 ${CONFIG_FILE_TEMPLATE}    ${CURDIR}/../variables/coe/odlovs-cni.conf.j2
22 ${HOST_INVENTORY}    ${CURDIR}/../variables/coe/hosts.yaml
23 ${K8s_MASTER_IP}    ${TOOLS_SYSTEM_1_IP}
24 ${HOSTS_FILE_TEMPLATE}    ${CURDIR}/../variables/coe/minions_template.yaml
25 ${NODE_READY_STATUS}    \\sReady    # The check using this variable should not mess up with NotReady
26 ${PLAYBOOK_FILE}    ${CURDIR}/../variables/coe/coe_play.yaml
27 ${POD_RUNNING_STATUS}    \\sRunning
28 ${VARIABLES_PATH}    ${CURDIR}/../variables/coe
29 ${WATCHER_COE}    ${CURDIR}/../variables/coe/coe.yaml
30 @{COE_DIAG_SERVICES}    OPENFLOW    IFM    ITM    DATASTORE    ELAN    OVSDB
31
32 *** Keywords ***
33 Coe Suite Setup
34     [Documentation]    COE project requires start suite to be executed only for the first test suite.This keyword find the current suite,compares it with the stored first suite value and executes Coe.Start suite only if the cuurent suite is equal to the first suite.
35     ToolsSystem.Get Tools System Nodes Data
36     Coe.Set Connection ids and Bridge
37     Coe.Derive Coe Data Models
38     ${current suite}    ${suite names updated}    Extract current suite name
39     ${first_suite} =    Set Variable    ${suite names updated[0]}
40     ${status} =    BuiltIn.Evaluate    '${first_suite}' == '${current suite}'
41     Run Keyword If    '${status}' == 'True'    Coe.Start Suite
42
43 Start Suite
44     [Documentation]    Suite setup keyword.
45     Coe.Configuration Playbook
46     Coe.Verify Config Files
47     Coe.Verify Watcher Is Running
48     BuiltIn.Wait Until Keyword Succeeds    40s    2s    Coe.Check Node Status Is Ready
49     Coe.Label Nodes
50     BuiltIn.Wait Until Keyword Succeeds    60    2    ClusterManagement.Check Status Of Services Is OPERATIONAL    @{COE_DIAG_SERVICES}
51     BuiltIn.Wait Until Keyword Succeeds    85    2    Genius.Verify Tunnel Status as UP    default-transport-zone
52
53 Set Connection ids and Bridge
54     [Documentation]    Sets the connection ids for all the nodes and get the bridge from configuration file .
55     : FOR    ${conn_id}    IN    @{TOOLS_SYSTEM_ALL_CONN_IDS}
56     \    SSHLibrary.Switch Connection    ${conn_id}
57     \    SSHKeywords.Flexible_SSH_Login    ${DEFAULT_USER}    ${DEFAULT_PASSWORD}
58     ${file} =    OperatingSystem.Get File    ${CONFIG_FILE_TEMPLATE}
59     ${ovs bridge output}    ${bridge} =    BuiltIn.Should Match Regexp    ${file}    "ovsBridge": "(\\w.*)"
60     BuiltIn.Set Suite Variable    ${bridge}
61
62 Configuration Playbook
63     [Documentation]    Ansible playbook which does all basic configuration for kubernetes nodes.
64     ${playbook minions}    ${playbook hosts}    ${host file}    Modifying templates in playbook
65     ${playbook} =    OperatingSystem.Get File    ${PLAYBOOK_FILE}
66     ${playbook} =    String.Replace String    ${playbook}    coe-hosts    ${playbook hosts}
67     ${playbook} =    String.Replace String    ${playbook}    coe-minions    ${playbook minions}
68     OperatingSystem.Create File    ${PLAYBOOK_FILE}    ${playbook}
69     OperatingSystem.Create File    ${USER_HOME}/hosts.yaml    ${host file}
70     ${watcher} =    OperatingSystem.Get File    ${WATCHER_COE}
71     ${watcher} =    String.Replace String    ${watcher}    odlip    ${ODL_SYSTEM_IP}
72     ${watcher} =    String.Replace String    ${watcher}    port    ${RESTCONFPORT}
73     OperatingSystem.Create File    ${WATCHER_COE}    ${watcher}
74     SSHKeywords.Copy_File_To_Remote_System    ${K8s_MASTER_IP}    ${WATCHER_COE}    ${USER_HOME}
75     OperatingSystem.Copy File    ${PLAYBOOK_FILE}    ${USER_HOME}
76     ${branch_ref_spec} =    BuiltIn.Catenate    SEPARATOR=    refs/heads/    ${GERRIT_BRANCH}
77     ${gerrit_ref_spec} =    BuiltIn.Set Variable If    '${GERRIT_PROJECT}' != 'coe'    ${branch_ref_spec}    ${GERRIT_REFSPEC}
78     Run Coe Playbook    ${gerrit_ref_spec}
79
80 Run Coe Playbook
81     [Arguments]    ${gerrit_ref_spec}
82     ${play_output} =    OperatingSystem.Run    ansible-playbook -v ${USER_HOME}/coe_play.yaml -i ${USER_HOME}/hosts.yaml --extra-vars '{"gerrit_branch":"FETCH_HEAD","gerrit_refspec":"${gerrit_ref_spec}"}'
83     BuiltIn.Log    ${play_output}
84
85 Modifying templates in playbook
86     ${inventory} =    OperatingSystem.Get File    ${HOST_INVENTORY}
87     ${template} =    OperatingSystem.Get File    ${HOSTS_FILE_TEMPLATE}
88     ${template} =    String.Replace String    ${template}    minion_ip    ${TOOLS_SYSTEM_ALL_IPS[0]}
89     @{minions}    Create List    coe-minion
90     ${hosts}    Set Variable    coe-master:
91     : FOR    ${i}    IN RANGE    1    ${NUM_TOOLS_SYSTEM}
92     \    Append To List    ${minions}    coe-minion${i}
93     \    ${hosts} =    Catenate    ${hosts}    coe-minion${i}:
94     ${hosts} =    Replace String Using Regexp    ${hosts}    :$    ${EMPTY}
95     ${hosts} =    Remove Space on String    ${hosts}
96     ${minion hosts} =    Replace String Using Regexp    ${hosts}    ^[\\w-]+:    ${EMPTY}
97     : FOR    ${i}    IN RANGE    1    ${NUM_TOOLS_SYSTEM}
98     \    ${j} =    Evaluate    ${i}+1
99     \    ${template} =    String.Replace String    ${template}    ${minions[${i}-1]}    ${minions[${i}]}
100     \    ${template} =    String.Replace String    ${template}    ${TOOLS_SYSTEM_ALL_IPS[${i}-1]}    ${TOOLS_SYSTEM_ALL_IPS[${i}]}
101     \    ${template} =    String.Replace String    ${template}    192.168.50.1${i}    192.168.50.1${j}
102     \    ${template} =    String.Replace String    ${template}    10.11.${i}.0/24    10.11.${j}.0/24
103     \    ${template} =    String.Replace String    ${template}    10.11.${i}.1    10.11.${j}.1
104     \    Append To File    ${HOST_INVENTORY}    ${template}
105     ${host file} =    OperatingSystem.Get File    ${HOST_INVENTORY}
106     ${host file} =    String.Replace String    ${host file}    master_ip    ${TOOLS_SYSTEM_ALL_IPS[0]}
107     ${host file} =    String.Replace String    ${host file}    odl_ip    ${ODL_SYSTEM_IP}
108     ${host file} =    String.Replace String    ${host file}    mport    ${OVSDBPORT}
109     ${host file} =    String.Replace String    ${host file}    cport    ${ODL_OF_PORT_6653}
110     ${host file} =    String.Replace String    ${host file}    filepath    ${CONFIG_FILE_TEMPLATE}
111     ${host file} =    String.Replace String    ${host file}    yamlpath    ${USER_HOME}/coe.yaml
112     log    ${host file}
113     [Return]    ${minion hosts}    ${hosts}    ${host file}
114
115 Verify Config Files
116     [Documentation]    Checks if the configuration files are present in all nodes
117     : FOR    ${nodes}    IN    @{TOOLS_SYSTEM_ALL_IPS}
118     \    Utils.Verify File Exists On Remote System    ${nodes}    ${CONFIG_FILE}
119     : FOR    ${nodes}    IN    @{TOOLS_SYSTEM_ALL_IPS}
120     \    Utils.Verify File Exists On Remote System    ${nodes}    ${CNI_BINARY_FILE}
121
122 Verify Watcher Is Running
123     [Documentation]    Checks if watcher is running in the background
124     ${watcher status} =    Utils.Run Command On Remote System    ${K8s_MASTER_IP}    ps -ef | grep watcher
125     BuiltIn.Should Match Regexp    ${watcher status}    .* watcher odl
126
127 Check Node Status Is Ready
128     [Documentation]    Checks the status of nodes.This keyword is repeated until the status of all nodes is Ready
129     ${nodes} =    Utils.Run Command On Remote System And Log    ${K8s_MASTER_IP}    kubectl get nodes    ${DEFAULT_USER}    ${DEFAULT_PASSWORD}    ${DEFAULT_LINUX_PROMPT_STRICT}
130     ${node_status} =    String.Get Lines Matching Regexp    ${nodes}    ${NODE_READY_STATUS}    partial_match=True
131     ${lines_containing_ready} =    String.Get Line Count    ${node_status}
132     BuiltIn.Should Be Equal As Strings    ${lines_containing_ready}    ${NUM_TOOLS_SYSTEM}
133
134 Label Nodes
135     [Documentation]    Create labels for minions so that random allocation of pods to minions is avoided
136     ${i} =    BuiltIn.Set Variable    1
137     ${get nodes} =    Utils.Run Command On Remote System And Log    ${K8s_MASTER_IP}    kubectl get nodes
138     @{get nodes} =    String.Split To Lines    ${get nodes}    2
139     : FOR    ${status}    IN    @{get nodes}
140     \    ${minion} =    BuiltIn.Should Match Regexp    ${status}    ^\\w+-.*-\\d+
141     \    Utils.Run Command On Remote System And Log    ${K8s_MASTER_IP}    kubectl label nodes ${minion} disktype=ss${i}
142     \    ${i} =    BuiltIn.Evaluate    ${i}+1
143     Utils.Run Command On Remote System And Log    ${K8s_MASTER_IP}    kubectl get nodes --show-labels
144
145 Derive Coe Data Models
146     [Documentation]    Data models is created by integrating netvirt and coe data models which is given as input to get the model dumps
147     : FOR    ${models}    IN    @{netvirt_data_models}
148     \    Collections.Append To List    ${coe_data_models}    ${models}
149
150 Check Pod Status Is Running
151     [Documentation]    Checks the status of pods.This keyword is repeated until the status of all pods is Running
152     ${pods} =    Utils.Run Command On Remote System And Log    ${K8s_MASTER_IP}    kubectl get pods -o wide    ${DEFAULT_USER}    ${DEFAULT_PASSWORD}    ${DEFAULT_LINUX_PROMPT_STRICT}
153     @{cluster} =    String.Split To Lines    ${pods}    1
154     : FOR    ${pod}    IN    @{cluster}
155     \    BuiltIn.Should Match Regexp    ${pod}    ${POD_RUNNING_STATUS}
156
157 Tear Down
158     [Documentation]    Test teardown to get dumpflows,ovsconfig,model dump,node status,pod status and to dump config files \ and delete pods.
159     : FOR    ${conn_id}    IN    @{TOOLS_SYSTEM_ALL_CONN_IDS}
160     \    OVSDB.Get DumpFlows And Ovsconfig    ${conn_id}    ${bridge}
161     BuiltIn.Run Keyword And Ignore Error    DataModels.Get Model Dump    ${ODL_SYSTEM_IP}    ${coe_data_models}
162     Coe.DumpConfig File
163     Utils.Run Command On Remote System And Log    ${K8s_MASTER_IP}    kubectl get nodes    ${DEFAULT_USER}    ${DEFAULT_PASSWORD}    ${DEFAULT_LINUX_PROMPT_STRICT}
164     Utils.Run Command On Remote System And Log    ${K8s_MASTER_IP}    kubectl get pods -o wide    ${DEFAULT_USER}    ${DEFAULT_PASSWORD}    ${DEFAULT_LINUX_PROMPT_STRICT}
165     Coe.Delete Pods
166
167 Delete Pods
168     [Documentation]    Waits till the keyword delete status succeeds implying that all pods created have been deleted
169     ${get pods} =    Utils.Run Command On Remote System    ${K8s_MASTER_IP}    kubectl get pods -o wide
170     @{get pods} =    String.Split To Lines    ${get pods}    1
171     : FOR    ${status}    IN    @{get pods}
172     \    ${pod_name} =    BuiltIn.Should Match Regexp    ${status}    ^\\w+-\\w+
173     \    Utils.Run Command On Remote System    ${K8s_MASTER_IP}    kubectl delete pods ${pod_name}
174     BuiltIn.Wait Until Keyword Succeeds    60s    3s    Coe.Check If Pods Are Terminated
175     Coe.Check For Stale veth Ports
176
177 Check If Pods Are Terminated
178     [Documentation]    Checks if the pods created have been terminated.The keyword is repeated until the pods are deleted
179     ${status} =    Utils.Run Command On Remote System    ${K8s_MASTER_IP}    kubectl get pods -o wide    ${DEFAULT_USER}    ${DEFAULT_PASSWORD}    ${DEFAULT_LINUX_PROMPT_STRICT}
180     ...    ${DEFAULT_TIMEOUT}    return_stdout=False    return_stderr=True
181     BuiltIn.Should Contain    ${status}    No resources
182
183 Dump Config File
184     [Documentation]    Logs the configuration files present in all nodes
185     : FOR    ${nodes}    IN    @{TOOLS_SYSTEM_ALL_IPS}
186     \    Utils.Run Command On Remote System And Log    ${nodes}    cat ${CONFIG_FILE}
187
188 Stop Suite
189     [Documentation]    Suite teardown keyword
190     Coe.Collect Watcher Log
191     Coe.Collect Journalctl Log
192     Coe.Stop_Watcher
193     Coe.Kube_reset
194     SSHLibrary.Close All Connections
195
196 Collect Watcher Log
197     [Documentation]    Watcher running in background logs into watcher.out which is copied to ${JENKINS_WORKSPACE}/archives/watcher.log
198     SSHLibrary.Switch Connection    ${TOOLS_SYSTEM_ALL_CONN_IDS[0]}
199     SSHLibrary.Get File    /tmp/watcher.out    ${JENKINS_WORKSPACE}/archives/watcher.log
200
201 Collect Journalctl Log
202     [Documentation]    Logs of the command journalctl -u kubelet is copied to ${JENKINS_WORKSPACE}/archives/journal.log
203     Utils.Run Command On Remote System And Log    ${K8s_MASTER_IP}    sudo journalctl -u kubelet > ${USER_HOME}/journal.txt
204     SSHLibrary.Switch Connection    ${TOOLS_SYSTEM_ALL_CONN_IDS[0]}
205     SSHLibrary.Get File    ${USER_HOME}/journal.txt    ${JENKINS_WORKSPACE}/archives/journalctl.log
206
207 Stop Watcher
208     [Documentation]    Kill the watcher running at the background after completion of tests cases
209     ${watcher status} =    Utils.Run Command On Remote System    ${K8s_MASTER_IP}    ps -ef | grep watcher
210     ${watcher}    ${pid} =    BuiltIn.Should Match Regexp    ${watcher status}    \\w+\\s+(\\d+).*watcher odl
211     Utils.Run Command On Remote System    ${K8s_MASTER_IP}    kill -9 ${pid}
212
213 Kube reset
214     [Documentation]    Reset K8s to clear up all stale entries
215     : FOR    ${nodes}    IN    @{TOOLS_SYSTEM_ALL_IPS}
216     \    ${kube} =    Utils.Run Command On Remote System And Log    ${nodes}    sudo kubeadm reset
217     \    BuiltIn.Should Contain    ${kube}    Stopping the kubelet service.
218
219 Create Pods
220     [Arguments]    ${label}    ${yaml}    ${name}
221     [Documentation]    Creates pods using the labels of the nodes and busy box names passed as arguments.
222     ${busybox} =    OperatingSystem.Get File    ${BUSY_BOX}
223     ${busybox} =    String.Replace String    ${busybox}    string    ${label}
224     ${busybox} =    String.Replace String    ${busybox}    busyboxname    ${name}
225     OperatingSystem.Create File    ${VARIABLES_PATH}/${yaml}    ${busybox}
226     SSHKeywords.Move_file_To_Remote_System    ${K8s_MASTER_IP}    ${VARIABLES_PATH}/${yaml}    ${USER_HOME}
227     Utils.Run Command On Remote System And Log    ${K8s_MASTER_IP}    kubectl create -f ${yaml}
228
229 Collect Pod Names and Ping
230     [Documentation]    This keyword collects the pod names and checks connectivity between each and every pod with respect to one another.
231     SSHLibrary.Switch Connection    ${TOOLS_SYSTEM_ALL_CONN_IDS[0]}
232     ${get pods} =    Write Commands Until Expected Prompt    kubectl get pods -o wide    ${DEFAULT_LINUX_PROMPT_STRICT}
233     @{pod ips} =    String.Get Regexp Matches    ${get pods}    \\d+\\.\\d+\\.\\d+\\.\\d+
234     @{pod names} =    String.Get Regexp Matches    ${get pods}    ss\\w+-\\w+
235     : FOR    ${pod_name}    IN    @{pod names}
236     \    ${logs} =    Log Statements    ${pod ips}    ${pod names}    ${pod_name}
237     \    Ping Pods    ${pod_name}    ${pod ips}    ${logs}
238
239 Log Statements
240     [Arguments]    ${pod ips}    ${pod names}    ${pod_name}
241     @{log statement} =    Create List
242     ${i} =    Set Variable    0
243     : FOR    ${pod_ip}    IN    @{pod ips}
244     \    ${ping statement}    Set Variable    Ping from ${pod_name} to ${pod names[${i}]} (${pod ip})
245     \    Append To List    ${log statement}    ${ping statement}
246     \    ${i} =    Evaluate    ${i}+1
247     [Return]    @{log statement}
248
249 Ping Pods
250     [Arguments]    ${pod_name}    ${pod ips}    ${logs}
251     ${i} =    Set Variable    0
252     : FOR    ${ping info}    IN    @{logs}
253     \    ${ping} =    Write Commands Until Expected Prompt    kubectl exec -it ${pod_name} -- ping -c 3 ${pod ips[${i}]}    ${DEFAULT_LINUX_PROMPT_STRICT}
254     \    BuiltIn.log    ${ping}
255     \    Builtin.Should Match Regexp    ${ping}    ${PING_REGEXP}
256     \    ${i}    Evaluate    ${i}+1
257
258 Coe Suite Teardown
259     [Documentation]    COE project requires stop suite to be executed only for the last test suite.This keyword find the current suite,compares it with the stored last suite value and executes Coe.Stop suite only if the cuurent suite is equal to the last suite.
260     ${current suite}    ${suite names updated}    Extract current suite name
261     ${last_suite} =    Set Variable    ${suite names updated[-1]}
262     ${status} =    BuiltIn.Evaluate    '${last_suite}' == '${current suite}'
263     Run Keyword If    '${status}' == 'True'    Coe.Stop Suite
264
265 Extract current suite name
266     [Documentation]    This keyword returns the name of current test suite.Appropriate replacement in text is done to make test suite names in SUITES and SUITE_NAME similar.
267     BuiltIn.Log    SUITE_NAME: ${SUITE_NAME}
268     BuiltIn.Log    SUITES: ${SUITES}
269     @{suite_names}    Get Regexp Matches    ${SUITES}    coe\\/(\\w+).robot    1
270     @{suite_names_updated}    Create List
271     : FOR    ${suite}    IN    @{suite_names}
272     \    ${suite}    Replace String    ${suite}    _    ${SPACE}
273     \    Append To List    ${suite_names_updated}    ${suite}
274     ${num_suites} =    BuiltIn.Get Length    ${suite_names_updated}
275     ${suite line}    ${current_suite} =    BuiltIn.Run Keyword If    ${num_suites} > ${1}    Should Match Regexp    ${SUITE_NAME}    .txt.(\\w.*)
276     ...    ELSE    BuiltIn.Set Variable    @{suite_names_updated}[0]    @{suite_names_updated}[0]
277     [Return]    ${current_suite}    ${suite_names_updated}
278
279 Check For Stale veth Ports
280     [Documentation]    Check on switches(except master) where pods were created and deleted to ensure there are no stale veth ports left behind.
281     : FOR    ${minion_index}    IN RANGE    2    ${NUM_TOOLS_SYSTEM}+1
282     \    ${switch output} =    Utils.Run Command On Remote System And Log    ${TOOLS_SYSTEM_${minion_index}_IP}    sudo ovs-vsctl show
283     \    BuiltIn.Should Not Contain    ${switch output}    veth