diff options
author | nikoskarandreas <nick@intracom-telecom.com> | 2018-07-12 19:40:30 +0300 |
---|---|---|
committer | nikoskarandreas <nick@intracom-telecom.com> | 2018-10-15 12:13:29 +0300 |
commit | cfcb04c938abdcddd76bcdd2375b4a81ea28fa51 (patch) | |
tree | f01f95abff0319d4b7d562e2b39b98b4d0fd2822 /sdnvpn/lib | |
parent | edd6cfe15ecd4e2bf608c23c6ca4612334df044b (diff) |
Using heat orchestrator for sdnvpn - test case 1
Heat orchestrator and the use of Heat Orchestrator Templates
is introduced in sdnvpn test cases. The deployment of the
nodes and networks under test is performed as a stack with the
use of the heat api and the use of the other apis is kept to
as little as possible. The scenarios that are executed are the
same as in the orginal test cases.
This is the implementation of sdnvpn test case 1:
VPN provides connectivity between subnets
and also base functions for heat api access and some utilities.
JIRA: SDNVPN-219
Change-Id: Ic284722e600652c9058da96d349dff9398bcacf1
Signed-off-by: nikoskarandreas <nick@intracom-telecom.com>
Diffstat (limited to 'sdnvpn/lib')
-rw-r--r-- | sdnvpn/lib/openstack_utils.py | 40 | ||||
-rw-r--r-- | sdnvpn/lib/utils.py | 123 |
2 files changed, 163 insertions, 0 deletions
diff --git a/sdnvpn/lib/openstack_utils.py b/sdnvpn/lib/openstack_utils.py index fc36c5b..3fa17e6 100644 --- a/sdnvpn/lib/openstack_utils.py +++ b/sdnvpn/lib/openstack_utils.py @@ -1441,3 +1441,43 @@ def get_resource(heat_client, stack_id, resource): except Exception as e: logger.error("Error [get_resource]: %s" % e) return None + + +def create_stack(heat_client, **kwargs): + try: + stack = heat_client.stacks.create(**kwargs) + stack_id = stack['stack']['id'] + if stack_id is None: + logger.error("Stack create start failed") + raise SystemError("Stack create start failed") + return stack_id + except Exception as e: + logger.error("Error [create_stack]: %s" % e) + return None + + +def delete_stack(heat_client, stack_id): + try: + heat_client.stacks.delete(stack_id) + return True + except Exception as e: + logger.error("Error [delete_stack]: %s" % e) + return False + + +def list_stack(heat_client, **kwargs): + try: + result = heat_client.stacks.list(**kwargs) + return result + except Exception as e: + logger.error("Error [list_stack]: %s" % e) + return None + + +def get_output(heat_client, stack_id, output_key): + try: + output = heat_client.stacks.output_show(stack_id, output_key) + return output + except Exception as e: + logger.error("Error [get_output]: %s" % e) + return None diff --git a/sdnvpn/lib/utils.py b/sdnvpn/lib/utils.py index 9a5e181..135501c 100644 --- a/sdnvpn/lib/utils.py +++ b/sdnvpn/lib/utils.py @@ -14,6 +14,7 @@ import time import requests import re import subprocess +import yaml from concurrent.futures import ThreadPoolExecutor from openstack.exceptions import ResourceNotFound from requests.auth import HTTPBasicAuth @@ -987,3 +988,125 @@ def is_fib_entry_present_on_odl(controllers, ip_prefix, vrf_id): logger.error('Failed to find ip prefix %s with error %s' % (ip_prefix, e)) return False + + +def wait_stack_for_status(heat_client, stack_id, stack_status, limit=12): + """ Waits to reach specified stack status. To be used with + CREATE_COMPLETE and UPDATE_COMPLETE. + Will try a specific number of attempts at 10sec intervals + (default 2min) + + :param stack_id: the stack id returned by create_stack api call + :param stack_status: the stack status waiting for + :param limit: the maximum number of attempts + """ + logger.debug("Stack '%s' create started" % stack_id) + + stack_create_complete = False + attempts = 0 + while attempts < limit: + kwargs = { + "filters": { + "id": stack_id + } + } + stack_st = os_utils.list_stack( + heat_client, **kwargs).next().stack_status + if stack_st == stack_status: + stack_create_complete = True + break + attempts += 1 + time.sleep(10) + + logger.debug("Stack status check: %s times" % attempts) + if stack_create_complete is False: + logger.error("Stack create failed") + raise SystemError("Stack create failed") + return False + + return True + + +def delete_stack_and_wait(heat_client, stack_id, limit=12): + """ Starts and waits for completion of delete stack + + Will try a specific number of attempts at 10sec intervals + (default 2min) + + :param stack_id: the id of the stack to be deleted + :param limit: the maximum number of attempts + """ + delete_started = False + if stack_id is not None: + delete_started = os_utils.delete_stack(heat_client, stack_id) + + if delete_started is True: + logger.debug("Stack delete succesfully started") + else: + logger.error("Stack delete start failed") + + stack_delete_complete = False + attempts = 0 + while attempts < limit: + kwargs = { + "filters": { + "id": stack_id + } + } + try: + stack_st = os_utils.list_stack( + heat_client, **kwargs).next().stack_status + if stack_st == 'DELETE_COMPLETE': + stack_delete_complete = True + break + attempts += 1 + time.sleep(10) + except StopIteration: + stack_delete_complete = True + break + + logger.debug("Stack status check: %s times" % attempts) + if not stack_delete_complete: + logger.error("Stack delete failed") + raise SystemError("Stack delete failed") + return False + + return True + + +def get_heat_environment(testcase, common_config): + """ Reads the heat parameters of a testcase into a yaml object + + Each testcase where Heat Orchestratoin Template (HOT) is introduced + has an associated parameters section. + Reads testcase.heat_parameters section and read COMMON_CONFIG.flavor + and place it under parameters tree. + + :param testcase: the tescase for which the HOT file is fetched + :param common_config: the common config section + :return environment: a yaml object to be used as environment + """ + fl = common_config.default_flavor + param_dict = testcase.heat_parameters + param_dict['flavor'] = fl + env_dict = {'parameters': param_dict} + environment = yaml.safe_dump(env_dict, default_flow_style=False) + return environment + + +def get_vms_from_stack_outputs(heat_client, conn, + stack_id, vm_stack_output_keys): + """ Converts a vm name from a heat stack output to a nova vm object + + :param stack_id: the id of the stack to fetch the vms from + :param vm_stack_output_keys: a list of stack outputs with the vm names + :return vms: a list of vm objects corresponding to the outputs + """ + vms = [] + for vmk in vm_stack_output_keys: + vm_output = os_utils.get_output(heat_client, stack_id, vmk) + vm_name = vm_output['output']['output_value'] + logger.debug("vm '%s' read from heat output" % vm_name) + vm = os_utils.get_instance_by_name(conn, vm_name) + vms.append(vm) + return vms |