X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=osm_mon%2Ftests%2Fcollector%2Ftest_collector.py;fp=osm_mon%2Ftests%2Fcollector%2Ftest_collector.py;h=f558e23a30d1e555f578e8e3e767bd527d255439;hb=51f4486b06781541ee15ea332261247ed3e930f6;hp=0000000000000000000000000000000000000000;hpb=e27def0d99cc73c5c0b7550a28e95abd6c1cd996;p=osm%2FMON.git diff --git a/osm_mon/tests/collector/test_collector.py b/osm_mon/tests/collector/test_collector.py new file mode 100644 index 0000000..f558e23 --- /dev/null +++ b/osm_mon/tests/collector/test_collector.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- + +# Copyright 2018 Whitestack, LLC +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Whitestack, LLC + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# For those usages not covered by the Apache License, Version 2.0 please +# contact: bdiaz@whitestack.com or glavado@whitestack.com +## +import unittest +from unittest import mock + +from peewee import SqliteDatabase + +from osm_mon.collector.collector import Collector +from osm_mon.collector.collectors.openstack import OpenstackCollector +from osm_mon.core import database +from osm_mon.core.database import DatabaseManager, VimCredentials, Alarm + +test_db = SqliteDatabase(':memory:') + +MODELS = [VimCredentials, Alarm] + + +class CollectorTest(unittest.TestCase): + def setUp(self): + super().setUp() + database.db = test_db + test_db.bind(MODELS) + test_db.connect() + test_db.drop_tables(MODELS) + test_db.create_tables(MODELS) + + def tearDown(self): + super().tearDown() + test_db.close() + + @mock.patch("osm_mon.collector.collector.CommonDbClient", mock.Mock()) + @mock.patch.object(OpenstackCollector, "__init__", lambda *args, **kwargs: None) + @mock.patch.object(OpenstackCollector, "collect") + @mock.patch.object(DatabaseManager, "get_vim_type") + def test_init_vim_collector_and_collect_openstack(self, _get_vim_type, collect): + _get_vim_type.return_value = 'openstack' + collector = Collector() + collector._collect_vim_metrics({}, 'test_vim_account_id') + collect.assert_called_once_with({}) + + @mock.patch("osm_mon.collector.collector.CommonDbClient", mock.Mock()) + @mock.patch.object(OpenstackCollector, "collect") + @mock.patch.object(DatabaseManager, "get_vim_type") + def test_init_vim_collector_and_collect_unknown(self, _get_vim_type, openstack_collect): + _get_vim_type.return_value = 'unknown' + collector = Collector() + collector._collect_vim_metrics({}, 'test_vim_account_id') + openstack_collect.assert_not_called() + + @mock.patch("osm_mon.collector.collector.CommonDbClient", mock.Mock()) + @mock.patch("osm_mon.collector.collector.VCACollector", autospec=True) + def test_collect_vca_metrics(self, vca_collector): + collector = Collector() + collector._collect_vca_metrics({}) + vca_collector.assert_called_once_with() + vca_collector.return_value.collect.assert_called_once_with({})