Integration test for metrics + bug fix 08/6408/1
authorAdam Israel <adam.israel@canonical.com>
Wed, 8 Aug 2018 16:54:55 +0000 (12:54 -0400)
committerAdam Israel <adam.israel@canonical.com>
Wed, 8 Aug 2018 16:59:39 +0000 (12:59 -0400)
This commit:
- adds the beginnings of an integration testing framework
- adds an integration test to exercise metric collection
- adds a test charm with metrics collection
- fixes a potential bug that can cause N2VC to fail if no
initial-config-primitive is specified in the VNF descriptor

Signed-off-by: Adam Israel <adam.israel@canonical.com>
16 files changed:
n2vc/vnf.py
tests/charms/layers/metrics-ci/README.ex [new file with mode: 0755]
tests/charms/layers/metrics-ci/config.yaml [new file with mode: 0755]
tests/charms/layers/metrics-ci/deps/layer/basic [new submodule]
tests/charms/layers/metrics-ci/deps/layer/metrics [new submodule]
tests/charms/layers/metrics-ci/deps/layer/options [new submodule]
tests/charms/layers/metrics-ci/icon.svg [new file with mode: 0755]
tests/charms/layers/metrics-ci/layer.yaml [new file with mode: 0755]
tests/charms/layers/metrics-ci/metadata.yaml [new file with mode: 0755]
tests/charms/layers/metrics-ci/metrics.yaml [new file with mode: 0755]
tests/charms/layers/metrics-ci/reactive/metrics_ci.py [new file with mode: 0755]
tests/charms/layers/metrics-ci/tests/00-setup [new file with mode: 0755]
tests/charms/layers/metrics-ci/tests/10-deploy [new file with mode: 0755]
tests/integration/__init__.py [new file with mode: 0644]
tests/integration/test_metrics.py [new file with mode: 0644]
tests/utils.py

index 7c39fa1..d3ad90c 100644 (file)
@@ -332,6 +332,10 @@ class N2VC:
         if 'rw_mgmt_ip' in params:
             rw_mgmt_ip = params['rw_mgmt_ip']
 
+        # initial_config = {}
+        if 'initial-config-primitive' not in params:
+            params['initial-config-primitive'] = {}
+
         initial_config = self._get_config_from_dict(
             params['initial-config-primitive'],
             {'<rw_mgmt_ip>': rw_mgmt_ip}
diff --git a/tests/charms/layers/metrics-ci/README.ex b/tests/charms/layers/metrics-ci/README.ex
new file mode 100755 (executable)
index 0000000..b6816b2
--- /dev/null
@@ -0,0 +1,65 @@
+# Overview
+
+Describe the intended usage of this charm and anything unique about how this
+charm relates to others here.
+
+This README will be displayed in the Charm Store, it should be either Markdown
+or RST. Ideal READMEs include instructions on how to use the charm, expected
+usage, and charm features that your audience might be interested in. For an
+example of a well written README check out Hadoop:
+http://jujucharms.com/charms/precise/hadoop
+
+Use this as a Markdown reference if you need help with the formatting of this
+README: http://askubuntu.com/editing-help
+
+This charm provides [service][]. Add a description here of what the service
+itself actually does.
+
+Also remember to check the [icon guidelines][] so that your charm looks good
+in the Juju GUI.
+
+# Usage
+
+Step by step instructions on using the charm:
+
+juju deploy servicename
+
+and so on. If you're providing a web service or something that the end user
+needs to go to, tell them here, especially if you're deploying a service that
+might listen to a non-default port.
+
+You can then browse to http://ip-address to configure the service.
+
+## Scale out Usage
+
+If the charm has any recommendations for running at scale, outline them in
+examples here. For example if you have a memcached relation that improves
+performance, mention it here.
+
+## Known Limitations and Issues
+
+This not only helps users but gives people a place to start if they want to help
+you add features to your charm.
+
+# Configuration
+
+The configuration options will be listed on the charm store, however If you're
+making assumptions or opinionated decisions in the charm (like setting a default
+administrator password), you should detail that here so the user knows how to
+change it immediately, etc.
+
+# Contact Information
+
+Though this will be listed in the charm store itself don't assume a user will
+know that, so include that information here:
+
+## Upstream Project Name
+
+  - Upstream website
+  - Upstream bug tracker
+  - Upstream mailing list or contact information
+  - Feel free to add things if it's useful for users
+
+
+[service]: http://example.com
+[icon guidelines]: https://jujucharms.com/docs/stable/authors-charm-icon
diff --git a/tests/charms/layers/metrics-ci/config.yaml b/tests/charms/layers/metrics-ci/config.yaml
new file mode 100755 (executable)
index 0000000..51f2ce4
--- /dev/null
@@ -0,0 +1,14 @@
+options:
+  string-option:
+    type: string
+    default: "Default Value"
+    description: "A short description of the configuration option"
+  boolean-option:
+    type: boolean
+    default: False
+    description: "A short description of the configuration option"
+  int-option:
+    type: int
+    default: 9001
+    description: "A short description of the configuration option"
+
diff --git a/tests/charms/layers/metrics-ci/deps/layer/basic b/tests/charms/layers/metrics-ci/deps/layer/basic
new file mode 160000 (submodule)
index 0000000..d59d361
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit d59d3613006a5afe1b9322aed9d77b5945b44356
diff --git a/tests/charms/layers/metrics-ci/deps/layer/metrics b/tests/charms/layers/metrics-ci/deps/layer/metrics
new file mode 160000 (submodule)
index 0000000..6861ce3
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 6861ce384f0dcf4e3eb1eaddf421143f4f76e64e
diff --git a/tests/charms/layers/metrics-ci/deps/layer/options b/tests/charms/layers/metrics-ci/deps/layer/options
new file mode 160000 (submodule)
index 0000000..fcdcea4
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit fcdcea4e5de3e1556c24e6704607862d0ba00a56
diff --git a/tests/charms/layers/metrics-ci/icon.svg b/tests/charms/layers/metrics-ci/icon.svg
new file mode 100755 (executable)
index 0000000..e092eef
--- /dev/null
@@ -0,0 +1,279 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>\r
+<!-- Created with Inkscape (http://www.inkscape.org/) -->\r
+\r
+<svg\r
+   xmlns:dc="http://purl.org/dc/elements/1.1/"\r
+   xmlns:cc="http://creativecommons.org/ns#"\r
+   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\r
+   xmlns:svg="http://www.w3.org/2000/svg"\r
+   xmlns="http://www.w3.org/2000/svg"\r
+   xmlns:xlink="http://www.w3.org/1999/xlink"\r
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"\r
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"\r
+   width="96"\r
+   height="96"\r
+   id="svg6517"\r
+   version="1.1"\r
+   inkscape:version="0.48+devel r12274"\r
+   sodipodi:docname="Juju_charm_icon_template.svg">\r
+  <defs\r
+     id="defs6519">\r
+    <linearGradient\r
+       inkscape:collect="always"\r
+       xlink:href="#Background"\r
+       id="linearGradient6461"\r
+       gradientUnits="userSpaceOnUse"\r
+       x1="0"\r
+       y1="970.29498"\r
+       x2="144"\r
+       y2="970.29498"\r
+       gradientTransform="matrix(0,-0.66666669,0.6660448,0,-866.25992,731.29077)" />\r
+    <linearGradient\r
+       id="Background">\r
+      <stop\r
+         id="stop4178"\r
+         offset="0"\r
+         style="stop-color:#b8b8b8;stop-opacity:1" />\r
+      <stop\r
+         id="stop4180"\r
+         offset="1"\r
+         style="stop-color:#c9c9c9;stop-opacity:1" />\r
+    </linearGradient>\r
+    <filter\r
+       style="color-interpolation-filters:sRGB;"\r
+       inkscape:label="Inner Shadow"\r
+       id="filter1121">\r
+      <feFlood\r
+         flood-opacity="0.59999999999999998"\r
+         flood-color="rgb(0,0,0)"\r
+         result="flood"\r
+         id="feFlood1123" />\r
+      <feComposite\r
+         in="flood"\r
+         in2="SourceGraphic"\r
+         operator="out"\r
+         result="composite1"\r
+         id="feComposite1125" />\r
+      <feGaussianBlur\r
+         in="composite1"\r
+         stdDeviation="1"\r
+         result="blur"\r
+         id="feGaussianBlur1127" />\r
+      <feOffset\r
+         dx="0"\r
+         dy="2"\r
+         result="offset"\r
+         id="feOffset1129" />\r
+      <feComposite\r
+         in="offset"\r
+         in2="SourceGraphic"\r
+         operator="atop"\r
+         result="composite2"\r
+         id="feComposite1131" />\r
+    </filter>\r
+    <filter\r
+       style="color-interpolation-filters:sRGB;"\r
+       inkscape:label="Drop Shadow"\r
+       id="filter950">\r
+      <feFlood\r
+         flood-opacity="0.25"\r
+         flood-color="rgb(0,0,0)"\r
+         result="flood"\r
+         id="feFlood952" />\r
+      <feComposite\r
+         in="flood"\r
+         in2="SourceGraphic"\r
+         operator="in"\r
+         result="composite1"\r
+         id="feComposite954" />\r
+      <feGaussianBlur\r
+         in="composite1"\r
+         stdDeviation="1"\r
+         result="blur"\r
+         id="feGaussianBlur956" />\r
+      <feOffset\r
+         dx="0"\r
+         dy="1"\r
+         result="offset"\r
+         id="feOffset958" />\r
+      <feComposite\r
+         in="SourceGraphic"\r
+         in2="offset"\r
+         operator="over"\r
+         result="composite2"\r
+         id="feComposite960" />\r
+    </filter>\r
+    <clipPath\r
+       clipPathUnits="userSpaceOnUse"\r
+       id="clipPath873">\r
+      <g\r
+         transform="matrix(0,-0.66666667,0.66604479,0,-258.25992,677.00001)"\r
+         id="g875"\r
+         inkscape:label="Layer 1"\r
+         style="fill:#ff00ff;fill-opacity:1;stroke:none;display:inline">\r
+        <path\r
+           style="fill:#ff00ff;fill-opacity:1;stroke:none;display:inline"\r
+           d="m 46.702703,898.22775 50.594594,0 C 138.16216,898.22775 144,904.06497 144,944.92583 l 0,50.73846 c 0,40.86071 -5.83784,46.69791 -46.702703,46.69791 l -50.594594,0 C 5.8378378,1042.3622 0,1036.525 0,995.66429 L 0,944.92583 C 0,904.06497 5.8378378,898.22775 46.702703,898.22775 Z"\r
+           id="path877"\r
+           inkscape:connector-curvature="0"\r
+           sodipodi:nodetypes="sssssssss" />\r
+      </g>\r
+    </clipPath>\r
+    <filter\r
+       inkscape:collect="always"\r
+       id="filter891"\r
+       inkscape:label="Badge Shadow">\r
+      <feGaussianBlur\r
+         inkscape:collect="always"\r
+         stdDeviation="0.71999962"\r
+         id="feGaussianBlur893" />\r
+    </filter>\r
+  </defs>\r
+  <sodipodi:namedview\r
+     id="base"\r
+     pagecolor="#ffffff"\r
+     bordercolor="#666666"\r
+     borderopacity="1.0"\r
+     inkscape:pageopacity="0.0"\r
+     inkscape:pageshadow="2"\r
+     inkscape:zoom="4.0745362"\r
+     inkscape:cx="18.514671"\r
+     inkscape:cy="49.018169"\r
+     inkscape:document-units="px"\r
+     inkscape:current-layer="layer1"\r
+     showgrid="true"\r
+     fit-margin-top="0"\r
+     fit-margin-left="0"\r
+     fit-margin-right="0"\r
+     fit-margin-bottom="0"\r
+     inkscape:window-width="1920"\r
+     inkscape:window-height="1029"\r
+     inkscape:window-x="0"\r
+     inkscape:window-y="24"\r
+     inkscape:window-maximized="1"\r
+     showborder="true"\r
+     showguides="true"\r
+     inkscape:guide-bbox="true"\r
+     inkscape:showpageshadow="false">\r
+    <inkscape:grid\r
+       type="xygrid"\r
+       id="grid821" />\r
+    <sodipodi:guide\r
+       orientation="1,0"\r
+       position="16,48"\r
+       id="guide823" />\r
+    <sodipodi:guide\r
+       orientation="0,1"\r
+       position="64,80"\r
+       id="guide825" />\r
+    <sodipodi:guide\r
+       orientation="1,0"\r
+       position="80,40"\r
+       id="guide827" />\r
+    <sodipodi:guide\r
+       orientation="0,1"\r
+       position="64,16"\r
+       id="guide829" />\r
+  </sodipodi:namedview>\r
+  <metadata\r
+     id="metadata6522">\r
+    <rdf:RDF>\r
+      <cc:Work\r
+         rdf:about="">\r
+        <dc:format>image/svg+xml</dc:format>\r
+        <dc:type\r
+           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />\r
+        <dc:title></dc:title>\r
+      </cc:Work>\r
+    </rdf:RDF>\r
+  </metadata>\r
+  <g\r
+     inkscape:label="BACKGROUND"\r
+     inkscape:groupmode="layer"\r
+     id="layer1"\r
+     transform="translate(268,-635.29076)"\r
+     style="display:inline">\r
+    <path\r
+       style="fill:url(#linearGradient6461);fill-opacity:1;stroke:none;display:inline;filter:url(#filter1121)"\r
+       d="m -268,700.15563 0,-33.72973 c 0,-27.24324 3.88785,-31.13513 31.10302,-31.13513 l 33.79408,0 c 27.21507,0 31.1029,3.89189 31.1029,31.13513 l 0,33.72973 c 0,27.24325 -3.88783,31.13514 -31.1029,31.13514 l -33.79408,0 C -264.11215,731.29077 -268,727.39888 -268,700.15563 Z"\r
+       id="path6455"\r
+       inkscape:connector-curvature="0"\r
+       sodipodi:nodetypes="sssssssss" />\r
+  </g>\r
+  <g\r
+     inkscape:groupmode="layer"\r
+     id="layer3"\r
+     inkscape:label="PLACE YOUR PICTOGRAM HERE"\r
+     style="display:inline" />\r
+  <g\r
+     inkscape:groupmode="layer"\r
+     id="layer2"\r
+     inkscape:label="BADGE"\r
+     style="display:none"\r
+     sodipodi:insensitive="true">\r
+    <g\r
+       style="display:inline"\r
+       transform="translate(-340.00001,-581)"\r
+       id="g4394"\r
+       clip-path="none">\r
+      <g\r
+         id="g855">\r
+        <g\r
+           inkscape:groupmode="maskhelper"\r
+           id="g870"\r
+           clip-path="url(#clipPath873)"\r
+           style="opacity:0.6;filter:url(#filter891)">\r
+          <path\r
+             transform="matrix(1.4999992,0,0,1.4999992,-29.999795,-237.54282)"\r
+             d="m 264,552.36218 a 12,12 0 1 1 -24,0 A 12,12 0 1 1 264,552.36218 Z"\r
+             sodipodi:ry="12"\r
+             sodipodi:rx="12"\r
+             sodipodi:cy="552.36218"\r
+             sodipodi:cx="252"\r
+             id="path844"\r
+             style="color:#000000;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:4;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate"\r
+             sodipodi:type="arc" />\r
+        </g>\r
+        <g\r
+           id="g862">\r
+          <path\r
+             sodipodi:type="arc"\r
+             style="color:#000000;fill:#f5f5f5;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:4;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate"\r
+             id="path4398"\r
+             sodipodi:cx="252"\r
+             sodipodi:cy="552.36218"\r
+             sodipodi:rx="12"\r
+             sodipodi:ry="12"\r
+             d="m 264,552.36218 a 12,12 0 1 1 -24,0 A 12,12 0 1 1 264,552.36218 Z"\r
+             transform="matrix(1.4999992,0,0,1.4999992,-29.999795,-238.54282)" />\r
+          <path\r
+             transform="matrix(1.25,0,0,1.25,33,-100.45273)"\r
+             d="m 264,552.36218 a 12,12 0 1 1 -24,0 A 12,12 0 1 1 264,552.36218 Z"\r
+             sodipodi:ry="12"\r
+             sodipodi:rx="12"\r
+             sodipodi:cy="552.36218"\r
+             sodipodi:cx="252"\r
+             id="path4400"\r
+             style="color:#000000;fill:#dd4814;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:4;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate"\r
+             sodipodi:type="arc" />\r
+          <path\r
+             sodipodi:type="star"\r
+             style="color:#000000;fill:#f5f5f5;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:3;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate"\r
+             id="path4459"\r
+             sodipodi:sides="5"\r
+             sodipodi:cx="666.19574"\r
+             sodipodi:cy="589.50385"\r
+             sodipodi:r1="7.2431178"\r
+             sodipodi:r2="4.3458705"\r
+             sodipodi:arg1="1.0471976"\r
+             sodipodi:arg2="1.6755161"\r
+             inkscape:flatsided="false"\r
+             inkscape:rounded="0.1"\r
+             inkscape:randomized="0"\r
+             d="m 669.8173,595.77657 c -0.39132,0.22593 -3.62645,-1.90343 -4.07583,-1.95066 -0.44938,-0.0472 -4.05653,1.36297 -4.39232,1.06062 -0.3358,-0.30235 0.68963,-4.03715 0.59569,-4.47913 -0.0939,-0.44198 -2.5498,-3.43681 -2.36602,-3.8496 0.18379,-0.41279 4.05267,-0.59166 4.44398,-0.81759 0.39132,-0.22593 2.48067,-3.48704 2.93005,-3.4398 0.44938,0.0472 1.81505,3.67147 2.15084,3.97382 0.3358,0.30236 4.08294,1.2817 4.17689,1.72369 0.0939,0.44198 -2.9309,2.86076 -3.11469,3.27355 C 669.9821,591.68426 670.20862,595.55064 669.8173,595.77657 Z"\r
+             transform="matrix(1.511423,-0.16366377,0.16366377,1.511423,-755.37346,-191.93651)" />\r
+        </g>\r
+      </g>\r
+    </g>\r
+  </g>\r
+</svg>\r
diff --git a/tests/charms/layers/metrics-ci/layer.yaml b/tests/charms/layers/metrics-ci/layer.yaml
new file mode 100755 (executable)
index 0000000..bd3a2b9
--- /dev/null
@@ -0,0 +1 @@
+includes: ['layer:basic', 'layer:metrics']  # if you use any interfaces, add them here
diff --git a/tests/charms/layers/metrics-ci/metadata.yaml b/tests/charms/layers/metrics-ci/metadata.yaml
new file mode 100755 (executable)
index 0000000..060274d
--- /dev/null
@@ -0,0 +1,12 @@
+name: metrics-ci
+summary: <Fill in summary here>
+maintainer: Adam Israel <Adam.Israel@ronin>
+description: |
+  <Multi-line description here>
+tags:
+  # Replace "misc" with one or more whitelisted tags from this list:
+  # https://jujucharms.com/docs/stable/authors-charm-metadata
+  - misc
+subordinate: false
+series:
+  - xenial
diff --git a/tests/charms/layers/metrics-ci/metrics.yaml b/tests/charms/layers/metrics-ci/metrics.yaml
new file mode 100755 (executable)
index 0000000..dae092f
--- /dev/null
@@ -0,0 +1,9 @@
+metrics:
+  users:
+    type: gauge
+    description: "# of users"
+    command: who|wc -l
+  load:
+    type: gauge
+    description: "5 minute load average"
+    command: cat /proc/loadavg |awk '{print $1}'
diff --git a/tests/charms/layers/metrics-ci/reactive/metrics_ci.py b/tests/charms/layers/metrics-ci/reactive/metrics_ci.py
new file mode 100755 (executable)
index 0000000..9217be4
--- /dev/null
@@ -0,0 +1,13 @@
+from charmhelpers.core.hookenv import (
+    status_set,
+)
+from charms.reactive import (
+    set_flag,
+    when_not,
+)
+
+
+@when_not('metrics-ci.installed')
+def install_metrics_ci():
+    status_set('active', "Ready!")
+    set_flag('metrics-ci.installed')
diff --git a/tests/charms/layers/metrics-ci/tests/00-setup b/tests/charms/layers/metrics-ci/tests/00-setup
new file mode 100755 (executable)
index 0000000..f0616a5
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+sudo add-apt-repository ppa:juju/stable -y
+sudo apt-get update
+sudo apt-get install amulet python-requests -y
diff --git a/tests/charms/layers/metrics-ci/tests/10-deploy b/tests/charms/layers/metrics-ci/tests/10-deploy
new file mode 100755 (executable)
index 0000000..7595ecf
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/python3
+
+import amulet
+import requests
+import unittest
+
+
+class TestCharm(unittest.TestCase):
+    def setUp(self):
+        self.d = amulet.Deployment()
+
+        self.d.add('metrics-demo')
+        self.d.expose('metrics-demo')
+
+        self.d.setup(timeout=900)
+        self.d.sentry.wait()
+
+        self.unit = self.d.sentry['metrics-demo'][0]
+
+    def test_service(self):
+        # test we can access over http
+        page = requests.get('http://{}'.format(self.unit.info['public-address']))
+        self.assertEqual(page.status_code, 200)
+        # Now you can use self.d.sentry[SERVICE][UNIT] to address each of the units and perform
+        # more in-depth steps. Each self.d.sentry[SERVICE][UNIT] has the following methods:
+        # - .info - An array of the information of that unit from Juju
+        # - .file(PATH) - Get the details of a file on that unit
+        # - .file_contents(PATH) - Get plain text output of PATH file from that unit
+        # - .directory(PATH) - Get details of directory
+        # - .directory_contents(PATH) - List files and folders in PATH on that unit
+        # - .relation(relation, service:rel) - Get relation data from return service
+
+        
+if __name__ == '__main__':
+    unittest.main()
diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/integration/test_metrics.py b/tests/integration/test_metrics.py
new file mode 100644 (file)
index 0000000..1151d46
--- /dev/null
@@ -0,0 +1,315 @@
+"""Test the collection of charm metrics.
+    1. Deploy a charm w/metrics to a unit
+    2. Collect metrics or wait for collection to run
+    3. Execute n2vc.GetMetrics()
+    5. Destroy Juju unit
+"""
+import asyncio
+import functools
+import logging
+import sys
+import time
+import unittest
+from .. import utils
+
+NSD_YAML = """
+nsd:nsd-catalog:
+    nsd:
+    -   id: singlecharmvdu-ns
+        name: singlecharmvdu-ns
+        short-name: singlecharmvdu-ns
+        description: NS with 1 VNFs singlecharmvdu-vnf connected by datanet and mgmtnet VLs
+        version: '1.0'
+        logo: osm.png
+        constituent-vnfd:
+        -   vnfd-id-ref: singlecharmvdu-vnf
+            member-vnf-index: '1'
+        vld:
+        -   id: mgmtnet
+            name: mgmtnet
+            short-name: mgmtnet
+            type: ELAN
+            mgmt-network: 'true'
+            vim-network-name: mgmt
+            vnfd-connection-point-ref:
+            -   vnfd-id-ref: singlecharmvdu-vnf
+                member-vnf-index-ref: '1'
+                vnfd-connection-point-ref: vnf-mgmt
+            -   vnfd-id-ref: singlecharmvdu-vnf
+                member-vnf-index-ref: '2'
+                vnfd-connection-point-ref: vnf-mgmt
+        -   id: datanet
+            name: datanet
+            short-name: datanet
+            type: ELAN
+            vnfd-connection-point-ref:
+            -   vnfd-id-ref: singlecharmvdu-vnf
+                member-vnf-index-ref: '1'
+                vnfd-connection-point-ref: vnf-data
+            -   vnfd-id-ref: singlecharmvdu-vnf
+                member-vnf-index-ref: '2'
+                vnfd-connection-point-ref: vnf-data
+"""
+
+VNFD_YAML = """
+vnfd:vnfd-catalog:
+    vnfd:
+    -   id: singlecharmvdu-vnf
+        name: singlecharmvdu-vnf
+        short-name: singlecharmvdu-vnf
+        version: '1.0'
+        description: A VNF consisting of 2 VDUs w/charms connected to an internal VL, and one VDU with cloud-init
+        logo: osm.png
+        connection-point:
+        -   id: vnf-mgmt
+            name: vnf-mgmt
+            short-name: vnf-mgmt
+            type: VPORT
+        -   id: vnf-data
+            name: vnf-data
+            short-name: vnf-data
+            type: VPORT
+        mgmt-interface:
+            cp: vnf-mgmt
+        internal-vld:
+        -   id: internal
+            name: internal
+            short-name: internal
+            type: ELAN
+            internal-connection-point:
+            -   id-ref: mgmtVM-internal
+            -   id-ref: dataVM-internal
+        vdu:
+        -   id: mgmtVM
+            name: mgmtVM
+            image: xenial
+            count: '1'
+            vm-flavor:
+                vcpu-count: '1'
+                memory-mb: '1024'
+                storage-gb: '10'
+            interface:
+            -   name: mgmtVM-eth0
+                position: '1'
+                type: EXTERNAL
+                virtual-interface:
+                    type: VIRTIO
+                external-connection-point-ref: vnf-mgmt
+            -   name: mgmtVM-eth1
+                position: '2'
+                type: INTERNAL
+                virtual-interface:
+                    type: VIRTIO
+                internal-connection-point-ref: mgmtVM-internal
+            internal-connection-point:
+            -   id: mgmtVM-internal
+                name: mgmtVM-internal
+                short-name: mgmtVM-internal
+                type: VPORT
+            cloud-init-file: cloud-config.txt
+        vnf-configuration:
+            juju:
+                charm: metrics-ci
+            config-primitive:
+            -   name: touch
+                parameter:
+                -   name: filename
+                    data-type: STRING
+                    default-value: '/home/ubuntu/touched'
+"""
+
+
+class PythonTest(unittest.TestCase):
+    n2vc = None
+    charm = None
+
+    def setUp(self):
+        self.log = logging.getLogger()
+        self.log.level = logging.DEBUG
+
+        self.stream_handler = logging.StreamHandler(sys.stdout)
+        self.log.addHandler(self.stream_handler)
+
+        self.loop = asyncio.get_event_loop()
+
+        self.n2vc = utils.get_n2vc()
+
+        # Parse the descriptor
+        self.log.debug("Parsing the descriptor")
+        self.nsd = utils.get_descriptor(NSD_YAML)
+        self.vnfd = utils.get_descriptor(VNFD_YAML)
+
+
+        # Build the charm
+
+        vnf_config = self.vnfd.get("vnf-configuration")
+        if vnf_config:
+            juju = vnf_config['juju']
+            charm = juju['charm']
+
+            self.log.debug("Building charm {}".format(charm))
+            self.charm = utils.build_charm(charm)
+
+    def tearDown(self):
+        self.loop.run_until_complete(self.n2vc.logout())
+        self.log.removeHandler(self.stream_handler)
+
+    def n2vc_callback(self, model_name, application_name, workload_status,\
+                      workload_message, task=None):
+        """We pass the vnfd when setting up the callback, so expect it to be
+        returned as a tuple."""
+        self.log.debug("status: {}; task: {}".format(workload_status, task))
+
+        # if workload_status in ["stop_test"]:
+        #     # Stop the test
+        #     self.log.debug("Stopping the test1")
+        #     self.loop.call_soon_threadsafe(self.loop.stop)
+        #     return
+
+        if workload_status:
+            if workload_status in ["active"] and not task:
+                # Force a run of the metric collector, so we don't have
+                # to wait for it's normal 5 minute interval run.
+                # NOTE: this shouldn't be done outside of CI
+                utils.collect_metrics(application_name)
+
+                # get the current metrics
+                task = asyncio.ensure_future(
+                    self.n2vc.GetMetrics(
+                        model_name,
+                        application_name,
+                    )
+                )
+                task.add_done_callback(
+                    functools.partial(
+                        self.n2vc_callback,
+                        model_name,
+                        application_name,
+                        "collect_metrics",
+                        task,
+                    )
+                )
+
+            elif workload_status in ["collect_metrics"]:
+
+                if task:
+                    # Check if task returned metrics
+                    results = task.result()
+
+                    foo = utils.parse_metrics(application_name, results)
+                    if 'load' in foo:
+                        self.log.debug("Removing charm")
+                        task = asyncio.ensure_future(
+                            self.n2vc.RemoveCharms(model_name, application_name, self.n2vc_callback)
+                        )
+                        task.add_done_callback(
+                            functools.partial(
+                                self.n2vc_callback,
+                                model_name,
+                                application_name,
+                                "stop_test",
+                                task,
+                            )
+                        )
+                        return
+
+                # No metrics are available yet, so try again in a minute.
+                self.log.debug("Sleeping for 60 seconds")
+                time.sleep(60)
+                task = asyncio.ensure_future(
+                    self.n2vc.GetMetrics(
+                        model_name,
+                        application_name,
+                    )
+                )
+                task.add_done_callback(
+                    functools.partial(
+                        self.n2vc_callback,
+                        model_name,
+                        application_name,
+                        "collect_metrics",
+                        task,
+                    )
+                )
+            elif workload_status in ["stop_test"]:
+                # Stop the test
+                self.log.debug("Stopping the test2")
+                self.loop.call_soon_threadsafe(self.loop.stop)
+
+    def test_deploy_application(self):
+        """Deploy proxy charm to a unit."""
+        if self.nsd and self.vnfd:
+            params = {}
+            vnf_index = 0
+
+            def deploy():
+                """An inner function to do the deployment of a charm from
+                either a vdu or vnf.
+                """
+                charm_dir = "{}/builds/{}".format(utils.get_charm_path(), charm)
+
+                # Setting this to an IP that will fail the initial config.
+                # This will be detected in the callback, which will execute
+                # the "config" primitive with the right IP address.
+                # mgmtaddr = self.container.state().network['eth0']['addresses']
+                # params['rw_mgmt_ip'] = mgmtaddr[0]['address']
+
+                # Legacy method is to set the ssh-private-key config
+                # with open(utils.get_juju_private_key(), "r") as f:
+                #     pkey = f.readline()
+                #     params['ssh-private-key'] = pkey
+
+                ns_name = "default"
+
+                vnf_name = self.n2vc.FormatApplicationName(
+                    ns_name,
+                    self.vnfd['name'],
+                    str(vnf_index),
+                )
+
+                self.loop.run_until_complete(
+                    self.n2vc.DeployCharms(
+                        ns_name,
+                        vnf_name,
+                        self.vnfd,
+                        charm_dir,
+                        params,
+                        {},
+                        self.n2vc_callback
+                    )
+                )
+
+            # Check if the VDUs in this VNF have a charm
+            # for vdu in vnfd['vdu']:
+            #     vdu_config = vdu.get('vdu-configuration')
+            #     if vdu_config:
+            #         juju = vdu_config['juju']
+            #         self.assertIsNotNone(juju)
+            #
+            #         charm = juju['charm']
+            #         self.assertIsNotNone(charm)
+            #
+            #         params['initial-config-primitive'] = vdu_config['initial-config-primitive']
+            #
+            #         deploy()
+            #         vnf_index += 1
+            #
+            # # Check if this VNF has a charm
+            vnf_config = self.vnfd.get("vnf-configuration")
+            if vnf_config:
+                juju = vnf_config['juju']
+                self.assertIsNotNone(juju)
+
+                charm = juju['charm']
+                self.assertIsNotNone(charm)
+
+                if 'initial-config-primitive' in vnf_config:
+                    params['initial-config-primitive'] = vnf_config['initial-config-primitive']
+
+                deploy()
+                vnf_index += 1
+
+            self.loop.run_forever()
+            # while self.loop.is_running():
+            #     # await asyncio.sleep(1)
+            #     time.sleep(1)
index 9f9000e..d86d6f5 100644 (file)
@@ -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