list Open_vSwitch table to show local_ip
[integration/test.git] / csit / libraries / OpenStackOperations.robot
1 *** Settings ***
2 Documentation     Openstack library. This library is useful for tests to create network, subnet, router and vm instances
3 Library           SSHLibrary
4 Resource          Utils.robot
5 Variables         ../variables/Variables.py
6
7 *** Keywords ***
8 Source Password
9     [Arguments]    ${force}=no    ${source_pwd}=yes
10     [Documentation]    Sourcing the Openstack PAsswords for neutron configurations
11     Run Keyword If    '${source_pwd}' == 'yes' or '${force}' == 'yes'    Write Commands Until Prompt    cd ${DEVSTACK_DEPLOY_PATH}; source openrc admin admin
12
13 Get Tenant ID From Security Group
14     [Documentation]    Returns tenant ID by reading it from existing default security-group.
15     ${devstack_conn_id}=    Get ControlNode Connection
16     Switch Connection    ${devstack_conn_id}
17     ${output}=    Write Commands Until Prompt    neutron security-group-show default | grep "| tenant_id" | awk '{print $4}'
18     Log    ${output}
19     [Return]    ${output}
20
21 Create Network
22     [Arguments]    ${network_name}    ${additional_args}=${EMPTY}    ${verbose}=TRUE
23     [Documentation]    Create Network with neutron request.
24     ${devstack_conn_id}=    Get ControlNode Connection
25     Switch Connection    ${devstack_conn_id}
26     ${command}    Set Variable If    "${verbose}" == "TRUE"    neutron -v net-create ${network_name} ${additional_args}    neutron net-create ${network_name} ${additional_args} | grep -w id | awk '{print $4}'
27     ${output}=    Write Commands Until Prompt    ${command}    30s
28     Log    ${output}
29     [Return]    ${output}
30
31 List Networks
32     [Documentation]    List networks and return output with neutron client.
33     ${devstack_conn_id}=    Get ControlNode Connection
34     Switch Connection    ${devstack_conn_id}
35     ${output}=    Write Commands Until Prompt    neutron net-list    30s
36     Close Connection
37     Log    ${output}
38     [Return]    ${output}
39
40 List Subnets
41     [Documentation]    List subnets and return output with neutron client.
42     ${devstack_conn_id}=    Get ControlNode Connection
43     Switch Connection    ${devstack_conn_id}
44     ${output}=    Write Commands Until Prompt    neutron subnet-list    30s
45     Close Connection
46     Log    ${output}
47     [Return]    ${output}
48
49 Delete Network
50     [Arguments]    ${network_name}
51     [Documentation]    Delete Network with neutron request.
52     ${devstack_conn_id}=    Get ControlNode Connection
53     Switch Connection    ${devstack_conn_id}
54     ${output}=    Write Commands Until Prompt    neutron -v net-delete ${network_name}    30s
55     Close Connection
56     Log    ${output}
57     Should Contain    ${output}    Deleted network: ${network_name}
58
59 Create SubNet
60     [Arguments]    ${network_name}    ${subnet}    ${range_ip}
61     [Documentation]    Create SubNet for the Network with neutron request.
62     ${devstack_conn_id}=    Get ControlNode Connection
63     Switch Connection    ${devstack_conn_id}
64     ${output}=    Write Commands Until Prompt    neutron -v subnet-create ${network_name} ${range_ip} --name ${subnet}    30s
65     Close Connection
66     Log    ${output}
67     Should Contain    ${output}    Created a new subnet
68
69 Create Port
70     [Arguments]    ${network_name}    ${port_name}
71     [Documentation]    Create Port with neutron request.
72     ${devstack_conn_id}=    Get ControlNode Connection
73     Switch Connection    ${devstack_conn_id}
74     ${output}=    Write Commands Until Prompt    neutron -v port-create ${network_name} --name ${port_name}    30s
75     Close Connection
76     Log    ${output}
77     Should Contain    ${output}    Created a new port
78
79 Delete Port
80     [Arguments]    ${port_name}
81     [Documentation]    Delete Port with neutron request.
82     ${devstack_conn_id}=    Get ControlNode Connection
83     Switch Connection    ${devstack_conn_id}
84     ${output}=    Write Commands Until Prompt    neutron -v port-delete ${port_name}    30s
85     Close Connection
86     Log    ${output}
87     Should Contain    ${output}    Deleted a new port
88
89 Verify Gateway Ips
90     [Documentation]    Verifies the Gateway Ips with dump flow.
91     ${output}=    Write Commands Until Prompt    sudo ovs-ofctl -O OpenFlow13 dump-flows br-int
92     Log    ${output}
93     : FOR    ${GatewayIpElement}    IN    @{GATEWAY_IPS}
94     \    Should Contain    ${output}    ${GatewayIpElement}
95
96 Verify Dhcp Ips
97     [Documentation]    Verifies the Dhcp Ips with dump flow.
98     ${output}=    Write Commands Until Prompt    sudo ovs-ofctl -O OpenFlow13 dump-flows br-int
99     Log    ${output}
100     : FOR    ${DhcpIpElement}    IN    @{DHCP_IPS}
101     \    Should Contain    ${output}    ${DhcpIpElement}
102
103 Verify No Dhcp Ips
104     [Documentation]    Verifies the Dhcp Ips with dump flow.
105     ${output}=    Write Commands Until Prompt    sudo ovs-ofctl -O OpenFlow13 dump-flows br-int
106     Log    ${output}
107     : FOR    ${DhcpIpElement}    IN    @{DHCP_IPS}
108     \    Should Not Contain    ${output}    ${DhcpIpElement}
109
110 Delete SubNet
111     [Arguments]    ${subnet}
112     [Documentation]    Delete SubNet for the Network with neutron request.
113     Log    ${subnet}
114     ${devstack_conn_id}=    Get ControlNode Connection
115     Switch Connection    ${devstack_conn_id}
116     ${output}=    Write Commands Until Prompt    neutron -v subnet-delete ${subnet}
117     Close Connection
118     Log    ${output}
119     Should Contain    ${output}    Deleted subnet: ${subnet}
120
121 Verify No Gateway Ips
122     [Documentation]    Verifies the Gateway Ips removed with dump flow.
123     ${output}=    Write Commands Until Prompt    sudo ovs-ofctl -O OpenFlow13 dump-flows br-int
124     Log    ${output}
125     : FOR    ${GatewayIpElement}    IN    @{GATEWAY_IPS}
126     \    Should Not Contain    ${output}    ${GatewayIpElement}
127
128 Delete Vm Instance
129     [Arguments]    ${vm_name}
130     [Documentation]    Delete Vm instances using instance names.
131     ${devstack_conn_id}=    Get ControlNode Connection
132     Switch Connection    ${devstack_conn_id}
133     ${output}=    Write Commands Until Prompt    nova force-delete ${vm_name}    40s
134     Close Connection
135
136 Get Net Id
137     [Arguments]    ${network_name}    ${devstack_conn_id}
138     [Documentation]    Retrieve the net id for the given network name to create specific vm instance
139     Switch Connection    ${devstack_conn_id}
140     ${output}=    Write Commands Until Prompt    neutron net-list | grep "${network_name}" | get_field 1    30s
141     Log    ${output}
142     ${splitted_output}=    Split String    ${output}    ${EMPTY}
143     ${net_id}=    Get from List    ${splitted_output}    0
144     Log    ${net_id}
145     [Return]    ${net_id}
146
147 Get Port Id
148     [Arguments]    ${port_name}    ${devstack_conn_id}
149     [Documentation]    Retrieve the port id for the given port name to attach specific vm instance to a particular port
150     Switch Connection    ${devstack_conn_id}
151     ${port_id}=    Write Commands Until Prompt    neutron port-list | grep "${port_name}" | awk '{print $2}'    30s
152     Log    ${port_id}
153     [Return]    ${port_id}
154
155 Create Vm Instances
156     [Arguments]    ${net_name}    ${vm_instance_names}    ${image}=cirros-0.3.4-x86_64-uec    ${flavor}=m1.nano      ${sg}=default
157     [Documentation]    Create X Vm Instance with the net id of the Netowrk.
158     ${devstack_conn_id}=    Get ControlNode Connection
159     Switch Connection    ${devstack_conn_id}
160     ${net_id}=    Get Net Id    ${net_name}    ${devstack_conn_id}
161     : FOR    ${VmElement}    IN    @{vm_instance_names}
162     \    ${output}=    Write Commands Until Prompt    nova boot --image ${image} --flavor ${flavor} --nic net-id=${net_id} ${VmElement} --security-groups ${sg}    30s
163     \    Log    ${output}
164     \    Wait Until Keyword Succeeds    25s    5s    Verify VM Is ACTIVE    ${VmElement}
165
166 Create Vm Instance With Port On Compute Node
167     [Arguments]    ${port_name}    ${vm_instance_name}    ${compute_node}    ${image}=cirros-0.3.4-x86_64-uec    ${flavor}=m1.nano
168     [Documentation]    Create One VM instance using given ${port_name} and for given ${compute_node}
169     ${devstack_conn_id}=    Get ControlNode Connection
170     Switch Connection    ${devstack_conn_id}
171     ${port_id}=    Get Port Id    ${port_name}    ${devstack_conn_id}
172     ${hostname_compute_node}=    Run Command On Remote System    ${compute_node}    hostname
173     ${output}=    Write Commands Until Prompt    nova boot --image ${image} --flavor ${flavor} --nic port-id=${port_id} ${vm_instance_name} --availability-zone nova:${hostname_compute_node}    30s
174     Log    ${output}
175     Wait Until Keyword Succeeds    25s    5s    Verify VM Is ACTIVE    ${vm_instance_name}
176
177 Verify VM Is ACTIVE
178     [Arguments]    ${vm_name}
179     [Documentation]    Run these commands to check whether the created vm instance is active or not.
180     ${output}=    Write Commands Until Prompt    nova show ${vm_name} | grep OS-EXT-STS:vm_state    30s
181     Log    ${output}
182     Should Contain    ${output}    active
183
184 View Vm Console
185     [Arguments]    ${vm_instance_names}
186     [Documentation]    View Console log of the created vm instances using nova show.
187     : FOR    ${VmElement}    IN    @{vm_instance_names}
188     \    ${output}=    Write Commands Until Prompt    nova show ${VmElement}
189     \    Log    ${output}
190     \    ${output}=    Write Commands Until Prompt    nova console-log ${VmElement}
191     \    Log    ${output}
192
193 Ping Vm From DHCP Namespace
194     [Arguments]    ${net_name}    ${vm_ip}
195     [Documentation]    Reach all Vm Instance with the net id of the Netowrk.
196     Log    ${vm_ip}
197     ${devstack_conn_id}=    Get ControlNode Connection
198     Switch Connection    ${devstack_conn_id}
199     ${net_id}=    Get Net Id    ${net_name}    ${devstack_conn_id}
200     Log    ${net_id}
201     ${output}=    Write Commands Until Prompt    sudo ip netns exec qdhcp-${net_id} ping -c 3 ${vm_ip}    20s
202     Log    ${output}
203     Close Connection
204     Should Contain    ${output}    64 bytes
205
206 Ping From DHCP Should Not Succeed
207     [Arguments]    ${net_name}    ${vm_ip}
208     [Documentation]    Should Not Reach Vm Instance with the net id of the Netowrk.
209     Log    ${vm_ip}
210     ${devstack_conn_id}=    Get ControlNode Connection
211     Switch Connection    ${devstack_conn_id}
212     ${net_id}=    Get Net Id    ${net_name}    ${devstack_conn_id}
213     Log    ${net_id}
214     ${output}=    Write Commands Until Prompt    sudo ip netns exec qdhcp-${net_id} ping -c 3 ${vm_ip}    20s
215     Close Connection
216     Log    ${output}
217     Should Not Contain    ${output}    64 bytes
218
219 Ping From Instance
220     [Arguments]    ${dest_vm}
221     [Documentation]    Ping to the expected destination ip.
222     ${output}=    Write Commands Until Expected Prompt    ping -c 3 ${dest_vm}    ${OS_SYSTEM_PROMPT}
223     Log    ${output}
224     [Return]    ${output}
225
226 Curl Metadata Server
227     [Documentation]    Ping to the expected destination ip.
228     ${output}=    Write Commands Until Expected Prompt    curl -i http://169.254.169.254    ${OS_SYSTEM_PROMPT}
229     Write Commands Until Prompt    exit
230     Should Contain    ${output}    200
231
232 Close Vm Instance
233     [Documentation]    Exit the vm instance.
234     ${output}=    Write Commands Until Prompt    exit
235     Log    ${output}
236
237 Check If Console Is VmInstance
238     [Arguments]    ${console}=cirros
239     [Documentation]    Check if the session has been able to login to the VM instance
240     ${output}=    Write Commands Until Expected Prompt    id    ${OS_SYSTEM_PROMPT}
241     Should Contain    ${output}    ${console}
242
243 Exit From Vm Console
244     [Documentation]    Check if the session has been able to login to the VM instance and exit the instance
245     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance    cirros
246     Run Keyword If    ${rcode}    Write Commands Until Prompt    exit
247     Get OvsDebugInfo
248
249 Check Ping
250     [Arguments]    ${ip_address}
251     [Documentation]    Run Ping command on the IP available as argument
252     ${output}=    Write Commands Until Expected Prompt    ping -c 3 ${ip_address}    ${OS_SYSTEM_PROMPT}
253     Should Contain    ${output}    64 bytes
254
255 Check Metadata Access
256     [Documentation]    Try curl on the Metadataurl and check if it is okay
257     ${output}=    Write Commands Until Expected Prompt    curl -i http://169.254.169.254    ${OS_SYSTEM_PROMPT}
258     Should Contain    ${output}    200
259
260 Test Operations From Vm Instance
261     [Arguments]    ${net_name}    ${src_ip}    ${list_of_local_dst_ips}    ${l2_or_l3}=l2    ${list_of_external_dst_ips}=${NONE}    ${user}=cirros
262     ...    ${password}=cubswin:)
263     [Documentation]    Login to the vm instance using ssh in the network.
264     ${devstack_conn_id}=    Get ControlNode Connection
265     Switch Connection    ${devstack_conn_id}
266     ${net_id}=    Get Net Id    ${net_name}    ${devstack_conn_id}
267     ${output}=    Write Commands Until Expected Prompt    sudo ip netns exec qdhcp-${net_id} ssh ${user}@${src_ip} -o ConnectTimeout=10 -o StrictHostKeyChecking=no    d:
268     Log    ${output}
269     ${output}=    Write Commands Until Expected Prompt    ${password}    ${OS_SYSTEM_PROMPT}
270     Log    ${output}
271     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance
272     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    ifconfig    ${OS_SYSTEM_PROMPT}
273     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    route    ${OS_SYSTEM_PROMPT}
274     ${dest_vm}=    Get From List    ${list_of_local_dst_ips}    0
275     Log    ${dest_vm}
276     Run Keyword If    ${rcode}    Check Ping    ${dest_vm}
277     ${dest_dhcp}=    Get From List    ${list_of_local_dst_ips}    1
278     Log    ${dest_dhcp}
279     Run Keyword If    ${rcode}    Check Ping    ${dest_dhcp}
280     ${dest_vm}=    Get From List    ${list_of_local_dst_ips}    2
281     Log    ${dest_vm}
282     Run Keyword If    ${rcode}    Check Ping    ${dest_vm}
283     Run Keyword If    ${rcode}    Check Metadata Access
284     Run Keyword If    '${l2_or_l3}' == 'l3'    Ping Other Instances    ${list_of_external_dst_ips}
285     [Teardown]    Exit From Vm Console
286
287 Ping Other Instances
288     [Arguments]    ${list_of_external_dst_ips}
289     [Documentation]    Check reachability with other network's instances.
290     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance
291     ${dest_vm}=    Get From List    ${list_of_external_dst_ips}    0
292     Log    ${dest_vm}
293     Run Keyword If    ${rcode}    Check Ping    ${dest_vm}
294     ${dest_dhcp}=    Get From List    ${list_of_external_dst_ips}    1
295     Log    ${dest_dhcp}
296     Run Keyword If    ${rcode}    Check Ping    ${dest_dhcp}
297     ${dest_vm}=    Get From List    ${list_of_external_dst_ips}    2
298     Log    ${dest_vm}
299     Run Keyword If    ${rcode}    Check Ping    ${dest_vm}
300
301 Create Router
302     [Arguments]    ${router_name}
303     [Documentation]    Create Router and Add Interface to the subnets.
304     ${devstack_conn_id}=    Get ControlNode Connection
305     Switch Connection    ${devstack_conn_id}
306     ${output}=    Write Commands Until Prompt    neutron -v router-create ${router_name}    30s
307     Close Connection
308     Should Contain    ${output}    Created a new router
309
310 Add Router Interface
311     [Arguments]    ${router_name}    ${interface_name}
312     ${devstack_conn_id}=    Get ControlNode Connection
313     Switch Connection    ${devstack_conn_id}
314     ${output}=    Write Commands Until Prompt    neutron -v router-interface-add ${router_name} ${interface_name}
315     Close Connection
316     Should Contain    ${output}    Added interface
317
318 Remove Interface
319     [Arguments]    ${router_name}    ${interface_name}
320     [Documentation]    Remove Interface to the subnets.
321     ${devstack_conn_id}=    Get ControlNode Connection
322     Switch Connection    ${devstack_conn_id}
323     ${output}=    Write Commands Until Prompt    neutron -v router-interface-delete ${router_name} ${interface_name}
324     Close Connection
325     Should Contain    ${output}    Removed interface from router
326
327 Delete Router
328     [Arguments]    ${router_name}
329     [Documentation]    Delete Router and Interface to the subnets.
330     ${devstack_conn_id}=    Get ControlNode Connection
331     Switch Connection    ${devstack_conn_id}
332     ${output}=    Write Commands Until Prompt    neutron -v router-delete ${router_name}    60s
333     Close Connection
334     Should Contain    ${output}    Deleted router:
335
336 Get DumpFlows And Ovsconfig
337     [Arguments]    ${openstack_node_ip}
338     [Documentation]    Get the OvsConfig and Flow entries from OVS from the Openstack Node
339     Log    ${openstack_node_ip}
340     SSHLibrary.Open Connection    ${openstack_node_ip}    prompt=${DEFAULT_LINUX_PROMPT}
341     Utils.Flexible SSH Login    ${OS_USER}    ${DEVSTACK_SYSTEM_PASSWORD}
342     SSHLibrary.Set Client Configuration    timeout=${default_devstack_prompt_timeout}
343     Write Commands Until Expected Prompt    ip -o link    ]>
344     Write Commands Until Expected Prompt    ip -o addr    ]>
345     Write Commands Until Expected Prompt    ip route    ]>
346     Write Commands Until Expected Prompt    arp -an    ]>
347     ${nslist}=    Write Commands Until Expected Prompt    ip netns list | awk '{print $1}'    ]>
348     @{lines}    Split To Lines    ${nslist}
349     : FOR    ${line}    IN    @{lines}
350     \    Write Commands Until Expected Prompt    sudo ip netns exec ${line} ip -o link    ]>
351     \    Write Commands Until Expected Prompt    sudo ip netns exec ${line} ip -o addr    ]>
352     \    Write Commands Until Expected Prompt    sudo ip netns exec ${line} ip route    ]>
353     Write Commands Until Expected Prompt    sudo ovs-vsctl show    ]>
354     Write Commands Until Expected Prompt    sudo ovs-vsctl list Open_vSwitch    ]>
355     Write Commands Until Expected Prompt    sudo ovs-ofctl show br-int -OOpenFlow13    ]>
356     Write Commands Until Expected Prompt    sudo ovs-ofctl dump-flows br-int -OOpenFlow13    ]>
357     Write Commands Until Expected Prompt    sudo ovs-ofctl dump-groups br-int -OOpenFlow13    ]>
358     Write Commands Until Expected Prompt    sudo ovs-ofctl dump-group-stats br-int -OOpenFlow13    ]>
359
360 Get ControlNode Connection
361     ${control_conn_id}=    SSHLibrary.Open Connection    ${OS_CONTROL_NODE_IP}    prompt=${DEFAULT_LINUX_PROMPT_STRICT}
362     Utils.Flexible SSH Login    ${OS_USER}    ${DEVSTACK_SYSTEM_PASSWORD}
363     SSHLibrary.Set Client Configuration    timeout=30s
364     Source Password    force=yes
365     [Return]    ${control_conn_id}
366
367 Get OvsDebugInfo
368     [Documentation]    Get the OvsConfig and Flow entries from all Openstack nodes
369     Run Keyword If    0 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_CONTROL_NODE_IP}
370     Run Keyword If    1 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_COMPUTE_1_IP}
371     Run Keyword If    2 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_COMPUTE_2_IP}
372
373 Show Debugs
374     [Arguments]    ${vm_indices}
375     [Documentation]    Run these commands for debugging, it can list state of VM instances and ip information in control node
376     ${devstack_conn_id}=    Get ControlNode Connection
377     Switch Connection    ${devstack_conn_id}
378     ${output}=    Write Commands Until Prompt    sudo ip netns list
379     Log    ${output}
380     : FOR    ${index}    IN    @{vm_indices}
381     \    ${output}=    Write Commands Until Prompt    nova show ${index}    30s
382     \    Log    ${output}
383     Close Connection
384
385 Create Security Group
386     [Arguments]    ${sg_name}     ${desc}
387     ${devstack_conn_id}=    Get ControlNode Connection
388     Switch Connection    ${devstack_conn_id}
389     ${output}=    Write Commands Until Prompt    nova secgroup-create ${sg_name} ${desc}      40s
390     Close Connection
391
392 Create Security Rule
393     [Arguments]    ${direction}     ${protocol}      ${min_port}     ${max_port}     ${remote_ip}     ${sg_name}
394     ${devstack_conn_id}=    Get ControlNode Connection
395     Switch Connection    ${devstack_conn_id}
396     ${output}=    Write Commands Until Prompt    neutron security-group-rule-create --direction ${direction} --protocol ${protocol} --port-range-min ${min_port} --port-range-max ${max_port} --remote-ip-prefix ${remote_ip} ${sg_name}
397     Close Connection