]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix a microsecond format of isoformat()
authorfumihiko kakuma <kakuma@valinux.co.jp>
Mon, 27 Jul 2015 01:11:18 +0000 (10:11 +0900)
committerfumihiko kakuma <kakuma@valinux.co.jp>
Tue, 28 Jul 2015 04:54:56 +0000 (13:54 +0900)
isoformat() omits the microsecond from the format when the microsecond is 0.
Therefore, use strftime('%Y-%m-%dT%H:%M:%S.%f') instead.

Related Change-Id: Id6e8645362fe70b1427d45d5b44048fe47aba0f7
Closes-Bug: #1478418

Change-Id: I27059fa3476ceb51033534cc60d40047d88390d7

neutron/agent/rpc.py
neutron/common/constants.py
neutron/tests/unit/agent/test_rpc.py
neutron/tests/unit/extensions/test_agent.py

index 38fa4de127496aa7f0494985444a029c75b43449..6595c327119067793180b34db31d68889278e4af 100644 (file)
@@ -80,7 +80,7 @@ class PluginReportStateAPI(object):
         agent_state['uuid'] = uuidutils.generate_uuid()
         kwargs = {
             'agent_state': {'agent_state': agent_state},
-            'time': datetime.utcnow().isoformat(),
+            'time': datetime.utcnow().strftime(constants.ISO8601_TIME_FORMAT),
         }
         method = cctxt.call if use_call else cctxt.cast
         return method(context, 'report_state', **kwargs)
index fec9713ce3920e45f68caf3aef54cba775ec4084..d52f4312ae0e6065f6b0ae7972109491304b2350 100644 (file)
@@ -183,3 +183,6 @@ RPC_NAMESPACE_STATE = None
 DEFAULT_NETWORK_MTU = 0
 
 ROUTER_MARK_MASK = "0xffff"
+
+# Time format
+ISO8601_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%f'
index fe8b2484eda52aeb4b67bbe2f34823dc987b8ed0..2be1cb80068000d5026ff0be22c4a9a7b5322694 100644 (file)
@@ -13,6 +13,7 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+import datetime
 import mock
 from oslo_context import context as oslo_context
 import oslo_messaging
@@ -101,6 +102,26 @@ class AgentPluginReportState(base.BaseTestCase):
                              {'agent_state': expected_agent_state})
             self.assertIsInstance(mock_cast.call_args[1]['time'], str)
 
+    def test_plugin_report_state_microsecond_is_0(self):
+        topic = 'test'
+        expected_time = datetime.datetime(2015, 7, 27, 15, 33, 30, 0)
+        expected_time_str = '2015-07-27T15:33:30.000000'
+        expected_agent_state = {'agent': 'test'}
+        with mock.patch('neutron.agent.rpc.datetime') as mock_datetime:
+            reportStateAPI = rpc.PluginReportStateAPI(topic)
+            mock_datetime.utcnow.return_value = expected_time
+            with mock.patch.object(reportStateAPI.client, 'call'), \
+                    mock.patch.object(reportStateAPI.client, 'cast'
+                                      ) as mock_cast, \
+                    mock.patch.object(reportStateAPI.client, 'prepare'
+                                      ) as mock_prepare:
+                mock_prepare.return_value = reportStateAPI.client
+                ctxt = oslo_context.RequestContext('fake_user',
+                                                   'fake_project')
+                reportStateAPI.report_state(ctxt, expected_agent_state)
+                self.assertEqual(expected_time_str,
+                                 mock_cast.call_args[1]['time'])
+
 
 class AgentRPCMethods(base.BaseTestCase):
 
index 546b18467f0350789e1616adc0a4989b5c3f39cd..79397a5fe791c818657d9ef1491029f533cb48ac 100644 (file)
@@ -106,12 +106,14 @@ class AgentDBTestMixIn(object):
             lbaas_hostb = copy.deepcopy(lbaas_hosta)
             lbaas_hostb['host'] = LBAAS_HOSTB
             callback = agents_db.AgentExtRpcCallback()
-            callback.report_state(self.adminContext,
-                                  agent_state={'agent_state': lbaas_hosta},
-                                  time=datetime.utcnow().isoformat())
-            callback.report_state(self.adminContext,
-                                  agent_state={'agent_state': lbaas_hostb},
-                                  time=datetime.utcnow().isoformat())
+            callback.report_state(
+                self.adminContext,
+                agent_state={'agent_state': lbaas_hosta},
+                time=datetime.utcnow().strftime(constants.ISO8601_TIME_FORMAT))
+            callback.report_state(
+                self.adminContext,
+                agent_state={'agent_state': lbaas_hostb},
+                time=datetime.utcnow().strftime(constants.ISO8601_TIME_FORMAT))
             res += [lbaas_hosta, lbaas_hostb]
 
         return res