From 79ac3a3c311b393d7b64ce4c41d68b52801b52cb Mon Sep 17 00:00:00 2001 From: Alistair Coles Date: Mon, 8 Jan 2018 14:36:53 +0000 Subject: [PATCH] Fix intermittent check_delete_headers failure Use a utils.Timestamp object to set a more realistic x-timestamp header to avoid intermittent failure when str(time.time()) results in a rounded up value. Closes-Bug: 1741912 Change-Id: I0c54d07e30ecb391f9429e7bcfb782f965ece1ea --- test/unit/common/test_constraints.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/unit/common/test_constraints.py b/test/unit/common/test_constraints.py index b55ec6bdc9..a11d222653 100644 --- a/test/unit/common/test_constraints.py +++ b/test/unit/common/test_constraints.py @@ -245,11 +245,11 @@ class TestConstraints(unittest.TestCase): def test_check_delete_headers(self): # x-delete-at value should be relative to the request timestamp rather # than time.time() so separate the two to ensure the checks are robust - ts = time.time() + 100 + ts = utils.Timestamp(time.time() + 100) # X-Delete-After headers = {'X-Delete-After': '600', - 'X-Timestamp': str(ts)} + 'X-Timestamp': ts.internal} req = constraints.check_delete_headers( Request.blank('/', headers=headers)) self.assertIsInstance(req, Request) @@ -259,7 +259,7 @@ class TestConstraints(unittest.TestCase): self.assertEqual(req.headers.get('X-Delete-At'), expected_delete_at) headers = {'X-Delete-After': 'abc', - 'X-Timestamp': str(time.time())} + 'X-Timestamp': ts.internal} with self.assertRaises(HTTPException) as cm: constraints.check_delete_headers( @@ -268,7 +268,7 @@ class TestConstraints(unittest.TestCase): self.assertIn('Non-integer X-Delete-After', cm.exception.body) headers = {'X-Delete-After': '60.1', - 'X-Timestamp': str(time.time())} + 'X-Timestamp': ts.internal} with self.assertRaises(HTTPException) as cm: constraints.check_delete_headers( Request.blank('/', headers=headers)) @@ -276,7 +276,7 @@ class TestConstraints(unittest.TestCase): self.assertIn('Non-integer X-Delete-After', cm.exception.body) headers = {'X-Delete-After': '-1', - 'X-Timestamp': str(time.time())} + 'X-Timestamp': ts.internal} with self.assertRaises(HTTPException) as cm: constraints.check_delete_headers( Request.blank('/', headers=headers)) @@ -284,7 +284,7 @@ class TestConstraints(unittest.TestCase): self.assertIn('X-Delete-After in past', cm.exception.body) headers = {'X-Delete-After': '0', - 'X-Timestamp': str(time.time())} + 'X-Timestamp': ts.internal} with self.assertRaises(HTTPException) as cm: constraints.check_delete_headers( Request.blank('/', headers=headers)) @@ -292,9 +292,9 @@ class TestConstraints(unittest.TestCase): self.assertIn('X-Delete-After in past', cm.exception.body) # X-Delete-At - delete_at = str(int(ts + 100)) + delete_at = str(int(ts) + 100) headers = {'X-Delete-At': delete_at, - 'X-Timestamp': str(ts)} + 'X-Timestamp': ts.internal} req = constraints.check_delete_headers( Request.blank('/', headers=headers)) self.assertIsInstance(req, Request) @@ -302,25 +302,25 @@ class TestConstraints(unittest.TestCase): self.assertEqual(req.headers.get('X-Delete-At'), delete_at) headers = {'X-Delete-At': 'abc', - 'X-Timestamp': str(ts)} + 'X-Timestamp': ts.internal} with self.assertRaises(HTTPException) as cm: constraints.check_delete_headers( Request.blank('/', headers=headers)) self.assertEqual(cm.exception.status_int, HTTP_BAD_REQUEST) self.assertIn('Non-integer X-Delete-At', cm.exception.body) - delete_at = str(int(ts + 100)) + '.1' + delete_at = str(int(ts) + 100) + '.1' headers = {'X-Delete-At': delete_at, - 'X-Timestamp': str(ts)} + 'X-Timestamp': ts.internal} with self.assertRaises(HTTPException) as cm: constraints.check_delete_headers( Request.blank('/', headers=headers)) self.assertEqual(cm.exception.status_int, HTTP_BAD_REQUEST) self.assertIn('Non-integer X-Delete-At', cm.exception.body) - delete_at = str(int(ts - 1)) + delete_at = str(int(ts) - 1) headers = {'X-Delete-At': delete_at, - 'X-Timestamp': str(ts)} + 'X-Timestamp': ts.internal} with self.assertRaises(HTTPException) as cm: constraints.check_delete_headers( Request.blank('/', headers=headers))