Add support for fixture_id to stack fixtures

Change-Id: I286d1d6e9cb9d112360cf341836098989bde7e61
This commit is contained in:
Federico Ressi 2020-07-02 12:30:58 +02:00
parent 65ad99bec5
commit c0246318c9
4 changed files with 111 additions and 7 deletions

View File

@ -64,17 +64,16 @@ class HeatStackFixture(tobiko.SharedFixture):
client = None client = None
retry_create_stack = 1 retry_create_stack = 1
wait_interval = 5 wait_interval = 5
stack_name = None # type: str
template = None # type: _template.HeatTemplateFixture template = None # type: _template.HeatTemplateFixture
stack = None stack = None
_stack_name = None
parameters = None # type: HeatStackParametersFixture parameters = None # type: HeatStackParametersFixture
def __init__(self, stack_name=None, template=None, parameters=None, def __init__(self, stack_name=None, template=None, parameters=None,
wait_interval=None, client=None): wait_interval=None, client=None):
super(HeatStackFixture, self).__init__() super(HeatStackFixture, self).__init__()
self.stack_name = stack_name = (stack_name or if stack_name:
self.stack_name or self._stack_name = str(stack_name)
self.fixture_name)
self.template = _template.heat_template(template or self.template) self.template = _template.heat_template(template or self.template)
self.parameters = heat_stack_parameters( self.parameters = heat_stack_parameters(
@ -86,6 +85,15 @@ class HeatStackFixture(tobiko.SharedFixture):
if wait_interval: if wait_interval:
self.wait_interval = wait_interval self.wait_interval = wait_interval
@property
def stack_name(self):
"""Lazily assign stack name
"""
stack_name = self._stack_name
if not stack_name:
self._stack_name = stack_name = self.fixture_name
return stack_name
def _get_retry_value(self, retry): def _get_retry_value(self, retry):
if retry is None: if retry is None:
retry = self.retry_create_stack retry = self.retry_create_stack

View File

@ -0,0 +1,26 @@
heat_template_version: 2015-04-30
description: Simple template to test creating a stack with tobiko
parameters:
input_text:
type: string
label: some text
description: some input text
resources:
text:
type: OS::Heat::Value
description: some resource text
properties:
type: string
value: { get_param: input_text }
outputs:
output_text:
description: some output text
value: { get_attr: [text, value] }

View File

@ -0,0 +1,67 @@
# Copyright (c) 2019 Red Hat, 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.
from __future__ import absolute_import
import os
import random
import string
import testtools
import tobiko
from tobiko.openstack import heat
TEMPLATE_DIRS = [os.path.dirname(__file__)]
def random_string(length, letters=string.ascii_lowercase):
return ''.join(random.choice(letters) for _ in range(length))
class MyStack(heat.HeatStackFixture):
template = heat.heat_template_file(template_file='hot/my_stack.yaml',
template_dirs=TEMPLATE_DIRS)
input_text = random_string(8)
class HeatStackFixtureTest(testtools.TestCase):
stack = tobiko.required_setup_fixture(MyStack)
def test_get_stack(self):
stack = self.stack.get_stack()
self.assertIsNotNone(stack)
self.assertTrue(stack.id)
self.assertEqual(tobiko.get_fixture_name(MyStack), stack.stack_name)
self.assertEqual('CREATE_COMPLETE', stack.stack_status)
def test_get_fixture_with_fixture_id_0(self):
fixture_0 = tobiko.get_fixture(MyStack, fixture_id=0)
self.assertIs(fixture_0, self.stack)
def test_get_fixture_with_fixture_id_1(self):
fixture_0 = tobiko.get_fixture(MyStack)
fixture_1 = tobiko.get_fixture(MyStack, fixture_id=1)
self.assertIsNot(fixture_0, fixture_1)
stack_0 = tobiko.setup_fixture(fixture_0).get_stack()
stack_1 = tobiko.setup_fixture(fixture_1).get_stack()
self.assertNotEqual(stack_0.id, stack_1.id)
self.assertEqual(tobiko.get_fixture_name(MyStack), stack_0.stack_name)
self.assertEqual(tobiko.get_fixture_name(MyStack) + '-1',
stack_1.stack_name)

View File

@ -65,9 +65,12 @@ class HeatStackFixtureTest(openstack.OpenstackTest):
parameters=parameters, parameters=parameters,
wait_interval=wait_interval, client=client) wait_interval=wait_interval, client=client)
self.assertEqual(stack_name or fixture_class.stack_name or if stack_name:
tobiko.get_fixture_name(stack), self.assertEqual(stack_name, stack.stack_name)
stack.stack_name) elif isinstance(fixture_class.stack_name, property):
self.assertEqual(tobiko.get_fixture_name(stack), stack.stack_name)
else:
self.assertEqual(fixture_class.stack_name, stack.stack_name)
self.check_stack_template(stack=stack, template=template) self.check_stack_template(stack=stack, template=template)