summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--requirements.txt1
-rw-r--r--test-requirements.txt3
-rw-r--r--yardstick/common/exceptions.py6
-rw-r--r--yardstick/orchestrator/heat.py211
-rw-r--r--yardstick/tests/unit/orchestrator/test_heat.py572
5 files changed, 248 insertions, 545 deletions
diff --git a/requirements.txt b/requirements.txt
index 88c0e659a..aacafdf93 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -49,7 +49,6 @@ pyroute2==0.4.21 # dual license GPLv2+ and Apache v2; OSI Approved GNU G
pyrsistent==0.14.1 # LICENSE.mit; OSI Approved MIT License
python-cinderclient==3.1.0 # OSI Approved Apache Software License
python-glanceclient==2.8.0 # OSI Approved Apache Software License
-python-heatclient==1.11.1 # OSI Approved Apache Software License
python-keystoneclient==3.13.0 # OSI Approved Apache Software License
python-neutronclient==6.5.0 # OSI Approved Apache Software License
python-novaclient==9.1.1 # OSI Approved Apache Software License
diff --git a/test-requirements.txt b/test-requirements.txt
index f933df29a..ee9815c45 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -12,6 +12,9 @@ testrepository==0.0.20 # OSI Approved BSD License; OSI Approved Apache So
testtools==2.3.0 # OSI Approved MIT License
unittest2==1.1.0 # OSI Approved BSD License
+# NOTE(ralonsoh): to be removed, only for coverage support
+python-heatclient==1.8.1 # OSI Approved Apache Software License
+
# Yardstick F release <-> OpenStack Pike release
openstack_requirements==1.1.0 # OSI Approved Apache Software License
-e git+https://github.com/openstack/requirements.git@stable/pike#egg=os_requirements
diff --git a/yardstick/common/exceptions.py b/yardstick/common/exceptions.py
index 4780822a4..e38dd246c 100644
--- a/yardstick/common/exceptions.py
+++ b/yardstick/common/exceptions.py
@@ -57,3 +57,9 @@ class YardstickException(Exception):
class FunctionNotImplemented(YardstickException):
message = ('The function "%(function_name)s" is not implemented in '
'"%(class_name)" class.')
+
+
+class HeatTemplateError(YardstickException):
+ """Error in Heat during the stack deployment"""
+ message = ('Error in Heat during the creation of the OpenStack stack '
+ '"%(stack_name)"')
diff --git a/yardstick/orchestrator/heat.py b/yardstick/orchestrator/heat.py
index d58ae5618..1a8beaeb6 100644
--- a/yardstick/orchestrator/heat.py
+++ b/yardstick/orchestrator/heat.py
@@ -10,23 +10,21 @@
"""Heat template and stack management"""
from __future__ import absolute_import
-from __future__ import print_function
-from six.moves import range
-
import collections
import datetime
import getpass
import logging
-
+import pkg_resources
import socket
+import tempfile
import time
-import heatclient.client
-import pkg_resources
-
+from oslo_serialization import jsonutils
from oslo_utils import encodeutils
+import shade
import yardstick.common.openstack_utils as op_utils
+from yardstick.common import exceptions
from yardstick.common import template_format
log = logging.getLogger(__name__)
@@ -36,123 +34,82 @@ HEAT_KEY_UUID_LENGTH = 8
PROVIDER_SRIOV = "sriov"
+_DEPLOYED_STACKS = {}
+
def get_short_key_uuid(uuid):
return str(uuid)[:HEAT_KEY_UUID_LENGTH]
-class HeatObject(object):
- """base class for template and stack"""
-
- def __init__(self):
- self._heat_client = None
- self.uuid = None
-
- @property
- def heat_client(self):
- """returns a heat client instance"""
-
- if self._heat_client is None:
- sess = op_utils.get_session()
- heat_endpoint = op_utils.get_endpoint(service_type='orchestration')
- self._heat_client = heatclient.client.Client(
- op_utils.get_heat_api_version(),
- endpoint=heat_endpoint, session=sess)
-
- return self._heat_client
-
- def status(self):
- """returns stack state as a string"""
- heat_client = self.heat_client
- stack = heat_client.stacks.get(self.uuid)
- return stack.stack_status
-
-
-class HeatStack(HeatObject):
+class HeatStack(object):
"""Represents a Heat stack (deployed template) """
- stacks = []
def __init__(self, name):
- super(HeatStack, self).__init__()
- self.uuid = None
self.name = name
- self.outputs = None
- HeatStack.stacks.append(self)
+ self.outputs = {}
+ self._cloud = shade.openstack_cloud()
+ self._stack = None
+
+ def create(self, template, heat_parameters, wait, timeout):
+ """Creates an OpenStack stack from a template"""
+ with tempfile.NamedTemporaryFile('wb', delete=False) as template_file:
+ template_file.write(jsonutils.dumps(template))
+ template_file.close()
+ self._stack = self._cloud.create_stack(
+ self.name, template_file=template_file.name, wait=wait,
+ timeout=timeout, **heat_parameters)
+ outputs = self._stack.outputs
+ self.outputs = {output['output_key']: output['output_value'] for output
+ in outputs}
+ if self.uuid:
+ _DEPLOYED_STACKS[self.uuid] = self._stack
@staticmethod
def stacks_exist():
- """check if any stack has been deployed"""
- return len(HeatStack.stacks) > 0
+ """Check if any stack has been deployed"""
+ return len(_DEPLOYED_STACKS) > 0
- def _delete(self):
- """deletes a stack from the target cloud using heat"""
+ def delete(self, wait=True):
+ """Deletes a stack in the target cloud"""
if self.uuid is None:
return
- log.info("Deleting stack '%s' START, uuid:%s", self.name, self.uuid)
- heat = self.heat_client
- template = heat.stacks.get(self.uuid)
- start_time = time.time()
- template.delete()
-
- for status in iter(self.status, u'DELETE_COMPLETE'):
- log.debug("Deleting stack state: %s", status)
- if status == u'DELETE_FAILED':
- raise RuntimeError(
- heat.stacks.get(self.uuid).stack_status_reason)
-
- time.sleep(2)
-
- end_time = time.time()
- log.info("Deleting stack '%s' DONE in %d secs", self.name,
- end_time - start_time)
- self.uuid = None
-
- def delete(self, block=True, retries=3):
- """deletes a stack in the target cloud using heat (with retry)
- Sometimes delete fail with "InternalServerError" and the next attempt
- succeeds. So it is worthwhile to test a couple of times.
- """
- if self.uuid is None:
- return
-
- if not block:
- self._delete()
- return
-
- for _ in range(retries):
- try:
- self._delete()
- break
- except RuntimeError as err:
- log.warning(err.args)
- time.sleep(2)
-
- # if still not deleted try once more and let it fail everything
- if self.uuid is not None:
- self._delete()
-
- HeatStack.stacks.remove(self)
+ ret = self._cloud.delete_stack(self.uuid, wait=wait)
+ _DEPLOYED_STACKS.pop(self.uuid)
+ self._stack = None
+ return ret
@staticmethod
def delete_all():
- for stack in HeatStack.stacks[:]:
+ """Delete all deployed stacks"""
+ for stack in _DEPLOYED_STACKS:
stack.delete()
- def update(self):
- """update a stack"""
- raise RuntimeError("not implemented")
+ @property
+ def status(self):
+ """Retrieve the current stack status"""
+ if self._stack:
+ return self._stack.status
+
+ @property
+ def uuid(self):
+ """Retrieve the current stack ID"""
+ if self._stack:
+ return self._stack.id
-class HeatTemplate(HeatObject):
+class HeatTemplate(object):
"""Describes a Heat template and a method to deploy template to a stack"""
- DESCRIPTION_TEMPLATE = """\
+ DESCRIPTION_TEMPLATE = """
Stack built by the yardstick framework for %s on host %s %s.
All referred generated resources are prefixed with the template
-name (i.e. %s).\
+name (i.e. %s).
"""
+ HEAT_WAIT_LOOP_INTERVAL = 2
+ HEAT_STATUS_COMPLETE = 'COMPLETE'
+
def _init_template(self):
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self._template = {
@@ -171,9 +128,7 @@ name (i.e. %s).\
self.resources = self._template['resources']
def __init__(self, name, template_file=None, heat_parameters=None):
- super(HeatTemplate, self).__init__()
self.name = name
- self.state = "NOT_CREATED"
self.keystone_client = None
self.heat_parameters = {}
@@ -184,16 +139,13 @@ name (i.e. %s).\
if template_file:
with open(template_file) as stream:
- print("Parsing external template:", template_file)
+ log.info('Parsing external template: %s', template_file)
template_str = stream.read()
self._template = template_format.parse(template_str)
self._parameters = heat_parameters
else:
self._init_template()
- # holds results of requested output after deployment
- self.outputs = {}
-
log.debug("template object '%s' created", name)
def add_flavor(self, name, vcpus=1, ram=1024, disk=1, ephemeral=0,
@@ -202,9 +154,9 @@ name (i.e. %s).\
"""add to the template a Flavor description"""
if name is None:
name = 'auto'
- log.debug("adding Nova::Flavor '%s' vcpus '%d' ram '%d' disk '%d' " +
- "ephemeral '%d' is_public '%s' rxtx_factor '%d' " +
- "swap '%d' extra_specs '%s' ",
+ log.debug("adding Nova::Flavor '%s' vcpus '%d' ram '%d' disk '%d' "
+ "ephemeral '%d' is_public '%s' rxtx_factor '%d' "
+ "swap '%d' extra_specs '%s'",
name, vcpus, ram, disk, ephemeral, is_public,
rxtx_factor, swap, str(extra_specs))
@@ -600,57 +552,28 @@ name (i.e. %s).\
'value': {'get_resource': name}
}
- HEAT_WAIT_LOOP_INTERVAL = 2
- HEAT_CREATE_COMPLETE_STATUS = u'CREATE_COMPLETE'
-
def create(self, block=True, timeout=3600):
- """
- creates a template in the target cloud using heat
- returns a dict with the requested output values from the template
+ """Creates a stack in the target based on the stored template
- :param block: Wait for Heat create to finish
- :type block: bool
- :param: timeout: timeout in seconds for Heat create, default 3600s
- :type timeout: int
+ :param block: (bool) Wait for Heat create to finish
+ :param timeout: (int) Timeout in seconds for Heat create,
+ default 3600s
+ :return A dict with the requested output values from the template
"""
log.info("Creating stack '%s' START", self.name)
- # create stack early to support cleanup, e.g. ctrl-c while waiting
- stack = HeatStack(self.name)
-
- heat_client = self.heat_client
start_time = time.time()
- stack.uuid = self.uuid = heat_client.stacks.create(
- stack_name=self.name, template=self._template,
- parameters=self.heat_parameters)['stack']['id']
+ stack = HeatStack(self.name)
+ stack.create(self._template, self.heat_parameters, block, timeout)
if not block:
- self.outputs = stack.outputs = {}
- end_time = time.time()
log.info("Creating stack '%s' DONE in %d secs",
- self.name, end_time - start_time)
+ self.name, time.time() - start_time)
return stack
- time_limit = start_time + timeout
- for status in iter(self.status, self.HEAT_CREATE_COMPLETE_STATUS):
- log.debug("Creating stack state: %s", status)
- if status == u'CREATE_FAILED':
- stack_status_reason = heat_client.stacks.get(self.uuid).stack_status_reason
- heat_client.stacks.delete(self.uuid)
- raise RuntimeError(stack_status_reason)
- if time.time() > time_limit:
- raise RuntimeError("Heat stack create timeout")
-
- time.sleep(self.HEAT_WAIT_LOOP_INTERVAL)
+ if stack.status != self.HEAT_STATUS_COMPLETE:
+ raise exceptions.HeatTemplateError(stack_name=self.name)
- end_time = time.time()
- outputs = heat_client.stacks.get(self.uuid).outputs
log.info("Creating stack '%s' DONE in %d secs",
- self.name, end_time - start_time)
-
- # keep outputs as unicode
- self.outputs = {output["output_key"]: output["output_value"] for output
- in outputs}
-
- stack.outputs = self.outputs
+ self.name, time.time() - start_time)
return stack
diff --git a/yardstick/tests/unit/orchestrator/test_heat.py b/yardstick/tests/unit/orchestrator/test_heat.py
index faf70cdbc..9164197b8 100644
--- a/yardstick/tests/unit/orchestrator/test_heat.py
+++ b/yardstick/tests/unit/orchestrator/test_heat.py
@@ -1,5 +1,3 @@
-#!/usr/bin/env python
-
##############################################################################
# Copyright (c) 2017 Intel Corporation
#
@@ -9,62 +7,87 @@
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
-# Unittest for yardstick.benchmark.orchestrator.heat
-from contextlib import contextmanager
-from itertools import count
-from tempfile import NamedTemporaryFile
-import time
-import uuid
+import tempfile
import mock
+from oslo_serialization import jsonutils
+from oslo_utils import uuidutils
+import shade
import unittest
from yardstick.benchmark.contexts import node
+from yardstick.common import exceptions
from yardstick.orchestrator import heat
-TARGET_MODULE = 'yardstick.orchestrator.heat'
-
-
-def mock_patch_target_module(inner_import):
- return mock.patch('.'.join([TARGET_MODULE, inner_import]))
-
-
-@contextmanager
-def timer():
- start = time.time()
- data = {'start': start}
- try:
- yield data
- finally:
- data['end'] = end = time.time()
- data['delta'] = end - start
-
-
-def index_value_iter(index, index_value, base_value=None):
- for current_index in count():
- if current_index == index:
- yield index_value
- else:
- yield base_value
-
+class FakeStack(object):
-def get_error_message(error):
- try:
- # py2
- return error.message
- except AttributeError:
- # py3
- return next((arg for arg in error.args if isinstance(arg, str)), None)
+ def __init__(self, outputs=None, status=None, id=None):
+ self.outputs = outputs
+ self.status = status
+ self.id = id
-class HeatContextTestCase(unittest.TestCase):
+class HeatStackTestCase(unittest.TestCase):
- def test_get_short_key_uuid(self):
- u = uuid.uuid4()
- k = heat.get_short_key_uuid(u)
- self.assertEqual(heat.HEAT_KEY_UUID_LENGTH, len(k))
- self.assertIn(k, str(u))
+ def setUp(self):
+ self.stack_name = 'STACK NAME'
+ with mock.patch.object(shade, 'openstack_cloud'):
+ self.heatstack = heat.HeatStack(self.stack_name)
+ self._mock_stack_create = mock.patch.object(self.heatstack._cloud,
+ 'create_stack')
+ self.mock_stack_create = self._mock_stack_create.start()
+ self._mock_stack_delete = mock.patch.object(self.heatstack._cloud,
+ 'delete_stack')
+ self.mock_stack_delete = self._mock_stack_delete.start()
+
+ self.addCleanup(self._cleanup)
+
+ def _cleanup(self):
+ self._mock_stack_create.stop()
+ self._mock_stack_delete.stop()
+ heat._DEPLOYED_STACKS = {}
+
+ def test_create(self):
+ template = {'tkey': 'tval'}
+ heat_parameters = {'pkey': 'pval'}
+ outputs = [{'output_key': 'okey', 'output_value': 'oval'}]
+ id = uuidutils.generate_uuid()
+ self.mock_stack_create.return_value = FakeStack(
+ outputs=outputs, status=mock.Mock(), id=id)
+ mock_tfile = mock.Mock()
+ with mock.patch.object(tempfile._TemporaryFileWrapper, '__enter__',
+ return_value=mock_tfile):
+ self.heatstack.create(template, heat_parameters, True, 100)
+ mock_tfile.write.assert_called_once_with(jsonutils.dumps(template))
+ mock_tfile.close.assert_called_once()
+
+ self.mock_stack_create.assert_called_once_with(
+ self.stack_name, template_file=mock_tfile.name, wait=True,
+ timeout=100, pkey='pval')
+ self.assertEqual({'okey': 'oval'}, self.heatstack.outputs)
+ self.assertEqual(heat._DEPLOYED_STACKS[id], self.heatstack._stack)
+
+ def test_stacks_exist(self):
+ self.assertEqual(0, self.heatstack.stacks_exist())
+ heat._DEPLOYED_STACKS['id'] = 'stack'
+ self.assertEqual(1, self.heatstack.stacks_exist())
+
+ def test_delete_not_uuid(self):
+ self.assertIsNone(self.heatstack.delete())
+
+ def test_delete_existing_uuid(self):
+ id = uuidutils.generate_uuid()
+ self.heatstack._stack = FakeStack(
+ outputs=mock.Mock(), status=mock.Mock(), id=id)
+ heat._DEPLOYED_STACKS[id] = self.heatstack._stack
+ delete_return = mock.Mock()
+ self.mock_stack_delete.return_value = delete_return
+
+ ret = self.heatstack.delete(wait=True)
+ self.assertEqual(delete_return, ret)
+ self.assertFalse(heat._DEPLOYED_STACKS)
+ self.mock_stack_delete.assert_called_once_with(id, wait=True)
class HeatTemplateTestCase(unittest.TestCase):
@@ -75,63 +98,53 @@ class HeatTemplateTestCase(unittest.TestCase):
def test_add_tenant_network(self):
self.template.add_network('some-network')
- self.assertEqual(
- self.template.resources['some-network']['type'],
- 'OS::Neutron::Net')
+ self.assertEqual('OS::Neutron::Net',
+ self.template.resources['some-network']['type'])
def test_add_provider_network(self):
self.template.add_network('some-network', 'physnet2', 'sriov')
- self.assertEqual(
- self.template.resources['some-network']['type'],
- 'OS::Neutron::ProviderNet')
- self.assertEqual(
- self.template.resources['some-network']['properties']['physical_network'],
- 'physnet2')
+ self.assertEqual(self.template.resources['some-network']['type'],
+ 'OS::Neutron::ProviderNet')
+ self.assertEqual(self.template.resources['some-network'][
+ 'properties']['physical_network'], 'physnet2')
def test_add_subnet(self):
netattrs = {'cidr': '10.0.0.0/24',
- 'provider': None, 'external_network': 'ext_net'}
- self.template.add_subnet(
- 'some-subnet', "some-network", netattrs['cidr'])
+ 'provider': None,
+ 'external_network': 'ext_net'}
+ self.template.add_subnet('some-subnet', "some-network",
+ netattrs['cidr'])
- self.assertEqual(
- self.template.resources['some-subnet']['type'],
- 'OS::Neutron::Subnet')
- self.assertEqual(
- self.template.resources['some-subnet']['properties']['cidr'],
- '10.0.0.0/24')
+ self.assertEqual(self.template.resources['some-subnet']['type'],
+ 'OS::Neutron::Subnet')
+ self.assertEqual(self.template.resources['some-subnet']['properties'][
+ 'cidr'], '10.0.0.0/24')
def test_add_router(self):
self.template.add_router('some-router', 'ext-net', 'some-subnet')
- self.assertEqual(
- self.template.resources['some-router']['type'],
- 'OS::Neutron::Router')
- self.assertIn(
- 'some-subnet',
- self.template.resources['some-router']['depends_on'])
+ self.assertEqual(self.template.resources['some-router']['type'],
+ 'OS::Neutron::Router')
+ self.assertIn('some-subnet',
+ self.template.resources['some-router']['depends_on'])
def test_add_router_interface(self):
- self.template.add_router_interface(
- 'some-router-if', 'some-router', 'some-subnet')
+ self.template.add_router_interface('some-router-if', 'some-router',
+ 'some-subnet')
- self.assertEqual(
- self.template.resources['some-router-if']['type'],
- 'OS::Neutron::RouterInterface')
- self.assertIn(
- 'some-subnet',
- self.template.resources['some-router-if']['depends_on'])
+ self.assertEqual(self.template.resources['some-router-if']['type'],
+ 'OS::Neutron::RouterInterface')
+ self.assertIn('some-subnet',
+ self.template.resources['some-router-if']['depends_on'])
def test_add_servergroup(self):
self.template.add_servergroup('some-server-group', 'anti-affinity')
- self.assertEqual(
- self.template.resources['some-server-group']['type'],
- 'OS::Nova::ServerGroup')
- self.assertEqual(
- self.template.resources['some-server-group']['properties']['policies'],
- ['anti-affinity'])
+ self.assertEqual(self.template.resources['some-server-group']['type'],
+ 'OS::Nova::ServerGroup')
+ self.assertEqual(self.template.resources['some-server-group'][
+ 'properties']['policies'], ['anti-affinity'])
def test__add_resources_to_template_raw(self):
test_context = node.NodeContext()
@@ -142,16 +155,13 @@ class HeatTemplateTestCase(unittest.TestCase):
test_context.keypair_name = "foo-key"
test_context.secgroup_name = "foo-secgroup"
test_context.key_uuid = "2f2e4997-0a8e-4eb7-9fa4-f3f8fbbc393b"
- heat_object = heat.HeatObject()
- heat_stack = heat.HeatStack("tmpStack")
- self.assertTrue(heat_stack.stacks_exist())
-
- test_context.tmpfile = NamedTemporaryFile(delete=True, mode='w+t')
+ test_context.tmpfile = tempfile.NamedTemporaryFile(
+ delete=True, mode='w+t')
test_context.tmpfile.write("heat_template_version: 2015-04-30")
test_context.tmpfile.flush()
test_context.tmpfile.seek(0)
- heat_template = heat.HeatTemplate(heat_object)
+ heat_template = heat.HeatTemplate('template name')
heat_template.resources = {}
heat_template.add_network("network1")
@@ -163,324 +173,86 @@ class HeatTemplateTestCase(unittest.TestCase):
heat_template.add_router("router1", "gw1", "subnet1")
heat_template.add_router_interface("router_if1", "router1", "subnet1")
heat_template.add_port("port1", "network1", "subnet1", "normal")
- heat_template.add_port(
- "port2",
- "network2",
- "subnet2",
- "normal",
- sec_group_id="sec_group1",
- provider="not-sriov")
- heat_template.add_port(
- "port3",
- "network2",
- "subnet2",
- "normal",
- sec_group_id="sec_group1",
- provider="sriov")
- heat_template.add_floating_ip(
- "floating_ip1", "network1", "port1", "router_if1")
- heat_template.add_floating_ip(
- "floating_ip2", "network2", "port2", "router_if2", "foo-secgroup")
- heat_template.add_floating_ip_association(
- "floating_ip1_association", "floating_ip1", "port1")
+ heat_template.add_port("port2", "network2", "subnet2", "normal",
+ sec_group_id="sec_group1", provider="not-sriov")
+ heat_template.add_port("port3", "network2", "subnet2", "normal",
+ sec_group_id="sec_group1", provider="sriov")
+ heat_template.add_floating_ip("floating_ip1", "network1", "port1",
+ "router_if1")
+ heat_template.add_floating_ip("floating_ip2", "network2", "port2",
+ "router_if2", "foo-secgroup")
+ heat_template.add_floating_ip_association("floating_ip1_association",
+ "floating_ip1", "port1")
heat_template.add_servergroup("server_grp2", "affinity")
heat_template.add_servergroup("server_grp3", "anti-affinity")
heat_template.add_security_group("security_group")
+ heat_template.add_server(name="server1", image="image1",
+ flavor="flavor1", flavors=[])
+ heat_template.add_server_group(name="servergroup",
+ policies=["policy1", "policy2"])
+ heat_template.add_server_group(name="servergroup",
+ policies="policy1")
heat_template.add_server(
- name="server1", image="image1", flavor="flavor1", flavors=[])
- heat_template.add_server_group(
- name="servergroup", policies=["policy1", "policy2"])
- heat_template.add_server_group(name="servergroup", policies="policy1")
- heat_template.add_server(
- name="server2",
- image="image1",
- flavor="flavor1",
- flavors=[],
- ports=[
- "port1",
- "port2"],
- networks=[
- "network1",
- "network2"],
- scheduler_hints="hints1",
- user="user1",
- key_name="foo-key",
- user_data="user",
- metadata={
- "cat": 1,
- "doc": 2},
- additional_properties={
- "prop1": 1,
- "prop2": 2})
+ name="server2", image="image1", flavor="flavor1", flavors=[],
+ ports=["port1", "port2"], networks=["network1", "network2"],
+ scheduler_hints="hints1", user="user1", key_name="foo-key",
+ user_data="user", metadata={"cat": 1, "doc": 2},
+ additional_properties={"prop1": 1, "prop2": 2})
heat_template.add_server(
- name="server2",
- image="image1",
- flavor="flavor1",
- flavors=[
- "flavor1",
- "flavor2"],
- ports=[
- "port1",
- "port2"],
- networks=[
- "network1",
- "network2"],
- scheduler_hints="hints1",
- user="user1",
- key_name="foo-key",
- user_data="user",
- metadata={
- "cat": 1,
- "doc": 2},
- additional_properties={
- "prop1": 1,
- "prop2": 2})
+ name="server2", image="image1", flavor="flavor1",
+ flavors=["flavor1", "flavor2"], ports=["port1", "port2"],
+ networks=["network1", "network2"], scheduler_hints="hints1",
+ user="user1", key_name="foo-key", user_data="user",
+ metadata={"cat": 1, "doc": 2},
+ additional_properties={"prop1": 1, "prop2": 2})
heat_template.add_server(
- name="server2",
- image="image1",
- flavor="flavor1",
- flavors=[
- "flavor3",
- "flavor4"],
- ports=[
- "port1",
- "port2"],
- networks=[
- "network1",
- "network2"],
- scheduler_hints="hints1",
- user="user1",
- key_name="foo-key",
- user_data="user",
- metadata={
- "cat": 1,
- "doc": 2},
- additional_properties={
- "prop1": 1,
- "prop2": 2})
- heat_template.add_flavor(
- name="flavor1",
- vcpus=1,
- ram=2048,
- disk=1,
- extra_specs={
- "cat": 1,
- "dog": 2})
+ name="server2", image="image1", flavor="flavor1",
+ flavors=["flavor3", "flavor4"], ports=["port1", "port2"],
+ networks=["network1", "network2"], scheduler_hints="hints1",
+ user="user1", key_name="foo-key", user_data="user",
+ metadata={"cat": 1, "doc": 2},
+ additional_properties={"prop1": 1, "prop2": 2})
+ heat_template.add_flavor(name="flavor1", vcpus=1, ram=2048, disk=1,
+ extra_specs={"cat": 1, "dog": 2})
heat_template.add_flavor(name=None, vcpus=1, ram=2048)
heat_template.add_server(
- name="server1",
- image="image1",
- flavor="flavor1",
- flavors=[],
- ports=[
- "port1",
- "port2"],
- networks=[
- "network1",
- "network2"],
- scheduler_hints="hints1",
- user="user1",
- key_name="foo-key",
- user_data="user",
- metadata={
- "cat": 1,
- "doc": 2},
- additional_properties={
- "prop1": 1,
- "prop2": 2})
+ name="server1", image="image1", flavor="flavor1", flavors=[],
+ ports=["port1", "port2"], networks=["network1", "network2"],
+ scheduler_hints="hints1", user="user1", key_name="foo-key",
+ user_data="user", metadata={"cat": 1, "doc": 2},
+ additional_properties={"prop1": 1, "prop2": 2})
heat_template.add_network("network1")
heat_template.add_flavor("test")
- self.assertEqual(
- heat_template.resources['test']['type'], 'OS::Nova::Flavor')
-
- @mock_patch_target_module('op_utils')
- @mock_patch_target_module('heatclient')
- def test_create_negative(self, mock_heat_client_class, mock_op_utils):
- self.template.HEAT_WAIT_LOOP_INTERVAL = 0
- mock_heat_client = mock_heat_client_class() # get the constructed mock
-
- # populate attributes of the constructed mock
- mock_heat_client.stacks.get().stack_status_reason = 'the reason'
-
- expected_status_calls = 0
- expected_constructor_calls = 1 # above, to get the instance
- expected_create_calls = 0
- expected_op_utils_usage = 0
-
- with mock.patch.object(self.template, 'status', return_value=None) as mock_status:
- # block with timeout hit
- timeout = 0
- with self.assertRaises(RuntimeError) as raised, timer():
- self.template.create(block=True, timeout=timeout)
-
- # ensure op_utils was used
- expected_op_utils_usage += 1
- self.assertEqual(
- mock_op_utils.get_session.call_count, expected_op_utils_usage)
- self.assertEqual(
- mock_op_utils.get_endpoint.call_count, expected_op_utils_usage)
- self.assertEqual(
- mock_op_utils.get_heat_api_version.call_count,
- expected_op_utils_usage)
-
- # ensure the constructor and instance were used
- self.assertEqual(mock_heat_client_class.call_count,
- expected_constructor_calls)
- self.assertEqual(
- mock_heat_client.stacks.create.call_count,
- expected_create_calls)
-
- # ensure that the status was used
- self.assertGreater(mock_status.call_count, expected_status_calls)
- expected_status_calls = mock_status.call_count # synchronize the value
-
- # ensure the expected exception was raised
- error_message = get_error_message(raised.exception)
- self.assertIn('timeout', error_message)
- self.assertNotIn('the reason', error_message)
-
- # block with create failed
- timeout = 10
- mock_status.side_effect = iter([None, None, u'CREATE_FAILED'])
- with self.assertRaises(RuntimeError) as raised, timer():
- self.template.create(block=True, timeout=timeout)
-
- # ensure the existing heat_client was used and op_utils was used
- # again
- self.assertEqual(
- mock_op_utils.get_session.call_count, expected_op_utils_usage)
- self.assertEqual(
- mock_op_utils.get_endpoint.call_count, expected_op_utils_usage)
- self.assertEqual(
- mock_op_utils.get_heat_api_version.call_count,
- expected_op_utils_usage)
-
- # ensure the constructor was not used but the instance was used
- self.assertEqual(mock_heat_client_class.call_count,
- expected_constructor_calls)
- self.assertEqual(
- mock_heat_client.stacks.create.call_count,
- expected_create_calls)
-
- # ensure that the status was used three times
- expected_status_calls += 3
- self.assertEqual(mock_status.call_count, expected_status_calls)
-
- # NOTE(elfoley): This needs to be split into multiple tests.
- # The lines where the template is reset should serve as a guide for where
- # to split.
- @mock_patch_target_module('op_utils')
- @mock_patch_target_module('heatclient')
- def test_create(self, mock_heat_client_class, mock_op_utils):
- self.template.HEAT_WAIT_LOOP_INTERVAL = 0.2
- mock_heat_client = mock_heat_client_class()
-
- # populate attributes of the constructed mock
- mock_heat_client.stacks.get().outputs = [
- {'output_key': 'key1', 'output_value': 'value1'},
- {'output_key': 'key2', 'output_value': 'value2'},
- {'output_key': 'key3', 'output_value': 'value3'},
- ]
- expected_outputs = { # pylint: disable=unused-variable
- 'key1': 'value1',
- 'key2': 'value2',
- 'key3': 'value3',
- }
-
- expected_status_calls = 0
- expected_constructor_calls = 1 # above, to get the instance
- expected_create_calls = 0
- expected_op_utils_usage = 0
-
- with mock.patch.object(self.template, 'status') as mock_status:
- self.template.name = 'no block test'
- mock_status.return_value = None
-
- # no block
- self.assertIsInstance(self.template.create(
- block=False, timeout=2), heat.HeatStack)
-
- # ensure op_utils was used
- expected_op_utils_usage += 1
- self.assertEqual(
- mock_op_utils.get_session.call_count, expected_op_utils_usage)
- self.assertEqual(
- mock_op_utils.get_endpoint.call_count, expected_op_utils_usage)
- self.assertEqual(
- mock_op_utils.get_heat_api_version.call_count,
- expected_op_utils_usage)
-
- # ensure the constructor and instance were used
- self.assertEqual(mock_heat_client_class.call_count,
- expected_constructor_calls)
- self.assertEqual(
- mock_heat_client.stacks.create.call_count,
- expected_create_calls)
-
- # ensure that the status was not used
- self.assertEqual(mock_status.call_count, expected_status_calls)
-
- # ensure no outputs because this requires blocking
- self.assertEqual(self.template.outputs, {})
-
- # block with immediate complete
- self.template.name = 'block, immediate complete test'
-
- mock_status.return_value = self.template.HEAT_CREATE_COMPLETE_STATUS
- self.assertIsInstance(self.template.create(
- block=True, timeout=2), heat.HeatStack)
-
- # ensure existing instance was re-used and op_utils was not used
- self.assertEqual(mock_heat_client_class.call_count,
- expected_constructor_calls)
- self.assertEqual(
- mock_heat_client.stacks.create.call_count,
- expected_create_calls)
-
- # ensure status was checked once
- expected_status_calls += 1
- self.assertEqual(mock_status.call_count, expected_status_calls)
-
- # reset template outputs
- self.template.outputs = None
-
- # block with delayed complete
- self.template.name = 'block, delayed complete test'
-
- success_index = 2
- mock_status.side_effect = index_value_iter(
- success_index, self.template.HEAT_CREATE_COMPLETE_STATUS)
- self.assertIsInstance(self.template.create(
- block=True, timeout=2), heat.HeatStack)
-
- # ensure existing instance was re-used and op_utils was not used
- self.assertEqual(mock_heat_client_class.call_count,
- expected_constructor_calls)
- self.assertEqual(
- mock_heat_client.stacks.create.call_count,
- expected_create_calls)
-
- # ensure status was checked three more times
- expected_status_calls += 1 + success_index
- self.assertEqual(mock_status.call_count, expected_status_calls)
-
-
-class HeatStackTestCase(unittest.TestCase):
-
- def test_delete_calls__delete_multiple_times(self):
- stack = heat.HeatStack('test')
- stack.uuid = 1
- with mock.patch.object(stack, "_delete") as delete_mock:
- stack.delete()
- # call once and then call again if uuid is not none
- self.assertGreater(delete_mock.call_count, 1)
-
- def test_delete_all_calls_delete(self):
- # we must patch the object before we create an instance
- # so we can override delete() in all the instances
- with mock.patch.object(heat.HeatStack, "delete") as delete_mock:
- stack = heat.HeatStack('test')
- stack.uuid = 1
- stack.delete_all()
- self.assertGreater(delete_mock.call_count, 0)
+ self.assertEqual(heat_template.resources['test']['type'],
+ 'OS::Nova::Flavor')
+
+ def test_create_not_block(self):
+ heat_stack = mock.Mock()
+ with mock.patch.object(heat, 'HeatStack', return_value=heat_stack):
+ ret = self.template.create(block=False)
+ heat_stack.create.assert_called_once_with(
+ self.template._template, self.template.heat_parameters, False,
+ 3600)
+ self.assertEqual(heat_stack, ret)
+
+ def test_create_block(self):
+ heat_stack = mock.Mock()
+ heat_stack.status = self.template.HEAT_STATUS_COMPLETE
+ with mock.patch.object(heat, 'HeatStack', return_value=heat_stack):
+ ret = self.template.create(block=False)
+ heat_stack.create.assert_called_once_with(
+ self.template._template, self.template.heat_parameters, False,
+ 3600)
+ self.assertEqual(heat_stack, ret)
+
+
+ def test_create_block_status_no_complete(self):
+ heat_stack = mock.Mock()
+ heat_stack.status = 'other status'
+ with mock.patch.object(heat, 'HeatStack', return_value=heat_stack):
+ self.assertRaises(exceptions.HeatTemplateError,
+ self.template.create, block=True)
+ heat_stack.create.assert_called_once_with(
+ self.template._template, self.template.heat_parameters, True,
+ 3600)