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