X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=tests%2Futils.py;h=d86d6f58ef2277fef481b35d2da1adfbfb472f27;hp=9f9000efab1a315e619bca14de515b23caaea46c;hb=5afe05434f65dabf514636eff28cbb2069ccb419;hpb=b09436613925b2eb334c10f219b743868e4b3fe5 diff --git a/tests/utils.py b/tests/utils.py index 9f9000e..d86d6f5 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -4,6 +4,8 @@ import logging import n2vc.vnf import pylxd import os +import shlex +import subprocess import time import uuid import yaml @@ -12,6 +14,77 @@ import yaml import urllib3 urllib3.disable_warnings() +here = os.path.dirname(os.path.realpath(__file__)) + + +def get_charm_path(): + return "{}/charms".format(here) + + +def get_layer_path(): + return "{}/charms/layers".format(here) + + +def parse_metrics(application, results): + """Parse the returned metrics into a dict.""" + + # We'll receive the results for all units, to look for the one we want + # Caveat: we're grabbing results from the first unit of the application, + # which is enough for testing, since we're only deploying a single unit. + retval = {} + for unit in results: + if unit.startswith(application): + for result in results[unit]: + retval[result['key']] = result['value'] + return retval + +def collect_metrics(application): + """Invoke Juju's metrics collector. + + Caveat: this shells out to the `juju collect-metrics` command, rather than + making an API call. At the time of writing, that API is not exposed through + the client library. + """ + + try: + logging.debug("Collecting metrics") + subprocess.check_call(['juju', 'collect-metrics', application]) + except subprocess.CalledProcessError as e: + raise Exception("Unable to collect metrics: {}".format(e)) + + +def build_charm(charm): + """Build a test charm. + + Builds one of the charms in tests/charms/layers and returns the path + to the compiled charm. The calling test is responsible for removing + the charm artifact during cleanup. + """ + # stream_handler = logging.StreamHandler(sys.stdout) + # log.addHandler(stream_handler) + + # Make sure the charm snap is installed + try: + logging.debug("Looking for charm-tools") + subprocess.check_call(['which', 'charm']) + except subprocess.CalledProcessError as e: + raise Exception("charm snap not installed.") + + try: + builds = get_charm_path() + + cmd = "charm build {}/{} -o {}/".format( + get_layer_path(), + charm, + builds, + ) + subprocess.check_call(shlex.split(cmd)) + return "{}/{}".format(builds, charm) + except subprocess.CalledProcessError as e: + raise Exception("charm build failed: {}.".format(e)) + + return None + def get_descriptor(descriptor): desc = None