update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwlaunchpad / plugins / rwmonitor / test / repro.py
1 #!/usr/bin/env python3
2
3 import argparse
4 import asyncio
5 import concurrent.futures
6 import logging
7 import os
8 import sys
9 import unittest
10 import xmlrunner
11 import time
12
13 import gi
14 gi.require_version('RwLog', '1.0')
15
16 import rift.tasklets.rwmonitor.core as core
17 import rift.mano.cloud as cloud
18
19 from gi.repository import RwCloudYang, RwLog, RwVnfrYang
20 import rw_peas
21
22 @asyncio.coroutine
23 def update(loop, log, executor, account, plugin, vim_id):
24 """Update the NFVI metrics for the associated VDUR
25
26 This coroutine will request new metrics from the data-source and update
27 the current metrics.
28
29 """
30 try:
31 # Make the request to the plugin in a separate thread and do
32 # not exceed the timeout
33 _, metrics = yield from asyncio.wait_for(
34 loop.run_in_executor(
35 executor,
36 plugin.nfvi_metrics,
37 account,
38 vim_id
39 ),
40 timeout=10,
41 loop=loop,
42 )
43
44 except asyncio.TimeoutError:
45 msg = "timeout on request for nfvi metrics (vim-id = {})"
46 log.warning(msg.format(vim_id))
47 return
48
49 except Exception as e:
50 log.exception(e)
51 return
52
53 try:
54 # Create uninitialized metric structure
55 vdu_metrics = RwVnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vdur_NfviMetrics()
56
57 # VCPU
58 vdu_metrics.vcpu.total = 5
59 vdu_metrics.vcpu.utilization = metrics.vcpu.utilization
60
61 # Memory (in bytes)
62 vdu_metrics.memory.used = metrics.memory.used
63 vdu_metrics.memory.total = 5000
64 vdu_metrics.memory.utilization = 100 * vdu_metrics.memory.used / vdu_metrics.memory.total
65
66 # Storage
67 try:
68 vdu_metrics.storage.used = metrics.storage.used
69 utilization = 100 * vdu_metrics.storage.used / vdu_metrics.storage.total
70 if utilization > 100:
71 utilization = 100
72
73 vdu_metrics.storage.utilization = utilization
74
75 except ZeroDivisionError:
76 vdu_metrics.storage.utilization = 0
77
78 # Network (incoming)
79 vdu_metrics.network.incoming.packets = metrics.network.incoming.packets
80 vdu_metrics.network.incoming.packet_rate = metrics.network.incoming.packet_rate
81 vdu_metrics.network.incoming.bytes = metrics.network.incoming.bytes
82 vdu_metrics.network.incoming.byte_rate = metrics.network.incoming.byte_rate
83
84 # Network (outgoing)
85 vdu_metrics.network.outgoing.packets = metrics.network.outgoing.packets
86 vdu_metrics.network.outgoing.packet_rate = metrics.network.outgoing.packet_rate
87 vdu_metrics.network.outgoing.bytes = metrics.network.outgoing.bytes
88 vdu_metrics.network.outgoing.byte_rate = metrics.network.outgoing.byte_rate
89
90 # External ports
91 vdu_metrics.external_ports.total = 5
92
93 # Internal ports
94 vdu_metrics.internal_ports.total = 5
95
96 return vdu_metrics
97
98 except Exception as e:
99 log.exception(e)
100
101
102 class TestUploadProgress(unittest.TestCase):
103 ACCOUNT_MSG = RwCloudYang.YangData_RwProject_Project_CloudAccounts_CloudAccountList.from_dict({
104 "account_type": "openstack",
105 "openstack": {
106 "key": "admin",
107 "secret": "mypasswd",
108 "auth_url": 'http://10.66.4.18:5000/v3/',
109 "tenant": "demo",
110 "mgmt_network": "private"
111 }
112 })
113
114 def setUp(self):
115 self._loop = asyncio.get_event_loop()
116 self._log = logging.getLogger(__file__)
117 self._account = cloud.CloudAccount(
118 self._log,
119 RwLog.Ctx.new(__file__), TestUploadProgress.ACCOUNT_MSG
120 )
121
122 def test_many_updates(self):
123 vim_id = "a7f30def-0942-4425-8454-1ffe02b7db1e"
124 instances = 20
125
126 executor = concurrent.futures.ThreadPoolExecutor(10)
127 while True:
128 tasks = []
129 for _ in range(instances):
130 plugin = rw_peas.PeasPlugin("rwmon_ceilometer", 'RwMon-1.0')
131 impl = plugin.get_interface("Monitoring")
132 task = update(self._loop, self._log, executor, self._account.cal_account_msg, impl, vim_id)
133 tasks.append(task)
134 task = update(self._loop, self._log, executor, self._account.cal_account_msg, impl, vim_id)
135 tasks.append(task)
136 task = update(self._loop, self._log, executor, self._account.cal_account_msg, impl, vim_id)
137 tasks.append(task)
138 self._log.debug("Running %s update tasks", instances)
139 self._loop.run_until_complete(asyncio.wait(tasks, loop=self._loop, timeout=20))
140
141
142 def main(argv=sys.argv[1:]):
143 logging.basicConfig(format='TEST %(message)s')
144
145 runner = xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
146 parser = argparse.ArgumentParser()
147 parser.add_argument('-v', '--verbose', action='store_true')
148 parser.add_argument('-n', '--no-runner', action='store_true')
149
150 args, unknown = parser.parse_known_args(argv)
151 if args.no_runner:
152 runner = None
153
154 # Set the global logging level
155 logging.getLogger().setLevel(logging.DEBUG if args.verbose else logging.ERROR)
156
157 # The unittest framework requires a program name, so use the name of this
158 # file instead (we do not want to have to pass a fake program name to main
159 # when this is called from the interpreter).
160 unittest.main(argv=[__file__] + unknown + ["-v"], testRunner=runner)
161
162 if __name__ == '__main__':
163 main()