From 161cd100f2b756fdcadce46a4a49f49310aa7794 Mon Sep 17 00:00:00 2001 From: Lubomír Sedlář Date: Nov 22 2018 08:46:57 +0000 Subject: Send source mtime to dist-git This should be backwards compatible. Dist-git instances should hopefully ignore fields they don't understand. Related: https://github.com/release-engineering/dist-git/pull/22/ Fixes: https://pagure.io/fedpkg/issue/220 Signed-off-by: Lubomír Sedlář --- diff --git a/pyrpkg/lookaside.py b/pyrpkg/lookaside.py index 9282618..03dc67e 100644 --- a/pyrpkg/lookaside.py +++ b/pyrpkg/lookaside.py @@ -291,9 +291,12 @@ class CGILookasideCache(object): return self.log.info("Uploading: %s", filepath) - post_data = [('name', name), - ('%ssum' % self.hashtype, hash), - ('file', (pycurl.FORM_FILE, filepath))] + post_data = [ + ('name', name), + ('%ssum' % self.hashtype, hash), + ('file', (pycurl.FORM_FILE, filepath)), + ('mtime', str(int(os.stat(filepath).st_mtime))), + ] with io.BytesIO() as buf: c = pycurl.Curl() diff --git a/tests/test_lookaside.py b/tests/test_lookaside.py index 5b4a3a6..c10cbef 100644 --- a/tests/test_lookaside.py +++ b/tests/test_lookaside.py @@ -20,6 +20,16 @@ from pyrpkg.lookaside import CGILookasideCache from pyrpkg.errors import DownloadError, InvalidHashType, UploadError +old_stat = os.stat + + +def mock_stat(path): + """Fake mtime for tarballs, but keep stat working for any other file.""" + if path.endswith(".tar.xz"): + return mock.Mock(st_mtime=1234) + return old_stat(path) + + class CGILookasideCacheTestCase(unittest.TestCase): def setUp(self): self.workdir = tempfile.mkdtemp(prefix='rpkg-tests.') @@ -450,6 +460,7 @@ class CGILookasideCacheTestCase(unittest.TestCase): self.assertRaises(UploadError, lc.remote_file_exists, 'pyrpkg', 'pyrpkg-0.tar.xz', 'thehash') + @mock.patch('os.stat', new=mock_stat) @mock.patch('pyrpkg.lookaside.logging.getLogger') @mock.patch('pyrpkg.lookaside.pycurl.Curl') def test_upload(self, mock_curl, mock_logger): @@ -478,9 +489,15 @@ class CGILookasideCacheTestCase(unittest.TestCase): lc.upload('pyrpkg', 'pyrpkg-0.0.tar.xz', 'thehash') self.assertTrue(pycurl.HTTPPOST in curlopts) - self.assertEqual(curlopts[pycurl.HTTPPOST], [ - ('name', 'pyrpkg'), ('sha512sum', 'thehash'), - ('file', (pycurl.FORM_FILE, 'pyrpkg-0.0.tar.xz'))]) + self.assertEqual( + curlopts[pycurl.HTTPPOST], + [ + ('name', 'pyrpkg'), + ('sha512sum', 'thehash'), + ('file', (pycurl.FORM_FILE, 'pyrpkg-0.0.tar.xz')), + ('mtime', '1234'), + ], + ) self.assertEqual(debug_messages, [b'Some output']) @@ -497,6 +514,7 @@ class CGILookasideCacheTestCase(unittest.TestCase): self.assertEqual(curl.perform.call_count, 0) self.assertEqual(curl.setopt.call_count, 0) + @mock.patch('os.stat', new=mock_stat) @mock.patch('pyrpkg.lookaside.pycurl.Curl') def test_upload_with_custom_certs(self, mock_curl): def mock_setopt(opt, value): @@ -524,6 +542,7 @@ class CGILookasideCacheTestCase(unittest.TestCase): self.assertEqual(curlopts[pycurl.SSLCERT], client_cert) self.assertEqual(curlopts[pycurl.CAINFO], ca_cert) + @mock.patch('os.stat', new=mock_stat) @mock.patch('pyrpkg.lookaside.logging.getLogger') @mock.patch('pyrpkg.lookaside.pycurl.Curl') def test_upload_missing_custom_certs(self, mock_curl, mock_logger): @@ -557,6 +576,7 @@ class CGILookasideCacheTestCase(unittest.TestCase): self.assertTrue('Missing certificate: ' in warn_messages[0]) self.assertTrue('Missing certificate: ' in warn_messages[1]) + @mock.patch('os.stat', new=mock_stat) @mock.patch('pyrpkg.lookaside.pycurl.Curl') def test_upload_failed(self, mock_curl): curl = mock_curl.return_value @@ -569,6 +589,7 @@ class CGILookasideCacheTestCase(unittest.TestCase): self.assertRaises(UploadError, lc.upload, 'pyrpkg', 'pyrpkg-0.tar.xz', 'thehash') + @mock.patch('os.stat', new=mock_stat) @mock.patch('pyrpkg.lookaside.pycurl.Curl') def test_upload_failed_status_code(self, mock_curl): def mock_setopt(opt, value):