Skip to content

Commit 9c2d0cd

Browse files
Galyna Zholtkevychgalynazholtkevych
authored andcommitted
Correct failure message output when downloading
This fixes unreadable output on download image failure. Adding new instance variable to exception `ImageDownloadError` class to avoid redundant logs. Change-Id: I51782abd572588adfc62745eeab9c559eb8346dd Closes-Bug: #1657691
1 parent 8e1226e commit 9c2d0cd

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

ironic_python_agent/errors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class ImageDownloadError(RESTError):
143143

144144
def __init__(self, image_id, msg):
145145
details = 'Download of image id {} failed: {}'.format(image_id, msg)
146+
self.secondary_message = msg
146147
super(ImageDownloadError, self).__init__(details)
147148

148149

ironic_python_agent/extensions/standby.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,15 @@ def __init__(self, image_info, time_obj=None):
204204
failtime = time.time() - self._time
205205
log_msg = ('URL: {}; time: {} '
206206
'seconds. Error: {}').format(
207-
url, failtime, e.details)
208-
LOG.warning('Image download failed. %s', log_msg)
209-
details += log_msg
207+
url, failtime, e.secondary_message)
208+
LOG.warning(log_msg)
209+
details.append(log_msg)
210210
continue
211211
else:
212212
break
213213
else:
214-
details = '/n'.join(details)
215-
msg = ('Image download failed for all URLs with following errors: '
216-
'{}'.format(details))
217-
raise errors.ImageDownloadError(image_info['id'], msg)
214+
details = '\n '.join(details)
215+
raise errors.ImageDownloadError(image_info['id'], details)
218216

219217
def _download_file(self, image_info, url):
220218
"""Opens a download stream for the given URL.

ironic_python_agent/tests/unit/extensions/test_standby.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,3 +899,21 @@ def test_download_image(self, requests_mock, md5_mock):
899899
cert=None, verify=True,
900900
stream=True, proxies={})
901901
self.assertEqual(image_info['checksum'], image_download.md5sum())
902+
903+
@mock.patch('time.time', autospec=True)
904+
@mock.patch('requests.get', autospec=True)
905+
def test_download_image_fail(self, requests_mock, time_mock):
906+
response = requests_mock.return_value
907+
response.status_code = 401
908+
response.text = 'Unauthorized'
909+
time_mock.return_value = 0.0
910+
image_info = _build_fake_image_info()
911+
msg = ('Error downloading image: Download of image id fake_id failed: '
912+
'URL: http://example.org; time: 0.0 seconds. Error: '
913+
'Received status code 401 from http://example.org, expected '
914+
'200. Response body: Unauthorized')
915+
self.assertRaisesRegexp(errors.ImageDownloadError, msg,
916+
standby.ImageDownload, image_info)
917+
requests_mock.assert_called_once_with(image_info['urls'][0],
918+
cert=None, verify=True,
919+
stream=True, proxies={})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
fixes:
3+
- Correct message output when failed to download image.

0 commit comments

Comments
 (0)