Browse Source

[tests] initialize client objects inside setUp

All tests from contrib dir inizialize fakeclient at module level.
It is bad practice and we should do preparatory steps at setUp method.

Change-Id: I2dd2988cd015d1dfb576fe004cb9461b02b55db3
tags/4.0.0
Andrey Kurilin 3 years ago
parent
commit
7d8db71964

+ 11
- 11
novaclient/tests/unit/v2/contrib/test_assisted_volume_snapshots.py View File

@@ -22,23 +22,23 @@ from novaclient.tests.unit.v2.contrib import fakes
22 22
 from novaclient.v2.contrib import assisted_volume_snapshots as assisted_snaps
23 23
 
24 24
 
25
-extensions = [
26
-    extension.Extension(assisted_snaps.__name__.split(".")[-1],
27
-                        assisted_snaps),
28
-]
29
-cs = fakes.FakeClient(extensions=extensions)
30
-
31
-
32 25
 class AssistedVolumeSnapshotsTestCase(utils.TestCase):
26
+    def setUp(self):
27
+        super(AssistedVolumeSnapshotsTestCase, self).setUp()
28
+        extensions = [
29
+            extension.Extension(assisted_snaps.__name__.split(".")[-1],
30
+                                assisted_snaps),
31
+        ]
32
+        self.cs = fakes.FakeClient(extensions=extensions)
33 33
 
34 34
     def test_create_snap(self):
35
-        vs = cs.assisted_volume_snapshots.create('1', {})
35
+        vs = self.cs.assisted_volume_snapshots.create('1', {})
36 36
         self.assert_request_id(vs, fakes.FAKE_REQUEST_ID_LIST)
37
-        cs.assert_called('POST', '/os-assisted-volume-snapshots')
37
+        self.cs.assert_called('POST', '/os-assisted-volume-snapshots')
38 38
 
39 39
     def test_delete_snap(self):
40
-        vs = cs.assisted_volume_snapshots.delete('x', {})
40
+        vs = self.cs.assisted_volume_snapshots.delete('x', {})
41 41
         self.assert_request_id(vs, fakes.FAKE_REQUEST_ID_LIST)
42
-        cs.assert_called(
42
+        self.cs.assert_called(
43 43
             'DELETE',
44 44
             '/os-assisted-volume-snapshots/x?delete_info={}')

+ 22
- 22
novaclient/tests/unit/v2/contrib/test_baremetal.py View File

@@ -20,52 +20,52 @@ from novaclient.tests.unit.v2.contrib import fakes
20 20
 from novaclient.v2.contrib import baremetal
21 21
 
22 22
 
23
-extensions = [
24
-    extension.Extension(baremetal.__name__.split(".")[-1], baremetal),
25
-]
26
-cs = fakes.FakeClient(extensions=extensions)
27
-
28
-
29 23
 class BaremetalExtensionTest(utils.TestCase):
24
+    def setUp(self):
25
+        super(BaremetalExtensionTest, self).setUp()
26
+        extensions = [
27
+            extension.Extension(baremetal.__name__.split(".")[-1], baremetal),
28
+        ]
29
+        self.cs = fakes.FakeClient(extensions=extensions)
30 30
 
31 31
     def test_list_nodes(self):
32
-        nl = cs.baremetal.list()
32
+        nl = self.cs.baremetal.list()
33 33
         self.assert_request_id(nl, fakes.FAKE_REQUEST_ID_LIST)
34
-        cs.assert_called('GET', '/os-baremetal-nodes')
34
+        self.cs.assert_called('GET', '/os-baremetal-nodes')
35 35
         for n in nl:
36 36
             self.assertIsInstance(n, baremetal.BareMetalNode)
37 37
 
38 38
     def test_get_node(self):
39
-        n = cs.baremetal.get(1)
39
+        n = self.cs.baremetal.get(1)
40 40
         self.assert_request_id(n, fakes.FAKE_REQUEST_ID_LIST)
41
-        cs.assert_called('GET', '/os-baremetal-nodes/1')
41
+        self.cs.assert_called('GET', '/os-baremetal-nodes/1')
42 42
         self.assertIsInstance(n, baremetal.BareMetalNode)
43 43
 
44 44
     def test_create_node(self):
45
-        n = cs.baremetal.create("service_host", 1, 1024, 2048,
46
-                                "aa:bb:cc:dd:ee:ff")
45
+        n = self.cs.baremetal.create("service_host", 1, 1024, 2048,
46
+                                     "aa:bb:cc:dd:ee:ff")
47 47
         self.assert_request_id(n, fakes.FAKE_REQUEST_ID_LIST)
48
-        cs.assert_called('POST', '/os-baremetal-nodes')
48
+        self.cs.assert_called('POST', '/os-baremetal-nodes')
49 49
         self.assertIsInstance(n, baremetal.BareMetalNode)
50 50
 
51 51
     def test_delete_node(self):
52
-        n = cs.baremetal.get(1)
53
-        ret = cs.baremetal.delete(n)
52
+        n = self.cs.baremetal.get(1)
53
+        ret = self.cs.baremetal.delete(n)
54 54
         self.assert_request_id(ret, fakes.FAKE_REQUEST_ID_LIST)
55
-        cs.assert_called('DELETE', '/os-baremetal-nodes/1')
55
+        self.cs.assert_called('DELETE', '/os-baremetal-nodes/1')
56 56
 
57 57
     def test_node_add_interface(self):
58
-        i = cs.baremetal.add_interface(1, "bb:cc:dd:ee:ff:aa", 1, 2)
58
+        i = self.cs.baremetal.add_interface(1, "bb:cc:dd:ee:ff:aa", 1, 2)
59 59
         self.assert_request_id(i, fakes.FAKE_REQUEST_ID_LIST)
60
-        cs.assert_called('POST', '/os-baremetal-nodes/1/action')
60
+        self.cs.assert_called('POST', '/os-baremetal-nodes/1/action')
61 61
         self.assertIsInstance(i, baremetal.BareMetalNodeInterface)
62 62
 
63 63
     def test_node_remove_interface(self):
64
-        ret = cs.baremetal.remove_interface(1, "bb:cc:dd:ee:ff:aa")
64
+        ret = self.cs.baremetal.remove_interface(1, "bb:cc:dd:ee:ff:aa")
65 65
         self.assert_request_id(ret, fakes.FAKE_REQUEST_ID_LIST)
66
-        cs.assert_called('POST', '/os-baremetal-nodes/1/action')
66
+        self.cs.assert_called('POST', '/os-baremetal-nodes/1/action')
67 67
 
68 68
     def test_node_list_interfaces(self):
69
-        il = cs.baremetal.list_interfaces(1)
69
+        il = self.cs.baremetal.list_interfaces(1)
70 70
         self.assert_request_id(il, fakes.FAKE_REQUEST_ID_LIST)
71
-        cs.assert_called('GET', '/os-baremetal-nodes/1')
71
+        self.cs.assert_called('GET', '/os-baremetal-nodes/1')

+ 14
- 13
novaclient/tests/unit/v2/contrib/test_cells.py View File

@@ -19,27 +19,28 @@ from novaclient.tests.unit.v2.contrib import fakes
19 19
 from novaclient.v2.contrib import cells
20 20
 
21 21
 
22
-extensions = [
23
-    extension.Extension(cells.__name__.split(".")[-1],
24
-                        cells),
25
-]
26
-cs = fakes.FakeClient(extensions=extensions)
27
-
28
-
29 22
 class CellsExtensionTests(utils.TestCase):
23
+    def setUp(self):
24
+        super(CellsExtensionTests, self).setUp()
25
+        extensions = [
26
+            extension.Extension(cells.__name__.split(".")[-1],
27
+                                cells),
28
+        ]
29
+        self.cs = fakes.FakeClient(extensions=extensions)
30
+
30 31
     def test_get_cells(self):
31 32
         cell_name = 'child_cell'
32
-        cell = cs.cells.get(cell_name)
33
+        cell = self.cs.cells.get(cell_name)
33 34
         self.assert_request_id(cell, fakes.FAKE_REQUEST_ID_LIST)
34
-        cs.assert_called('GET', '/os-cells/%s' % cell_name)
35
+        self.cs.assert_called('GET', '/os-cells/%s' % cell_name)
35 36
 
36 37
     def test_get_capacities_for_a_given_cell(self):
37 38
         cell_name = 'child_cell'
38
-        ca = cs.cells.capacities(cell_name)
39
+        ca = self.cs.cells.capacities(cell_name)
39 40
         self.assert_request_id(ca, fakes.FAKE_REQUEST_ID_LIST)
40
-        cs.assert_called('GET', '/os-cells/%s/capacities' % cell_name)
41
+        self.cs.assert_called('GET', '/os-cells/%s/capacities' % cell_name)
41 42
 
42 43
     def test_get_capacities_for_all_cells(self):
43
-        ca = cs.cells.capacities()
44
+        ca = self.cs.cells.capacities()
44 45
         self.assert_request_id(ca, fakes.FAKE_REQUEST_ID_LIST)
45
-        cs.assert_called('GET', '/os-cells/capacities')
46
+        self.cs.assert_called('GET', '/os-cells/capacities')

+ 12
- 11
novaclient/tests/unit/v2/contrib/test_instance_actions.py View File

@@ -19,27 +19,28 @@ from novaclient.tests.unit.v2.contrib import fakes
19 19
 from novaclient.v2.contrib import instance_action
20 20
 
21 21
 
22
-extensions = [
23
-    extension.Extension(instance_action.__name__.split(".")[-1],
24
-                        instance_action),
25
-]
26
-cs = fakes.FakeClient(extensions=extensions)
27
-
28
-
29 22
 class InstanceActionExtensionTests(utils.TestCase):
23
+    def setUp(self):
24
+        super(InstanceActionExtensionTests, self).setUp()
25
+        extensions = [
26
+            extension.Extension(instance_action.__name__.split(".")[-1],
27
+                                instance_action),
28
+        ]
29
+        self.cs = fakes.FakeClient(extensions=extensions)
30
+
30 31
     def test_list_instance_actions(self):
31 32
         server_uuid = '1234'
32
-        ial = cs.instance_action.list(server_uuid)
33
+        ial = self.cs.instance_action.list(server_uuid)
33 34
         self.assert_request_id(ial, fakes.FAKE_REQUEST_ID_LIST)
34
-        cs.assert_called(
35
+        self.cs.assert_called(
35 36
             'GET', '/servers/%s/os-instance-actions' %
36 37
             server_uuid)
37 38
 
38 39
     def test_get_instance_action(self):
39 40
         server_uuid = '1234'
40 41
         request_id = 'req-abcde12345'
41
-        ia = cs.instance_action.get(server_uuid, request_id)
42
+        ia = self.cs.instance_action.get(server_uuid, request_id)
42 43
         self.assert_request_id(ia, fakes.FAKE_REQUEST_ID_LIST)
43
-        cs.assert_called(
44
+        self.cs.assert_called(
44 45
             'GET', '/servers/%s/os-instance-actions/%s'
45 46
             % (server_uuid, request_id))

+ 10
- 9
novaclient/tests/unit/v2/contrib/test_list_extensions.py View File

@@ -17,18 +17,19 @@ from novaclient.tests.unit.v2 import fakes
17 17
 from novaclient.v2.contrib import list_extensions
18 18
 
19 19
 
20
-extensions = [
21
-    extension.Extension(list_extensions.__name__.split(".")[-1],
22
-                        list_extensions),
23
-]
24
-cs = fakes.FakeClient(extensions=extensions)
25
-
26
-
27 20
 class ListExtensionsTests(utils.TestCase):
21
+    def setUp(self):
22
+        super(ListExtensionsTests, self).setUp()
23
+        extensions = [
24
+            extension.Extension(list_extensions.__name__.split(".")[-1],
25
+                                list_extensions),
26
+        ]
27
+        self.cs = fakes.FakeClient(extensions=extensions)
28
+
28 29
     def test_list_extensions(self):
29
-        all_exts = cs.list_extensions.show_all()
30
+        all_exts = self.cs.list_extensions.show_all()
30 31
         self.assert_request_id(all_exts, fakes.FAKE_REQUEST_ID_LIST)
31
-        cs.assert_called('GET', '/extensions')
32
+        self.cs.assert_called('GET', '/extensions')
32 33
         self.assertTrue(len(all_exts) > 0)
33 34
         for r in all_exts:
34 35
             self.assertTrue(len(r.summary) > 0)

+ 14
- 13
novaclient/tests/unit/v2/contrib/test_migrations.py View File

@@ -16,25 +16,26 @@ from novaclient.tests.unit import utils
16 16
 from novaclient.tests.unit.v2 import fakes
17 17
 from novaclient.v2.contrib import migrations
18 18
 
19
-extensions = [
20
-    extension.Extension(migrations.__name__.split(".")[-1],
21
-                        migrations),
22
-]
23
-cs = fakes.FakeClient(extensions=extensions)
24
-
25 19
 
26 20
 class MigrationsTest(utils.TestCase):
21
+    def setUp(self):
22
+        super(MigrationsTest, self).setUp()
23
+        self.extensions = [
24
+            extension.Extension(migrations.__name__.split(".")[-1],
25
+                                migrations),
26
+        ]
27
+        self.cs = fakes.FakeClient(extensions=self.extensions)
27 28
 
28 29
     def test_list_migrations(self):
29
-        ml = cs.migrations.list()
30
+        ml = self.cs.migrations.list()
30 31
         self.assert_request_id(ml, fakes.FAKE_REQUEST_ID_LIST)
31
-        cs.assert_called('GET', '/os-migrations')
32
+        self.cs.assert_called('GET', '/os-migrations')
32 33
         for m in ml:
33 34
             self.assertIsInstance(m, migrations.Migration)
34 35
             self.assertRaises(AttributeError, getattr, m, "migration_type")
35 36
 
36 37
     def test_list_migrations_v223(self):
37
-        cs = fakes.FakeClient(extensions=extensions,
38
+        cs = fakes.FakeClient(extensions=self.extensions,
38 39
                               api_version=api_versions.APIVersion("2.23"))
39 40
         ml = cs.migrations.list()
40 41
         self.assert_request_id(ml, fakes.FAKE_REQUEST_ID_LIST)
@@ -44,11 +45,11 @@ class MigrationsTest(utils.TestCase):
44 45
             self.assertEqual(m.migration_type, 'live-migration')
45 46
 
46 47
     def test_list_migrations_with_filters(self):
47
-        ml = cs.migrations.list('host1', 'finished', 'child1')
48
+        ml = self.cs.migrations.list('host1', 'finished', 'child1')
48 49
         self.assert_request_id(ml, fakes.FAKE_REQUEST_ID_LIST)
49 50
 
50
-        cs.assert_called('GET',
51
-                         '/os-migrations?cell_name=child1&host=host1'
52
-                         '&status=finished')
51
+        self.cs.assert_called(
52
+            'GET',
53
+            '/os-migrations?cell_name=child1&host=host1&status=finished')
53 54
         for m in ml:
54 55
             self.assertIsInstance(m, migrations.Migration)

+ 10
- 9
novaclient/tests/unit/v2/contrib/test_server_external_events.py View File

@@ -22,14 +22,15 @@ from novaclient.tests.unit.v2.contrib import fakes
22 22
 from novaclient.v2.contrib import server_external_events as ext_events
23 23
 
24 24
 
25
-extensions = [
26
-    extension.Extension(ext_events.__name__.split(".")[-1],
27
-                        ext_events),
28
-]
29
-cs = fakes.FakeClient(extensions=extensions)
30
-
31
-
32 25
 class ServerExternalEventsTestCase(utils.TestCase):
26
+    def setUp(self):
27
+        super(ServerExternalEventsTestCase, self).setUp()
28
+        extensions = [
29
+            extension.Extension(ext_events.__name__.split(".")[-1],
30
+                                ext_events),
31
+        ]
32
+        self.cs = fakes.FakeClient(extensions=extensions)
33
+
33 34
     def test_external_event(self):
34 35
         events = [{'server_uuid': 'fake-uuid1',
35 36
                    'name': 'test-event',
@@ -39,7 +40,7 @@ class ServerExternalEventsTestCase(utils.TestCase):
39 40
                    'name': 'test-event',
40 41
                    'status': 'completed',
41 42
                    'tag': 'tag'}]
42
-        result = cs.server_external_events.create(events)
43
+        result = self.cs.server_external_events.create(events)
43 44
         self.assert_request_id(result, fakes.FAKE_REQUEST_ID_LIST)
44 45
         self.assertEqual(events, result)
45
-        cs.assert_called('POST', '/os-server-external-events')
46
+        self.cs.assert_called('POST', '/os-server-external-events')

+ 17
- 15
novaclient/tests/unit/v2/contrib/test_tenant_networks.py View File

@@ -19,32 +19,34 @@ from novaclient.tests.unit.v2.contrib import fakes
19 19
 from novaclient.v2.contrib import tenant_networks
20 20
 
21 21
 
22
-extensions = [
23
-    extension.Extension(tenant_networks.__name__.split(".")[-1],
24
-                        tenant_networks),
25
-]
26
-cs = fakes.FakeClient(extensions=extensions)
22
+class TenantNetworkExtensionTests(utils.TestCase):
27 23
 
24
+    def setUp(self):
25
+        super(TenantNetworkExtensionTests, self).setUp()
26
+        extensions = [
27
+            extension.Extension(tenant_networks.__name__.split(".")[-1],
28
+                                tenant_networks),
29
+        ]
30
+        self.cs = fakes.FakeClient(extensions=extensions)
28 31
 
29
-class TenantNetworkExtensionTests(utils.TestCase):
30 32
     def test_list_tenant_networks(self):
31
-        nets = cs.tenant_networks.list()
33
+        nets = self.cs.tenant_networks.list()
32 34
         self.assert_request_id(nets, fakes.FAKE_REQUEST_ID_LIST)
33
-        cs.assert_called('GET', '/os-tenant-networks')
35
+        self.cs.assert_called('GET', '/os-tenant-networks')
34 36
         self.assertTrue(len(nets) > 0)
35 37
 
36 38
     def test_get_tenant_network(self):
37
-        net = cs.tenant_networks.get(1)
39
+        net = self.cs.tenant_networks.get(1)
38 40
         self.assert_request_id(net, fakes.FAKE_REQUEST_ID_LIST)
39
-        cs.assert_called('GET', '/os-tenant-networks/1')
41
+        self.cs.assert_called('GET', '/os-tenant-networks/1')
40 42
 
41 43
     def test_create_tenant_networks(self):
42
-        net = cs.tenant_networks.create(label="net",
43
-                                        cidr="10.0.0.0/24")
44
+        net = self.cs.tenant_networks.create(label="net",
45
+                                             cidr="10.0.0.0/24")
44 46
         self.assert_request_id(net, fakes.FAKE_REQUEST_ID_LIST)
45
-        cs.assert_called('POST', '/os-tenant-networks')
47
+        self.cs.assert_called('POST', '/os-tenant-networks')
46 48
 
47 49
     def test_delete_tenant_networks(self):
48
-        ret = cs.tenant_networks.delete(1)
50
+        ret = self.cs.tenant_networks.delete(1)
49 51
         self.assert_request_id(ret, fakes.FAKE_REQUEST_ID_LIST)
50
-        cs.assert_called('DELETE', '/os-tenant-networks/1')
52
+        self.cs.assert_called('DELETE', '/os-tenant-networks/1')

Loading…
Cancel
Save