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