Add FilePathOrUrlType
This type does check whether is path or url passed, check if file available and perform os.path.expanduser if needed. Change-Id: I2551fbf90b1180ec9246478ee6ed64938b52d923
This commit is contained in:
parent
d3a45d9228
commit
f9f5ad73ad
@ -27,6 +27,7 @@ class GlanceImages(utils.GlanceScenario, nova_utils.NovaScenario):
|
||||
RESOURCE_NAME_PREFIX = "rally_image_"
|
||||
RESOURCE_NAME_LENGTH = 16
|
||||
|
||||
@types.set(image_location=types.FilePathOrUrlType)
|
||||
@validation.required_services(consts.Service.GLANCE)
|
||||
@validation.required_openstack(users=True)
|
||||
@scenario.configure(context={"cleanup": ["glance"]})
|
||||
|
@ -19,6 +19,8 @@ import operator
|
||||
import os.path
|
||||
import re
|
||||
|
||||
import requests
|
||||
|
||||
from rally import exceptions
|
||||
from rally import osclients
|
||||
from rally.task import scenario
|
||||
@ -341,6 +343,32 @@ class NeutronNetworkResourceType(ResourceType):
|
||||
name=resource_config.get("name")))
|
||||
|
||||
|
||||
class FilePathOrUrlType(ResourceType):
|
||||
|
||||
@classmethod
|
||||
def transform(cls, clients, resource_config):
|
||||
"""Check whether file exists or url available.
|
||||
|
||||
:param clients: openstack admin client handles
|
||||
:param resource_config: path or url
|
||||
|
||||
:returns: url or expanded file path
|
||||
"""
|
||||
|
||||
path = os.path.expanduser(resource_config)
|
||||
if os.path.isfile(path):
|
||||
return path
|
||||
try:
|
||||
head = requests.head(path)
|
||||
if head.status_code == 200:
|
||||
return path
|
||||
raise exceptions.InvalidScenarioArgument(
|
||||
"Url %s unavailable (code %s)" % (path, head.status_code))
|
||||
except Exception as ex:
|
||||
raise exceptions.InvalidScenarioArgument(
|
||||
"Url error %s (%s)" % (path, ex))
|
||||
|
||||
|
||||
class FileType(ResourceType):
|
||||
|
||||
@classmethod
|
||||
|
@ -362,6 +362,30 @@ class PreprocessTestCase(test.TestCase):
|
||||
self.assertEqual({"a": 20, "b": 20}, result)
|
||||
|
||||
|
||||
class FilePathOrUrlTypeTestCase(test.TestCase):
|
||||
|
||||
@mock.patch("rally.task.types.os.path.isfile")
|
||||
@mock.patch("rally.task.types.requests")
|
||||
def test_transform_file(self, mock_requests, mock_isfile):
|
||||
mock_isfile.return_value = True
|
||||
path = types.FilePathOrUrlType.transform(None, "fake_path")
|
||||
self.assertEqual("fake_path", path)
|
||||
mock_isfile.return_value = False
|
||||
mock_requests.head.return_value = mock.Mock(status_code=500)
|
||||
self.assertRaises(exceptions.InvalidScenarioArgument,
|
||||
types.FilePathOrUrlType.transform,
|
||||
None, "fake_path")
|
||||
mock_requests.head.assert_called_once_with("fake_path")
|
||||
|
||||
@mock.patch("rally.task.types.os.path.isfile")
|
||||
@mock.patch("rally.task.types.requests")
|
||||
def test_transform_url(self, mock_requests, mock_isfile):
|
||||
mock_isfile.return_value = False
|
||||
mock_requests.head.return_value = mock.Mock(status_code=200)
|
||||
path = types.FilePathOrUrlType.transform(None, "fake_url")
|
||||
self.assertEqual("fake_url", path)
|
||||
|
||||
|
||||
class FileTypeTestCase(test.TestCase):
|
||||
|
||||
@mock.patch("rally.task.types.open",
|
||||
|
Loading…
Reference in New Issue
Block a user