From 35be498112cfc52cc95d2ed811fbc5427407902b Mon Sep 17 00:00:00 2001 From: Flaper Fesp Date: Mon, 21 Jan 2013 11:00:58 +0100 Subject: [PATCH] Prints list-cached dates in isoformat Converts dates printed by list-cached to a human readable format (isoformat). It now checks whether the image last_access time is == 0.0 and prints "Not Accessed Yet" if so. * Updates openstack.common.timeutils * Fixes bug 1102334 Change-Id: I46f023471ec87fdf74c18f33309279a2f39a4e2a --- bin/glance-cache-manage | 14 ++++++++++++-- glance/openstack/common/timeutils.py | 18 ++++++++++++++++++ .../functional/test_bin_glance_cache_manage.py | 13 +++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/bin/glance-cache-manage b/bin/glance-cache-manage index d2cf0a8679..cf504c583c 100755 --- a/bin/glance-cache-manage +++ b/bin/glance-cache-manage @@ -40,6 +40,7 @@ gettext.install('glance', unicode=1) import glance.image_cache.client from glance.common import exception from glance.common import utils +from glance.openstack.common import timeutils from glance.version import version_info as version @@ -102,10 +103,19 @@ List all images currently cached""" print pretty_table.make_header() for image in images: + last_modified = image['last_modified'] + last_modified = timeutils.iso8601_from_timestamp(last_modified) + + last_accessed = image['last_accessed'] + if last_accessed == 0: + last_accessed = "N/A" + else: + last_accessed = timeutils.iso8601_from_timestamp(last_accessed) + print pretty_table.make_row( image['image_id'], - image['last_accessed'], - image['last_modified'], + last_accessed, + last_modified, image['size'], image['hits']) diff --git a/glance/openstack/common/timeutils.py b/glance/openstack/common/timeutils.py index 0f346087f7..5a011e8181 100644 --- a/glance/openstack/common/timeutils.py +++ b/glance/openstack/common/timeutils.py @@ -98,6 +98,11 @@ def utcnow(): return datetime.datetime.utcnow() +def iso8601_from_timestamp(timestamp): + """Returns a iso8601 formated date from timestamp""" + return isotime(datetime.datetime.utcfromtimestamp(timestamp)) + + utcnow.override_time = None @@ -162,3 +167,16 @@ def delta_seconds(before, after): except AttributeError: return ((delta.days * 24 * 3600) + delta.seconds + float(delta.microseconds) / (10 ** 6)) + + +def is_soon(dt, window): + """ + Determines if time is going to happen in the next window seconds. + + :params dt: the time + :params window: minimum seconds to remain to consider the time not soon + + :return: True if expiration is within the given duration + """ + soon = (utcnow() + datetime.timedelta(seconds=window)) + return normalize_time(dt) < soon diff --git a/glance/tests/functional/test_bin_glance_cache_manage.py b/glance/tests/functional/test_bin_glance_cache_manage.py index 4cccfa1a8c..67b2d9688b 100644 --- a/glance/tests/functional/test_bin_glance_cache_manage.py +++ b/glance/tests/functional/test_bin_glance_cache_manage.py @@ -17,6 +17,7 @@ """Functional test case that utilizes the bin/glance-cache-manage CLI tool""" +import datetime import hashlib import httplib2 import json @@ -76,6 +77,16 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): self.assertEqual(0, exitcode) return image_id in out + def iso_date(self, image_id): + """ + Return True if supplied image ID is cached, False otherwise + """ + cmd = "bin/glance-cache-manage --port=%d list-cached" % self.api_port + + exitcode, out, err = execute(cmd) + + return datetime.date.today().isoformat() in out + def test_no_cache_enabled(self): """ Test that cache index command works @@ -132,6 +143,8 @@ class TestBinGlanceCacheManage(functional.FunctionalTest): self.assertTrue(self.is_image_cached(ids[1]), "%s is not cached." % ids[1]) + self.assertTrue(self.iso_date(ids[1])) + self.stop_servers() def test_queue(self):