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