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