1151d46355ae41ff48f3f307e6b85bd0f4c2ca01
1 """Test the collection of charm metrics.
2 1. Deploy a charm w/metrics to a unit
3 2. Collect metrics or wait for collection to run
4 3. Execute n2vc.GetMetrics()
18 - id: singlecharmvdu-ns
19 name: singlecharmvdu-ns
20 short-name: singlecharmvdu-ns
21 description: NS with 1 VNFs singlecharmvdu-vnf connected by datanet and mgmtnet VLs
25 - vnfd-id-ref: singlecharmvdu-vnf
33 vim-network-name: mgmt
34 vnfd-connection-point-ref:
35 - vnfd-id-ref: singlecharmvdu-vnf
36 member-vnf-index-ref: '1'
37 vnfd-connection-point-ref: vnf-mgmt
38 - vnfd-id-ref: singlecharmvdu-vnf
39 member-vnf-index-ref: '2'
40 vnfd-connection-point-ref: vnf-mgmt
45 vnfd-connection-point-ref:
46 - vnfd-id-ref: singlecharmvdu-vnf
47 member-vnf-index-ref: '1'
48 vnfd-connection-point-ref: vnf-data
49 - vnfd-id-ref: singlecharmvdu-vnf
50 member-vnf-index-ref: '2'
51 vnfd-connection-point-ref: vnf-data
57 - id: singlecharmvdu-vnf
58 name: singlecharmvdu-vnf
59 short-name: singlecharmvdu-vnf
61 description: A VNF consisting of 2 VDUs w/charms connected to an internal VL, and one VDU with cloud-init
79 internal-connection-point:
80 - id-ref: mgmtVM-internal
81 - id-ref: dataVM-internal
97 external-connection-point-ref: vnf-mgmt
103 internal-connection-point-ref: mgmtVM-internal
104 internal-connection-point:
105 - id: mgmtVM-internal
106 name: mgmtVM-internal
107 short-name: mgmtVM-internal
109 cloud-init-file: cloud-config.txt
118 default-value: '/home/ubuntu/touched'
122 class PythonTest(unittest
.TestCase
):
127 self
.log
= logging
.getLogger()
128 self
.log
.level
= logging
.DEBUG
130 self
.stream_handler
= logging
.StreamHandler(sys
.stdout
)
131 self
.log
.addHandler(self
.stream_handler
)
133 self
.loop
= asyncio
.get_event_loop()
135 self
.n2vc
= utils
.get_n2vc()
137 # Parse the descriptor
138 self
.log
.debug("Parsing the descriptor")
139 self
.nsd
= utils
.get_descriptor(NSD_YAML
)
140 self
.vnfd
= utils
.get_descriptor(VNFD_YAML
)
145 vnf_config
= self
.vnfd
.get("vnf-configuration")
147 juju
= vnf_config
['juju']
148 charm
= juju
['charm']
150 self
.log
.debug("Building charm {}".format(charm
))
151 self
.charm
= utils
.build_charm(charm
)
154 self
.loop
.run_until_complete(self
.n2vc
.logout())
155 self
.log
.removeHandler(self
.stream_handler
)
157 def n2vc_callback(self
, model_name
, application_name
, workload_status
,\
158 workload_message
, task
=None):
159 """We pass the vnfd when setting up the callback, so expect it to be
160 returned as a tuple."""
161 self
.log
.debug("status: {}; task: {}".format(workload_status
, task
))
163 # if workload_status in ["stop_test"]:
165 # self.log.debug("Stopping the test1")
166 # self.loop.call_soon_threadsafe(self.loop.stop)
170 if workload_status
in ["active"] and not task
:
171 # Force a run of the metric collector, so we don't have
172 # to wait for it's normal 5 minute interval run.
173 # NOTE: this shouldn't be done outside of CI
174 utils
.collect_metrics(application_name
)
176 # get the current metrics
177 task
= asyncio
.ensure_future(
178 self
.n2vc
.GetMetrics(
183 task
.add_done_callback(
193 elif workload_status
in ["collect_metrics"]:
196 # Check if task returned metrics
197 results
= task
.result()
199 foo
= utils
.parse_metrics(application_name
, results
)
201 self
.log
.debug("Removing charm")
202 task
= asyncio
.ensure_future(
203 self
.n2vc
.RemoveCharms(model_name
, application_name
, self
.n2vc_callback
)
205 task
.add_done_callback(
216 # No metrics are available yet, so try again in a minute.
217 self
.log
.debug("Sleeping for 60 seconds")
219 task
= asyncio
.ensure_future(
220 self
.n2vc
.GetMetrics(
225 task
.add_done_callback(
234 elif workload_status
in ["stop_test"]:
236 self
.log
.debug("Stopping the test2")
237 self
.loop
.call_soon_threadsafe(self
.loop
.stop
)
239 def test_deploy_application(self
):
240 """Deploy proxy charm to a unit."""
241 if self
.nsd
and self
.vnfd
:
246 """An inner function to do the deployment of a charm from
249 charm_dir
= "{}/builds/{}".format(utils
.get_charm_path(), charm
)
251 # Setting this to an IP that will fail the initial config.
252 # This will be detected in the callback, which will execute
253 # the "config" primitive with the right IP address.
254 # mgmtaddr = self.container.state().network['eth0']['addresses']
255 # params['rw_mgmt_ip'] = mgmtaddr[0]['address']
257 # Legacy method is to set the ssh-private-key config
258 # with open(utils.get_juju_private_key(), "r") as f:
259 # pkey = f.readline()
260 # params['ssh-private-key'] = pkey
264 vnf_name
= self
.n2vc
.FormatApplicationName(
270 self
.loop
.run_until_complete(
271 self
.n2vc
.DeployCharms(
282 # Check if the VDUs in this VNF have a charm
283 # for vdu in vnfd['vdu']:
284 # vdu_config = vdu.get('vdu-configuration')
286 # juju = vdu_config['juju']
287 # self.assertIsNotNone(juju)
289 # charm = juju['charm']
290 # self.assertIsNotNone(charm)
292 # params['initial-config-primitive'] = vdu_config['initial-config-primitive']
297 # # Check if this VNF has a charm
298 vnf_config
= self
.vnfd
.get("vnf-configuration")
300 juju
= vnf_config
['juju']
301 self
.assertIsNotNone(juju
)
303 charm
= juju
['charm']
304 self
.assertIsNotNone(charm
)
306 if 'initial-config-primitive' in vnf_config
:
307 params
['initial-config-primitive'] = vnf_config
['initial-config-primitive']
312 self
.loop
.run_forever()
313 # while self.loop.is_running():
314 # # await asyncio.sleep(1)