summaryrefslogtreecommitdiffstats
path: root/kernel/Documentation/networking/ixgbe.txt
blob: 6f0cb57b59c6b3713b3428c0dfadbbec46972ab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
Linux* Base Driver for the Intel(R) Ethernet 10 Gigabit PCI Express Family of
Adapters
=============================================================================

Intel 10 Gigabit Linux driver.
Copyright(c) 1999 - 2013 Intel Corporation.

Contents
========

- Identifying Your Adapter
- Additional Configurations
- Performance Tuning
- Known Issues
- Support

Identifying Your Adapter
========================

The driver in this release is compatible with 82598, 82599 and X540-based
Intel Network Connections.

For more information on how to identify your adapter, go to the Adapter &
Driver ID Guide at:

    http://support.intel.com/support/network/sb/CS-012904.htm

SFP+ Devices with Pluggable Optics
----------------------------------

82599-BASED ADAPTERS

NOTES: If your 82599-based Intel(R) Network Adapter came with Intel optics, or
is an Intel(R) Ethernet Server Adapter X520-2, then it only supports Intel
optics and/or the direct attach cables listed below.

When 82599-based SFP+ devices are connected back to back, they should be set to
the same Speed setting via ethtool. Results may vary if you mix speed settings.
82598-based adapters support all passive direct attach cables that comply
with SFF-8431 v4.1 and SFF-8472 v10.4 specifications. Active direct attach
cables are not supported.

Supplier    Type                                             Part Numbers

SR Modules
Intel       DUAL RATE 1G/10G SFP+ SR (bailed)                FTLX8571D3BCV-IT
Intel       DUAL RATE 1G/10G SFP+ SR (bailed)                AFBR-703SDDZ-IN1
Intel       DUAL RATE 1G/10G SFP+ SR (bailed)                AFBR-703SDZ-IN2
LR Modules
Intel       DUAL RATE 1G/10G SFP+ LR (bailed)                FTLX1471D3BCV-IT
Intel       DUAL RATE 1G/10G SFP+ LR (bailed)                AFCT-701SDDZ-IN1
Intel       DUAL RATE 1G/10G SFP+ LR (bailed)                AFCT-701SDZ-IN2

The following is a list of 3rd party SFP+ modules and direct attach cables that
have received some testing. Not all modules are applicable to all devices.

Supplier   Type                                              Part Numbers

Finisar    SFP+ SR bailed, 10g single rate                   FTLX8571D3BCL
Avago      SFP+ SR bailed, 10g single rate                   AFBR-700SDZ
Finisar    SFP+ LR bailed, 10g single rate                   FTLX1471D3BCL

Finisar    DUAL RATE 1G/10G SFP+ SR (No Bail)                FTLX8571D3QCV-IT
Avago      DUAL RATE 1G/10G SFP+ SR (No Bail)                AFBR-703SDZ-IN1
Finisar    DUAL RATE 1G/10G SFP+ LR (No Bail)                FTLX1471D3QCV-IT
Avago      DUAL RATE 1G/10G SFP+ LR (No Bail)                AFCT-701SDZ-IN1
Finistar   1000BASE-T SFP                                    FCLF8522P2BTL
Avago      1000BASE-T SFP                                    ABCU-5710RZ

82599-based adapters support all passive and active limiting direct attach
cables that comply with SFF-8431 v4.1 and SFF-8472 v10.4 specifications.

Laser turns off for SFP+ when device is down
-------------------------------------------
"ip link set down" turns off the laser for 82599-based SFP+ fiber adapters.
"ip link set up" turns on the laser.


82598-BASED ADAPTERS

NOTES for 82598-Based Adapters:
- Intel(R) Network Adapters that support removable optical modules only support
  their original module type (i.e., the Intel(R) 10 Gigabit SR Dual Port
  Express Module only supports SR optical modules). If you plug in a different
  type of module, the driver will not load.
- Hot Swapping/hot plugging optical modules is not supported.
- Only single speed, 10 gigabit modules are supported.
- LAN on Motherboard (LOMs) may support DA, SR, or LR modules. Other module
  types are not supported. Please see your system documentation for details.

The following is a list of 3rd party SFP+ modules and direct attach cables that
have received some testing. Not all modules are applicable to all devices.

Supplier   Type                                              Part Numbers

Finisar    SFP+ SR bailed, 10g single rate                   FTLX8571D3BCL
Avago      SFP+ SR bailed, 10g single rate                   AFBR-700SDZ
Finisar    SFP+ LR bailed, 10g single rate                   FTLX1471D3BCL

82598-based adapters support all passive direct attach cables that comply
with SFF-8431 v4.1 and SFF-8472 v10.4 specifications. Active direct attach
cables are not supported.


Flow Control
------------
Ethernet Flow Control (IEEE 802.3x) can be configured with ethtool to enable
receiving and transmitting pause frames for ixgbe. When TX is enabled, PAUSE
frames are generated when the receive packet buffer crosses a predefined
threshold.  When rx is enabled, the transmit unit will halt for the time delay
specified when a PAUSE frame is received.

Flow Control is enabled by default. If you want to disable a flow control
capable link partner, use ethtool:

     ethtool -A eth? autoneg off RX off TX off

NOTE: For 82598 backplane cards entering 1 gig mode, flow control default
behavior is changed to off.  Flow control in 1 gig mode on these devices can
lead to Tx hangs.

Intel(R) Ethernet Flow Director
-------------------------------
Supports advanced filters that direct receive packets by their flows to
different queues. Enables tight control on routing a flow in the platform.
Matches flows and CPU cores for flow affinity. Supports multiple parameters
for flexible flow classification and load balancing.

Flow director is enabled only if the kernel is multiple TX queue capable.

An included script (set_irq_affinity.sh) automates setting the IRQ to CPU
affinity.

You can verify that the driver is using Flow Director by looking at the counter
in ethtool: fdir_miss and fdir_match.

Other ethtool Commands:
To enable Flow Director
	ethtool -K ethX ntuple on
To add a filter
	Use -U switch. e.g., ethtool -U ethX flow-type tcp4 src-ip 10.0.128.23
        action 1
To see the list of filters currently present:
	ethtool -u ethX

Perfect Filter: Perfect filter is an interface to load the filter table that
funnels all flow into queue_0 unless an alternative queue is specified using
"action". In that case, any flow that matches the filter criteria will be
directed to the appropriate queue.

If the queue is defined as -1, filter will drop matching packets.

To account for filter matches and misses, there are two stats in ethtool:
fdir_match and fdir_miss. In addition, rx_queue_N_packets shows the number of
packets processed by the Nth queue.

NOTE: Receive Packet Steering (RPS) and Receive Flow Steering (RFS) are not
compatible with Flow Director. IF Flow Director is enabled, these will be
disabled.

The following three parameters impact Flow Director.

FdirMode
--------
Valid Range: 0-2 (0=off, 1=ATR, 2=Perfect filter mode)
Default Value: 1

  Flow Director filtering modes.

FdirPballoc
-----------
Valid Range: 0-2 (0=64k, 1=128k, 2=256k)
Default Value: 0

  Flow Director allocated packet buffer size.

AtrSampleRate
--------------
Valid Range: 1-100
Default Value: 20

  Software ATR Tx packet sample rate. For example, when set to 20, every 20th
  packet, looks to see if the packet will create a new flow.

Node
----
Valid Range:   0-n
Default Value: 1 (off)

  0 - n: where n is the number of NUMA nodes (i.e. 0 - 3) currently online in
  your system
  1: turns this option off

  The Node parameter will allow you to pick which NUMA node you want to have
  the adapter allocate memory on.

max_vfs
-------
Valid Range:   1-63
Default Value: 0

  If the value is greater than 0 it will also force the VMDq parameter to be 1
  or more.

  This parameter adds support for SR-IOV.  It causes the driver to spawn up to
  max_vfs worth of virtual function.


Additional Configurations
=========================

  Jumbo Frames
  ------------
  The driver supports Jumbo Frames for all adapters. Jumbo Frames support is
  enabled by changing the MTU to a value larger than the default of 1500.
  The maximum value for the MTU is 16110.  Use the ip command to
  increase the MTU size.  For example:

        ip link set dev ethx mtu 9000

  The maximum MTU setting for Jumbo Frames is 9710.  This value coincides
  with the maximum Jumbo Frames size of 9728.

  Generic Receive Offload, aka GRO
  --------------------------------
  The driver supports the in-kernel software implementation of GRO.  GRO has
  shown that by coalescing Rx traffic into larger chunks of data, CPU
  utilization can be significantly reduced when under large Rx load.  GRO is an
  evolution of the previously-used LRO interface.  GRO is able to coalesce
  other protocols besides TCP.  It's also safe to use with configurations that
  are problematic for LRO, namely bridging and iSCSI.

  Data Center Bridging, aka DCB
  -----------------------------
  DCB is a configuration Quality of Service implementation in hardware.
  It uses the VLAN priority tag (802.1p) to filter traffic.  That means
  that there are 8 different priorities that traffic can be filtered into.
  It also enables priority flow control which can limit or eliminate the
  number of dropped packets during network stress.  Bandwidth can be
  allocated to each of these priorities, which is enforced at the hardware
  level.

  To enable DCB support in ixgbe, you must enable the DCB netlink layer to
  allow the userspace tools (see below) to communicate with the driver.
  This can be found in the kernel configuration here:

        -> Networking support
          -> Networking options
            -> Data Center Bridging support

  Once this is selected, DCB support must be selected for ixgbe.  This can
  be found here:

        -> Device Drivers
          -> Network device support (NETDEVICES [=y])
            -> Ethernet (10000 Mbit) (NETDEV_10000 [=y])
              -> Intel(R) 10GbE PCI Express adapters support
                -> Data Center Bridging (DCB) Support

  After these options are selected, you must rebuild your kernel and your
  modules.

  In order to use DCB, userspace tools must be downloaded and installed.
  The dcbd tools can be found at:

        http://e1000.sf.net

  Ethtool
  -------
  The driver utilizes the ethtool interface for driver configuration and
  diagnostics, as well as displaying statistical information. The latest
  ethtool version is required for this functionality.

  The latest release of ethtool can be found from
  http://ftp.kernel.org/pub/software/network/ethtool/

  FCoE
  ----
  This release of the ixgbe driver contains new code to enable users to use
  Fiber Channel over Ethernet (FCoE) and Data Center Bridging (DCB)
  functionality that is supported by the 82598-based hardware.  This code has
  no default effect on the regular driver operation, and configuring DCB and
  FCoE is outside the scope of this driver README. Refer to
  http://www.open-fcoe.org/ for FCoE project information and contact
  e1000-eedc@lists.sourceforge.net for DCB information.

  MAC and VLAN anti-spoofing feature
  ----------------------------------
  When a malicious driver attempts to send a spoofed packet, it is dropped by
  the hardware and not transmitted.  An interrupt is sent to the PF driver
  notifying it of the spoof attempt.

  When a spoofed packet is detected the PF driver will send the following
  message to the system log (displayed by  the "dmesg" command):

  Spoof event(s) detected on VF (n)

  Where n=the VF that attempted to do the spoofing.


Performance Tuning
==================

An excellent article on performance tuning can be found at:

http://www.redhat.com/promo/summit/2008/downloads/pdf/Thursday/Mark_Wagner.pdf


Known Issues
============

  Enabling SR-IOV in a 32-bit or 64-bit Microsoft* Windows* Server 2008/R2
  Guest OS using Intel (R) 82576-based GbE or Intel (R) 82599-based 10GbE
  controller under KVM
  ------------------------------------------------------------------------
  KVM Hypervisor/VMM supports direct assignment of a PCIe device to a VM.  This
  includes traditional PCIe devices, as well as SR-IOV-capable devices using
  Intel 82576-based and 82599-based controllers.

  While direct assignment of a PCIe device or an SR-IOV Virtual Function (VF)
  to a Linux-based VM running 2.6.32 or later kernel works fine, there is a
  known issue with Microsoft Windows Server 2008 VM that results in a "yellow
  bang" error. This problem is within the KVM VMM itself, not the Intel driver,
  or the SR-IOV logic of the VMM, but rather that KVM emulates an older CPU
  model for the guests, and this older CPU model does not support MSI-X
  interrupts, which is a requirement for Intel SR-IOV.

  If you wish to use the Intel 82576 or 82599-based controllers in SR-IOV mode
  with KVM and a Microsoft Windows Server 2008 guest try the following
  workaround. The workaround is to tell KVM to emulate a different model of CPU
  when using qemu to create the KVM guest:

       "-cpu qemu64,model=13"


Support
=======

For general information, go to the Intel support website at:

    http://support.intel.com

or the Intel Wired Networking project hosted by Sourceforge at:

    http://e1000.sourceforge.net

If an issue is identified with the released source code on the supported
kernel with a supported adapter, email the specific information related
to the issue to e1000-devel@lists.sf.net
href='#n1009'>1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
#!/usr/bin/env python

# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

import copy
import logging
import os
import unittest

import mock

from functest.utils import openstack_utils


class OSUtilsTesting(unittest.TestCase):

    logging.disable(logging.CRITICAL)

    def _get_env_cred_dict(self, os_prefix=''):
        return {'OS_USERNAME': os_prefix + 'username',
                'OS_PASSWORD': os_prefix + 'password',
                'OS_AUTH_URL': os_prefix + 'auth_url',
                'OS_TENANT_NAME': os_prefix + 'tenant_name',
                'OS_USER_DOMAIN_NAME': os_prefix + 'user_domain_name',
                'OS_PROJECT_DOMAIN_NAME': os_prefix + 'project_domain_name',
                'OS_PROJECT_NAME': os_prefix + 'project_name',
                'OS_ENDPOINT_TYPE': os_prefix + 'endpoint_type',
                'OS_REGION_NAME': os_prefix + 'region_name',
                'OS_CACERT': os_prefix + 'https_cacert'}

    def _get_os_env_vars(self):
        return {'username': 'test_username', 'password': 'test_password',
                'auth_url': 'test_auth_url', 'tenant_name': 'test_tenant_name',
                'user_domain_name': 'test_user_domain_name',
                'project_domain_name': 'test_project_domain_name',
                'project_name': 'test_project_name',
                'endpoint_type': 'test_endpoint_type',
                'region_name': 'test_region_name',
                'https_cacert': 'test_https_cacert'}

    def setUp(self):
        self.env_vars = ['OS_AUTH_URL', 'OS_USERNAME', 'OS_PASSWORD']
        self.tenant_name = 'test_tenant_name'
        self.env_cred_dict = self._get_env_cred_dict()
        self.os_environs = self._get_env_cred_dict(os_prefix='test_')
        self.os_env_vars = self._get_os_env_vars()

        mock_obj = mock.Mock()
        attrs = {'name': 'test_flavor',
                 'id': 'flavor_id',
                 'ram': 2}
        mock_obj.configure_mock(**attrs)
        self.flavor = mock_obj

        mock_obj = mock.Mock()
        attrs = {'name': 'test_aggregate',
                 'id': 'aggregate_id',
                 'hosts': ['host_name']}
        mock_obj.configure_mock(**attrs)
        self.aggregate = mock_obj

        mock_obj = mock.Mock()
        attrs = {'id': 'instance_id',
                 'name': 'test_instance',
                 'status': 'ok'}
        mock_obj.configure_mock(**attrs)
        self.instance = mock_obj

        mock_obj = mock.Mock()
        attrs = {'id': 'azone_id',
                 'zoneName': 'test_azone',
                 'status': 'ok'}
        mock_obj.configure_mock(**attrs)
        self.availability_zone = mock_obj

        mock_obj = mock.Mock()
        attrs = {'id': 'floating_id',
                 'zoneName': 'test_floating_ip',
                 'status': 'ok'}
        mock_obj.configure_mock(**attrs)
        self.floating_ip = mock_obj

        mock_obj = mock.Mock()
        attrs = {'id': 'hypervisor_id',
                 'hypervisor_hostname': 'test_hostname',
                 'state': 'up'}
        mock_obj.configure_mock(**attrs)
        self.hypervisor = mock_obj

        mock_obj = mock.Mock()
        attrs = {'id': 'image_id',
                 'name': 'test_image'}
        mock_obj.configure_mock(**attrs)
        self.image = mock_obj

        mock_obj = mock.Mock()
        self.mock_return = mock_obj

        self.nova_client = mock.Mock()
        attrs = {'servers.list.return_value': [self.instance],
                 'servers.get.return_value': self.instance,
                 'servers.find.return_value': self.instance,
                 'servers.create.return_value': self.instance,
                 'flavors.list.return_value': [self.flavor],
                 'flavors.find.return_value': self.flavor,
                 'servers.add_floating_ip.return_value': mock.Mock(),
                 'servers.force_delete.return_value': mock.Mock(),
                 'aggregates.list.return_value': [self.aggregate],
                 'aggregates.add_host.return_value': mock.Mock(),
                 'aggregates.remove_host.return_value': mock.Mock(),
                 'aggregates.get.return_value': self.aggregate,
                 'aggregates.delete.return_value': mock.Mock(),
                 'availability_zones.list.return_value':
                 [self.availability_zone],
                 'floating_ips.list.return_value': [self.floating_ip],
                 'floating_ips.delete.return_value': mock.Mock(),
                 'hypervisors.list.return_value': [self.hypervisor],
                 'create.return_value': mock.Mock(),
                 'add_security_group.return_value': mock.Mock(),
                 'images.list.return_value': [self.image],
                 'images.delete.return_value': mock.Mock(),
                 }
        self.nova_client.configure_mock(**attrs)

        self.glance_client = mock.Mock()
        attrs = {'images.list.return_value': [self.image],
                 'images.create.return_value': self.image,
                 'images.upload.return_value': mock.Mock()}
        self.glance_client.configure_mock(**attrs)

        mock_obj = mock.Mock()
        attrs = {'id': 'volume_id',
                 'name': 'test_volume'}
        mock_obj.configure_mock(**attrs)
        self.volume = mock_obj

        mock_obj = mock.Mock()
        attrs = {'id': 'volume_type_id',
                 'name': 'test_volume_type',
                 'is_public': True}
        mock_obj.configure_mock(**attrs)
        self.volume_types = [mock_obj]

        mock_obj = mock.Mock()
        attrs = {'id': 'volume_type_id',
                 'name': 'test_volume_type',
                 'is_public': False}
        mock_obj.configure_mock(**attrs)
        self.volume_types.append(mock_obj)

        self.cinder_client = mock.Mock()
        attrs = {'volumes.list.return_value': [self.volume],
                 'volume_types.list.return_value': self.volume_types,
                 'volume_types.create.return_value': self.volume_types[0],
                 'volume_types.delete.return_value': mock.Mock(),
                 'quotas.update.return_value': mock.Mock(),
                 'volumes.detach.return_value': mock.Mock(),
                 'volumes.force_delete.return_value': mock.Mock(),
                 'volumes.delete.return_value': mock.Mock()
                 }
        self.cinder_client.configure_mock(**attrs)

        self.resource = mock.Mock()
        attrs = {'id': 'resource_test_id',
                 'name': 'resource_test_name'
                 }

        self.heat_client = mock.Mock()
        attrs = {'resources.get.return_value': self.resource}
        self.heat_client.configure_mock(**attrs)

        mock_obj = mock.Mock()
        attrs = {'id': 'tenant_id',
                 'name': 'test_tenant'}
        mock_obj.configure_mock(**attrs)
        self.tenant = mock_obj

        mock_obj = mock.Mock()
        attrs = {'id': 'user_id',
                 'name': 'test_user'}
        mock_obj.configure_mock(**attrs)
        self.user = mock_obj

        mock_obj = mock.Mock()
        attrs = {'id': 'role_id',
                 'name': 'test_role'}
        mock_obj.configure_mock(**attrs)
        self.role = mock_obj

        self.keystone_client = mock.Mock()
        attrs = {'projects.list.return_value': [self.tenant],
                 'tenants.list.return_value': [self.tenant],
                 'users.list.return_value': [self.user],
                 'roles.list.return_value': [self.role],
                 'projects.create.return_value': self.tenant,
                 'tenants.create.return_value': self.tenant,
                 'users.create.return_value': self.user,
                 'roles.grant.return_value': mock.Mock(),
                 'roles.add_user_role.return_value': mock.Mock(),
                 'projects.delete.return_value': mock.Mock(),
                 'tenants.delete.return_value': mock.Mock(),
                 'users.delete.return_value': mock.Mock(),
                 }
        self.keystone_client.configure_mock(**attrs)

        self.router = {'id': 'router_id',
                       'name': 'test_router'}

        self.subnet = {'id': 'subnet_id',
                       'name': 'test_subnet'}

        self.networks = [{'id': 'network_id',
                          'name': 'test_network',
                          'router:external': False,
                          'shared': True,
                          'subnets': [self.subnet]},
                         {'id': 'network_id1',
                          'name': 'test_network1',
                          'router:external': True,
                          'shared': True,
                          'subnets': [self.subnet]}]

        self.port = {'id': 'port_id',
                     'name': 'test_port'}

        self.sec_group = {'id': 'sec_group_id',
                          'name': 'test_sec_group'}

        self.sec_group_rule = {'id': 'sec_group_rule_id',
                               'direction': 'direction',
                               'protocol': 'protocol',
                               'port_range_max': 'port_max',
                               'security_group_id': self.sec_group['id'],
                               'port_range_min': 'port_min'}
        self.neutron_floatingip = {'id': 'fip_id',
                                   'floating_ip_address': 'test_ip'}
        self.neutron_client = mock.Mock()
        attrs = {'list_networks.return_value': {'networks': self.networks},
                 'list_routers.return_value': {'routers': [self.router]},
                 'list_ports.return_value': {'ports': [self.port]},
                 'list_subnets.return_value': {'subnets': [self.subnet]},
                 'create_network.return_value': {'network': self.networks[0]},
                 'create_subnet.return_value': {'subnets': [self.subnet]},
                 'create_router.return_value': {'router': self.router},
                 'create_port.return_value': {'port': self.port},
                 'create_floatingip.return_value': {'floatingip':
                                                    self.neutron_floatingip},
                 'update_network.return_value': mock.Mock(),
                 'update_port.return_value': {'port': self.port},
                 'add_interface_router.return_value': mock.Mock(),
                 'add_gateway_router.return_value': mock.Mock(),
                 'delete_network.return_value': mock.Mock(),
                 'delete_subnet.return_value': mock.Mock(),
                 'delete_router.return_value': mock.Mock(),
                 'delete_port.return_value': mock.Mock(),
                 'remove_interface_router.return_value': mock.Mock(),
                 'remove_gateway_router.return_value': mock.Mock(),
                 'create_bgpvpn.return_value': self.mock_return,
                 'create_network_association.return_value': self.mock_return,
                 'create_router_association.return_value': self.mock_return,
                 'update_bgpvpn.return_value': self.mock_return,
                 'delete_bgpvpn.return_value': self.mock_return,
                 'show_bgpvpn.return_value': self.mock_return,
                 'list_security_groups.return_value': {'security_groups':
                                                       [self.sec_group]},
                 'list_security_group_rules.'
                 'return_value': {'security_group_rules':
                                  [self.sec_group_rule]},
                 'create_security_group_rule.return_value': mock.Mock(),
                 'create_security_group.return_value': {'security_group':
                                                        self.sec_group},
                 'update_quota.return_value': mock.Mock(),
                 'delete_security_group.return_value': mock.Mock()
                 }
        self.neutron_client.configure_mock(**attrs)

        self.empty_client = mock.Mock()
        attrs = {'list_networks.return_value': {'networks': []},
                 'list_routers.return_value': {'routers': []},
                 'list_ports.return_value': {'ports': []},
                 'list_subnets.return_value': {'subnets': []}}
        self.empty_client.configure_mock(**attrs)

    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value=None)
    def test_is_keystone_v3_missing_identity(self, mock_os_getenv):
        self.assertEqual(openstack_utils.is_keystone_v3(), False)

    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value='3')
    def test_is_keystone_v3_default(self, mock_os_getenv):
        self.assertEqual(openstack_utils.is_keystone_v3(), True)

    @mock.patch('functest.utils.openstack_utils.is_keystone_v3',
                return_value=False)
    def test_get_rc_env_vars_missing_identity(self, mock_get_rc_env):
        exp_resp = self.env_vars
        exp_resp.extend(['OS_TENANT_NAME'])
        self.assertEqual(openstack_utils.get_rc_env_vars(), exp_resp)

    @mock.patch('functest.utils.openstack_utils.is_keystone_v3',
                return_value=True)
    def test_get_rc_env_vars_default(self, mock_get_rc_env):
        exp_resp = self.env_vars
        exp_resp.extend(['OS_PROJECT_NAME',
                         'OS_USER_DOMAIN_NAME',
                         'OS_PROJECT_DOMAIN_NAME'])
        self.assertEqual(openstack_utils.get_rc_env_vars(), exp_resp)

    @mock.patch('functest.utils.openstack_utils.get_rc_env_vars')
    def test_check_credentials_missing_env(self, mock_get_rc_env):
        exp_resp = self.env_vars
        exp_resp.extend(['OS_TENANT_NAME'])
        mock_get_rc_env.return_value = exp_resp
        with mock.patch.dict('functest.utils.openstack_utils.os.environ', {},
                             clear=True):
            self.assertEqual(openstack_utils.check_credentials(), False)

    @mock.patch('functest.utils.openstack_utils.get_rc_env_vars')
    def test_check_credentials_default(self, mock_get_rc_env):
        exp_resp = ['OS_TENANT_NAME']
        mock_get_rc_env.return_value = exp_resp
        with mock.patch.dict('functest.utils.openstack_utils.os.environ',
                             {'OS_TENANT_NAME': self.tenant_name},
                             clear=True):
            self.assertEqual(openstack_utils.check_credentials(), True)

    def test_get_env_cred_dict(self):
        self.assertDictEqual(openstack_utils.get_env_cred_dict(),
                             self.env_cred_dict)

    @mock.patch('functest.utils.openstack_utils.get_rc_env_vars')
    def test_get_credentials_default(self, mock_get_rc_env):
        mock_get_rc_env.return_value = self.env_cred_dict.keys()
        with mock.patch.dict('functest.utils.openstack_utils.os.environ',
                             self.os_environs,
                             clear=True):
            self.assertDictEqual(openstack_utils.get_credentials(),
                                 self.os_env_vars)

    def _get_credentials_missing_env(self, var):
        dic = copy.deepcopy(self.os_environs)
        dic.pop(var)
        with mock.patch('functest.utils.openstack_utils.get_rc_env_vars',
                        return_value=self.env_cred_dict.keys()), \
                mock.patch.dict('functest.utils.openstack_utils.os.environ',
                                dic,
                                clear=True):
            self.assertRaises(openstack_utils.MissingEnvVar,
                              lambda: openstack_utils.get_credentials())

    def test_get_credentials_missing_username(self):
        self._get_credentials_missing_env('OS_USERNAME')

    def test_get_credentials_missing_password(self):
        self._get_credentials_missing_env('OS_PASSWORD')

    def test_get_credentials_missing_auth_url(self):
        self._get_credentials_missing_env('OS_AUTH_URL')

    def test_get_credentials_missing_tenantname(self):
        self._get_credentials_missing_env('OS_TENANT_NAME')

    def test_get_credentials_missing_domainname(self):
        self._get_credentials_missing_env('OS_USER_DOMAIN_NAME')

    def test_get_credentials_missing_projectname(self):
        self._get_credentials_missing_env('OS_PROJECT_NAME')

    def test_get_credentials_missing_endpoint_type(self):
        self._get_credentials_missing_env('OS_ENDPOINT_TYPE')

    def _test_source_credentials(self, msg, key='OS_TENANT_NAME',
                                 value='admin'):
        try:
            del os.environ[key]
        except:
            pass
        f = 'rc_file'
        with mock.patch('__builtin__.open', mock.mock_open(read_data=msg),
                        create=True) as m:
            m.return_value.__iter__ = lambda self: iter(self.readline, '')
            openstack_utils.source_credentials(f)
            m.assert_called_once_with(f, 'r')
            self.assertEqual(os.environ[key], value)

    def test_source_credentials(self):
        self._test_source_credentials('OS_TENANT_NAME=admin')
        self._test_source_credentials('OS_TENANT_NAME= admin')
        self._test_source_credentials('OS_TENANT_NAME = admin')
        self._test_source_credentials('OS_TENANT_NAME = "admin"')
        self._test_source_credentials('export OS_TENANT_NAME=admin')
        self._test_source_credentials('export OS_TENANT_NAME =admin')
        self._test_source_credentials('export OS_TENANT_NAME = admin')
        self._test_source_credentials('export OS_TENANT_NAME = "admin"')
        self._test_source_credentials('OS_TENANT_NAME', value='')
        self._test_source_credentials('export OS_TENANT_NAME', value='')
        # This test will fail as soon as rc_file is fixed
        self._test_source_credentials(
            'export "\'OS_TENANT_NAME\'" = "\'admin\'"')

    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value=None)
    def test_get_keystone_client_version_missing_env(self, mock_os_getenv):
        self.assertEqual(openstack_utils.get_keystone_client_version(),
                         openstack_utils.DEFAULT_API_VERSION)

    @mock.patch('functest.utils.openstack_utils.logger.info')
    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value='3')
    def test_get_keystone_client_version_default(self, mock_os_getenv,
                                                 mock_logger_info):
        self.assertEqual(openstack_utils.get_keystone_client_version(),
                         '3')
        mock_logger_info.assert_called_once_with("OS_IDENTITY_API_VERSION is "
                                                 "set in env as '%s'", '3')

    def test_get_keystone_client(self):
        mock_keystone_obj = mock.Mock()
        mock_session_obj = mock.Mock()
        with mock.patch('functest.utils.openstack_utils'
                        '.get_keystone_client_version', return_value='3'), \
            mock.patch('functest.utils.openstack_utils'
                       '.keystoneclient.Client',
                       return_value=mock_keystone_obj) \
            as mock_key_client, \
            mock.patch('functest.utils.openstack_utils.get_session',
                       return_value=mock_session_obj):
            self.assertEqual(openstack_utils.get_keystone_client(),
                             mock_keystone_obj)
            mock_key_client.assert_called_once_with('3',
                                                    session=mock_session_obj)

    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value=None)
    def test_get_nova_client_version_missing_env(self, mock_os_getenv):
        self.assertEqual(openstack_utils.get_nova_client_version(),
                         openstack_utils.DEFAULT_API_VERSION)

    @mock.patch('functest.utils.openstack_utils.logger.info')
    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value='3')
    def test_get_nova_client_version_default(self, mock_os_getenv,
                                             mock_logger_info):
        self.assertEqual(openstack_utils.get_nova_client_version(),
                         '3')
        mock_logger_info.assert_called_once_with("OS_COMPUTE_API_VERSION is "
                                                 "set in env as '%s'", '3')

    def test_get_nova_client(self):
        mock_nova_obj = mock.Mock()
        mock_session_obj = mock.Mock()
        with mock.patch('functest.utils.openstack_utils'
                        '.get_nova_client_version', return_value='3'), \
            mock.patch('functest.utils.openstack_utils'
                       '.novaclient.Client',
                       return_value=mock_nova_obj) \
            as mock_nova_client, \
            mock.patch('functest.utils.openstack_utils.get_session',
                       return_value=mock_session_obj):
            self.assertEqual(openstack_utils.get_nova_client(),
                             mock_nova_obj)
            mock_nova_client.assert_called_once_with('3',
                                                     session=mock_session_obj)

    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value=None)
    def test_get_cinder_client_version_missing_env(self, mock_os_getenv):
        self.assertEqual(openstack_utils.get_cinder_client_version(),
                         openstack_utils.DEFAULT_API_VERSION)

    @mock.patch('functest.utils.openstack_utils.logger.info')
    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value='3')
    def test_get_cinder_client_version_default(self, mock_os_getenv,
                                               mock_logger_info):
        self.assertEqual(openstack_utils.get_cinder_client_version(),
                         '3')
        mock_logger_info.assert_called_once_with("OS_VOLUME_API_VERSION is "
                                                 "set in env as '%s'", '3')

    def test_get_cinder_client(self):
        mock_cinder_obj = mock.Mock()
        mock_session_obj = mock.Mock()
        with mock.patch('functest.utils.openstack_utils'
                        '.get_cinder_client_version', return_value='3'), \
            mock.patch('functest.utils.openstack_utils'
                       '.cinderclient.Client',
                       return_value=mock_cinder_obj) \
            as mock_cind_client, \
            mock.patch('functest.utils.openstack_utils.get_session',
                       return_value=mock_session_obj):
            self.assertEqual(openstack_utils.get_cinder_client(),
                             mock_cinder_obj)
            mock_cind_client.assert_called_once_with('3',
                                                     session=mock_session_obj)

    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value=None)
    def test_get_neutron_client_version_missing_env(self, mock_os_getenv):
        self.assertEqual(openstack_utils.get_neutron_client_version(),
                         openstack_utils.DEFAULT_API_VERSION)

    @mock.patch('functest.utils.openstack_utils.logger.info')
    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value='3')
    def test_get_neutron_client_version_default(self, mock_os_getenv,
                                                mock_logger_info):
        self.assertEqual(openstack_utils.get_neutron_client_version(),
                         '3')
        mock_logger_info.assert_called_once_with("OS_NETWORK_API_VERSION is "
                                                 "set in env as '%s'", '3')

    def test_get_neutron_client(self):
        mock_neutron_obj = mock.Mock()
        mock_session_obj = mock.Mock()
        with mock.patch('functest.utils.openstack_utils'
                        '.get_neutron_client_version', return_value='3'), \
            mock.patch('functest.utils.openstack_utils'
                       '.neutronclient.Client',
                       return_value=mock_neutron_obj) \
            as mock_neut_client, \
            mock.patch('functest.utils.openstack_utils.get_session',
                       return_value=mock_session_obj):
            self.assertEqual(openstack_utils.get_neutron_client(),
                             mock_neutron_obj)
            mock_neut_client.assert_called_once_with('3',
                                                     session=mock_session_obj)

    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value=None)
    def test_get_glance_client_version_missing_env(self, mock_os_getenv):
        self.assertEqual(openstack_utils.get_glance_client_version(),
                         openstack_utils.DEFAULT_API_VERSION)

    @mock.patch('functest.utils.openstack_utils.logger.info')
    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value='3')
    def test_get_glance_client_version_default(self, mock_os_getenv,
                                               mock_logger_info):
        self.assertEqual(openstack_utils.get_glance_client_version(),
                         '3')
        mock_logger_info.assert_called_once_with("OS_IMAGE_API_VERSION is "
                                                 "set in env as '%s'", '3')

    def test_get_glance_client(self):
        mock_glance_obj = mock.Mock()
        mock_session_obj = mock.Mock()
        with mock.patch('functest.utils.openstack_utils'
                        '.get_glance_client_version', return_value='3'), \
            mock.patch('functest.utils.openstack_utils'
                       '.glanceclient.Client',
                       return_value=mock_glance_obj) \
            as mock_glan_client, \
            mock.patch('functest.utils.openstack_utils.get_session',
                       return_value=mock_session_obj):
            self.assertEqual(openstack_utils.get_glance_client(),
                             mock_glance_obj)
            mock_glan_client.assert_called_once_with('3',
                                                     session=mock_session_obj)

    @mock.patch('functest.utils.openstack_utils.os.getenv',
                return_value=None)
    def test_get_heat_client_version_missing_env(self, mock_os_getenv):
        self.assertEqual(openstack_utils.get_heat_client_version(),
                         openstack_utils.DEFAULT_HEAT_API_VERSION)

    @mock.patch('functest.utils.openstack_utils.logger.info')
    @mock.patch('functest.utils.openstack_utils.os.getenv', return_value='1')
    def test_get_heat_client_version_default(self, mock_os_getenv,
                                             mock_logger_info):
        self.assertEqual(openstack_utils.get_heat_client_version(), '1')
        mock_logger_info.assert_called_once_with(
            "OS_ORCHESTRATION_API_VERSION is set in env as '%s'", '1')

    def test_get_heat_client(self):
        mock_heat_obj = mock.Mock()
        mock_session_obj = mock.Mock()
        with mock.patch('functest.utils.openstack_utils'
                        '.get_heat_client_version', return_value='1'), \
            mock.patch('functest.utils.openstack_utils'
                       '.heatclient.Client',
                       return_value=mock_heat_obj) \
            as mock_heat_client, \
            mock.patch('functest.utils.openstack_utils.get_session',
                       return_value=mock_session_obj):
            self.assertEqual(openstack_utils.get_heat_client(),
                             mock_heat_obj)
            mock_heat_client.assert_called_once_with('1',
                                                     session=mock_session_obj)

    def test_get_instances_default(self):
        self.assertEqual(openstack_utils.get_instances(self.nova_client),
                         [self.instance])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_instances_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             get_instances(Exception),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_get_instance_status_default(self):
        self.assertEqual(openstack_utils.get_instance_status(self.nova_client,
                                                             self.instance),
                         'ok')

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_instance_status_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             get_instance_status(Exception,
                                                 self.instance),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_get_instance_by_name_default(self):
        self.assertEqual(openstack_utils.
                         get_instance_by_name(self.nova_client,
                                              'test_instance'),
                         self.instance)

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_instance_by_name_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             get_instance_by_name(Exception,
                                                  'test_instance'),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_get_flavor_id_default(self):
        self.assertEqual(openstack_utils.
                         get_flavor_id(self.nova_client,
                                       'test_flavor'),
                         self.flavor.id)

    def test_get_flavor_id_by_ram_range_default(self):
        self.assertEqual(openstack_utils.
                         get_flavor_id_by_ram_range(self.nova_client,
                                                    1, 3),
                         self.flavor.id)

    def test_get_aggregates_default(self):
        self.assertEqual(openstack_utils.
                         get_aggregates(self.nova_client),
                         [self.aggregate])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_aggregates_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             get_aggregates(Exception),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_get_aggregate_id_default(self):
        with mock.patch('functest.utils.openstack_utils.get_aggregates',
                        return_value=[self.aggregate]):
            self.assertEqual(openstack_utils.
                             get_aggregate_id(self.nova_client,
                                              'test_aggregate'),
                             'aggregate_id')

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_aggregate_id_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             get_aggregate_id(Exception,
                                              'test_aggregate'),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_get_availability_zone_names_default(self):
        with mock.patch('functest.utils.openstack_utils'
                        '.get_availability_zones',
                        return_value=[self.availability_zone]):
            self.assertEqual(openstack_utils.
                             get_availability_zone_names(self.nova_client),
                             ['test_azone'])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_availability_zone_names_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             get_availability_zone_names(Exception),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_get_availability_zones_default(self):
            self.assertEqual(openstack_utils.
                             get_availability_zones(self.nova_client),
                             [self.availability_zone])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_availability_zones_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             get_availability_zones(Exception),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_get_floating_ips_default(self):
            self.assertEqual(openstack_utils.
                             get_floating_ips(self.nova_client),
                             [self.floating_ip])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_floating_ips_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             get_floating_ips(Exception),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_get_hypervisors_default(self):
            self.assertEqual(openstack_utils.
                             get_hypervisors(self.nova_client),
                             ['test_hostname'])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_hypervisors_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             get_hypervisors(Exception),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_create_aggregate_default(self):
            self.assertTrue(openstack_utils.
                            create_aggregate(self.nova_client,
                                             'test_aggregate',
                                             'azone'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_aggregate_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             create_aggregate(Exception,
                                              'test_aggregate',
                                              'azone'),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_add_host_to_aggregate_default(self):
        with mock.patch('functest.utils.openstack_utils.get_aggregate_id'):
                self.assertTrue(openstack_utils.
                                add_host_to_aggregate(self.nova_client,
                                                      'test_aggregate',
                                                      'test_hostname'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_add_host_to_aggregate_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             add_host_to_aggregate(Exception,
                                                   'test_aggregate',
                                                   'test_hostname'),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_create_aggregate_with_host_default(self):
        with mock.patch('functest.utils.openstack_utils.create_aggregate'), \
                mock.patch('functest.utils.openstack_utils.'
                           'add_host_to_aggregate'):
            self.assertTrue(openstack_utils.
                            create_aggregate_with_host(self.nova_client,
                                                       'test_aggregate',
                                                       'test_azone',
                                                       'test_hostname'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_aggregate_with_host_exception(self, mock_logger_error):
            with mock.patch('functest.utils.openstack_utils.create_aggregate',
                            side_effect=Exception):
                self.assertEqual(openstack_utils.
                                 create_aggregate_with_host(Exception,
                                                            'test_aggregate',
                                                            'test_azone',
                                                            'test_hostname'),
                                 None)
                self.assertTrue(mock_logger_error.called)

    def test_create_instance_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_nova_client',
                        return_value=self.nova_client):
            self.assertEqual(openstack_utils.
                             create_instance('test_flavor',
                                             'image_id',
                                             'network_id'),
                             self.instance)

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_instance_exception(self, mock_logger_error):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_nova_client',
                        return_value=self.nova_client):
            self.nova_client.flavors.find.side_effect = Exception
            self.assertEqual(openstack_utils.
                             create_instance('test_flavor',
                                             'image_id',
                                             'network_id'),
                             None)
            self.assertTrue(mock_logger_error)

    def test_create_floating_ip_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_external_net_id',
                        return_value='external_net_id'):
            exp_resp = {'fip_addr': 'test_ip', 'fip_id': 'fip_id'}
            self.assertEqual(openstack_utils.
                             create_floating_ip(self.neutron_client),
                             exp_resp)

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_floating_ip_exception(self, mock_logger_error):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_external_net_id',
                        return_value='external_net_id'):
            self.assertEqual(openstack_utils.
                             create_floating_ip(Exception),
                             None)
            self.assertTrue(mock_logger_error)

    def test_add_floating_ip_default(self):
        with mock.patch('functest.utils.openstack_utils.get_aggregate_id'):
                self.assertTrue(openstack_utils.
                                add_floating_ip(self.nova_client,
                                                'test_serverid',
                                                'test_floatingip_addr'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_add_floating_ip_exception(self, mock_logger_error):
            self.assertFalse(openstack_utils.
                             add_floating_ip(Exception,
                                             'test_serverid',
                                             'test_floatingip_addr'))
            self.assertTrue(mock_logger_error.called)

    def test_delete_instance_default(self):
            self.assertTrue(openstack_utils.
                            delete_instance(self.nova_client,
                                            'instance_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_instance_exception(self, mock_logger_error):
            self.assertFalse(openstack_utils.
                             delete_instance(Exception,
                                             'instance_id'))
            self.assertTrue(mock_logger_error.called)

    def test_delete_floating_ip_default(self):
            self.assertTrue(openstack_utils.
                            delete_floating_ip(self.nova_client,
                                               'floating_ip_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_floating_ip_exception(self, mock_logger_error):
            self.assertFalse(openstack_utils.
                             delete_floating_ip(Exception,
                                                'floating_ip_id'))
            self.assertTrue(mock_logger_error.called)

    def test_remove_host_from_aggregate_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_aggregate_id'):
                self.assertTrue(openstack_utils.
                                remove_host_from_aggregate(self.nova_client,
                                                           'agg_name',
                                                           'host_name'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_remove_host_from_aggregate_exception(self, mock_logger_error):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_aggregate_id', side_effect=Exception):
            self.assertFalse(openstack_utils.
                             remove_host_from_aggregate(self.nova_client,
                                                        'agg_name',
                                                        'host_name'))
            self.assertTrue(mock_logger_error.called)

    def test_remove_hosts_from_aggregate_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_aggregate_id'), \
            mock.patch('functest.utils.openstack_utils.'
                       'remove_host_from_aggregate',
                       return_value=True) \
                as mock_method:
            openstack_utils.remove_hosts_from_aggregate(self.nova_client,
                                                        'test_aggregate')
            mock_method.assert_any_call(self.nova_client,
                                        'test_aggregate',
                                        'host_name')

    def test_delete_aggregate_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'remove_hosts_from_aggregate'):
                self.assertTrue(openstack_utils.
                                delete_aggregate(self.nova_client,
                                                 'agg_name'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_aggregate_exception(self, mock_logger_error):
        with mock.patch('functest.utils.openstack_utils.'
                        'remove_hosts_from_aggregate', side_effect=Exception):
            self.assertFalse(openstack_utils.
                             delete_aggregate(self.nova_client,
                                              'agg_name'))
            self.assertTrue(mock_logger_error.called)

    def test_get_network_list_default(self):
        self.assertEqual(openstack_utils.
                         get_network_list(self.neutron_client),
                         self.networks)

    def test_get_network_list_missing_network(self):
        self.assertEqual(openstack_utils.
                         get_network_list(self.empty_client),
                         None)

    def test_get_router_list_default(self):
        self.assertEqual(openstack_utils.
                         get_router_list(self.neutron_client),
                         [self.router])

    def test_get_router_list_missing_router(self):
        self.assertEqual(openstack_utils.
                         get_router_list(self.empty_client),
                         None)

    def test_get_port_list_default(self):
        self.assertEqual(openstack_utils.
                         get_port_list(self.neutron_client),
                         [self.port])

    def test_get_port_list_missing_port(self):
        self.assertEqual(openstack_utils.
                         get_port_list(self.empty_client),
                         None)

    def test_get_network_id_default(self):
        self.assertEqual(openstack_utils.
                         get_network_id(self.neutron_client,
                                        'test_network'),
                         'network_id')

    def test_get_subnet_id_default(self):
        self.assertEqual(openstack_utils.
                         get_subnet_id(self.neutron_client,
                                       'test_subnet'),
                         'subnet_id')

    def test_get_router_id_default(self):
        self.assertEqual(openstack_utils.
                         get_router_id(self.neutron_client,
                                       'test_router'),
                         'router_id')

    def test_get_private_net_default(self):
        self.assertEqual(openstack_utils.
                         get_private_net(self.neutron_client),
                         self.networks[0])

    def test_get_private_net_missing_net(self):
        self.assertEqual(openstack_utils.
                         get_private_net(self.empty_client),
                         None)

    def test_get_external_net_default(self):
        self.assertEqual(openstack_utils.
                         get_external_net(self.neutron_client),
                         'test_network1')

    def test_get_external_net_missing_net(self):
        self.assertEqual(openstack_utils.
                         get_external_net(self.empty_client),
                         None)

    def test_get_external_net_id_default(self):
        self.assertEqual(openstack_utils.
                         get_external_net_id(self.neutron_client),
                         'network_id1')

    def test_get_external_net_id_missing_net(self):
        self.assertEqual(openstack_utils.
                         get_external_net_id(self.empty_client),
                         None)

    def test_check_neutron_net_default(self):
        self.assertTrue(openstack_utils.
                        check_neutron_net(self.neutron_client,
                                          'test_network'))

    def test_check_neutron_net_missing_net(self):
        self.assertFalse(openstack_utils.
                         check_neutron_net(self.empty_client,
                                           'test_network'))

    def test_create_neutron_net_default(self):
            self.assertEqual(openstack_utils.
                             create_neutron_net(self.neutron_client,
                                                'test_network'),
                             'network_id')

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_neutron_net_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             create_neutron_net(Exception,
                                                'test_network'),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_create_neutron_subnet_default(self):
            self.assertEqual(openstack_utils.
                             create_neutron_subnet(self.neutron_client,
                                                   'test_subnet',
                                                   'test_cidr',
                                                   'network_id'),
                             'subnet_id')

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_neutron_subnet_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             create_neutron_subnet(Exception,
                                                   'test_subnet',
                                                   'test_cidr',
                                                   'network_id'),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_create_neutron_router_default(self):
            self.assertEqual(openstack_utils.
                             create_neutron_router(self.neutron_client,
                                                   'test_router'),
                             'router_id')

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_neutron_router_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             create_neutron_router(Exception,
                                                   'test_router'),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_create_neutron_port_default(self):
            self.assertEqual(openstack_utils.
                             create_neutron_port(self.neutron_client,
                                                 'test_port',
                                                 'network_id',
                                                 'test_ip'),
                             'port_id')

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_neutron_port_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             create_neutron_port(Exception,
                                                 'test_port',
                                                 'network_id',
                                                 'test_ip'),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_update_neutron_net_default(self):
            self.assertTrue(openstack_utils.
                            update_neutron_net(self.neutron_client,
                                               'network_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_update_neutron_net_exception(self, mock_logger_error):
            self.assertFalse(openstack_utils.
                             update_neutron_net(Exception,
                                                'network_id'))
            self.assertTrue(mock_logger_error.called)

    def test_update_neutron_port_default(self):
            self.assertEqual(openstack_utils.
                             update_neutron_port(self.neutron_client,
                                                 'port_id',
                                                 'test_owner'),
                             'port_id')

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_update_neutron_port_exception(self, mock_logger_error):
            self.assertEqual(openstack_utils.
                             update_neutron_port(Exception,
                                                 'port_id',
                                                 'test_owner'),
                             None)
            self.assertTrue(mock_logger_error.called)

    def test_add_interface_router_default(self):
            self.assertTrue(openstack_utils.
                            add_interface_router(self.neutron_client,
                                                 'router_id',
                                                 'subnet_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_add_interface_router_exception(self, mock_logger_error):
            self.assertFalse(openstack_utils.
                             add_interface_router(Exception,
                                                  'router_id',
                                                  'subnet_id'))
            self.assertTrue(mock_logger_error.called)

    def test_add_gateway_router_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_external_net_id',
                        return_value='network_id'):
                self.assertTrue(openstack_utils.
                                add_gateway_router(self.neutron_client,
                                                   'router_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_add_gateway_router_exception(self, mock_logger_error):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_external_net_id',
                        return_value='network_id'):
                self.assertFalse(openstack_utils.
                                 add_gateway_router(Exception,
                                                    'router_id'))
                self.assertTrue(mock_logger_error.called)

    def test_delete_neutron_net_default(self):
            self.assertTrue(openstack_utils.
                            delete_neutron_net(self.neutron_client,
                                               'network_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_neutron_net_exception(self, mock_logger_error):
            self.assertFalse(openstack_utils.
                             delete_neutron_net(Exception,
                                                'network_id'))
            self.assertTrue(mock_logger_error.called)

    def test_delete_neutron_subnet_default(self):
            self.assertTrue(openstack_utils.
                            delete_neutron_subnet(self.neutron_client,
                                                  'subnet_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_neutron_subnet_exception(self, mock_logger_error):
            self.assertFalse(openstack_utils.
                             delete_neutron_subnet(Exception,
                                                   'subnet_id'))
            self.assertTrue(mock_logger_error.called)

    def test_delete_neutron_router_default(self):
            self.assertTrue(openstack_utils.
                            delete_neutron_router(self.neutron_client,
                                                  'router_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_neutron_router_exception(self, mock_logger_error):
            self.assertFalse(openstack_utils.
                             delete_neutron_router(Exception,
                                                   'router_id'))
            self.assertTrue(mock_logger_error.called)

    def test_delete_neutron_port_default(self):
            self.assertTrue(openstack_utils.
                            delete_neutron_port(self.neutron_client,
                                                'port_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_neutron_port_exception(self, mock_logger_error):
            self.assertFalse(openstack_utils.
                             delete_neutron_port(Exception,
                                                 'port_id'))
            self.assertTrue(mock_logger_error.called)

    def test_remove_interface_router_default(self):
            self.assertTrue(openstack_utils.
                            remove_interface_router(self.neutron_client,
                                                    'router_id',
                                                    'subnet_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_remove_interface_router_exception(self, mock_logger_error):
            self.assertFalse(openstack_utils.
                             remove_interface_router(Exception,
                                                     'router_id',
                                                     'subnet_id'))
            self.assertTrue(mock_logger_error.called)

    def test_remove_gateway_router_default(self):
            self.assertTrue(openstack_utils.
                            remove_gateway_router(self.neutron_client,
                                                  'router_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_remove_gateway_router_exception(self, mock_logger_error):
            self.assertFalse(openstack_utils.
                             remove_gateway_router(Exception,
                                                   'router_id'))
            self.assertTrue(mock_logger_error.called)

    def test_create_bgpvpn(self):
        self.assertEqual(openstack_utils.
                         create_bgpvpn(self.neutron_client),
                         self.mock_return)

    def test_create_network_association(self):
        self.assertEqual(openstack_utils.
                         create_network_association(self.neutron_client,
                                                    'bgpvpn_id',
                                                    'network_id'),
                         self.mock_return)

    def test_create_router_association(self):
        self.assertEqual(openstack_utils.
                         create_router_association(self.neutron_client,
                                                   'bgpvpn_id',
                                                   'router_id'),
                         self.mock_return)

    def test_update_bgpvpn(self):
        self.assertEqual(openstack_utils.
                         update_bgpvpn(self.neutron_client,
                                       'bgpvpn_id'),
                         self.mock_return)

    def test_delete_bgpvpn(self):
        self.assertEqual(openstack_utils.
                         delete_bgpvpn(self.neutron_client,
                                       'bgpvpn_id'),
                         self.mock_return)

    def test_get_bgpvpn(self):
        self.assertEqual(openstack_utils.
                         get_bgpvpn(self.neutron_client,
                                    'bgpvpn_id'),
                         self.mock_return)

    def test_get_bgpvpn_routers(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_bgpvpn',
                        return_value={'bgpvpn':
                                      {'routers': [self.router]}}):
            self.assertEqual(openstack_utils.
                             get_bgpvpn_routers(self.neutron_client,
                                                'bgpvpn_id'),
                             [self.router])

    def test_get_security_groups_default(self):
        self.assertEqual(openstack_utils.
                         get_security_groups(self.neutron_client),
                         [self.sec_group])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_security_groups_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         get_security_groups(Exception),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_get_security_group_id_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_security_groups',
                        return_value=[self.sec_group]):
            self.assertEqual(openstack_utils.
                             get_security_group_id(self.neutron_client,
                                                   'test_sec_group'),
                             'sec_group_id')

    def test_get_security_group_rules_default(self):
        self.assertEqual(openstack_utils.
                         get_security_group_rules(self.neutron_client,
                                                  self.sec_group['id']),
                         [self.sec_group_rule])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_security_group_rules_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         get_security_group_rules(Exception,
                                                  'sec_group_id'),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_check_security_group_rules_not_exists(self):
        self.assertEqual(openstack_utils.
                         check_security_group_rules(self.neutron_client,
                                                    'sec_group_id_2',
                                                    'direction',
                                                    'protocol',
                                                    'port_min',
                                                    'port_max'),
                         True)

    def test_check_security_group_rules_exists(self):
        self.assertEqual(openstack_utils.
                         check_security_group_rules(self.neutron_client,
                                                    self.sec_group['id'],
                                                    'direction',
                                                    'protocol',
                                                    'port_min',
                                                    'port_max'),
                         False)

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_check_security_group_rules_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         check_security_group_rules(Exception,
                                                    'sec_group_id',
                                                    'direction',
                                                    'protocol',
                                                    'port_max',
                                                    'port_min'),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_create_security_group_default(self):
        self.assertEqual(openstack_utils.
                         create_security_group(self.neutron_client,
                                               'test_sec_group',
                                               'sec_group_desc'),
                         self.sec_group)

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_security_group_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         create_security_group(Exception,
                                               'test_sec_group',
                                               'sec_group_desc'),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_create_secgroup_rule_default(self):
        self.assertTrue(openstack_utils.
                        create_secgroup_rule(self.neutron_client,
                                             'sg_id',
                                             'direction',
                                             'protocol',
                                             80,
                                             80))
        self.assertTrue(openstack_utils.
                        create_secgroup_rule(self.neutron_client,
                                             'sg_id',
                                             'direction',
                                             'protocol'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_secgroup_rule_invalid_port_range(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         create_secgroup_rule(self.neutron_client,
                                              'sg_id',
                                              'direction',
                                              'protocol',
                                              80))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_secgroup_rule_exception(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         create_secgroup_rule(Exception,
                                              'sg_id',
                                              'direction',
                                              'protocol'))

    @mock.patch('functest.utils.openstack_utils.logger.info')
    def test_create_security_group_full_default(self, mock_logger_info):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_security_group_id',
                        return_value='sg_id'):
            self.assertEqual(openstack_utils.
                             create_security_group_full(self.neutron_client,
                                                        'sg_name',
                                                        'sg_desc'),
                             'sg_id')
            self.assertTrue(mock_logger_info)

    @mock.patch('functest.utils.openstack_utils.logger.info')
    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_security_group_full_sec_group_fail(self,
                                                       mock_logger_error,
                                                       mock_logger_info):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_security_group_id',
                        return_value=''), \
            mock.patch('functest.utils.openstack_utils.'
                       'create_security_group',
                       return_value=False):
            self.assertEqual(openstack_utils.
                             create_security_group_full(self.neutron_client,
                                                        'sg_name',
                                                        'sg_desc'),
                             None)
            self.assertTrue(mock_logger_error)
            self.assertTrue(mock_logger_info)

    @mock.patch('functest.utils.openstack_utils.logger.debug')
    @mock.patch('functest.utils.openstack_utils.logger.info')
    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_security_group_full_secgroup_rule_fail(self,
                                                           mock_logger_error,
                                                           mock_logger_info,
                                                           mock_logger_debug):
        with mock.patch('functest.utils.openstack_utils.'
                        'get_security_group_id',
                        return_value=''), \
            mock.patch('functest.utils.openstack_utils.'
                       'create_security_group',
                       return_value={'id': 'sg_id',
                                     'name': 'sg_name'}), \
            mock.patch('functest.utils.openstack_utils.'
                       'create_secgroup_rule',
                       return_value=False):
            self.assertEqual(openstack_utils.
                             create_security_group_full(self.neutron_client,
                                                        'sg_name',
                                                        'sg_desc'),
                             None)
            self.assertTrue(mock_logger_error)
            self.assertTrue(mock_logger_info)
            self.assertTrue(mock_logger_debug)

    def test_add_secgroup_to_instance_default(self):
        self.assertTrue(openstack_utils.
                        add_secgroup_to_instance(self.nova_client,
                                                 'instance_id',
                                                 'sec_group_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_add_secgroup_to_instance_exception(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         add_secgroup_to_instance(Exception,
                                                  'instance_id',
                                                  'sec_group_id'))
        self.assertTrue(mock_logger_error.called)

    def test_update_sg_quota_default(self):
        self.assertTrue(openstack_utils.
                        update_sg_quota(self.neutron_client,
                                        'tenant_id',
                                        'sg_quota',
                                        'sg_rule_quota'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_update_sg_quota_exception(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         update_sg_quota(Exception,
                                         'tenant_id',
                                         'sg_quota',
                                         'sg_rule_quota'))
        self.assertTrue(mock_logger_error.called)

    def test_delete_security_group_default(self):
        self.assertTrue(openstack_utils.
                        delete_security_group(self.neutron_client,
                                              'sec_group_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_security_group_exception(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         delete_security_group(Exception,
                                               'sec_group_id'))
        self.assertTrue(mock_logger_error.called)

    def test_get_images_default(self):
        self.assertEqual(openstack_utils.
                         get_images(self.nova_client),
                         [self.image])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_images_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         get_images(Exception),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_get_image_id_default(self):
        self.assertEqual(openstack_utils.
                         get_image_id(self.glance_client,
                                      'test_image'),
                         'image_id')

    # create_glance_image, get_or_create_image
    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_glance_image_file_present(self, mock_logger_error):
        with mock.patch('functest.utils.openstack_utils.'
                        'os.path.isfile',
                        return_value=False):
            self.assertEqual(openstack_utils.
                             create_glance_image(self.glance_client,
                                                 'test_image',
                                                 'file_path'),
                             None)
            self.assertTrue(mock_logger_error.called)

    @mock.patch('functest.utils.openstack_utils.logger.info')
    def test_create_glance_image_already_exist(self, mock_logger_info):
        with mock.patch('functest.utils.openstack_utils.'
                        'os.path.isfile',
                        return_value=True), \
            mock.patch('functest.utils.openstack_utils.get_image_id',
                       return_value='image_id'):
                self.assertEqual(openstack_utils.
                                 create_glance_image(self.glance_client,
                                                     'test_image',
                                                     'file_path'),
                                 'image_id')
                self.assertTrue(mock_logger_info.called)

    @mock.patch('functest.utils.openstack_utils.logger.info')
    def test_create_glance_image_default(self, mock_logger_info):
        with mock.patch('functest.utils.openstack_utils.'
                        'os.path.isfile',
                        return_value=True), \
            mock.patch('functest.utils.openstack_utils.get_image_id',
                       return_value=''), \
            mock.patch('__builtin__.open',
                       mock.mock_open(read_data='1')) as m:
                self.assertEqual(openstack_utils.
                                 create_glance_image(self.glance_client,
                                                     'test_image',
                                                     'file_path'),
                                 'image_id')
                m.assert_called_once_with('file_path')
                self.assertTrue(mock_logger_info.called)

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_glance_image_exception(self, mock_logger_error):
        with mock.patch('functest.utils.openstack_utils.'
                        'os.path.isfile',
                        return_value=True), \
            mock.patch('functest.utils.openstack_utils.get_image_id',
                       side_effect=Exception):
                self.assertEqual(openstack_utils.
                                 create_glance_image(self.glance_client,
                                                     'test_image',
                                                     'file_path'),
                                 None)
                self.assertTrue(mock_logger_error.called)

    def test_delete_glance_image_default(self):
        self.assertTrue(openstack_utils.
                        delete_glance_image(self.nova_client,
                                            'image_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_glance_image_exception(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         delete_glance_image(Exception,
                                             'image_id'))
        self.assertTrue(mock_logger_error.called)

    def test_get_volumes_default(self):
        self.assertEqual(openstack_utils.
                         get_volumes(self.cinder_client),
                         [self.volume])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_volumes_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         get_volumes(Exception),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_list_volume_types_default_private(self):
        self.assertEqual(openstack_utils.
                         list_volume_types(self.cinder_client,
                                           public=False,
                                           private=True),
                         [self.volume_types[1]])

    def test_list_volume_types_default_public(self):
        self.assertEqual(openstack_utils.
                         list_volume_types(self.cinder_client,
                                           public=True,
                                           private=False),
                         [self.volume_types[0]])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_list_volume_types_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         list_volume_types(Exception),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_create_volume_type_default(self):
        self.assertEqual(openstack_utils.
                         create_volume_type(self.cinder_client,
                                            'test_volume_type'),
                         self.volume_types[0])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_volume_type_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         create_volume_type(Exception,
                                            'test_volume_type'),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_update_cinder_quota_default(self):
        self.assertTrue(openstack_utils.
                        update_cinder_quota(self.cinder_client,
                                            'tenant_id',
                                            'vols_quota',
                                            'snap_quota',
                                            'giga_quota'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_update_cinder_quota_exception(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         update_cinder_quota(Exception,
                                             'tenant_id',
                                             'vols_quota',
                                             'snap_quota',
                                             'giga_quota'))
        self.assertTrue(mock_logger_error.called)

    def test_delete_volume_default(self):
        self.assertTrue(openstack_utils.
                        delete_volume(self.cinder_client,
                                      'volume_id',
                                      forced=False))

        self.assertTrue(openstack_utils.
                        delete_volume(self.cinder_client,
                                      'volume_id',
                                      forced=True))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_volume_exception(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         delete_volume(Exception,
                                       'volume_id',
                                       forced=True))
        self.assertTrue(mock_logger_error.called)

    def test_delete_volume_type_default(self):
        self.assertTrue(openstack_utils.
                        delete_volume_type(self.cinder_client,
                                           self.volume_types[0]))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_volume_type_exception(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         delete_volume_type(Exception,
                                            self.volume_types[0]))
        self.assertTrue(mock_logger_error.called)

    def test_get_tenants_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'is_keystone_v3', return_value=True):
            self.assertEqual(openstack_utils.
                             get_tenants(self.keystone_client),
                             [self.tenant])
        with mock.patch('functest.utils.openstack_utils.'
                        'is_keystone_v3', return_value=False):
            self.assertEqual(openstack_utils.
                             get_tenants(self.keystone_client),
                             [self.tenant])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_tenants_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         get_tenants(Exception),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_get_users_default(self):
        self.assertEqual(openstack_utils.
                         get_users(self.keystone_client),
                         [self.user])

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_users_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         get_users(Exception),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_get_tenant_id_default(self):
        self.assertEqual(openstack_utils.
                         get_tenant_id(self.keystone_client,
                                       'test_tenant'),
                         'tenant_id')

    def test_get_user_id_default(self):
        self.assertEqual(openstack_utils.
                         get_user_id(self.keystone_client,
                                     'test_user'),
                         'user_id')

    def test_get_role_id_default(self):
        self.assertEqual(openstack_utils.
                         get_role_id(self.keystone_client,
                                     'test_role'),
                         'role_id')

    def test_create_tenant_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'is_keystone_v3', return_value=True):
            self.assertEqual(openstack_utils.
                             create_tenant(self.keystone_client,
                                           'test_tenant',
                                           'tenant_desc'),
                             'tenant_id')
        with mock.patch('functest.utils.openstack_utils.'
                        'is_keystone_v3', return_value=False):
            self.assertEqual(openstack_utils.
                             create_tenant(self.keystone_client,
                                           'test_tenant',
                                           'tenant_desc'),
                             'tenant_id')

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_tenant_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         create_tenant(Exception,
                                       'test_tenant',
                                       'tenant_desc'),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_create_user_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'is_keystone_v3', return_value=True):
            self.assertEqual(openstack_utils.
                             create_user(self.keystone_client,
                                         'test_user',
                                         'password',
                                         'email',
                                         'tenant_id'),
                             'user_id')
        with mock.patch('functest.utils.openstack_utils.'
                        'is_keystone_v3', return_value=False):
            self.assertEqual(openstack_utils.
                             create_user(self.keystone_client,
                                         'test_user',
                                         'password',
                                         'email',
                                         'tenant_id'),
                             'user_id')

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_create_user_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         create_user(Exception,
                                     'test_user',
                                     'password',
                                     'email',
                                     'tenant_id'),
                         None)
        self.assertTrue(mock_logger_error.called)

    def test_add_role_user_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'is_keystone_v3', return_value=True):
            self.assertTrue(openstack_utils.
                            add_role_user(self.keystone_client,
                                          'user_id',
                                          'role_id',
                                          'tenant_id'))

        with mock.patch('functest.utils.openstack_utils.'
                        'is_keystone_v3', return_value=False):
            self.assertTrue(openstack_utils.
                            add_role_user(self.keystone_client,
                                          'user_id',
                                          'role_id',
                                          'tenant_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_add_role_user_exception(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         add_role_user(Exception,
                                       'user_id',
                                       'role_id',
                                       'tenant_id'))
        self.assertTrue(mock_logger_error.called)

    def test_delete_tenant_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'is_keystone_v3', return_value=True):
            self.assertTrue(openstack_utils.
                            delete_tenant(self.keystone_client,
                                          'tenant_id'))

        with mock.patch('functest.utils.openstack_utils.'
                        'is_keystone_v3', return_value=False):
            self.assertTrue(openstack_utils.
                            delete_tenant(self.keystone_client,
                                          'tenant_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_tenant_exception(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         delete_tenant(Exception,
                                       'tenant_id'))
        self.assertTrue(mock_logger_error.called)

    def test_delete_user_default(self):
        self.assertTrue(openstack_utils.
                        delete_user(self.keystone_client,
                                    'user_id'))

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_delete_user_exception(self, mock_logger_error):
        self.assertFalse(openstack_utils.
                         delete_user(Exception,
                                     'user_id'))
        self.assertTrue(mock_logger_error.called)

    def test_get_resource_default(self):
        with mock.patch('functest.utils.openstack_utils.'
                        'is_keystone_v3', return_value=True):
            self.assertEqual(openstack_utils.
                             get_resource(self.heat_client,
                                          'stack_id',
                                          'resource'),
                             self.resource)

    @mock.patch('functest.utils.openstack_utils.logger.error')
    def test_get_resource_exception(self, mock_logger_error):
        self.assertEqual(openstack_utils.
                         get_resource(Exception,
                                      'stack_id',
                                      'resource'),
                         None)
        self.assertTrue(mock_logger_error.called)


if __name__ == "__main__":
    unittest.main(verbosity=2)