Add wait flag to create command.

Change-Id: If7f37c20932bc1b8fb5f295b5ace91bd666fc73c
This commit is contained in:
Federico Ressi 2018-12-07 12:00:21 +01:00
parent e92c7dfa73
commit 912931ce54
6 changed files with 188 additions and 101 deletions

View File

@ -14,22 +14,16 @@
from __future__ import absolute_import
import argparse
import logging
import os
import sys
from oslo_log import log
from tobiko.cmd import base
from tobiko.common import constants
from tobiko.common import exceptions
try:
# Python 3
from urllib import error as url_error
except ImportError:
# Python 2
import urllib2 as url_error
LOG = logging.getLogger(__name__)
LOG = log.getLogger(__name__)
class CreateUtil(base.TobikoCMD):
@ -48,28 +42,25 @@ class CreateUtil(base.TobikoCMD):
parser.add_argument(
'--all', '-a', action='store_true', dest='all',
help="Create all the stacks defined in Tobiko.")
parser.add_argument(
'--wait', '-w', action='store_true', dest='wait',
help="Wait for stack to reach CREATE_COMPLETE status before "
"exiting.")
return parser
def create_stack(self, stack_name=None, all_stacks=False):
def create_stacks(self, stack_name=None, all_stacks=False, wait=False):
"""Creates a stack based on given arguments."""
if all_stacks or stack_name is None:
templates = self.stackManager.get_templates_names()
for template in templates:
stack_name = template.split(constants.TEMPLATE_SUFFIX)[0]
self.stackManager.create_stack(
stack_name, template, parameters=constants.DEFAULT_PARAMS)
LOG.info("Created stack: %s", stack_name)
else:
try:
self.stackManager.create_stack(
stack_name, ''.join([stack_name,
constants.TEMPLATE_SUFFIX]),
parameters=constants.DEFAULT_PARAMS)
LOG.info("Created stack: %s", stack_name)
except url_error.URLError:
stacks = self.stackManager.get_templates_names(
strip_suffix=True)
raise NoSuchTemplateError(templates="\n".join(stacks))
templates = [stack_name + constants.TEMPLATE_SUFFIX]
for template in templates:
stack_name = os.path.splitext(template)[0]
self.stackManager.create_stack(
stack_name=stack_name,
template_name=template,
parameters=constants.DEFAULT_PARAMS,
wait=wait)
class NoSuchTemplateError(exceptions.TobikoException):
@ -79,8 +70,9 @@ class NoSuchTemplateError(exceptions.TobikoException):
def main():
"""Create CLI main entry."""
create_cmd = CreateUtil()
create_cmd.create_stack(create_cmd.args.stack,
create_cmd.args.all)
create_cmd.create_stacks(stack_name=create_cmd.args.stack,
all_stacks=create_cmd.args.all,
wait=create_cmd.args.wait)
if __name__ == '__main__':

View File

@ -17,13 +17,22 @@ import os
import time
from heatclient.common import template_utils
from heatclient import exc as heat_exc
from heatclient import exc
from oslo_log import log
import yaml
from tobiko.common import constants
LOG = log.getLogger(__name__)
# Status
CREATE_IN_PROGRESS = 'CREATE_IN_PROGRESS'
CREATE_COMPLETE = 'CREATE_COMPLETE'
CREATE_FAILED = 'CREATE_FAILED'
DELETE_IN_PROGRESS = 'DELETE_IN_PROGRESS'
DELETE_COMPLETE = 'DELETE_COMPLETE'
class StackManager(object):
@ -39,42 +48,79 @@ class StackManager(object):
_, template = template_utils.get_template_contents(template_path)
return yaml.safe_dump(template)
def create_stack(self, stack_name, template_name, parameters,
status=CREATE_COMPLETE):
def create_stack(self, stack_name, template_name, parameters, wait=True):
"""Creates stack based on passed parameters."""
stack = self.wait_for_stack_status(
stack_name=stack_name, status={DELETE_COMPLETE,
CREATE_COMPLETE,
CREATE_FAILED})
if stack and stack.stack_status == CREATE_COMPLETE:
LOG.debug('Stack %r already exists.', stack_name)
return stack
if stack and stack.stack_status.endswith('_FAILED'):
self.delete_stack(stack_name, wait=True)
template = self.load_template(os.path.join(self.templates_dir,
template_name))
self.client.stacks.create(stack_name=stack_name, template=template,
parameters=parameters)
return self.wait_for_stack_status(stack_name, status)
try:
self.client.stacks.create(stack_name=stack_name,
template=template,
parameters=parameters)
except exc.HTTPConflict:
LOG.debug('Stack %r already exists.', stack_name)
else:
LOG.debug('Crating stack %r...', stack_name)
def delete_stack(self, sid):
if wait:
return self.wait_for_stack_status(stack_name=stack_name)
else:
return self.get_stack(stack_name=stack_name)
def delete_stack(self, stack_name, wait=False):
"""Deletes stack."""
self.client.stacks.delete(sid)
self.client.stacks.delete(stack_name)
if wait:
self.wait_for_stack_status(stack_name, status={DELETE_COMPLETE})
def get_stack(self, stack_name):
"""Returns stack ID."""
try:
return self.client.stacks.get(stack_name)
except heat_exc.HTTPNotFound:
return
except exc.HTTPNotFound:
return None
def wait_for_resource_status(self, stack_id, resource_name,
status="CREATE_COMPLETE"):
status=CREATE_COMPLETE):
"""Waits for resource to reach the given status."""
res = self.client.resources.get(stack_id, resource_name)
while (res.resource_status != status):
time.sleep(self.wait_interval)
res = self.client.resources.get(stack_id, resource_name)
def wait_for_stack_status(self, stack_name,
status=CREATE_COMPLETE):
def wait_for_stack_status(self, stack_name, status=None, stack=None,
check=True):
"""Waits for the stack to reach the given status."""
stack = self.get_stack(stack_name=stack_name)
while (stack.stack_status != status):
status = status or {CREATE_COMPLETE}
stack = stack or self.get_stack(stack_name=stack_name)
while (stack and stack.stack_status.endswith('_IN_PROGRESS') and
stack.stack_status not in status):
LOG.debug('Waiting for %r stack status (expected=%r, acual=%r)...',
stack_name, status, stack.stack_status)
time.sleep(self.wait_interval)
stack = self.get_stack(stack_name=stack_name)
if check:
if stack is None:
if DELETE_COMPLETE not in status:
msg = "Stack {!r} not found".format(stack_name)
raise RuntimeError(msg)
elif stack.stack_status not in status:
msg = ("Invalid stack {!r} status (expected={!r}, "
"actual={!r})").format(stack_name, status,
stack.stack_status)
raise RuntimeError(msg)
return stack
def get_output(self, stack, key):

View File

@ -17,13 +17,21 @@ from __future__ import absolute_import
import os.path
from tobiko.cmd import base
from tobiko.tests.base import TobikoTest
from tobiko.tests.unit import TobikoUnitTest
class TobikoCMDTest(TobikoTest):
class TobikoCMDTest(TobikoUnitTest):
def test_init(self):
cmd = base.TobikoCMD()
command_name = None
command_class = base.TobikoCMD
def test_init(self, argv=None):
self.patch_argv(argv=argv)
cmd = self.command_class()
self.assertIsNotNone(cmd.clientManager)
self.assertTrue(os.path.isdir(cmd.templates_dir))
self.assertIsNotNone(cmd.stackManager)
return cmd
def patch_argv(self, argv=None):
return self.patch('sys.argv', [self.command_name] + (argv or []))

View File

@ -14,76 +14,68 @@
# under the License.
from __future__ import absolute_import
import os.path
from heatclient import exc
import mock
from tobiko.cmd import create
from tobiko.common.managers import stack as stack_manager
from tobiko.common import constants
from tobiko.common.managers import stack
from tobiko.tests.base import TobikoTest
from tobiko.tests.cmd import test_base
class CreateUtilTest(TobikoTest):
class CreateTest(test_base.TobikoCMDTest):
@mock.patch('sys.argv', ['tobiko-create'])
def test_init(self):
cmd = create.CreateUtil()
self.assertIsNotNone(cmd.clientManager)
self.assertTrue(os.path.isdir(cmd.templates_dir))
self.assertIsNotNone(cmd.stackManager)
command_name = 'tobiko-create'
command_class = create.CreateUtil
def test_init(self, argv=None, all_stacks=False, stack=None, wait=False):
# pylint: disable=arguments-differ,no-member
cmd = super(CreateTest, self).test_init(argv=argv)
self.assertIsNotNone(cmd.parser)
self.assertFalse(cmd.args.all)
self.assertIsNone(cmd.args.stack)
self.assertIs(all_stacks, cmd.args.all)
self.assertEqual(stack, cmd.args.stack)
self.assertIs(wait, cmd.args.wait)
return cmd
@mock.patch('sys.argv', ['tobiko-create', '--all'])
def test_init_with_all(self):
cmd = create.CreateUtil()
self.assertIsNotNone(cmd.clientManager)
self.assertTrue(os.path.isdir(cmd.templates_dir))
self.assertIsNotNone(cmd.stackManager)
self.assertIsNotNone(cmd.parser)
self.assertTrue(cmd.args.all)
self.assertIsNone(cmd.args.stack)
self.test_init(argv=['--all'], all_stacks=True)
@mock.patch('sys.argv', ['tobiko-create', '--stack', 'my-stack'])
def test_init_with_stack(self):
cmd = create.CreateUtil()
self.assertIsNotNone(cmd.clientManager)
self.assertTrue(os.path.isdir(cmd.templates_dir))
self.assertIsNotNone(cmd.stackManager)
self.assertIsNotNone(cmd.parser)
self.assertFalse(cmd.args.all)
self.assertEqual('my-stack', cmd.args.stack)
self.test_init(argv=['--stack', 'my-stack'], stack='my-stack')
def test_init_with_wait(self):
self.test_init(argv=['--wait'], wait=True)
class TestMain(TobikoTest):
def test_init_with_w(self):
self.test_init(argv=['-w'], wait=True)
@mock.patch('sys.argv', ['tobiko-create', '--stack', 'test_floatingip'])
def test_main_with_stack(self):
# pylint: disable=no-value-for-parameter
self._test_main(stack_names=['test_floatingip'],
walk_dir=False)
def test_main(self, argv=None, stack_names=None, walk_dir=True,
wait=False):
@mock.patch('sys.argv', ['tobiko-create'])
def test_main(self):
# pylint: disable=no-value-for-parameter
self._test_main(stack_names=['test_floatingip', 'test_mtu'],
walk_dir=True)
if stack_names is None:
stack_names = ['test_mtu', 'test_floatingip']
@mock.patch('sys.argv', ['tobiko-create', '--all'])
def test_main_with_all(self):
# pylint: disable=no-value-for-parameter
self._test_main(stack_names=['test_mtu', 'test_security_groups'],
walk_dir=True)
self.patch_argv(argv=argv)
@mock.patch('heatclient.client.Client')
@mock.patch('os.walk')
def _test_main(self, mock_walk, MockClient, stack_names, walk_dir):
# Break wait for stack status loop
MockClient().stacks.get().stack_status = stack.CREATE_COMPLETE
mock_walk.return_value = [(None, None, [(name + '.yaml')
for name in stack_names])]
mock_sleep = self.patch('time.sleep')
mock_walk = self.patch('os.walk', return_value=[
(None, None, [(name + '.yaml') for name in stack_names])])
def mock_client_get():
for name in stack_names:
# This would cause to create stack
yield exc.HTTPNotFound
# This would cause to wait for CREATE_COMPLETE status
yield mock.Mock(stack_status=stack_manager.CREATE_IN_PROGRESS,
name=name)
if wait:
# Break wait for stack status loop
yield mock.Mock(stack_status=stack_manager.CREATE_COMPLETE,
name=name)
MockClient = self.patch('heatclient.client.Client')
MockClient().stacks.get.side_effect = mock_client_get()
create.main()
@ -98,3 +90,20 @@ class TestMain(TobikoTest):
mock_walk.assert_called_once_with(mock.ANY)
else:
mock_walk.assert_not_called()
if wait:
mock_sleep.assert_called()
def test_main_with_stack(self):
self.test_main(argv=['--stack', 'test_floatingip'],
stack_names=['test_floatingip'], walk_dir=False)
def test_main_with_all(self):
self.test_main(argv=['--all'],
stack_names=['test_mtu', 'test_security_groups'])
def test_main_with_wait(self):
self.test_main(argv=['--wait'], wait=True)
def test_main_with_w(self):
self.test_main(argv=['-w'], wait=True)

View File

@ -69,5 +69,4 @@ class ScenarioTestsBase(base.TobikoTest):
return self.stackManager.create_stack(
stack_name=self.stack_name,
template_name="%s.yaml" % self.stack_name,
parameters=self.params,
status=stack.CREATE_COMPLETE)
parameters=self.params)

33
tobiko/tests/unit.py Normal file
View File

@ -0,0 +1,33 @@
#
# 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 mock
from tobiko.tests import base
class TobikoUnitTest(base.TobikoTest):
def setUp(self):
super(TobikoUnitTest, self).setUp()
# Protect from mis-configuring logging
self.patch('oslo_log.log.setup')
def patch(self, target, *args, **kwargs):
# pylint: disable=arguments-differ
context = mock.patch(target, *args, **kwargs)
mock_object = context.start()
self.addCleanup(context.stop)
return mock_object