diff options
| author | Jenkins <jenkins@review.openstack.org> | 2013-05-31 14:23:44 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2013-05-31 14:23:44 +0000 |
| commit | babe5fd2f3cb5b3fb08597d537e9a44aa39609bd (patch) | |
| tree | 05470882de79ebb0d00e20991aa0557efe93296b /tests/unit/test_fileutils.py | |
| parent | 3eac3ba29caa58a57b6c997eb3af6a129b44d94f (diff) | |
| parent | 1091b4f3bed9b0ad8c23e5db6d4a5cee15fc786c (diff) | |
| download | oslo-babe5fd2f3cb5b3fb08597d537e9a44aa39609bd.tar.gz oslo-babe5fd2f3cb5b3fb08597d537e9a44aa39609bd.tar.xz oslo-babe5fd2f3cb5b3fb08597d537e9a44aa39609bd.zip | |
Merge "Reduce duplicated code related to policies"
Diffstat (limited to 'tests/unit/test_fileutils.py')
| -rw-r--r-- | tests/unit/test_fileutils.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/unit/test_fileutils.py b/tests/unit/test_fileutils.py index c0bf0ac..7cd067c 100644 --- a/tests/unit/test_fileutils.py +++ b/tests/unit/test_fileutils.py @@ -15,10 +15,13 @@ # License for the specific language governing permissions and limitations # under the License. +import __builtin__ import os import shutil import tempfile +import mox + from openstack.common import fileutils from tests import utils @@ -34,3 +37,48 @@ class EnsureTree(utils.BaseTestCase): finally: if os.path.exists(tmpdir): shutil.rmtree(tmpdir) + + +class TestCachedFile(utils.BaseTestCase): + + def setUp(self): + super(TestCachedFile, self).setUp() + self.mox = mox.Mox() + self.addCleanup(self.mox.UnsetStubs) + + def test_read_cached_file(self): + self.mox.StubOutWithMock(os.path, "getmtime") + os.path.getmtime(mox.IgnoreArg()).AndReturn(1) + self.mox.ReplayAll() + + fileutils._FILE_CACHE = { + '/this/is/a/fake': {"data": 1123, "mtime": 1} + } + fresh, data = fileutils.read_cached_file("/this/is/a/fake") + fdata = fileutils._FILE_CACHE['/this/is/a/fake']["data"] + self.assertEqual(fdata, data) + + def test_read_modified_cached_file(self): + self.mox.StubOutWithMock(os.path, "getmtime") + self.mox.StubOutWithMock(__builtin__, 'open') + os.path.getmtime(mox.IgnoreArg()).AndReturn(2) + + fake_contents = "lorem ipsum" + fake_file = self.mox.CreateMockAnything() + fake_file.read().AndReturn(fake_contents) + fake_context_manager = self.mox.CreateMockAnything() + fake_context_manager.__enter__().AndReturn(fake_file) + fake_context_manager.__exit__(mox.IgnoreArg(), + mox.IgnoreArg(), + mox.IgnoreArg()) + + __builtin__.open(mox.IgnoreArg()).AndReturn(fake_context_manager) + + self.mox.ReplayAll() + fileutils._FILE_CACHE = { + '/this/is/a/fake': {"data": 1123, "mtime": 1} + } + + fresh, data = fileutils.read_cached_file("/this/is/a/fake") + self.assertEqual(data, fake_contents) + self.assertTrue(fresh) |
