1 # -*- coding: utf-8 -*-
3 # Copyright 2016-2019 VMware Inc.
4 # This file is part of ETSI OSM
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: osslegalrouting@vmware.com
23 from osm_mon
.collector
.vnf_collectors
.vio
import VIOCollector
24 from osm_mon
.core
.config
import Config
25 from osm_mon
.tests
.unit
.collector
.vnf_collectors
.vmware
.mock_http
import (
28 from unittest
import TestCase
, mock
35 "vrops_site": "https://vrops",
45 class CollectorTest(TestCase
):
46 @mock.patch
.object(VIOCollector
, "get_vim_account", spec_set
=True, autospec
=True)
47 @mock.patch("osm_mon.collector.vnf_collectors.vio.CommonDbClient")
48 def setUp(self
, mock_db
, mock_get_vim_account
):
50 self
.mock_db
= mock_db
51 mock_vim_session
= mock
.Mock()
52 mock_get_vim_account
.return_value
= VIM_ACCOUNT
53 self
.collector
= VIOCollector(Config(), "9de6df67-b820-48c3-bcae-ee4838c5c5f4", mock_vim_session
)
55 os
.path
.join(os
.path
.dirname(__file__
), "osm_mocks", "VNFR.json"), "r"
57 self
.vnfr
= json
.load(f
)
59 os
.path
.join(os
.path
.dirname(__file__
), "osm_mocks", "VNFD.json"), "r"
61 self
.vnfd
= json
.load(f
)
66 def test_collect_cpu_and_memory(self
):
67 self
.vnfd
["vdu"][0]["monitoring-parameter"] = [
68 {"id": "ubuntu_vnf_cpu_util", "performance-metric": "cpu_utilization"},
70 "id": "ubuntu_vnf_average_memory_utilization",
71 "performance-metric": "average_memory_utilization",
74 self
.mock_db
.return_value
.get_vnfd
.return_value
= self
.vnfd
75 with requests_mock
.Mocker() as mock_requests
:
79 url_pattern
="/suite-api/api/auth/token/acquire",
80 response_file
="vrops_token.json",
84 url_pattern
="/suite-api/api/resources\\?resourceKind=VirtualMachine",
85 response_file
="vrops_resources.json",
89 url_pattern
="/suite-api/api/resources/stats.*",
90 response_file
="vrops_multi.json",
92 metrics
= self
.collector
.collect(self
.vnfr
)
93 self
.assertEqual(len(metrics
), 4, "Number of metrics returned")
94 self
.assertEqual(metrics
[0].name
, "cpu_utilization", "First metric name")
95 self
.assertEqual(metrics
[0].value
, 100.0, "CPU metric value")
97 metrics
[1].name
, "average_memory_utilization", "Second metric name"
100 metrics
[1].value
, 20.515941619873047, "Memory metric value"
102 self
.assertEqual(metrics
[2].name
, "cpu_utilization", "First metric name")
103 self
.assertEqual(metrics
[2].value
, 0.05400000140070915, "CPU metric value")
105 metrics
[3].name
, "average_memory_utilization", "Second metric name"
107 self
.assertEqual(metrics
[3].value
, 15.23439884185791, "Memory metric value")
109 def test_collect_no_monitoring_param(self
):
110 self
.vnfd
["vdu"][0]["monitoring-parameter"] = []
111 self
.mock_db
.return_value
.get_vnfd
.return_value
= self
.vnfd
112 with requests_mock
.Mocker() as mock_requests
:
116 url_pattern
="/suite-api/api/auth/token/acquire",
117 response_file
="vrops_token.json",
121 url_pattern
="/suite-api/api/resources\\?resourceKind=VirtualMachine",
122 response_file
="vrops_resources.json",
126 url_pattern
="/suite-api/api/resources/stats.*",
127 response_file
="vrops_multi.json",
129 metrics
= self
.collector
.collect(self
.vnfr
)
130 self
.assertEqual(len(metrics
), 0, "Number of metrics returned")
132 def test_collect_empty_monitoring_param(self
):
133 del self
.vnfd
["vdu"][0]["monitoring-parameter"]
134 self
.mock_db
.return_value
.get_vnfd
.return_value
= self
.vnfd
135 with requests_mock
.Mocker() as mock_requests
:
139 url_pattern
="/suite-api/api/auth/token/acquire",
140 response_file
="vrops_token.json",
144 url_pattern
="/suite-api/api/resources\\?resourceKind=VirtualMachine",
145 response_file
="vrops_resources.json",
149 url_pattern
="/suite-api/api/resources/stats.*",
150 response_file
="vrops_multi.json",
152 metrics
= self
.collector
.collect(self
.vnfr
)
153 self
.assertEqual(len(metrics
), 0, "Number of metrics returned")
155 def test_collect_no_name(self
):
156 del self
.vnfr
["vdur"][0]["name"]
157 del self
.vnfr
["vdur"][1]["name"]
158 self
.mock_db
.return_value
.get_vnfd
.return_value
= self
.vnfd
159 with requests_mock
.Mocker() as mock_requests
:
163 url_pattern
="/suite-api/api/auth/token/acquire",
164 response_file
="vrops_token.json",
168 url_pattern
="/suite-api/api/resources\\?resourceKind=VirtualMachine",
169 response_file
="vrops_resources.json",
173 url_pattern
="/suite-api/api/resources/stats.*",
174 response_file
="vrops_multi.json",
176 metrics
= self
.collector
.collect(self
.vnfr
)
177 self
.assertEqual(len(metrics
), 0, "Number of metrics returned")