add missing quote for grep string
[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 Get Port Id
140     [Arguments]    ${port_name}      ${devstack_conn_id}
141     [Documentation]    Retrieve the port id for the given port name to attach specific vm instance to a particular port
142     Switch Connection    ${devstack_conn_id}
143     ${port_id}=    Write Commands Until Prompt    neutron port-list | grep "${port_name}" | awk '{print $2}'       30s
144     Log    ${port_id}
145     [Return]    ${port_id}
146
147 Create Vm Instances
148     [Arguments]    ${net_name}    ${vm_instance_names}    ${image}=cirros-0.3.4-x86_64-uec    ${flavor}=m1.nano
149     [Documentation]    Create X Vm Instance with the net id of the Netowrk.
150     ${devstack_conn_id}=       Get ControlNode Connection
151     Switch Connection    ${devstack_conn_id}
152     ${net_id}=    Get Net Id    ${net_name}      ${devstack_conn_id}
153     : FOR    ${VmElement}    IN    @{vm_instance_names}
154     \    ${output}=    Write Commands Until Prompt    nova boot --image ${image} --flavor ${flavor} --nic net-id=${net_id} ${VmElement}    30s
155     \    Log    ${output}
156     \    Wait Until Keyword Succeeds    25s    5s    Verify VM Is ACTIVE    ${VmElement}
157
158 Create Vm Instance With Port On Compute Node
159     [Arguments]    ${port_name}    ${vm_instance_name}    ${compute_node}    ${image}=cirros-0.3.4-x86_64-uec    ${flavor}=m1.nano
160     [Documentation]    Create One VM instance using given ${port_name} and for given ${compute_node}
161     ${devstack_conn_id}=       Get ControlNode Connection
162     Switch Connection    ${devstack_conn_id}
163     ${port_id}=    Get Port Id    ${port_name}      ${devstack_conn_id}
164     ${hostname_compute_node}=    Run Command On Remote System    ${compute_node}    hostname
165     ${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
166     Log    ${output}
167     Wait Until Keyword Succeeds    25s    5s    Verify VM Is ACTIVE    ${vm_instance_name}
168
169 Verify VM Is ACTIVE
170     [Arguments]    ${vm_name}
171     [Documentation]    Run these commands to check whether the created vm instance is active or not.
172     ${output}=    Write Commands Until Prompt    nova show ${vm_name} | grep OS-EXT-STS:vm_state      30s
173     Log    ${output}
174     Should Contain    ${output}    active
175
176 View Vm Console
177     [Arguments]    ${vm_instance_names}
178     [Documentation]    View Console log of the created vm instances using nova show.
179     : FOR    ${VmElement}    IN    @{vm_instance_names}
180     \    ${output}=    Write Commands Until Prompt    nova show ${VmElement}
181     \    Log    ${output}
182     \    ${output}=    Write Commands Until Prompt    nova console-log ${VmElement}
183     \    Log    ${output}
184
185 Ping Vm From DHCP Namespace
186     [Arguments]    ${net_name}    ${vm_ip}
187     [Documentation]    Reach all Vm Instance with the net id of the Netowrk.
188     Log    ${vm_ip}
189     ${devstack_conn_id}=       Get ControlNode Connection
190     Switch Connection    ${devstack_conn_id}
191     ${net_id}=    Get Net Id    ${net_name}     ${devstack_conn_id}
192     Log    ${net_id}
193     ${output}=    Write Commands Until Prompt    sudo ip netns exec qdhcp-${net_id} ping -c 3 ${vm_ip}    20s
194     Log    ${output}
195     Close Connection
196     Should Contain    ${output}    64 bytes
197
198 Ping From DHCP Should Not Succeed
199     [Arguments]    ${net_name}    ${vm_ip}
200     [Documentation]    Should Not Reach Vm Instance with the net id of the Netowrk.
201     Log    ${vm_ip}
202     ${devstack_conn_id}=       Get ControlNode Connection
203     Switch Connection    ${devstack_conn_id}
204     ${net_id}=    Get Net Id    ${net_name}     ${devstack_conn_id}
205     Log    ${net_id}
206     ${output}=    Write Commands Until Prompt    sudo ip netns exec qdhcp-${net_id} ping -c 3 ${vm_ip}    20s
207     Close Connection
208     Log    ${output}
209     Should Not Contain    ${output}    64 bytes
210
211 Ping From Instance
212     [Arguments]    ${dest_vm}
213     [Documentation]    Ping to the expected destination ip.
214     ${output}=    Write Commands Until Expected Prompt    ping -c 3 ${dest_vm}    ${OS_SYSTEM_PROMPT}
215     Log    ${output}
216     [Return]    ${output}
217
218 Curl Metadata Server
219     [Documentation]    Ping to the expected destination ip.
220     ${output}=    Write Commands Until Expected Prompt    curl -i http://169.254.169.254    ${OS_SYSTEM_PROMPT}
221     Write Commands Until Prompt    exit
222     Should Contain    ${output}    200
223
224 Close Vm Instance
225     [Documentation]    Exit the vm instance.
226     ${output}=    Write Commands Until Prompt    exit
227     Log    ${output}
228
229 Check If Console Is VmInstance
230     [Arguments]    ${console}=cirros
231     [Documentation]    Check if the session has been able to login to the VM instance
232     ${output}=    Write Commands Until Expected Prompt    id    ${OS_SYSTEM_PROMPT}
233     Should Contain    ${output}    ${console}
234
235 Exit From Vm Console
236     [Documentation]    Check if the session has been able to login to the VM instance and exit the instance
237     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance    cirros
238     Run Keyword If    ${rcode}    Write Commands Until Prompt    exit
239     Get OvsDebugInfo
240
241 Check Ping
242     [Arguments]    ${ip_address}
243     [Documentation]    Run Ping command on the IP available as argument
244     ${output}=    Write Commands Until Expected Prompt    ping -c 3 ${ip_address}    ${OS_SYSTEM_PROMPT}
245     Should Contain    ${output}    64 bytes
246
247 Check Metadata Access
248     [Documentation]    Try curl on the Metadataurl and check if it is okay
249     ${output}=    Write Commands Until Expected Prompt    curl -i http://169.254.169.254    ${OS_SYSTEM_PROMPT}
250     Should Contain    ${output}    200
251
252 Test Operations From Vm Instance
253     [Arguments]    ${net_name}    ${src_ip}    ${list_of_local_dst_ips}    ${l2_or_l3}=l2    ${list_of_external_dst_ips}=${NONE}    ${user}=cirros
254     ...    ${password}=cubswin:)
255     [Documentation]    Login to the vm instance using ssh in the network.
256     ${devstack_conn_id}=       Get ControlNode Connection
257     Switch Connection    ${devstack_conn_id}
258     ${net_id}=    Get Net Id    ${net_name}      ${devstack_conn_id}
259     ${output}=    Write Commands Until Expected Prompt    sudo ip netns exec qdhcp-${net_id} ssh ${user}@${src_ip} -o ConnectTimeout=10 -o StrictHostKeyChecking=no    d:
260     Log    ${output}
261     ${output}=    Write Commands Until Expected Prompt    ${password}    ${OS_SYSTEM_PROMPT}
262     Log    ${output}
263     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance
264     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    ifconfig    ${OS_SYSTEM_PROMPT}
265     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    route    ${OS_SYSTEM_PROMPT}
266     ${dest_vm}=    Get From List    ${list_of_local_dst_ips}    0
267     Log    ${dest_vm}
268     Run Keyword If    ${rcode}    Check Ping    ${dest_vm}
269     ${dest_dhcp}=    Get From List    ${list_of_local_dst_ips}    1
270     Log    ${dest_dhcp}
271     Run Keyword If    ${rcode}    Check Ping    ${dest_dhcp}
272     ${dest_vm}=    Get From List    ${list_of_local_dst_ips}    2
273     Log    ${dest_vm}
274     Run Keyword If    ${rcode}    Check Ping    ${dest_vm}
275     Run Keyword If    ${rcode}    Check Metadata Access
276     Run Keyword If    '${l2_or_l3}' == 'l3'    Ping Other Instances    ${list_of_external_dst_ips}
277     [Teardown]    Exit From Vm Console
278
279 Ping Other Instances
280     [Arguments]    ${list_of_external_dst_ips}
281     [Documentation]    Check reachability with other network's instances.
282     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance
283     ${dest_vm}=    Get From List    ${list_of_external_dst_ips}    0
284     Log    ${dest_vm}
285     Run Keyword If    ${rcode}    Check Ping    ${dest_vm}
286     ${dest_dhcp}=    Get From List    ${list_of_external_dst_ips}    1
287     Log    ${dest_dhcp}
288     Run Keyword If    ${rcode}    Check Ping    ${dest_dhcp}
289     ${dest_vm}=    Get From List    ${list_of_external_dst_ips}    2
290     Log    ${dest_vm}
291     Run Keyword If    ${rcode}    Check Ping    ${dest_vm}
292
293 Create Router
294     [Arguments]    ${router_name}
295     [Documentation]    Create Router and Add Interface to the subnets.
296     ${devstack_conn_id}=       Get ControlNode Connection
297     Switch Connection    ${devstack_conn_id}
298     ${output}=    Write Commands Until Prompt    neutron -v router-create ${router_name}    30s
299     Close Connection
300     Should Contain    ${output}    Created a new router
301
302 Add Router Interface
303     [Arguments]    ${router_name}    ${interface_name}
304     ${devstack_conn_id}=       Get ControlNode Connection
305     Switch Connection    ${devstack_conn_id}
306     ${output}=    Write Commands Until Prompt    neutron -v router-interface-add ${router_name} ${interface_name}
307     Close Connection
308     Should Contain    ${output}    Added interface
309
310 Remove Interface
311     [Arguments]    ${router_name}    ${interface_name}
312     [Documentation]    Remove Interface to the subnets.
313     ${devstack_conn_id}=       Get ControlNode Connection
314     Switch Connection    ${devstack_conn_id}
315     ${output}=    Write Commands Until Prompt    neutron -v router-interface-delete ${router_name} ${interface_name}
316     Close Connection
317     Should Contain    ${output}    Removed interface from router
318
319 Delete Router
320     [Arguments]    ${router_name}
321     [Documentation]    Delete Router and Interface to the subnets.
322     ${devstack_conn_id}=       Get ControlNode Connection
323     Switch Connection    ${devstack_conn_id}
324     ${output}=    Write Commands Until Prompt    neutron -v router-delete ${router_name}       60s
325     Close Connection
326     Should Contain    ${output}    Deleted router:
327
328 Get DumpFlows And Ovsconfig
329     [Arguments]    ${openstack_node_ip}
330     [Documentation]    Get the OvsConfig and Flow entries from OVS from the Openstack Node
331     Log    ${openstack_node_ip}
332     SSHLibrary.Open Connection    ${openstack_node_ip}    prompt=${DEFAULT_LINUX_PROMPT}
333     Utils.Flexible SSH Login    ${OS_USER}    ${DEVSTACK_SYSTEM_PASSWORD}
334     SSHLibrary.Set Client Configuration    timeout=${default_devstack_prompt_timeout}
335     Write Commands Until Expected Prompt    sudo ovs-vsctl show    ]>
336     Write Commands Until Expected Prompt    sudo ovs-ofctl dump-flows br-int -OOpenFlow13    ]>
337
338 Get ControlNode Connection
339     ${control_conn_id}=    SSHLibrary.Open Connection    ${OS_CONTROL_NODE_IP}    prompt=${DEFAULT_LINUX_PROMPT_STRICT}
340     Utils.Flexible SSH Login    ${OS_USER}    ${DEVSTACK_SYSTEM_PASSWORD}
341     SSHLibrary.Set Client Configuration    timeout=30s
342     Source Password      force=yes
343     [Return]    ${control_conn_id}
344
345 Get OvsDebugInfo
346     [Documentation]    Get the OvsConfig and Flow entries from all Openstack nodes
347     Run Keyword If    0 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_CONTROL_NODE_IP}
348     Run Keyword If    1 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_COMPUTE_1_IP}
349     Run Keyword If    2 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_COMPUTE_2_IP}
350
351 Show Debugs
352     [Arguments]    ${vm_indices}
353     [Documentation]    Run these commands for debugging, it can list state of VM instances and ip information in control node
354     ${output}=    Write Commands Until Prompt    sudo ip netns list
355     Log    ${output}
356     : FOR    ${index}    IN    @{vm_indices}
357     \    ${output}=    Write Commands Until Prompt    nova show ${index}     30s
358     \    Log    ${output}
359     Close Connection