Skip to content

Commit

Permalink
Don't fail when we can't overwrite the summary file in the backup upl…
Browse files Browse the repository at this point in the history
…oad (#16452)

* Don't fail when we can't overwrite the summary file

* Simplify

* better output msgs

* Typo

* Auth test
  • Loading branch information
AbrilRBS committed Jun 13, 2024
1 parent e670142 commit 0a4ecfb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
16 changes: 11 additions & 5 deletions conan/api/subapi/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,23 @@ def upload_backup_sources(self, files):
for file in files:
basename = os.path.basename(file)
full_url = url + basename
is_summary = file.endswith(".json")
file_kind = "summary" if is_summary else "file"
try:
# Always upload summary .json but only upload blob if it does not already exist
if file.endswith(".json") or not uploader.exists(full_url, auth=None):
output.info(f"Uploading file '{basename}' to backup sources server")
if is_summary or not uploader.exists(full_url, auth=None):
output.info(f"Uploading {file_kind} '{basename}' to backup sources server")
uploader.upload(full_url, file, dedup=False, auth=None)
else:
output.info(f"File '{basename}' already in backup sources server, "
"skipping upload")
except (AuthenticationException, ForbiddenException) as e:
raise ConanException(f"The source backup server '{url}' needs authentication"
f"/permissions, please provide 'source_credentials.json': {e}")
if is_summary:
output.warning(f"Could not update summary '{basename}' in backup sources server. "
"Skipping updating file but continuing with upload. "
f"Missing permissions?: {e}")
else:
raise ConanException(f"The source backup server '{url}' needs authentication"
f"/permissions, please provide 'source_credentials.json': {e}")

output.success("Upload backup sources complete\n")
return files
28 changes: 24 additions & 4 deletions test/integration/cache/backup_sources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,16 @@ def get_internet_file(file):
def get_file(file):
return static_file(file, http_server_base_folder_backup1)

@http_server.server.put("/downloader1/<file>")
def put_file(file):
if file in os.listdir(http_server_base_folder_backup1):
return HTTPError(401, "You Are Not Allowed Here")
dest = os.path.join(http_server_base_folder_backup1, file)
with open(dest, 'wb') as f:
f.write(request.body.read())

@http_server.server.get("/downloader2/<file>")
def get_file(file):
def get_file_2(file):
return static_file(file, http_server_base_folder_backup2)

http_server.run_server()
Expand All @@ -555,16 +563,28 @@ def source(self):
sha256="{sha256}")
""")
client.save_home({"global.conf": f"core.sources:download_cache={download_cache_folder}\n"
f"core.sources:download_urls=['http://localhost:{http_server.port}/downloader1/', "
f"'origin', 'http://localhost:{http_server.port}/downloader2/']\n"})

f"core.sources:download_urls=['http://localhost:{http_server.port}/downloader1/', "
f"'origin', 'http://localhost:{http_server.port}/downloader2/']\n"
f"core.sources:upload_url=http://localhost:{http_server.port}/downloader1/\n"
"core.upload:retry=0\ncore.download:retry=0"})

client.save({"conanfile.py": conanfile})
client.run("create .")
assert f"Sources for http://localhost:{http_server.port}/internet/myfile.txt found in remote backup http://localhost:{http_server.port}/downloader2/" in client.out
# TODO: Check better message with Authentication error message
assert "failed in 'origin'" in client.out

# Now try to upload once to the first backup server. It's configured so it has write permissions but no overwrite
client.run("upload * -c -r=default")
upload_server_contents = os.listdir(http_server_base_folder_backup1)
assert sha256 in upload_server_contents
assert sha256 + ".json" in upload_server_contents

client.run("remove * -c -r=default")

client.run("upload * -c -r=default")
assert f"Could not update summary '{sha256}.json' in backup sources server" in client.out

def test_ok_when_origin_bad_sha256(self):
http_server_base_folder_internet = os.path.join(self.file_server.store, "internet")
http_server_base_folder_backup2 = os.path.join(self.file_server.store, "backup2")
Expand Down

0 comments on commit 0a4ecfb

Please sign in to comment.