From 4b5c5712eb457b4777eb65a95e9b42f0c8d6cdc8 Mon Sep 17 00:00:00 2001 From: Franklin Naval Date: Wed, 20 Aug 2014 17:11:56 -0500 Subject: [PATCH] Neutron LBaaS: LoadBalancer Behaviors * Behaviors for LoadBalancers * commonly used methods * includes metatests for de/serialization of json/xml Change-Id: Ica474d1bfade2efae85779ebbb5676487bfa8c10 --- .../lbaas_api/load_balancer/behaviors.py | 90 ++++++++++++++++- .../lbaas_api/load_balancer/behaviors.py | 99 ++++++++++++++++++- 2 files changed, 187 insertions(+), 2 deletions(-) diff --git a/cloudcafe/networking/lbaas/lbaas_api/load_balancer/behaviors.py b/cloudcafe/networking/lbaas/lbaas_api/load_balancer/behaviors.py index 591c9390..86862c7f 100644 --- a/cloudcafe/networking/lbaas/lbaas_api/load_balancer/behaviors.py +++ b/cloudcafe/networking/lbaas/lbaas_api/load_balancer/behaviors.py @@ -14,5 +14,93 @@ See the License for the specific language governing permissions and limitations under the License. """ -from cafe.engine.behaviors import BaseBehavior +from cloudcafe.networking.lbaas.common.behaviors import \ + BaseLoadBalancersBehaviors + +class LoadBalancerBehaviors(BaseLoadBalancersBehaviors): + + OBJECT_MODEL = 'loadbalancer' + + def __init__(self, load_balancers_client, config): + super(LoadBalancerBehaviors, self).__init__( + lbaas_client_type=load_balancers_client, config=config) + + def create_active_load_balancer( + self, name, vip_subnet, tenant_id, + description=None, vip_address=None, admin_state_up=None): + """ + @summary: Creates a load balancer and waits for it to become active + @param name: Name of the load balancer. + @type name: str + @param vip_subnet: Subnet from which to allocate a virtual IP address. + @type vip_subnet: str + @param tenant_id: Tenant that will own this load balancer. + @type tenant_id: str + @param description: Detailed description of the load balancer. + @type description: str + @param vip_address: IP address to assign to VIP. + @type vip_address: str + @param admin_state_up: Defines whether an active load balancer is + functioning or not + @type admin_state_up: bool + @return: Response object containing response and the load balancer + domain object + @rtype: requests.Response + """ + kwargs = {'name': name, 'vip_subnet': vip_subnet, + 'tenant_id': tenant_id, 'description': description, + 'vip_address': vip_address, 'admin_state_up': admin_state_up} + resp = self.create_active_lbaas_object( + lbaas_model_type=self.OBJECT_MODEL, + kwargs=kwargs) + return resp + + def update_load_balancer_and_wait_for_active( + self, name=None, description=None, admin_state_up=None): + """ + @summary: Updates a load balancer and waits for it to become active + @param name: Name of the load balancer. + @type name: str + @param description: Detailed description of the load balancer. + @type description: str + @param admin_state_up: Defines whether an active load balancer is + functioning or not + @type admin_state_up: bool + @return: Response object containing response and the load balancer + domain object + @rtype: requests.Response + """ + kwargs = {'name': name, + 'description': description, + 'admin_state_up': admin_state_up} + resp = self.update_lbaas_object_and_wait_for_active( + lbaas_model_type=self.OBJECT_MODEL, + kwargs=kwargs) + return resp + + def wait_for_load_balancer_status(self, load_balancer_id, desired_status, + interval_time=None, timeout=None): + """ + @summary: Waits for a load balancer to reach a desired status + @param load_balancer_id: The id of the load balancer + @type load_balancer_id: String + @param desired_status: The desired final status of the load balancer + @type desired_status: String + @param interval_time: The amount of time in seconds to wait + between polling + @type interval_time: Integer + @param interval_time: The amount of time in seconds to wait + before aborting + @type interval_time: Integer + @return: Response object containing response and the load balancer + domain object + @rtype: requests.Response + """ + kwargs = {'load_balancer_id': load_balancer_id, + 'desired_status': desired_status, + 'interval_time': interval_time, + 'timeout': timeout} + resp = self.wait_for_lbaas_object_status( + lbaas_model_type=self.OBJECT_MODEL, **kwargs) + return resp diff --git a/metatests/networking/lbaas/lbaas_api/load_balancer/behaviors.py b/metatests/networking/lbaas/lbaas_api/load_balancer/behaviors.py index 591c9390..907f72f0 100644 --- a/metatests/networking/lbaas/lbaas_api/load_balancer/behaviors.py +++ b/metatests/networking/lbaas/lbaas_api/load_balancer/behaviors.py @@ -14,5 +14,102 @@ See the License for the specific language governing permissions and limitations under the License. """ -from cafe.engine.behaviors import BaseBehavior +import mock +import unittest +from cloudcafe.networking.lbaas.lbaas_api.load_balancer.behaviors import \ + LoadBalancerBehaviors +from cloudcafe.networking.lbaas.lbaas_api.load_balancer.client import \ + LoadBalancersClient + + +class LoadBalancerBehaviorsFixture(unittest.TestCase): + """ + @summary: Load Balancer Behaviors Tests + """ + @classmethod + def setUpClass(cls): + super(LoadBalancerBehaviorsFixture, cls).setUpClass() + + cls.auth_token = "fake_auth_token" + cls.url = "http://fake.url.endpoint" + cls.load_balancer_id = "12345" + cls.name = "a-new-loadbalancer" + cls.vip_subnet = "SUBNET_ID" + cls.tenant_id = "7725fe12-1c14-4f45-ba8e-44bf01763578" + cls.admin_state_up = True + cls.description = "A very simple example load balancer." + cls.vip_address = "1.2.3.4" + + cls.desired_status = "ACTIVE" + cls.interval_time = 20 + cls.timeout = 120 + + cls.load_balancers_client = LoadBalancersClient( + url=cls.url, + auth_token=cls.auth_token, + serialize_format=cls.SERIALIZE, + deserialize_format=cls.DESERIALIZE) + + cls.load_balancer_behaviors = LoadBalancerBehaviors( + load_balancers_client=cls.load_balancers_client, config=None) + + +class LoadBalancerBehaviorsTests(object): + + @mock.patch.object(LoadBalancerBehaviors, 'create_active_load_balancer', + autospec=True) + def test_create_active_load_balancer(self, mock_request): + create_active_load_balancer_kwargs = ( + {'name': self.name, + 'vip_subnet': self.vip_subnet, + 'tenant_id': self.tenant_id, + 'description': self.description, + 'vip_address': self.vip_address, + 'admin_state_up': self.admin_state_up}) + self.load_balancer_behaviors.create_active_load_balancer( + **create_active_load_balancer_kwargs) + mock_request.assert_called_once_with( + self.load_balancer_behaviors, + **create_active_load_balancer_kwargs) + + @mock.patch.object(LoadBalancerBehaviors, + 'update_load_balancer_and_wait_for_active', + autospec=True) + def test_update_load_balancer_and_wait_for_active(self, mock_request): + update_load_balancer_and_wait_for_active_kwargs = ( + {'name': self.name, + 'description': self.description, + 'admin_state_up': self.admin_state_up}) + self.load_balancer_behaviors.\ + update_load_balancer_and_wait_for_active( + **update_load_balancer_and_wait_for_active_kwargs) + mock_request.assert_called_once_with( + self.load_balancer_behaviors, + **update_load_balancer_and_wait_for_active_kwargs) + + @mock.patch.object(LoadBalancerBehaviors, 'wait_for_load_balancer_status', + autospec=True) + def test_wait_for_load_balancer_status(self, mock_request): + wait_for_load_balancer_status_kwargs = ( + {'load_balancer_id': self.load_balancer_id, + 'desired_status': self.desired_status, + 'interval_time': self.interval_time, + 'timeout': self.timeout}) + self.load_balancer_behaviors.wait_for_load_balancer_status( + **wait_for_load_balancer_status_kwargs) + mock_request.assert_called_once_with( + self.load_balancer_behaviors, + **wait_for_load_balancer_status_kwargs) + + +class LoadBalancersClientTestsXML(LoadBalancerBehaviorsFixture, + LoadBalancerBehaviorsTests): + SERIALIZE = 'xml' + DESERIALIZE = 'xml' + + +class LoadBalancersClientTestsJSON(LoadBalancerBehaviorsFixture, + LoadBalancerBehaviorsTests): + SERIALIZE = 'json' + DESERIALIZE = 'json'