Move 'external-network-exists' validator to 'openstack/validators.py'
Change-Id: Ieaea40a9df60d9b0b2997dd5dc9c077e81c12b20
This commit is contained in:
@@ -449,7 +449,7 @@ class CreateAndDeletePorts(utils.NeutronScenario):
|
||||
|
||||
@validation.required_services(consts.Service.NEUTRON)
|
||||
@validation.add("required_platform", platform="openstack", users=True)
|
||||
@validation.external_network_exists("floating_network")
|
||||
@validation.add("external_network_exists", param_name="floating_network")
|
||||
@scenario.configure(context={"cleanup": ["neutron"]},
|
||||
name="NeutronNetworks.create_and_list_floating_ips")
|
||||
class CreateAndListFloatingIps(utils.NeutronScenario):
|
||||
@@ -470,7 +470,7 @@ class CreateAndListFloatingIps(utils.NeutronScenario):
|
||||
|
||||
@validation.required_services(consts.Service.NEUTRON)
|
||||
@validation.add("required_platform", platform="openstack", users=True)
|
||||
@validation.external_network_exists("floating_network")
|
||||
@validation.add("external_network_exists", param_name="floating_network")
|
||||
@scenario.configure(context={"cleanup": ["neutron"]},
|
||||
name="NeutronNetworks.create_and_delete_floating_ips")
|
||||
class CreateAndDeleteFloatingIps(utils.NeutronScenario):
|
||||
|
||||
@@ -40,7 +40,7 @@ LOG = logging.getLogger(__name__)
|
||||
@validation.valid_command("command")
|
||||
@validation.add("number", param_name="port", minval=1, maxval=65535,
|
||||
nullable=True, integer_only=True)
|
||||
@validation.external_network_exists("floating_network")
|
||||
@validation.add("external_network_exists", param_name="floating_network")
|
||||
@validation.required_param_or_context(arg_name="image",
|
||||
ctx_name="image_command_customizer")
|
||||
@validation.required_services(consts.Service.NOVA, consts.Service.CINDER)
|
||||
@@ -395,7 +395,7 @@ EOF
|
||||
@validation.valid_command("command")
|
||||
@validation.add("number", param_name="port", minval=1, maxval=65535,
|
||||
nullable=True, integer_only=True)
|
||||
@validation.external_network_exists("floating_network")
|
||||
@validation.add("external_network_exists", param_name="floating_network")
|
||||
@validation.required_services(consts.Service.NOVA, consts.Service.CINDER)
|
||||
@validation.add("required_platform", platform="openstack", users=True)
|
||||
@scenario.configure(context={"cleanup": ["nova", "cinder"],
|
||||
|
||||
@@ -70,3 +70,40 @@ class ImageExistsValidator(validation.Validator):
|
||||
except (glance_exc.HTTPNotFound, exceptions.InvalidScenarioArgument):
|
||||
message = ("Image '%s' not found") % image_args
|
||||
return self.fail(message)
|
||||
|
||||
|
||||
@validation.add("required_platform", platform="openstack", users=True)
|
||||
@validation.configure(name="external_network_exists", namespace="openstack")
|
||||
class ExternalNetworkExistsValidator(validation.Validator):
|
||||
|
||||
def __init__(self, param_name):
|
||||
"""Validator checks that external network with given name exists.
|
||||
|
||||
:param param_name: name of validated network
|
||||
"""
|
||||
super(ExternalNetworkExistsValidator, self).__init__()
|
||||
self.param_name = param_name
|
||||
|
||||
def validate(self, config, credentials, plugin_cls, plugin_cfg):
|
||||
|
||||
ext_network = config.get("args", {}).get(self.param_name)
|
||||
if not ext_network:
|
||||
return
|
||||
|
||||
users = credentials["openstack"]["users"]
|
||||
result = []
|
||||
for user in users:
|
||||
creds = user["credential"]
|
||||
|
||||
networks = creds.clients().neutron().list_networks()["networks"]
|
||||
external_networks = [net["name"] for net in networks if
|
||||
net.get("router:external", False)]
|
||||
if ext_network not in external_networks:
|
||||
message = ("External (floating) network with name {1} "
|
||||
"not found by user {0}. "
|
||||
"Available networks: {2}").format(creds.username,
|
||||
ext_network,
|
||||
networks)
|
||||
result.append(message)
|
||||
if result:
|
||||
return self.fail(result)
|
||||
|
||||
@@ -22,28 +22,25 @@ from rally import exceptions
|
||||
from rally.plugins.openstack import validators
|
||||
from tests.unit import test
|
||||
|
||||
|
||||
credentials = {
|
||||
"openstack": {
|
||||
"admin": mock.Mock(),
|
||||
"users": [mock.Mock()],
|
||||
"users": [mock.MagicMock()],
|
||||
}
|
||||
}
|
||||
|
||||
config = {"args": {
|
||||
"image": {
|
||||
"id": "fake_id",
|
||||
"image_name": "foo_image"
|
||||
},
|
||||
"flavor": {
|
||||
"id": "fake_flavor_id",
|
||||
},
|
||||
"foo_image": {
|
||||
"id": "fake_image_id",
|
||||
}},
|
||||
"context": {
|
||||
"images": {
|
||||
"image_name": "foo_image"
|
||||
}}}
|
||||
config = dict(args={"image": {"id": "fake_id",
|
||||
"min_ram": 10,
|
||||
"size": 1024 ** 3,
|
||||
"min_disk": 10.0 * (1024 ** 3),
|
||||
"image_name": "foo_image"},
|
||||
"flavor": {"id": "fake_flavor_id",
|
||||
"name": "test"},
|
||||
"foo_image": {"id": "fake_image_id"}
|
||||
},
|
||||
context={"images": {"image_name": "foo_image"}}
|
||||
)
|
||||
|
||||
|
||||
@ddt.ddt
|
||||
@@ -113,3 +110,48 @@ class ImageExistsValidatorTestCase(test.TestCase):
|
||||
result = validator.validate(config, credentials, None, None)
|
||||
|
||||
self.assertEqual("Image 'fake_image' not found", result.msg)
|
||||
|
||||
|
||||
@ddt.ddt
|
||||
class ExternalNetworkExistsValidatorTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(ExternalNetworkExistsValidatorTestCase, self).setUp()
|
||||
self.validator = validators.ExternalNetworkExistsValidator("net")
|
||||
self.config = config
|
||||
self.credentials = credentials
|
||||
|
||||
@ddt.unpack
|
||||
@ddt.data(
|
||||
{"foo_conf": {}},
|
||||
{"foo_conf": {"args": {"net": "custom"}}},
|
||||
{"foo_conf": {"args": {"net": "non_exist"}},
|
||||
"err_msg": "External (floating) network with name non_exist"
|
||||
" not found by user {}. Available networks:"
|
||||
" [{}, {}]"},
|
||||
{"foo_conf": {"args": {"net": "custom"}},
|
||||
"net1_name": {"name": {"net": "public"}},
|
||||
"net2_name": {"name": {"net": "custom"}},
|
||||
"err_msg": "External (floating) network with name custom"
|
||||
" not found by user {}. Available networks:"
|
||||
" [{}, {}]"}
|
||||
)
|
||||
def test_validator(self, foo_conf, net1_name="public", net2_name="custom",
|
||||
err_msg=""):
|
||||
|
||||
user = self.credentials["openstack"]["users"][0]
|
||||
|
||||
net1 = {"name": net1_name, "router:external": True}
|
||||
net2 = {"name": net2_name, "router:external": True}
|
||||
|
||||
user["credential"].clients().neutron().list_networks.return_value = {
|
||||
"networks": [net1, net2]}
|
||||
|
||||
result = self.validator.validate(foo_conf, self.credentials,
|
||||
None, None)
|
||||
if err_msg:
|
||||
self.assertTrue(result)
|
||||
self.assertEqual(err_msg.format(user["credential"].username,
|
||||
net1, net2), result.msg[0])
|
||||
elif result:
|
||||
self.assertIsNone(result, "Unexpected result '%s'" % result)
|
||||
|
||||
Reference in New Issue
Block a user