Store keypair and secgroup data on per-user basis
Keypairs and secgroup data are now stored separately for each user. Secgroup data is now a dict not Nova object. Change-Id: Ic4bacad2ff41c4ddcb010e8203559c0dd3945a63
This commit is contained in:
parent
3f5e5ee36f
commit
707bd0341f
@ -31,24 +31,28 @@ class Keypair(base.Context):
|
||||
KEYPAIR_NAME = "rally_ssh_key"
|
||||
|
||||
def _generate_keypair(self, endpoint):
|
||||
keypair_name = "%s_%s" % (
|
||||
self.KEYPAIR_NAME, self.context["task"]["uuid"])
|
||||
|
||||
nova_client = osclients.Clients(endpoint).nova()
|
||||
|
||||
# NOTE(hughsaunders): If keypair exists, it must be deleted as we can't
|
||||
# retrieve the private key
|
||||
try:
|
||||
nova_client.keypairs.delete(self.KEYPAIR_NAME)
|
||||
nova_client.keypairs.delete(keypair_name)
|
||||
except novaclient.exceptions.NotFound:
|
||||
pass
|
||||
|
||||
keypair = nova_client.keypairs.create(self.KEYPAIR_NAME)
|
||||
keypair = nova_client.keypairs.create(keypair_name)
|
||||
return {"private": keypair.private_key,
|
||||
"public": keypair.public_key}
|
||||
"public": keypair.public_key,
|
||||
"name": keypair_name,
|
||||
"id": keypair.id}
|
||||
|
||||
@utils.log_task_wrapper(LOG.info, _("Enter context: `keypair`"))
|
||||
def setup(self):
|
||||
for user in self.context["users"]:
|
||||
keypair = self._generate_keypair(user["endpoint"])
|
||||
user["keypair"] = keypair
|
||||
user["keypair"] = self._generate_keypair(user["endpoint"])
|
||||
|
||||
@utils.log_task_wrapper(LOG.info, _("Exit context: `keypair`"))
|
||||
def cleanup(self):
|
||||
|
@ -28,19 +28,24 @@ LOG = logging.getLogger(__name__)
|
||||
SSH_GROUP_NAME = "rally_ssh_open"
|
||||
|
||||
|
||||
def _prepare_open_secgroup(endpoint):
|
||||
def _prepare_open_secgroup(endpoint, secgroup_name):
|
||||
"""Generate secgroup allowing all tcp/udp/icmp access.
|
||||
|
||||
In order to run tests on instances it is necessary to have SSH access.
|
||||
This function generates a secgroup which allows all tcp/udp/icmp access
|
||||
This function generates a secgroup which allows all tcp/udp/icmp access.
|
||||
|
||||
:param endpoint: clients endpoint
|
||||
:param secgroup_name: security group name
|
||||
|
||||
:returns: dict with security group details
|
||||
"""
|
||||
nova = osclients.Clients(endpoint).nova()
|
||||
|
||||
if SSH_GROUP_NAME not in [sg.name for sg in nova.security_groups.list()]:
|
||||
if secgroup_name not in [sg.name for sg in nova.security_groups.list()]:
|
||||
descr = "Allow ssh access to VMs created by Rally for benchmarking"
|
||||
rally_open = nova.security_groups.create(SSH_GROUP_NAME, descr)
|
||||
rally_open = nova.security_groups.create(secgroup_name, descr)
|
||||
|
||||
rally_open = nova.security_groups.find(name=SSH_GROUP_NAME)
|
||||
rally_open = nova.security_groups.find(name=secgroup_name)
|
||||
|
||||
rules_to_add = [
|
||||
{
|
||||
@ -77,7 +82,7 @@ def _prepare_open_secgroup(endpoint):
|
||||
ip_protocol=new_rule["ip_protocol"],
|
||||
cidr=new_rule["ip_range"]["cidr"])
|
||||
|
||||
return rally_open
|
||||
return rally_open.to_dict()
|
||||
|
||||
|
||||
@base.context(name="allow_ssh", order=320)
|
||||
@ -85,12 +90,14 @@ class AllowSSH(base.Context):
|
||||
|
||||
def __init__(self, context):
|
||||
super(AllowSSH, self).__init__(context)
|
||||
self.secgroup = []
|
||||
|
||||
@utils.log_task_wrapper(LOG.info, _("Enter context: `allow_ssh`"))
|
||||
def setup(self):
|
||||
admin_or_user = (self.context.get("admin") or
|
||||
self.context.get("users")[0])
|
||||
|
||||
net_wrapper = network.wrap(
|
||||
osclients.Clients(self.context["admin"]["endpoint"]),
|
||||
osclients.Clients(admin_or_user["endpoint"]),
|
||||
self.config)
|
||||
use_sg, msg = net_wrapper.supports_security_group()
|
||||
if not use_sg:
|
||||
@ -98,16 +105,21 @@ class AllowSSH(base.Context):
|
||||
% {"message": msg})
|
||||
return
|
||||
|
||||
self.context["allow_ssh"] = SSH_GROUP_NAME
|
||||
secgroup_name = "%s_%s" % (SSH_GROUP_NAME,
|
||||
self.context["task"]["uuid"])
|
||||
|
||||
for user, tenant_id in utils.iterate_per_tenants(
|
||||
self.context["users"]):
|
||||
endpoint = user["endpoint"]
|
||||
secgroup = _prepare_open_secgroup(endpoint)
|
||||
self.secgroup.append(secgroup)
|
||||
user["secgroup"] = _prepare_open_secgroup(user["endpoint"],
|
||||
secgroup_name)
|
||||
|
||||
@utils.log_task_wrapper(LOG.info, _("Exit context: `allow_ssh`"))
|
||||
def cleanup(self):
|
||||
for secgroup in self.secgroup:
|
||||
for user, tenant_id in utils.iterate_per_tenants(
|
||||
self.context["users"]):
|
||||
with logging.ExceptionLogger(
|
||||
LOG, _("Unable to delete secgroup: %s.") % secgroup.id):
|
||||
secgroup.delete()
|
||||
LOG, _("Unable to delete secgroup: %s.") %
|
||||
user["secgroup"]["name"]):
|
||||
clients = osclients.Clients(user["endpoint"])
|
||||
clients.nova().security_groups.get(
|
||||
user["secgroup"]["id"]).delete()
|
||||
|
@ -96,12 +96,12 @@ class NovaScenario(base.Scenario):
|
||||
:returns: nova Server instance
|
||||
"""
|
||||
server_name = name or self._generate_random_name()
|
||||
allow_ssh_secgroup = self.context.get("allow_ssh")
|
||||
if allow_ssh_secgroup:
|
||||
secgroup = self.context.get("user", {}).get("secgroup")
|
||||
if secgroup:
|
||||
if "security_groups" not in kwargs:
|
||||
kwargs["security_groups"] = [allow_ssh_secgroup]
|
||||
elif allow_ssh_secgroup not in kwargs["security_groups"]:
|
||||
kwargs["security_groups"].append(allow_ssh_secgroup)
|
||||
kwargs["security_groups"] = [secgroup["name"]]
|
||||
elif secgroup["name"] not in kwargs["security_groups"]:
|
||||
kwargs["security_groups"].append(secgroup["name"])
|
||||
|
||||
if auto_assign_nic and not kwargs.get("nics", False):
|
||||
nets = [net["id"]
|
||||
|
@ -15,7 +15,6 @@
|
||||
|
||||
import json
|
||||
|
||||
from rally.benchmark.context import keypair
|
||||
from rally.benchmark.scenarios import base
|
||||
from rally.benchmark.scenarios.cinder import utils as cinder_utils
|
||||
from rally.benchmark.scenarios.nova import utils as nova_utils
|
||||
@ -80,8 +79,10 @@ class VMTasks(nova_utils.NovaScenario, vm_utils.VMScenario,
|
||||
|
||||
fip = server = None
|
||||
net_wrap = network_wrapper.wrap(self.clients)
|
||||
kwargs.update({"auto_assign_nic": True,
|
||||
"key_name": keypair.Keypair.KEYPAIR_NAME})
|
||||
kwargs.update({
|
||||
"auto_assign_nic": True,
|
||||
"key_name": self.context["user"]["keypair"]["name"]
|
||||
})
|
||||
server = self._boot_server(image, flavor, **kwargs)
|
||||
|
||||
if not server.networks:
|
||||
|
@ -26,10 +26,19 @@ class KeyPairContextTestCase(test.TestCase):
|
||||
def setUp(self):
|
||||
super(KeyPairContextTestCase, self).setUp()
|
||||
self.users = 2
|
||||
task = mock.MagicMock()
|
||||
self.keypair_name = keypair.Keypair.KEYPAIR_NAME + "_foo_task_id"
|
||||
|
||||
task = {"uuid": "foo_task_id"}
|
||||
self.ctx_with_keys = {
|
||||
"users": [
|
||||
{"keypair": "key", "endpoint": "endpoint"},
|
||||
{
|
||||
"keypair": {
|
||||
"id": "key_id",
|
||||
"key": "key",
|
||||
"name": self.keypair_name
|
||||
},
|
||||
"endpoint": "endpoint"
|
||||
},
|
||||
] * self.users,
|
||||
"task": task
|
||||
}
|
||||
@ -40,10 +49,18 @@ class KeyPairContextTestCase(test.TestCase):
|
||||
|
||||
@mock.patch("%s.keypair.Keypair._generate_keypair" % CTX)
|
||||
def test_keypair_setup(self, mock_generate):
|
||||
mock_generate.return_value = "key"
|
||||
mock_generate.side_effect = [
|
||||
{"id": "key_id", "key": "key", "name": self.keypair_name},
|
||||
{"id": "key_id", "key": "key", "name": self.keypair_name},
|
||||
]
|
||||
|
||||
keypair_ctx = keypair.Keypair(self.ctx_without_keys)
|
||||
keypair_ctx.setup()
|
||||
self.assertEqual(self.ctx_without_keys, self.ctx_with_keys)
|
||||
self.assertEqual(self.ctx_with_keys, keypair_ctx.context)
|
||||
|
||||
self.assertEqual(
|
||||
[mock.call("endpoint")] * 2,
|
||||
mock_generate.mock_calls)
|
||||
|
||||
@mock.patch("%s.keypair.resource_manager.cleanup" % CTX)
|
||||
def test_keypair_cleanup(self, mock_cleanup):
|
||||
@ -54,10 +71,22 @@ class KeyPairContextTestCase(test.TestCase):
|
||||
|
||||
@mock.patch("rally.osclients.Clients")
|
||||
def test_keypair_generate(self, mock_osclients):
|
||||
mock_keypairs = mock_osclients.return_value.nova.return_value.keypairs
|
||||
mock_keypair = mock_keypairs.create.return_value
|
||||
mock_keypair.public_key = "public_key"
|
||||
mock_keypair.private_key = "private_key"
|
||||
mock_keypair.id = "key_id"
|
||||
keypair_ctx = keypair.Keypair(self.ctx_without_keys)
|
||||
keypair_ctx._generate_keypair("endpoint")
|
||||
key = keypair_ctx._generate_keypair("endpoint")
|
||||
|
||||
self.assertEqual({
|
||||
"id": "key_id",
|
||||
"name": "rally_ssh_key_foo_task_id",
|
||||
"private": "private_key",
|
||||
"public": "public_key"
|
||||
}, key)
|
||||
|
||||
mock_osclients.assert_has_calls([
|
||||
mock.call().nova().keypairs.delete("rally_ssh_key"),
|
||||
mock.call().nova().keypairs.create("rally_ssh_key"),
|
||||
mock.call().nova().keypairs.delete("rally_ssh_key_foo_task_id"),
|
||||
mock.call().nova().keypairs.create("rally_ssh_key_foo_task_id"),
|
||||
])
|
||||
|
142
tests/unit/benchmark/context/test_secgroup.py
Normal file
142
tests/unit/benchmark/context/test_secgroup.py
Normal file
@ -0,0 +1,142 @@
|
||||
# Copyright 2014: Mirantis Inc.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
|
||||
from rally.benchmark.context import secgroup
|
||||
from tests.unit import fakes
|
||||
from tests.unit import test
|
||||
|
||||
|
||||
class SecGroupContextTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(SecGroupContextTestCase, self).setUp()
|
||||
self.users = 2
|
||||
task = {"uuid": "foo_task_id"}
|
||||
self.secgroup_name = secgroup.SSH_GROUP_NAME + "_foo"
|
||||
self.ctx_with_secgroup = {
|
||||
"users": [
|
||||
{
|
||||
"tenant_id": "uuid1",
|
||||
"endpoint": "endpoint",
|
||||
"secgroup": {"id": "secgroup_id", "name": "secgroup"}
|
||||
}
|
||||
] * self.users,
|
||||
"admin": {"tenant_id": "uuid2", "endpoint": "admin_endpoint"},
|
||||
"tenants": {"uuid1": {"id": "uuid1", "name": "uuid1"}},
|
||||
"task": task
|
||||
}
|
||||
self.ctx_without_secgroup = {
|
||||
"users": [{"tenant_id": "uuid1",
|
||||
"endpoint": "endpoint"}] * self.users,
|
||||
"admin": {"tenant_id": "uuid2", "endpoint": "admin_endpoint"},
|
||||
"tenants": {"uuid1": {"id": "uuid1", "name": "uuid1"}},
|
||||
"task": task
|
||||
}
|
||||
|
||||
@mock.patch("rally.benchmark.context.secgroup.osclients.Clients")
|
||||
def test__prepare_open_secgroup(self, mock_osclients):
|
||||
fake_nova = fakes.FakeNovaClient()
|
||||
self.assertEqual(len(fake_nova.security_groups.list()), 1)
|
||||
mock_cl = mock.MagicMock()
|
||||
mock_cl.nova.return_value = fake_nova
|
||||
mock_osclients.return_value = mock_cl
|
||||
|
||||
ret = secgroup._prepare_open_secgroup("endpoint", self.secgroup_name)
|
||||
self.assertEqual(self.secgroup_name, ret["name"])
|
||||
|
||||
self.assertEqual(2, len(fake_nova.security_groups.list()))
|
||||
self.assertIn(
|
||||
self.secgroup_name,
|
||||
[sg.name for sg in fake_nova.security_groups.list()])
|
||||
|
||||
# run prep again, check that another security group is not created
|
||||
secgroup._prepare_open_secgroup("endpoint", self.secgroup_name)
|
||||
self.assertEqual(2, len(fake_nova.security_groups.list()))
|
||||
|
||||
@mock.patch("rally.benchmark.context.secgroup.osclients.Clients")
|
||||
def test__prepare_open_secgroup_rules(self, mock_osclients):
|
||||
fake_nova = fakes.FakeNovaClient()
|
||||
|
||||
# NOTE(hughsaunders) Default security group is precreated
|
||||
self.assertEqual(1, len(fake_nova.security_groups.list()))
|
||||
mock_cl = mock.MagicMock()
|
||||
mock_cl.nova.return_value = fake_nova
|
||||
mock_osclients.return_value = mock_cl
|
||||
|
||||
secgroup._prepare_open_secgroup("endpoint", self.secgroup_name)
|
||||
|
||||
self.assertEqual(2, len(fake_nova.security_groups.list()))
|
||||
rally_open = fake_nova.security_groups.find(self.secgroup_name)
|
||||
self.assertEqual(3, len(rally_open.rules))
|
||||
|
||||
# run prep again, check that extra rules are not created
|
||||
secgroup._prepare_open_secgroup("endpoint", self.secgroup_name)
|
||||
rally_open = fake_nova.security_groups.find(self.secgroup_name)
|
||||
self.assertEqual(3, len(rally_open.rules))
|
||||
|
||||
@mock.patch("rally.benchmark.context.secgroup.osclients.Clients")
|
||||
@mock.patch("rally.benchmark.context.secgroup._prepare_open_secgroup")
|
||||
@mock.patch("rally.benchmark.wrappers.network.wrap")
|
||||
def test_secgroup_setup_cleanup_with_secgroup_supported(
|
||||
self, mock_network_wrap, mock_prepare_open_secgroup,
|
||||
mock_osclients):
|
||||
mock_network_wrapper = mock.MagicMock()
|
||||
mock_network_wrapper.supports_security_group.return_value = (
|
||||
True, "")
|
||||
mock_network_wrap.return_value = mock_network_wrapper
|
||||
mock_prepare_open_secgroup.return_value = {
|
||||
"name": "secgroup",
|
||||
"id": "secgroup_id"}
|
||||
mock_osclients.return_value = mock.MagicMock()
|
||||
|
||||
secgrp_ctx = secgroup.AllowSSH(self.ctx_without_secgroup)
|
||||
secgrp_ctx.setup()
|
||||
self.assertEqual(self.ctx_with_secgroup, secgrp_ctx.context)
|
||||
secgrp_ctx.cleanup()
|
||||
|
||||
self.assertEqual(
|
||||
[
|
||||
mock.call("admin_endpoint"),
|
||||
mock.call("endpoint"),
|
||||
mock.call().nova(),
|
||||
mock.call().nova().security_groups.get("secgroup_id"),
|
||||
mock.call().nova().security_groups.get().delete()
|
||||
],
|
||||
mock_osclients.mock_calls)
|
||||
|
||||
mock_network_wrap.assert_called_once_with(
|
||||
mock_osclients.return_value, {})
|
||||
|
||||
@mock.patch("rally.benchmark.context.secgroup.osclients.Clients")
|
||||
@mock.patch("rally.benchmark.wrappers.network.wrap")
|
||||
def test_secgroup_setup_with_secgroup_unsupported(self,
|
||||
mock_network_wrap,
|
||||
mock_osclients):
|
||||
mock_network_wrapper = mock.MagicMock()
|
||||
mock_network_wrapper.supports_security_group.return_value = (
|
||||
False, "Not supported")
|
||||
mock_network_wrap.return_value = mock_network_wrapper
|
||||
mock_osclients.return_value = mock.MagicMock()
|
||||
|
||||
secgrp_ctx = secgroup.AllowSSH(dict(self.ctx_without_secgroup))
|
||||
secgrp_ctx.setup()
|
||||
self.assertEqual(self.ctx_without_secgroup, secgrp_ctx.context)
|
||||
|
||||
mock_osclients.assert_called_once_with("admin_endpoint")
|
||||
|
||||
mock_network_wrap.assert_called_once_with(
|
||||
mock_osclients.return_value, {})
|
@ -1,115 +0,0 @@
|
||||
# Copyright 2014: Mirantis Inc.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import mock
|
||||
|
||||
from rally.benchmark.context import secgroup
|
||||
from tests.unit import fakes
|
||||
from tests.unit import test
|
||||
|
||||
|
||||
class SecGroupContextTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(SecGroupContextTestCase, self).setUp()
|
||||
self.users = 2
|
||||
task = mock.MagicMock()
|
||||
self.ctx_without_keys = {
|
||||
"admin": {"endpoint": "endpoint"},
|
||||
"users": [{"tenant_id": "uuid1",
|
||||
"endpoint": mock.MagicMock()}] * self.users,
|
||||
"tenants": {"uuid1": {"id": "uuid1", "name": "uuid1"}},
|
||||
"task": task
|
||||
}
|
||||
|
||||
@mock.patch("rally.benchmark.context.secgroup.osclients.Clients")
|
||||
def test_prep_ssh_sec_group(self, mock_osclients):
|
||||
fake_nova = fakes.FakeNovaClient()
|
||||
self.assertEqual(len(fake_nova.security_groups.list()), 1)
|
||||
mock_cl = mock.MagicMock()
|
||||
mock_cl.nova.return_value = fake_nova
|
||||
mock_osclients.return_value = mock_cl
|
||||
|
||||
secgroup._prepare_open_secgroup("endpoint")
|
||||
|
||||
self.assertEqual(len(fake_nova.security_groups.list()), 2)
|
||||
self.assertTrue(
|
||||
secgroup.SSH_GROUP_NAME in [
|
||||
sg.name for sg in fake_nova.security_groups.list()
|
||||
])
|
||||
|
||||
# run prep again, check that another security group is not created
|
||||
secgroup._prepare_open_secgroup("endpoint")
|
||||
self.assertEqual(len(fake_nova.security_groups.list()), 2)
|
||||
|
||||
@mock.patch("rally.benchmark.context.secgroup.osclients.Clients")
|
||||
def test_prep_ssh_sec_group_rules(self, mock_osclients):
|
||||
fake_nova = fakes.FakeNovaClient()
|
||||
|
||||
# NOTE(hughsaunders) Default security group is precreated
|
||||
self.assertEqual(len(fake_nova.security_groups.list()), 1)
|
||||
mock_cl = mock.MagicMock()
|
||||
mock_cl.nova.return_value = fake_nova
|
||||
mock_osclients.return_value = mock_cl
|
||||
|
||||
secgroup._prepare_open_secgroup("endpoint")
|
||||
|
||||
self.assertEqual(len(fake_nova.security_groups.list()), 2)
|
||||
rally_open = fake_nova.security_groups.find(secgroup.SSH_GROUP_NAME)
|
||||
self.assertEqual(len(rally_open.rules), 3)
|
||||
|
||||
# run prep again, check that extra rules are not created
|
||||
secgroup._prepare_open_secgroup("endpoint")
|
||||
rally_open = fake_nova.security_groups.find(secgroup.SSH_GROUP_NAME)
|
||||
self.assertEqual(len(rally_open.rules), 3)
|
||||
|
||||
@mock.patch("rally.benchmark.context.secgroup.osclients.Clients")
|
||||
@mock.patch("rally.benchmark.context.secgroup._prepare_open_secgroup")
|
||||
@mock.patch("rally.benchmark.wrappers.network.NetworkWrapper")
|
||||
@mock.patch("rally.benchmark.wrappers.network.wrap")
|
||||
@mock.patch("novaclient.v1_1.security_groups.SecurityGroup")
|
||||
def test_sec_group_setup_secgroup_supported(self,
|
||||
mock_security_group,
|
||||
mock_network_wrap,
|
||||
mock_network_wrapper,
|
||||
mock_prepare_open_secgroup,
|
||||
mock_osclients):
|
||||
mock_network_wrap.return_value = mock_network_wrapper
|
||||
mock_network_wrapper.supports_security_group.return_value = (
|
||||
True, "")
|
||||
mock_prepare_open_secgroup.return_value = mock_security_group
|
||||
mock_osclients.return_value = mock.MagicMock()
|
||||
|
||||
secgrp_ctx = secgroup.AllowSSH(self.ctx_without_keys)
|
||||
secgrp_ctx.setup()
|
||||
self.assertEqual(len(secgrp_ctx.secgroup), 1)
|
||||
secgrp_ctx.cleanup()
|
||||
self.assertTrue(mock_security_group.delete.called)
|
||||
|
||||
@mock.patch("rally.benchmark.context.secgroup.osclients.Clients")
|
||||
@mock.patch("rally.benchmark.wrappers.network.NetworkWrapper")
|
||||
@mock.patch("rally.benchmark.wrappers.network.wrap")
|
||||
def test_sec_group_setup_secgroup_unsupported(self,
|
||||
mock_network_wrap,
|
||||
mock_network_wrapper,
|
||||
mock_osclients):
|
||||
mock_network_wrap.return_value = mock_network_wrapper
|
||||
mock_network_wrapper.supports_security_group.return_value = (
|
||||
False, "Not supported")
|
||||
mock_osclients.return_value = mock.MagicMock()
|
||||
|
||||
secgrp_ctx = secgroup.AllowSSH(self.ctx_without_keys)
|
||||
secgrp_ctx.setup()
|
||||
self.assertEqual(len(secgrp_ctx.secgroup), 0)
|
@ -26,6 +26,7 @@ from tests.unit import test
|
||||
|
||||
BM_UTILS = "rally.benchmark.utils"
|
||||
NOVA_UTILS = "rally.benchmark.scenarios.nova.utils"
|
||||
SCN = "rally.benchmark.scenarios.base"
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
@ -95,8 +96,10 @@ class NovaScenarioTestCase(test.TestCase):
|
||||
self._test_atomic_action_timer(nova_scenario.atomic_actions(),
|
||||
"nova.list_servers")
|
||||
|
||||
@mock.patch(SCN + ".Scenario._generate_random_name",
|
||||
return_value="foo_server_name")
|
||||
@mock.patch(NOVA_UTILS + ".NovaScenario.clients")
|
||||
def test__boot_server(self, mock_clients):
|
||||
def test__boot_server(self, mock_clients, mock_generate_random_name):
|
||||
mock_clients("nova").servers.create.return_value = self.server
|
||||
nova_scenario = utils.NovaScenario(context={})
|
||||
return_server = nova_scenario._boot_server("image_id",
|
||||
@ -107,11 +110,16 @@ class NovaScenarioTestCase(test.TestCase):
|
||||
CONF.benchmark.nova_server_boot_timeout)
|
||||
self.res_is.mock.assert_has_calls([mock.call("ACTIVE")])
|
||||
self.assertEqual(self.wait_for.mock(), return_server)
|
||||
mock_clients("nova").servers.create.assert_called_once_with(
|
||||
"foo_server_name", "image_id", "flavor_id")
|
||||
self._test_atomic_action_timer(nova_scenario.atomic_actions(),
|
||||
"nova.boot_server")
|
||||
|
||||
@mock.patch(SCN + ".Scenario._generate_random_name",
|
||||
return_value="foo_server_name")
|
||||
@mock.patch(NOVA_UTILS + ".NovaScenario.clients")
|
||||
def test__boot_server_with_network(self, mock_clients):
|
||||
def test__boot_server_with_network(self, mock_clients,
|
||||
mock_generate_random_name):
|
||||
mock_clients("nova").servers.create.return_value = self.server
|
||||
networks = [{"id": "foo_id", "external": False},
|
||||
{"id": "bar_id", "external": False}]
|
||||
@ -128,6 +136,9 @@ class NovaScenarioTestCase(test.TestCase):
|
||||
CONF.benchmark.nova_server_boot_poll_interval,
|
||||
CONF.benchmark.nova_server_boot_timeout)
|
||||
self.res_is.mock.assert_has_calls([mock.call("ACTIVE")])
|
||||
mock_clients("nova").servers.create.assert_called_once_with(
|
||||
"foo_server_name", "image_id", "flavor_id",
|
||||
nics=[{"net-id": "bar_id"}])
|
||||
self.assertEqual(self.wait_for.mock(), return_server)
|
||||
self._test_atomic_action_timer(nova_scenario.atomic_actions(),
|
||||
"nova.boot_server")
|
||||
@ -141,10 +152,15 @@ class NovaScenarioTestCase(test.TestCase):
|
||||
"image_id", "flavor_id",
|
||||
auto_assign_nic=True)
|
||||
|
||||
@mock.patch(SCN + ".Scenario._generate_random_name",
|
||||
return_value="foo_server_name")
|
||||
@mock.patch(NOVA_UTILS + ".NovaScenario.clients")
|
||||
def test__boot_server_with_ssh(self, mock_clients):
|
||||
def test__boot_server_with_ssh(self, mock_clients,
|
||||
mock_generate_random_name):
|
||||
mock_clients("nova").servers.create.return_value = self.server
|
||||
nova_scenario = utils.NovaScenario(context={"allow_ssh": "test"})
|
||||
nova_scenario = utils.NovaScenario(context={
|
||||
"user": {"secgroup": {"name": "test"}}}
|
||||
)
|
||||
return_server = nova_scenario._boot_server("image_id", "flavor_id")
|
||||
self._test_assert_called_once_with(
|
||||
self.wait_for.mock, self.server,
|
||||
@ -152,13 +168,45 @@ class NovaScenarioTestCase(test.TestCase):
|
||||
CONF.benchmark.nova_server_boot_timeout)
|
||||
self.res_is.mock.assert_has_calls([mock.call("ACTIVE")])
|
||||
self.assertEqual(self.wait_for.mock(), return_server)
|
||||
mock_clients("nova").servers.create.assert_called_once_with(
|
||||
"foo_server_name", "image_id", "flavor_id",
|
||||
security_groups=["test"])
|
||||
self._test_atomic_action_timer(nova_scenario.atomic_actions(),
|
||||
"nova.boot_server")
|
||||
|
||||
@mock.patch(SCN + ".Scenario._generate_random_name",
|
||||
return_value="foo_server_name")
|
||||
@mock.patch(NOVA_UTILS + ".NovaScenario.clients")
|
||||
def test__boot_server_with_sec_group(self, mock_clients):
|
||||
def test__boot_server_with_sec_group(self, mock_clients,
|
||||
mock_generate_random_name):
|
||||
mock_clients("nova").servers.create.return_value = self.server
|
||||
nova_scenario = utils.NovaScenario(context={"allow_ssh": "new"})
|
||||
nova_scenario = utils.NovaScenario(context={
|
||||
"user": {"secgroup": {"name": "new"}}}
|
||||
)
|
||||
return_server = nova_scenario._boot_server(
|
||||
"image_id", "flavor_id",
|
||||
security_groups=["test"])
|
||||
self._test_assert_called_once_with(
|
||||
self.wait_for.mock, self.server,
|
||||
CONF.benchmark.nova_server_boot_poll_interval,
|
||||
CONF.benchmark.nova_server_boot_timeout)
|
||||
self.res_is.mock.assert_has_calls([mock.call("ACTIVE")])
|
||||
self.assertEqual(self.wait_for.mock(), return_server)
|
||||
mock_clients("nova").servers.create.assert_called_once_with(
|
||||
"foo_server_name", "image_id", "flavor_id",
|
||||
security_groups=["test", "new"])
|
||||
self._test_atomic_action_timer(nova_scenario.atomic_actions(),
|
||||
"nova.boot_server")
|
||||
|
||||
@mock.patch(SCN + ".Scenario._generate_random_name",
|
||||
return_value="foo_server_name")
|
||||
@mock.patch(NOVA_UTILS + ".NovaScenario.clients")
|
||||
def test__boot_server_with_similar_sec_group(self, mock_clients,
|
||||
mock_generate_random_name):
|
||||
mock_clients("nova").servers.create.return_value = self.server
|
||||
nova_scenario = utils.NovaScenario(context={
|
||||
"user": {"secgroup": {"name": "test1"}}}
|
||||
)
|
||||
return_server = nova_scenario._boot_server(
|
||||
"image_id", "flavor_id",
|
||||
security_groups=["test1"])
|
||||
@ -168,22 +216,9 @@ class NovaScenarioTestCase(test.TestCase):
|
||||
CONF.benchmark.nova_server_boot_timeout)
|
||||
self.res_is.mock.assert_has_calls([mock.call("ACTIVE")])
|
||||
self.assertEqual(self.wait_for.mock(), return_server)
|
||||
self._test_atomic_action_timer(nova_scenario.atomic_actions(),
|
||||
"nova.boot_server")
|
||||
|
||||
@mock.patch(NOVA_UTILS + ".NovaScenario.clients")
|
||||
def test__boot_server_with_similar_sec_group(self, mock_clients):
|
||||
mock_clients("nova").servers.create.return_value = self.server
|
||||
nova_scenario = utils.NovaScenario(context={"allow_ssh": "test1"})
|
||||
return_server = nova_scenario._boot_server(
|
||||
"image_id", "flavor_id",
|
||||
mock_clients("nova").servers.create.assert_called_once_with(
|
||||
"foo_server_name", "image_id", "flavor_id",
|
||||
security_groups=["test1"])
|
||||
self._test_assert_called_once_with(
|
||||
self.wait_for.mock, self.server,
|
||||
CONF.benchmark.nova_server_boot_poll_interval,
|
||||
CONF.benchmark.nova_server_boot_timeout)
|
||||
self.res_is.mock.assert_has_calls([mock.call("ACTIVE")])
|
||||
self.assertEqual(self.wait_for.mock(), return_server)
|
||||
self._test_atomic_action_timer(nova_scenario.atomic_actions(),
|
||||
"nova.boot_server")
|
||||
|
||||
|
@ -15,7 +15,6 @@
|
||||
|
||||
import mock
|
||||
|
||||
from rally.benchmark.context import keypair
|
||||
from rally.benchmark.scenarios.vm import vmtasks
|
||||
from rally import exceptions
|
||||
from tests.unit import test
|
||||
@ -28,7 +27,10 @@ class VMTasksTestCase(test.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(VMTasksTestCase, self).setUp()
|
||||
self.scenario = vmtasks.VMTasks()
|
||||
self.context = {
|
||||
"user": {"keypair": {"name": "keypair_name"}}
|
||||
}
|
||||
self.scenario = vmtasks.VMTasks(self.context)
|
||||
self.clients = mock.Mock()
|
||||
self.server = mock.Mock(networks={"foo_net": "foo_net_data"},
|
||||
addresses={"foo_net": [{"addr": "foo_addr"}]},
|
||||
@ -78,7 +80,7 @@ class VMTasksTestCase(test.TestCase):
|
||||
"foo_image", "foo_flavor",
|
||||
block_device_mapping={"vdrally": "foo_volume:::1"},
|
||||
nics=[{"net-id": "foo_network"}], auto_assign_nic=True,
|
||||
key_name=keypair.Keypair.KEYPAIR_NAME)
|
||||
key_name="keypair_name")
|
||||
|
||||
self.scenario._associate_floating_ip.assert_called_once_with(
|
||||
self.server, "foo_fip", fixed_address="foo_addr")
|
||||
@ -102,7 +104,7 @@ class VMTasksTestCase(test.TestCase):
|
||||
|
||||
self.scenario._boot_server.assert_called_once_with(
|
||||
"foo_image", "foo_flavor", auto_assign_nic=True,
|
||||
key_name=keypair.Keypair.KEYPAIR_NAME)
|
||||
key_name="keypair_name")
|
||||
self.scenario._associate_floating_ip.assert_called_once_with(
|
||||
self.server, "foo_fip", fixed_address="foo_addr")
|
||||
|
||||
@ -118,7 +120,7 @@ class VMTasksTestCase(test.TestCase):
|
||||
|
||||
self.scenario._boot_server.assert_called_once_with(
|
||||
"foo_image", "foo_flavor", auto_assign_nic=True,
|
||||
key_name=keypair.Keypair.KEYPAIR_NAME)
|
||||
key_name="keypair_name")
|
||||
|
||||
net_wrap.create_floating_ip.assert_called_once_with(
|
||||
tenant_id="foo_tenant", fixed_ip="foo_addr",
|
||||
|
@ -96,7 +96,7 @@ class FakeResource(object):
|
||||
def __getattr__(self, name):
|
||||
# NOTE(msdubov): e.g. server.delete() -> manager.delete(server)
|
||||
def manager_func(*args, **kwargs):
|
||||
getattr(self.manager, name)(self, *args, **kwargs)
|
||||
return getattr(self.manager, name)(self, *args, **kwargs)
|
||||
return manager_func
|
||||
|
||||
def __getitem__(self, key):
|
||||
@ -529,6 +529,9 @@ class FakeSecurityGroupManager(FakeManager):
|
||||
sg.description = description
|
||||
return self._cache(sg)
|
||||
|
||||
def to_dict(self, obj):
|
||||
return {"id": obj.id, "name": obj.name}
|
||||
|
||||
def find(self, name, **kwargs):
|
||||
kwargs["name"] = name
|
||||
for resource in self.cache.values():
|
||||
|
Loading…
Reference in New Issue
Block a user