##############################################################################
from collections import OrderedDict
-from itertools import count
import logging
+import os
import mock
import unittest
-import shade
-
from yardstick.benchmark.contexts import base
from yardstick.benchmark.contexts import heat
from yardstick.benchmark.contexts import model
+from yardstick.common import constants as consts
from yardstick.common import exceptions as y_exc
-from yardstick.orchestrator import heat as orch_heat
+from yardstick.common import openstack_utils
from yardstick import ssh
class HeatContextTestCase(unittest.TestCase):
+ HEAT_POD_SAMPLE = {
+ "nodes": [
+ {
+ "name": "node1",
+ "role": "Controller",
+ "ip": "10.229.47.137",
+ "user": "root",
+ "key_filename": "/root/.yardstick_key"
+ },
+ {
+ "name": "node2",
+ "role": "Compute",
+ "ip": "10.229.47.139",
+ "user": "root",
+ "key_filename": "/root/.yardstick_key"
+ }
+ ]
+ }
+
def __init__(self, *args, **kwargs):
+
super(HeatContextTestCase, self).__init__(*args, **kwargs)
- self.name_iter = ('vnf{:03}'.format(x) for x in count(0, step=3))
def setUp(self):
self.test_context = heat.HeatContext()
self.assertIsNone(self.test_context.heat_parameters)
self.assertIsNone(self.test_context.key_filename)
- @mock.patch.object(ssh.SSH, 'gen_keys')
+ @mock.patch('yardstick.common.utils.read_yaml_file')
@mock.patch('yardstick.benchmark.contexts.heat.PlacementGroup')
@mock.patch('yardstick.benchmark.contexts.heat.ServerGroup')
@mock.patch('yardstick.benchmark.contexts.heat.Network')
@mock.patch('yardstick.benchmark.contexts.heat.Server')
- def test_init(self, mock_server, mock_network, mock_sg, mock_pg, mock_ssh_gen_keys):
+ def test_init(self, mock_server, mock_network, mock_sg, mock_pg, mock_read_yaml):
+ mock_read_yaml.return_value = self.HEAT_POD_SAMPLE
pgs = {'pgrp1': {'policy': 'availability'}}
sgs = {'servergroup1': {'policy': 'affinity'}}
networks = {'bar': {'cidr': '10.0.1.0/24'}}
servers = {'baz': {'floating_ip': True, 'placement': 'pgrp1'}}
attrs = {'name': 'foo',
+ 'file': 'pod.yaml',
'task_id': '1234567890',
'placement_groups': pgs,
'server_groups': sgs,
'networks': networks,
'servers': servers}
- self.test_context.init(attrs)
+ with mock.patch.object(openstack_utils, 'get_shade_client'), \
+ mock.patch.object(openstack_utils, 'get_shade_operator_client'):
+ self.test_context.init(attrs)
self.assertFalse(self.test_context._flags.no_setup)
self.assertFalse(self.test_context._flags.no_teardown)
servers['baz'])
self.assertEqual(len(self.test_context.servers), 1)
- mock_ssh_gen_keys.assert_called()
-
def test_init_no_name_or_task_id(self):
attrs = {}
self.assertRaises(KeyError, self.test_context.init, attrs)
self.assertEqual(self.test_context.name, 'foo')
self.assertEqual(self.test_context.assigned_name, 'foo')
- @mock.patch('yardstick.ssh.SSH.gen_keys')
- def test_init_no_setup_no_teardown(self, *args):
+ def test_init_no_setup_no_teardown(self):
attrs = {'name': 'foo',
'task_id': '1234567890',
'server_groups': {},
'networks': {},
'servers': {},
+ 'file': "pod.yaml",
'flags': {
'no_setup': True,
'no_teardown': True,
},
}
- self.test_context.init(attrs)
+ with mock.patch.object(openstack_utils, 'get_shade_client'), \
+ mock.patch.object(openstack_utils, 'get_shade_operator_client'):
+ self.test_context.init(attrs)
+
self.assertTrue(self.test_context._flags.no_setup)
self.assertTrue(self.test_context._flags.no_teardown)
self.test_context._create_new_stack,
template)
- @mock.patch.object(orch_heat.HeatTemplate, 'add_keypair')
+ @mock.patch.object(os.path, 'exists', return_value=True)
+ @mock.patch.object(heat.HeatContext, '_add_resources_to_template')
@mock.patch.object(heat.HeatContext, '_create_new_stack')
- def test_deploy_stack_creation_failed(self, mock_create, *args):
+ def test_deploy_stack_creation_failed(self, mock_create,
+ mock_resources_template, mock_path_exists):
self.test_context._name = 'foo'
self.test_context._task_id = '1234567890'
self.test_context._name_task_id = 'foo-12345678'
self.assertRaises(y_exc.HeatTemplateError,
self.test_context.deploy)
- @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
- def test_deploy(self, mock_template):
+ mock_path_exists.assert_called()
+ mock_resources_template.assert_called_once()
+
+ @mock.patch.object(os.path, 'exists', return_value=False)
+ @mock.patch.object(ssh.SSH, 'gen_keys')
+ @mock.patch.object(heat, 'HeatTemplate')
+ def test_deploy(self, mock_template, mock_genkeys, mock_path_exists):
self.test_context._name = 'foo'
self.test_context._task_id = '1234567890'
self.test_context._name_task_id = '{}-{}'.format(
self.test_context.get_neutron_info = mock.MagicMock()
self.test_context.deploy()
- mock_template.assert_called_with('foo-12345678',
- '/bar/baz/some-heat-file',
- {'image': 'cirros'})
+ mock_template.assert_called_with(
+ 'foo-12345678', template_file='/bar/baz/some-heat-file',
+ heat_parameters={'image': 'cirros'},
+ os_cloud_config=self.test_context._flags.os_cloud_config)
self.assertIsNotNone(self.test_context.stack)
+ key_filename = ''.join(
+ [consts.YARDSTICK_ROOT_PATH,
+ 'yardstick/resources/files/yardstick_key-',
+ self.test_context._name_task_id])
+ mock_genkeys.assert_called_once_with(key_filename)
+ mock_path_exists.assert_any_call(key_filename)
- # TODO: patch objects
@mock.patch.object(heat, 'HeatTemplate')
+ @mock.patch.object(os.path, 'exists', return_value=False)
+ @mock.patch.object(ssh.SSH, 'gen_keys')
@mock.patch.object(heat.HeatContext, '_retrieve_existing_stack')
@mock.patch.object(heat.HeatContext, '_create_new_stack')
- def test_deploy_no_setup(self, mock_create_new_stack, mock_retrieve_existing_stack, *args):
+ def test_deploy_no_setup(self, mock_create_new_stack,
+ mock_retrieve_existing_stack, mock_genkeys, mock_path_exists,
+ *args):
self.test_context._name = 'foo'
self.test_context._task_id = '1234567890'
- # Might be able to get rid of these
self.test_context.template_file = '/bar/baz/some-heat-file'
self.test_context.heat_parameters = {'image': 'cirros'}
self.test_context.get_neutron_info = mock.MagicMock()
self.test_context._flags.no_setup = True
self.test_context.deploy()
- # check that heat client is called...
mock_create_new_stack.assert_not_called()
mock_retrieve_existing_stack.assert_called_with(self.test_context.name)
self.assertIsNotNone(self.test_context.stack)
+ key_filename = ''.join(
+ [consts.YARDSTICK_ROOT_PATH,
+ 'yardstick/resources/files/yardstick_key-',
+ self.test_context._name])
+ mock_genkeys.assert_called_once_with(key_filename)
+ mock_path_exists.assert_any_call(key_filename)
- @mock.patch.object(shade, 'openstack_cloud')
- @mock.patch.object(heat.HeatTemplate, 'add_keypair')
+ @mock.patch.object(heat, 'HeatTemplate')
+ @mock.patch.object(os.path, 'exists', return_value=False)
+ @mock.patch.object(ssh.SSH, 'gen_keys')
@mock.patch.object(heat.HeatContext, '_create_new_stack')
- @mock.patch.object(heat.HeatStack, 'get')
+ @mock.patch.object(heat.HeatContext, '_retrieve_existing_stack',
+ return_value=None)
def test_deploy_try_retrieve_context_does_not_exist(self,
- mock_get_stack,
- mock_create_new_stack,
- *args):
+ mock_retrieve_stack, mock_create_new_stack, mock_genkeys,
+ mock_path_exists, *args):
self.test_context._name = 'demo'
self.test_context._task_id = '1234567890'
self.test_context._flags.no_setup = True
+ self.test_context.template_file = '/bar/baz/some-heat-file'
self.test_context.get_neutron_info = mock.MagicMock()
-
- # TODo: Check is this the right value to return, should it be None instead?
- mock_get_stack.return_value = []
-
self.test_context.deploy()
- mock_get_stack.assert_called()
+ mock_retrieve_stack.assert_called_once_with(self.test_context._name)
mock_create_new_stack.assert_called()
+ key_filename = ''.join(
+ [consts.YARDSTICK_ROOT_PATH,
+ 'yardstick/resources/files/yardstick_key-',
+ self.test_context._name])
+ mock_genkeys.assert_called_once_with(key_filename)
+ mock_path_exists.assert_any_call(key_filename)
+
+ @mock.patch.object(heat, 'HeatTemplate', return_value='heat_template')
+ @mock.patch.object(heat.HeatContext, '_add_resources_to_template')
+ @mock.patch.object(os.path, 'exists', return_value=False)
+ @mock.patch.object(ssh.SSH, 'gen_keys')
+ def test_deploy_ssh_key_before_adding_resources(self, mock_genkeys,
+ mock_path_exists, mock_add_resources, *args):
+ mock_manager = mock.Mock()
+ mock_manager.attach_mock(mock_add_resources,
+ '_add_resources_to_template')
+ mock_manager.attach_mock(mock_genkeys, 'gen_keys')
+ mock_manager.reset_mock()
+ self.test_context._name_task_id = 'demo-12345678'
+ self.test_context.get_neutron_info = mock.Mock()
+ with mock.patch.object(self.test_context, '_create_new_stack') as \
+ mock_create_stack, \
+ mock.patch.object(self.test_context, 'get_neutron_info') as \
+ mock_neutron_info:
+ self.test_context.deploy()
+
+ mock_neutron_info.assert_called_once()
+ mock_create_stack.assert_called_once()
+ key_filename = ''.join(
+ [consts.YARDSTICK_ROOT_PATH,
+ 'yardstick/resources/files/yardstick_key-',
+ self.test_context._name_task_id])
+ mock_genkeys.assert_called_once_with(key_filename)
+ mock_path_exists.assert_any_call(key_filename)
+
+ mock_call_gen_keys = mock.call.gen_keys(key_filename)
+ mock_call_add_resources = (
+ mock.call._add_resources_to_template('heat_template'))
+ self.assertTrue(mock_manager.mock_calls.index(mock_call_gen_keys) <
+ mock_manager.mock_calls.index(mock_call_add_resources))
def test_check_for_context(self):
pass
self.test_context.key_filename = 'foo/bar/foobar'
self.test_context.undeploy()
mock_delete_key.assert_called()
- self.assertTrue(mock_template.delete.called)
+ mock_template.delete.assert_called_once()
@mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
def test_undeploy_no_teardown(self, mock_template):
}
result = self.test_context._get_network(attr_name)
self.assertDictEqual(result, expected)
+
+ def _get_file_abspath(self, filename):
+ curr_path = os.path.dirname(os.path.abspath(__file__))
+ file_path = os.path.join(curr_path, filename)
+ return file_path
+
+ def test__get_physical_nodes(self):
+ self.test_context.nodes = {}
+ nodes = self.test_context._get_physical_nodes()
+ self.assertEquals(nodes, {})
+
+ @mock.patch('yardstick.common.utils.read_yaml_file')
+ def test__get_physical_node_for_server(self, mock_read_yaml):
+ attrs = {'name': 'foo',
+ 'task_id': '12345678',
+ 'file': "pod.yaml",
+ 'servers': {'vnf': {}},
+ 'networks': {'mgmt': {'cidr': '10.0.1.0/24'}}
+ }
+
+ with mock.patch.object(openstack_utils, 'get_shade_client'), \
+ mock.patch.object(openstack_utils, 'get_shade_operator_client'):
+ mock_read_yaml.return_value = self.HEAT_POD_SAMPLE
+ self.test_context.init(attrs)
+
+ with mock.patch('yardstick.common.openstack_utils.get_server') as mock_get_server:
+ mock_get_server.return_value = {'vnf': {}}
+
+ # When server is not from this context
+ result = self.test_context._get_physical_node_for_server('node1.foo-context')
+ self.assertIsNone(result)
+
+ # When node_name is not from this context
+ result = self.test_context._get_physical_node_for_server('fake.foo-12345678')
+ self.assertIsNone(result)
+
+ mock_munch = mock.Mock()
+ mock_munch.toDict = mock.Mock(return_value={
+ 'OS-EXT-SRV-ATTR:hypervisor_hostname': 'hypervisor_hostname'
+ })
+ mock_get_server.return_value = mock_munch
+
+ hypervisor = mock.Mock()
+ hypervisor.hypervisor_hostname = 'hypervisor_hostname'
+ hypervisor.host_ip = '10.229.47.137'
+
+ self.test_context.operator_client.list_hypervisors = mock.Mock(
+ return_value=[hypervisor])
+
+ mock_get_server.return_value = mock_munch
+
+ result = self.test_context._get_physical_node_for_server('vnf.foo-12345678')
+ self.assertEqual(result, 'node1.foo')