Fix openstack image import --method web-download --uri 'invalid value'

although python-openstackclient run command(image import) with invalid uri,
but the request succeeds. Fixed it to throw an exception
when requesting with an invalid URI.

unit test added. the test cover --uri 'invalid value'

Task: 52251
Story: 2011468
Closes-Bug: 2111777
Change-Id: I62cd8cdf054b6a5e07d664a543b0923ce5f20f83
This commit is contained in:
djp
2025-05-28 00:35:28 +09:00
parent c923ed5893
commit 9ad18c4967
2 changed files with 37 additions and 0 deletions
openstackclient
image
tests
unit

@ -22,6 +22,7 @@ import logging
import os
import sys
import typing as ty
import urllib.parse
from openstack import exceptions as sdk_exceptions
from openstack.image import image_signer
@ -1744,6 +1745,12 @@ class ImportImage(command.ShowOne):
"'--method=web-download'"
)
raise exceptions.CommandError(msg)
_parsed = urllib.parse.urlparse(parsed_args.uri)
if not all({_parsed.scheme, _parsed.netloc}):
msg = _("'%(uri)s' is not a valid url")
raise exceptions.CommandError(
msg % {'uri': parsed_args.uri},
)
else:
if parsed_args.uri:
msg = _(

@ -2039,6 +2039,36 @@ class TestImageImport(TestImage):
self.image_client.import_image.assert_not_called()
def test_import_image__web_download_invalid_url(self):
arglist = [
self.image.name,
'--method',
'web-download',
'--uri',
'invalid:1234',
]
verifylist = [
('image', self.image.name),
('import_method', 'web-download'),
('uri', 'invalid:1234'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
exc = self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args,
)
self.assertIn(
"'invalid:1234' is not a valid url",
str(exc),
)
self.image_client.import_image.assert_not_called()
def test_import_image__web_download_invalid_image_state(self):
self.image.status = 'uploading' # != 'queued'
arglist = [