Browse Source

Removes use of timeutils.set_time_override

The set_time_override function in timeutils was written as a
helper function to mock utcnow for unittests before 'mock' was
generally used. Now that we have mock and fixture, we no longer
need to use it.

Change-Id: Ibcf6d4abee4a52bc459fc322e6a5268d39d70450
Partial-Bug: #1266962
tags/2014.1.b3
Mikhail Durnosvistov 5 years ago
parent
commit
db14216159

+ 12
- 13
ironic/tests/api/test_chassis.py View File

@@ -18,6 +18,7 @@ Tests for the API /chassis/ methods.
18 18
 
19 19
 import datetime
20 20
 
21
+import mock
21 22
 from oslo.config import cfg
22 23
 
23 24
 from ironic.common import utils
@@ -147,7 +148,6 @@ class TestPatch(base.FunctionalTest):
147 148
         super(TestPatch, self).setUp()
148 149
         cdict = dbutils.get_test_chassis()
149 150
         self.dbapi.create_chassis(cdict)
150
-        self.addCleanup(timeutils.clear_time_override)
151 151
 
152 152
     def test_update_not_found(self):
153 153
         uuid = utils.generate_uuid()
@@ -159,11 +159,13 @@ class TestPatch(base.FunctionalTest):
159 159
         self.assertEqual('application/json', response.content_type)
160 160
         self.assertTrue(response.json['error_message'])
161 161
 
162
-    def test_replace_singular(self):
162
+    @mock.patch.object(timeutils, 'utcnow')
163
+    def test_replace_singular(self, mock_utcnow):
163 164
         cdict = dbutils.get_test_chassis()
164 165
         description = 'chassis-new-description'
165
-        t1 = datetime.datetime(2000, 1, 1, 0, 0)
166
-        timeutils.set_time_override(t1)
166
+        test_time = datetime.datetime(2000, 1, 1, 0, 0)
167
+
168
+        mock_utcnow.return_value = test_time
167 169
         response = self.patch_json('/chassis/%s' % cdict['uuid'],
168 170
                                    [{'path': '/description',
169 171
                                      'value': description, 'op': 'replace'}])
@@ -173,7 +175,7 @@ class TestPatch(base.FunctionalTest):
173 175
         self.assertEqual(description, result['description'])
174 176
         return_updated_at = timeutils.parse_isotime(
175 177
                             result['updated_at']).replace(tzinfo=None)
176
-        self.assertEqual(t1, return_updated_at)
178
+        self.assertEqual(test_time, return_updated_at)
177 179
 
178 180
     def test_replace_multi(self):
179 181
         extra = {"foo1": "bar1", "foo2": "bar2", "foo3": "bar3"}
@@ -287,14 +289,11 @@ class TestPatch(base.FunctionalTest):
287 289
 
288 290
 class TestPost(base.FunctionalTest):
289 291
 
290
-    def setUp(self):
291
-        super(TestPost, self).setUp()
292
-        self.addCleanup(timeutils.clear_time_override)
293
-
294
-    def test_create_chassis(self):
292
+    @mock.patch.object(timeutils, 'utcnow')
293
+    def test_create_chassis(self, mock_utcnow):
295 294
         cdict = dbutils.get_test_chassis()
296
-        t1 = datetime.datetime(2000, 1, 1, 0, 0)
297
-        timeutils.set_time_override(t1)
295
+        test_time = datetime.datetime(2000, 1, 1, 0, 0)
296
+        mock_utcnow.return_value = test_time
298 297
         response = self.post_json('/chassis', cdict)
299 298
         self.assertEqual(201, response.status_int)
300 299
         result = self.get_json('/chassis/%s' % cdict['uuid'])
@@ -302,7 +301,7 @@ class TestPost(base.FunctionalTest):
302 301
         self.assertFalse(result['updated_at'])
303 302
         return_created_at = timeutils.parse_isotime(
304 303
                             result['created_at']).replace(tzinfo=None)
305
-        self.assertEqual(t1, return_created_at)
304
+        self.assertEqual(test_time, return_created_at)
306 305
 
307 306
     def test_create_chassis_generate_uuid(self):
308 307
         cdict = dbutils.get_test_chassis()

+ 5
- 5
ironic/tests/api/test_nodes.py View File

@@ -559,16 +559,16 @@ class TestPost(base.FunctionalTest):
559 559
         super(TestPost, self).setUp()
560 560
         cdict = dbutils.get_test_chassis()
561 561
         self.chassis = self.dbapi.create_chassis(cdict)
562
-        self.addCleanup(timeutils.clear_time_override)
563 562
         p = mock.patch.object(rpcapi.ConductorAPI, 'get_topic_for')
564 563
         self.mock_gtf = p.start()
565 564
         self.mock_gtf.return_value = 'test-topic'
566 565
         self.addCleanup(p.stop)
567 566
 
568
-    def test_create_node(self):
567
+    @mock.patch.object(timeutils, 'utcnow')
568
+    def test_create_node(self, mock_utcnow):
569 569
         ndict = post_get_test_node()
570
-        t1 = datetime.datetime(2000, 1, 1, 0, 0)
571
-        timeutils.set_time_override(t1)
570
+        test_time = datetime.datetime(2000, 1, 1, 0, 0)
571
+        mock_utcnow.return_value = test_time
572 572
         response = self.post_json('/nodes', ndict)
573 573
         self.assertEqual(201, response.status_int)
574 574
         result = self.get_json('/nodes/%s' % ndict['uuid'])
@@ -576,7 +576,7 @@ class TestPost(base.FunctionalTest):
576 576
         self.assertFalse(result['updated_at'])
577 577
         return_created_at = timeutils.parse_isotime(
578 578
                 result['created_at']).replace(tzinfo=None)
579
-        self.assertEqual(t1, return_created_at)
579
+        self.assertEqual(test_time, return_created_at)
580 580
 
581 581
     def test_create_node_valid_extra(self):
582 582
         ndict = post_get_test_node(extra={'foo': 123})

+ 11
- 10
ironic/tests/api/test_ports.py View File

@@ -18,6 +18,7 @@ Tests for the API /ports/ methods.
18 18
 
19 19
 import datetime
20 20
 
21
+import mock
21 22
 from oslo.config import cfg
22 23
 
23 24
 from ironic.common import utils
@@ -137,12 +138,12 @@ class TestPatch(base.FunctionalTest):
137 138
         self.node = self.dbapi.create_node(ndict)
138 139
         self.pdict = dbutils.get_test_port(id=None)
139 140
         self.dbapi.create_port(self.pdict)
140
-        self.addCleanup(timeutils.clear_time_override)
141 141
 
142
-    def test_update_byid(self):
142
+    @mock.patch.object(timeutils, 'utcnow')
143
+    def test_update_byid(self, mock_utcnow):
143 144
         extra = {'foo': 'bar'}
144
-        t1 = datetime.datetime(2000, 1, 1, 0, 0)
145
-        timeutils.set_time_override(t1)
145
+        test_time = datetime.datetime(2000, 1, 1, 0, 0)
146
+        mock_utcnow.return_value = test_time
146 147
         response = self.patch_json('/ports/%s' % self.pdict['uuid'],
147 148
                                    [{'path': '/extra/foo',
148 149
                                      'value': 'bar',
@@ -153,7 +154,7 @@ class TestPatch(base.FunctionalTest):
153 154
         self.assertEqual(extra, result['extra'])
154 155
         return_updated_at = timeutils.parse_isotime(
155 156
                             result['updated_at']).replace(tzinfo=None)
156
-        self.assertEqual(t1, return_updated_at)
157
+        self.assertEqual(test_time, return_updated_at)
157 158
 
158 159
     def test_update_byaddress(self):
159 160
         response = self.patch_json('/ports/%s' % self.pdict['address'],
@@ -338,12 +339,12 @@ class TestPost(base.FunctionalTest):
338 339
         super(TestPost, self).setUp()
339 340
         ndict = dbutils.get_test_node()
340 341
         self.node = self.dbapi.create_node(ndict)
341
-        self.addCleanup(timeutils.clear_time_override)
342 342
 
343
-    def test_create_port(self):
343
+    @mock.patch.object(timeutils, 'utcnow')
344
+    def test_create_port(self, mock_utcnow):
344 345
         pdict = post_get_test_port()
345
-        t1 = datetime.datetime(2000, 1, 1, 0, 0)
346
-        timeutils.set_time_override(t1)
346
+        test_time = datetime.datetime(2000, 1, 1, 0, 0)
347
+        mock_utcnow.return_value = test_time
347 348
         response = self.post_json('/ports', pdict)
348 349
         self.assertEqual(201, response.status_int)
349 350
         result = self.get_json('/ports/%s' % pdict['uuid'])
@@ -351,7 +352,7 @@ class TestPost(base.FunctionalTest):
351 352
         self.assertFalse(result['updated_at'])
352 353
         return_created_at = timeutils.parse_isotime(
353 354
                             result['created_at']).replace(tzinfo=None)
354
-        self.assertEqual(t1, return_created_at)
355
+        self.assertEqual(test_time, return_created_at)
355 356
 
356 357
     def test_create_port_generate_uuid(self):
357 358
         pdict = post_get_test_port()

+ 0
- 10
ironic/tests/base.py View File

@@ -42,7 +42,6 @@ from ironic.common import paths
42 42
 from ironic.objects import base as objects_base
43 43
 from ironic.openstack.common.db.sqlalchemy import session
44 44
 from ironic.openstack.common import log as logging
45
-from ironic.openstack.common import timeutils
46 45
 from ironic.tests import conf_fixture
47 46
 from ironic.tests import policy_fixture
48 47
 
@@ -211,12 +210,3 @@ class TestCase(testtools.TestCase):
211 210
             return os.path.join(root, project_file)
212 211
         else:
213 212
             return root
214
-
215
-
216
-class TimeOverride(fixtures.Fixture):
217
-    """Fixture to start and remove time override."""
218
-
219
-    def setUp(self):
220
-        super(TimeOverride, self).setUp()
221
-        timeutils.set_time_override()
222
-        self.addCleanup(timeutils.clear_time_override)

+ 35
- 29
ironic/tests/db/test_conductor.py View File

@@ -19,10 +19,11 @@
19 19
 
20 20
 import datetime
21 21
 
22
-from ironic.openstack.common import timeutils
22
+import mock
23 23
 
24 24
 from ironic.common import exception
25 25
 from ironic.db import api as dbapi
26
+from ironic.openstack.common import timeutils
26 27
 from ironic.tests.db import base
27 28
 from ironic.tests.db import utils
28 29
 
@@ -32,8 +33,6 @@ class DbConductorTestCase(base.DbTestCase):
32 33
     def setUp(self):
33 34
         super(DbConductorTestCase, self).setUp()
34 35
         self.dbapi = dbapi.get_instance()
35
-        timeutils.set_time_override()
36
-        self.addCleanup(timeutils.clear_time_override)
37 36
 
38 37
     def _create_test_cdr(self, **kwargs):
39 38
         c = utils.get_test_conductor(**kwargs)
@@ -66,17 +65,18 @@ class DbConductorTestCase(base.DbTestCase):
66 65
                 self.dbapi.unregister_conductor,
67 66
                 c['hostname'])
68 67
 
69
-    def test_touch_conductor(self):
70
-        t = datetime.datetime(2000, 1, 1, 0, 0)
71
-        timeutils.set_time_override(override_time=t)
72
-        c = self._create_test_cdr(updated_at=t)
73
-        self.assertEqual(t, timeutils.normalize_time(c['updated_at']))
68
+    @mock.patch.object(timeutils, 'utcnow')
69
+    def test_touch_conductor(self, mock_utcnow):
70
+        test_time = datetime.datetime(2000, 1, 1, 0, 0)
71
+        mock_utcnow.return_value = test_time
72
+        c = self._create_test_cdr(updated_at=test_time)
73
+        self.assertEqual(test_time, timeutils.normalize_time(c['updated_at']))
74 74
 
75
-        t = datetime.datetime(2000, 1, 1, 0, 1)
76
-        timeutils.set_time_override(override_time=t)
75
+        test_time = datetime.datetime(2000, 1, 1, 0, 1)
76
+        mock_utcnow.return_value = test_time
77 77
         self.dbapi.touch_conductor(c['hostname'])
78 78
         c = self.dbapi.get_conductor(c['hostname'])
79
-        self.assertEqual(t, timeutils.normalize_time(c['updated_at']))
79
+        self.assertEqual(test_time, timeutils.normalize_time(c['updated_at']))
80 80
 
81 81
     def test_touch_conductor_not_found(self):
82 82
         self._create_test_cdr()
@@ -85,49 +85,54 @@ class DbConductorTestCase(base.DbTestCase):
85 85
                 self.dbapi.touch_conductor,
86 86
                 'bad-hostname')
87 87
 
88
-    def test_get_active_driver_dict_one_host_no_driver(self):
88
+    @mock.patch.object(timeutils, 'utcnow')
89
+    def test_get_active_driver_dict_one_host_no_driver(self, mock_utcnow):
89 90
         h = 'fake-host'
90 91
         expected = {}
91 92
 
92
-        timeutils.set_time_override()
93
+        mock_utcnow.return_value = datetime.datetime.utcnow()
93 94
         self._create_test_cdr(hostname=h, drivers=[])
94
-        result = self.dbapi.get_active_driver_dict(interval=1)
95
+        result = self.dbapi.get_active_driver_dict()
95 96
         self.assertEqual(expected, result)
96 97
 
97
-    def test_get_active_driver_dict_one_host_one_driver(self):
98
+    @mock.patch.object(timeutils, 'utcnow')
99
+    def test_get_active_driver_dict_one_host_one_driver(self, mock_utcnow):
98 100
         h = 'fake-host'
99 101
         d = 'fake-driver'
100 102
         expected = {d: set([h])}
101 103
 
102
-        timeutils.set_time_override()
104
+        mock_utcnow.return_value = datetime.datetime.utcnow()
103 105
         self._create_test_cdr(hostname=h, drivers=[d])
104
-        result = self.dbapi.get_active_driver_dict(interval=1)
106
+        result = self.dbapi.get_active_driver_dict()
105 107
         self.assertEqual(expected, result)
106 108
 
107
-    def test_get_active_driver_dict_one_host_many_drivers(self):
109
+    @mock.patch.object(timeutils, 'utcnow')
110
+    def test_get_active_driver_dict_one_host_many_drivers(self, mock_utcnow):
108 111
         h = 'fake-host'
109 112
         d1 = 'driver-one'
110 113
         d2 = 'driver-two'
111 114
         expected = {d1: set([h]), d2: set([h])}
112 115
 
113
-        timeutils.set_time_override()
116
+        mock_utcnow.return_value = datetime.datetime.utcnow()
114 117
         self._create_test_cdr(hostname=h, drivers=[d1, d2])
115
-        result = self.dbapi.get_active_driver_dict(interval=1)
118
+        result = self.dbapi.get_active_driver_dict()
116 119
         self.assertEqual(expected, result)
117 120
 
118
-    def test_get_active_driver_dict_many_hosts_one_driver(self):
121
+    @mock.patch.object(timeutils, 'utcnow')
122
+    def test_get_active_driver_dict_many_hosts_one_driver(self, mock_utcnow):
119 123
         h1 = 'host-one'
120 124
         h2 = 'host-two'
121 125
         d = 'fake-driver'
122 126
         expected = {d: set([h1, h2])}
123 127
 
124
-        timeutils.set_time_override()
128
+        mock_utcnow.return_value = datetime.datetime.utcnow()
125 129
         self._create_test_cdr(id=1, hostname=h1, drivers=[d])
126 130
         self._create_test_cdr(id=2, hostname=h2, drivers=[d])
127
-        result = self.dbapi.get_active_driver_dict(interval=1)
131
+        result = self.dbapi.get_active_driver_dict()
128 132
         self.assertEqual(expected, result)
129 133
 
130
-    def test_get_active_driver_dict_many_hosts_and_drivers(self):
134
+    @mock.patch.object(timeutils, 'utcnow')
135
+    def test_get_active_driver_dict_many_hosts_and_drivers(self, mock_utcnow):
131 136
         h1 = 'host-one'
132 137
         h2 = 'host-two'
133 138
         h3 = 'host-three'
@@ -135,14 +140,15 @@ class DbConductorTestCase(base.DbTestCase):
135 140
         d2 = 'driver-two'
136 141
         expected = {d1: set([h1, h2]), d2: set([h2, h3])}
137 142
 
138
-        timeutils.set_time_override()
143
+        mock_utcnow.return_value = datetime.datetime.utcnow()
139 144
         self._create_test_cdr(id=1, hostname=h1, drivers=[d1])
140 145
         self._create_test_cdr(id=2, hostname=h2, drivers=[d1, d2])
141 146
         self._create_test_cdr(id=3, hostname=h3, drivers=[d2])
142
-        result = self.dbapi.get_active_driver_dict(interval=1)
147
+        result = self.dbapi.get_active_driver_dict()
143 148
         self.assertEqual(expected, result)
144 149
 
145
-    def test_get_active_driver_dict_with_old_conductor(self):
150
+    @mock.patch.object(timeutils, 'utcnow')
151
+    def test_get_active_driver_dict_with_old_conductor(self, mock_utcnow):
146 152
         past = datetime.datetime(2000, 1, 1, 0, 0)
147 153
         present = past + datetime.timedelta(minutes=2)
148 154
 
@@ -150,12 +156,12 @@ class DbConductorTestCase(base.DbTestCase):
150 156
 
151 157
         h1 = 'old-host'
152 158
         d1 = 'old-driver'
153
-        timeutils.set_time_override(override_time=past)
159
+        mock_utcnow.return_value = past
154 160
         self._create_test_cdr(id=1, hostname=h1, drivers=[d, d1])
155 161
 
156 162
         h2 = 'new-host'
157 163
         d2 = 'new-driver'
158
-        timeutils.set_time_override(override_time=present)
164
+        mock_utcnow.return_value = present
159 165
         self._create_test_cdr(id=2, hostname=h2, drivers=[d, d2])
160 166
 
161 167
         # verify that old-host does not show up in current list

Loading…
Cancel
Save