Opens and closes db connections manually to avoid errors when using MySQL backend
[osm/MON.git] / osm_mon / tests / collector / test_collector.py
index f558e23..4bbe10e 100644 (file)
 # For those usages not covered by the Apache License, Version 2.0 please
 # contact: bdiaz@whitestack.com or glavado@whitestack.com
 ##
+import os
 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]
+from osm_mon.collector.vnf_collectors.openstack import OpenstackCollector
+from osm_mon.core.config import Config
+from osm_mon.core.database import DatabaseManager, db
 
 
 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)
+        os.environ["OSMMON_SQL_DATABASE_URI"] = "sqlite:///:memory:"
+        self.config = Config()
+        db_manager = DatabaseManager(self.config)
+        db_manager.create_tables()
 
     def tearDown(self):
         super().tearDown()
-        test_db.close()
+        db.close()
 
     @mock.patch("osm_mon.collector.collector.CommonDbClient", mock.Mock())
+    @mock.patch.object(Collector, "_init_backends", 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 = Collector(self.config)
         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(Collector, "_init_backends", 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 = Collector(self.config)
         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 = Collector(self.config)
         collector._collect_vca_metrics({})
-        vca_collector.assert_called_once_with()
+        vca_collector.assert_called_once_with(self.config)
         vca_collector.return_value.collect.assert_called_once_with({})