Add deploy engine factory
rally.deploy.engine.EngineFactory is base class for every engine. All engines should be added to rally.deploy.engines.some_module.py Example of usage: # Add new engine with __name__ == 'A' class A(EngineFactory): def __init__(self, config): # do something def deploy(self): # Do deployment and return endpoints of openstack return {} # here should be endpoints def cleanup(self): # Destory OpenStack deployment and free resource Now to use new engine 'A' we should use with statement: with EngineFactory.get_engine('A', some_config) as deployment: # deployment is returned value of deploy() method # do all stuff that you need with your cloud
This commit is contained in:
0
rally/deploy/__init__.py
Normal file
0
rally/deploy/__init__.py
Normal file
76
rally/deploy/engine.py
Normal file
76
rally/deploy/engine.py
Normal file
@@ -0,0 +1,76 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2013: 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 abc
|
||||
|
||||
from rally import exceptions
|
||||
from rally import utils
|
||||
|
||||
|
||||
class EngineFactory(object):
|
||||
"""rally.deploy.engine.EngineFactory is base class for every engine.
|
||||
|
||||
All engines should be added to rally.deploy.engines.some_module.py
|
||||
|
||||
Example of usage:
|
||||
|
||||
# Add new engine with __name__ == 'A'
|
||||
class A(EngineFactory):
|
||||
def __init__(self, config):
|
||||
# do something
|
||||
|
||||
def deploy(self):
|
||||
# Do deployment and return endpoints of openstack
|
||||
return {} # here should be endpoints
|
||||
|
||||
def cleanup(self):
|
||||
# Destory OpenStack deployment and free resource
|
||||
|
||||
Now to use new engine 'A' we should use with statement:
|
||||
|
||||
with EngineFactory.get_engine('A', some_config) as deployment:
|
||||
# deployment is returned value of deploy() method
|
||||
# do all stuff that you need with your cloud
|
||||
"""
|
||||
__metaclass__ = abc.ABCMeta
|
||||
|
||||
@staticmethod
|
||||
def get_engine(name, config):
|
||||
"""Returns instance of deploy engine with corresponding name."""
|
||||
for engine in utils.itersubclasses(EngineFactory):
|
||||
if name == engine.__name__:
|
||||
return engine(config)
|
||||
raise exceptions.NoSuchEngine(engine_name=name)
|
||||
|
||||
@staticmethod
|
||||
def get_available_engines():
|
||||
"""Returns list of names of available engines."""
|
||||
return [e.__name__ for e in utils.itersubclasses(EngineFactory)]
|
||||
|
||||
@abc.abstractmethod
|
||||
def deploy(self):
|
||||
"""Deploy OpenStack cloud and return endpoints."""
|
||||
|
||||
@abc.abstractmethod
|
||||
def cleanup(self):
|
||||
"""Cleanup OpenStack deployment."""
|
||||
|
||||
def __enter__(self):
|
||||
return self.deploy()
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.cleanup()
|
0
rally/deploy/engines/__init__.py
Normal file
0
rally/deploy/engines/__init__.py
Normal file
@@ -84,9 +84,13 @@ class ImmutableException(RallyException):
|
||||
msg_fmt = _("This object is immutable.")
|
||||
|
||||
|
||||
class NotFound(RallyException):
|
||||
class NotFoundException(RallyException):
|
||||
msg_fmt = _("Not found.")
|
||||
|
||||
|
||||
class TaskNotFound(NotFound):
|
||||
class NoSuchEngine(NotFoundException):
|
||||
msg_fmt = _("There is no engine with name `%(engine_name)s`.")
|
||||
|
||||
|
||||
class TaskNotFound(NotFoundException):
|
||||
msg_fmt = _("Task with uuid=%(uuid)s not found.")
|
||||
|
0
tests/deploy/__init__.py
Normal file
0
tests/deploy/__init__.py
Normal file
87
tests/deploy/test_engine_factory.py
Normal file
87
tests/deploy/test_engine_factory.py
Normal file
@@ -0,0 +1,87 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2013: 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.
|
||||
|
||||
"""Test for deploy engines."""
|
||||
|
||||
from rally.deploy import engine as deploy_engine
|
||||
from rally import exceptions
|
||||
from rally import test
|
||||
|
||||
|
||||
class EngineFactoryTestCase(test.NoDBTestCase):
|
||||
|
||||
def test_get_engine_not_found(self):
|
||||
self.assertRaises(exceptions.NoSuchEngine,
|
||||
deploy_engine.EngineFactory.get_engine,
|
||||
"non_existing_engine", None)
|
||||
|
||||
def _create_fake_engines(self):
|
||||
class EngineMixIn(object):
|
||||
def deploy(self):
|
||||
pass
|
||||
|
||||
def cleanup(self):
|
||||
pass
|
||||
|
||||
class EngineFake1(EngineMixIn, deploy_engine.EngineFactory):
|
||||
def __init__(self, config):
|
||||
pass
|
||||
|
||||
class EngineFake2(EngineMixIn, deploy_engine.EngineFactory):
|
||||
def __init__(self, config):
|
||||
pass
|
||||
|
||||
class EngineFake3(EngineFake2):
|
||||
def __init__(self, config):
|
||||
pass
|
||||
|
||||
return [EngineFake1, EngineFake2, EngineFake3]
|
||||
|
||||
def test_get_engine(self):
|
||||
engines = self._create_fake_engines()
|
||||
for e in engines:
|
||||
engine_inst = deploy_engine.EngineFactory.get_engine(e.__name__,
|
||||
None)
|
||||
# TODO(boris-42): make it work through assertIsInstance
|
||||
self.assertEqual(str(type(engine_inst)), str(e))
|
||||
|
||||
def test_get_available_engines(self):
|
||||
engines = [e.__name__ for e in self._create_fake_engines()]
|
||||
real_engines = deploy_engine.EngineFactory.get_available_engines()
|
||||
self.assertEqual(sorted(engines), sorted(real_engines))
|
||||
|
||||
def test_engine_factory_is_abstract(self):
|
||||
self.assertRaises(TypeError, deploy_engine.EngineFactory)
|
||||
|
||||
def test_with_statement(self):
|
||||
|
||||
class A(deploy_engine.EngineFactory):
|
||||
|
||||
def __init__(self, config):
|
||||
pass
|
||||
|
||||
def deploy(self):
|
||||
self.deployed = True
|
||||
return self
|
||||
|
||||
def cleanup(self):
|
||||
self.cleanuped = True
|
||||
|
||||
with deploy_engine.EngineFactory.get_engine('A', None) as deployment:
|
||||
self.assertTrue(deployment.deployed)
|
||||
|
||||
self.assertTrue(deployment.cleanuped)
|
Reference in New Issue
Block a user