async def test_mirror_sync_package(mirror: BandersnatchMirror) -> None: mirror.master.all_packages = mock.AsyncMock(return_value={"foo": 1}) # type: ignore mirror.json_save = True # Recall bootstrap so we have the json dirs mirror._bootstrap() await mirror.synchronize() assert """\ json{0}foo last-modified packages{0}2.7{0}f{0}foo{0}foo.whl packages{0}any{0}f{0}foo{0}foo.zip pypi{0}foo{0}json simple{0}foo{0}index.html simple{0}index.html""".format( sep ) == utils.find( mirror.webdir, dirs=False ) assert ( open("web{0}simple{0}index.html".format(sep)).read() == """\ <!DOCTYPE html> <html> <head> <title>Simple Index</title> </head> <body> <a href="foo/">foo</a><br/> </body> </html>""" ) assert open("status", "rb").read() == b"1"
async def test_sync_specific_packages(mirror: BandersnatchMirror) -> None: FAKE_SERIAL = b"112233" with open("status", "wb") as f: f.write(FAKE_SERIAL) # Package names should be normalized by synchronize() specific_packages = ["Foo"] mirror.master.all_packages = AsyncMock(return_value={"foo": 1}) # type: ignore mirror.json_save = True # Recall bootstrap so we have the json dirs mirror._bootstrap() await mirror.synchronize(specific_packages) assert """\ json{0}foo packages{0}2.7{0}f{0}foo{0}foo.whl packages{0}any{0}f{0}foo{0}foo.zip pypi{0}foo{0}json simple{0}foo{0}index.html simple{0}index.html""".format(sep) == utils.find(mirror.webdir, dirs=False) assert (open("web{0}simple{0}index.html".format(sep)).read() == """\ <!DOCTYPE html> <html> <head> <title>Simple Index</title> </head> <body> <a href="foo/">foo</a><br/> </body> </html>""") # The "sync" method shouldn't update the serial assert open("status", "rb").read() == FAKE_SERIAL
async def test_mirror_sync_package_download_mirror_fails( mirror: BandersnatchMirror, ) -> None: mirror.master.all_packages = mock.AsyncMock(return_value={"foo": 1}) # type: ignore mirror.json_save = True # Recall bootstrap so we have the json dirs mirror._bootstrap() # This download mirror URL does not work, forcing not to fallback mirror.download_mirror = "https://not-working.example.com" mirror.download_mirror_no_fallback = True with mock.patch("bandersnatch.mirror.logger.exception") as mock_log: await mirror.synchronize() # 3 calls are: # * Tried and failed to get /packages/any/f/foo/foo.zip # * Tried and failed to get /packages/2.7/f/foo/foo.whl # * Declared error on syncing package foo@1 assert mock_log.call_count == 3
async def test_mirror_sync_package_download_mirror_fallback( mirror: BandersnatchMirror, ) -> None: mirror.master.all_packages = mock.AsyncMock(return_value={"foo": 1}) # type: ignore mirror.json_save = True # Recall bootstrap so we have the json dirs mirror._bootstrap() # This download mirror URL does not work, should fallback to normal logic mirror.download_mirror = "https://not-working.example.com/pypi" await mirror.synchronize() assert """\ json{0}foo last-modified packages{0}2.7{0}f{0}foo{0}foo.whl packages{0}any{0}f{0}foo{0}foo.zip pypi{0}foo{0}json simple{0}foo{0}index.html simple{0}index.html""".format( sep ) == utils.find( mirror.webdir, dirs=False ) assert ( open("web{0}simple{0}index.html".format(sep)).read() == """\ <!DOCTYPE html> <html> <head> <title>Simple Index</title> </head> <body> <a href="foo/">foo</a><br/> </body> </html>""" )