Adds use of CustomCollector in Prometheus exporter
[osm/MON.git] / osm_mon / test / collector / test_collector.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
22 ##
23 import unittest
24
25 import mock
26 from kafka import KafkaProducer
27 from kafka.errors import KafkaError
28 from osm_common import dbmongo
29
30 from osm_mon.collector.collector import MonCollector
31 from osm_mon.core.database import VimCredentials, DatabaseManager
32 from osm_mon.core.message_bus.common_consumer import CommonConsumer
33
34
35 @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
36 class MonCollectorTest(unittest.TestCase):
37 def test_generate_metric_data_payloads(self):
38 vnfr = {
39 "_id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
40 "ip-address": "192.168.160.2",
41 "created-time": 1535392482.0044956,
42 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
43 "vdur": [
44 {
45 "interfaces": [
46 {
47 "mac-address": "fa:16:3e:71:fd:b8",
48 "name": "eth0",
49 "ip-address": "192.168.160.2"
50 }
51 ],
52 "status": "ACTIVE",
53 "vim-id": "63a65636-9fc8-4022-b070-980823e6266a",
54 "name": "cirros_ns-1-cirros_vnfd-VM-1",
55 "status-detailed": None,
56 "ip-address": "192.168.160.2",
57 "vdu-id-ref": "cirros_vnfd-VM"
58 }
59 ],
60 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
61 "vnfd-ref": "cirros_vdu_scaling_vnf",
62 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
63 "_admin": {
64 "created": 1535392482.0067868,
65 "projects_read": [
66 "admin"
67 ],
68 "modified": 1535392482.0067868,
69 "projects_write": [
70 "admin"
71 ]
72 },
73 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
74 "member-vnf-index-ref": "1",
75 "connection-point": [
76 {
77 "name": "eth0",
78 "id": None,
79 "connection-point-id": None
80 }
81 ]
82 }
83 vnfd = {
84 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
85 "name": "cirros_vdu_scaling_vnf",
86 "vendor": "OSM",
87 "vdu": [
88 {
89 "name": "cirros_vnfd-VM",
90 "monitoring-param": [
91 {
92 "id": "cirros_vnfd-VM_memory_util",
93 "nfvi-metric": "average_memory_utilization"
94 }
95 ],
96 "vm-flavor": {
97 "vcpu-count": 1,
98 "memory-mb": 256,
99 "storage-gb": 2
100 },
101 "description": "cirros_vnfd-VM",
102 "count": 1,
103 "id": "cirros_vnfd-VM",
104 "interface": [
105 {
106 "name": "eth0",
107 "external-connection-point-ref": "eth0",
108 "type": "EXTERNAL",
109 "virtual-interface": {
110 "bandwidth": "0",
111 "type": "VIRTIO",
112 "vpci": "0000:00:0a.0"
113 }
114 }
115 ],
116 "image": "cirros034"
117 }
118 ],
119 "monitoring-param": [
120 {
121 "id": "cirros_vnf_memory_util",
122 "name": "cirros_vnf_memory_util",
123 "aggregation-type": "AVERAGE",
124 "vdu-monitoring-param-ref": "cirros_vnfd-VM_memory_util",
125 "vdu-ref": "cirros_vnfd-VM"
126 }
127 ],
128 "description": "Simple VNF example with a cirros and a scaling group descriptor",
129 "id": "cirros_vdu_scaling_vnf",
130 "logo": "cirros-64.png",
131 "version": "1.0",
132 "connection-point": [
133 {
134 "name": "eth0",
135 "type": "VPORT"
136 }
137 ],
138 "mgmt-interface": {
139 "cp": "eth0"
140 },
141 "scaling-group-descriptor": [
142 {
143 "name": "scale_cirros_vnfd-VM",
144 "min-instance-count": 1,
145 "vdu": [
146 {
147 "count": 1,
148 "vdu-id-ref": "cirros_vnfd-VM"
149 }
150 ],
151 "max-instance-count": 10,
152 "scaling-policy": [
153 {
154 "name": "auto_memory_util_above_threshold",
155 "scaling-type": "automatic",
156 "cooldown-time": 60,
157 "threshold-time": 10,
158 "scaling-criteria": [
159 {
160 "name": "group1_memory_util_above_threshold",
161 "vnf-monitoring-param-ref": "cirros_vnf_memory_util",
162 "scale-out-threshold": 80,
163 "scale-out-relational-operation": "GT",
164 "scale-in-relational-operation": "LT",
165 "scale-in-threshold": 20
166 }
167 ]
168 }
169 ]
170 }
171 ],
172 "short-name": "cirros_vdu_scaling_vnf",
173 "_admin": {
174 "created": 1535392242.6281035,
175 "modified": 1535392242.6281035,
176 "storage": {
177 "zipfile": "package.tar.gz",
178 "pkg-dir": "cirros_vnf",
179 "path": "/app/storage/",
180 "folder": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
181 "fs": "local",
182 "descriptor": "cirros_vnf/cirros_vdu_scaling_vnfd.yaml"
183 },
184 "usageSate": "NOT_IN_USE",
185 "onboardingState": "ONBOARDED",
186 "userDefinedData": {
187
188 },
189 "projects_read": [
190 "admin"
191 ],
192 "operationalState": "ENABLED",
193 "projects_write": [
194 "admin"
195 ]
196 }
197 }
198 payloads = MonCollector._generate_metric_data_payloads(vnfr, vnfd)
199 expected_payload = {'ns_id': '87776f33-b67c-417a-8119-cb08e4098951',
200 'vnf_member_index': '1',
201 'metric_name': 'average_memory_utilization',
202 'collection_period': 1,
203 'collection_unit': 'DAY',
204 'vdu_name': 'cirros_ns-1-cirros_vnfd-VM-1'}
205 self.assertEqual(len(payloads), 1)
206 self.assertEqual(set(expected_payload.items()).issubset(set(payloads[0].items())), True)