Skip to content

Commit

Permalink
Improve error when downoad of required file fails (#60)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Waltlova <[email protected]>
  • Loading branch information
andywaltlova authored Feb 14, 2024
1 parent 18e6f47 commit 7df959f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
17 changes: 13 additions & 4 deletions scripts/c2r_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import copy
from time import gmtime, strftime

from urllib2 import urlopen
from urllib2 import urlopen, URLError

# SCRIPT_TYPE is either 'CONVERSION' or 'ANALYSIS'
# Value is set in signed yaml envelope in content_vars (SCRIPT_MODE)
Expand Down Expand Up @@ -377,9 +377,18 @@ def generate_report_message(highest_status):
def setup_convert2rhel(required_files):
"""Setup convert2rhel tool by downloading the required files."""
print("Downloading required files.")
for required_file in required_files:
required_file.backup()
required_file.create_from_host_url_data()
try:
for required_file in required_files:
required_file.backup()
required_file.create_from_host_url_data()
except URLError as err:
url = required_file.host
# pylint: disable=raise-missing-from
raise ProcessError(
message="Failed to download required files needed for convert2rhel to run.",
report="Download of required file from %s failed with error: %s"
% (url, err),
)


# Code taken from
Expand Down
22 changes: 21 additions & 1 deletion tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest
from mock import Mock
from urllib2 import URLError

from scripts.c2r_script import setup_convert2rhel

from scripts.c2r_script import setup_convert2rhel, ProcessError


class MockRequiredFile(object):
Expand All @@ -25,3 +28,20 @@ def test_setup_calls_backup_and_create_for_every_file():

test_file.backup.assert_called_once()
test_file.create_from_host_url_data.assert_called_once()


def test_setup_urlerror():
# pylint: disable=too-many-arguments
mocked_required_file = Mock(spec=MockRequiredFile)
test_file = mocked_required_file("/mock/path/file.txt", "foo.bar")
test_file.create_from_host_url_data.side_effect = URLError("foo")
required_files = [test_file]

with pytest.raises(ProcessError) as error:
setup_convert2rhel(required_files)
assert "Download of required file from foo.bar failed with error: foo" in str(
error
)

test_file.backup.assert_called_once()
test_file.create_from_host_url_data.assert_called_once()

0 comments on commit 7df959f

Please sign in to comment.