Merge "Move null_safe funcs to module level"

This commit is contained in:
Jenkins 2017-05-25 19:54:45 +00:00 committed by Gerrit Code Review
commit 39b67842d0
3 changed files with 76 additions and 13 deletions

View File

@ -386,6 +386,21 @@ def image_meta(system_metadata):
return image_meta
def null_safe_str(s):
return str(s) if s else ''
def null_safe_int(s):
return int(s) if s else ''
def null_safe_isotime(s):
if isinstance(s, datetime.datetime):
return utils.strtime(s)
else:
return str(s) if s else ''
def info_from_instance(context, instance, network_info,
system_metadata, **kw):
"""Get detailed instance information for an instance which is common to all
@ -402,19 +417,6 @@ def info_from_instance(context, instance, network_info,
modifications.
"""
def null_safe_str(s):
return str(s) if s else ''
def null_safe_int(s):
return int(s) if s else ''
def null_safe_isotime(s):
if isinstance(s, datetime.datetime):
return utils.strtime(s)
else:
return str(s) if s else ''
image_ref_url = glance.generate_image_url(instance.image_ref)
instance_type = instance.get_flavor()

View File

@ -0,0 +1,46 @@
# Copyright (c) 2017 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from nova.notifications import base
from nova import test
from nova import utils
class TestNullSafeUtils(test.NoDBTestCase):
def test_null_safe_isotime(self):
dt = None
self.assertEqual('', base.null_safe_isotime(dt))
dt = datetime.datetime(second=1,
minute=1,
hour=1,
day=1,
month=1,
year=2017)
self.assertEqual(utils.strtime(dt), base.null_safe_isotime(dt))
def test_null_safe_str(self):
line = None
self.assertEqual('', base.null_safe_str(line))
line = 'test'
self.assertEqual(line, base.null_safe_str(line))
def test_null_safe_int(self):
number = None
# TODO(gibi): Fix null_safe_int to return 0 as default instead of empty
# string
self.assertEqual('', base.null_safe_int(number))
number = 10
self.assertEqual(number, base.null_safe_int(number))

View File

@ -16,6 +16,7 @@
"""Tests for common notifications."""
import copy
import datetime
import mock
from oslo_context import context as o_context
@ -491,6 +492,20 @@ class NotificationsTestCase(test.TestCase):
self.assertEqual(40, info['ephemeral_gb'])
self.assertEqual(60, info['disk_gb'])
def test_payload_has_timestamp_fields(self):
time = datetime.datetime(2017, 2, 2, 16, 45, 0)
# do not define deleted_at to test that missing value is handled
# properly
self.instance.terminated_at = time
self.instance.launched_at = time
info = notifications.info_from_instance(self.context, self.instance,
self.net_info, None)
self.assertEqual('2017-02-02T16:45:00.000000', info['terminated_at'])
self.assertEqual('2017-02-02T16:45:00.000000', info['launched_at'])
self.assertEqual('', info['deleted_at'])
def test_send_access_ip_update(self):
notifications.send_update(self.context, self.instance, self.instance)
self.assertEqual(1, len(fake_notifier.NOTIFICATIONS))