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