コード例 #1
0
 def setUp(self):
     self.logger = logging.getLogger('test-logger')
     self.plugins = NfviMetricsPluginManager(self.logger)
     self.account = RwcalYang.CloudAccount(
         name='test-cloud-account',
         account_type="mock",
     )
コード例 #2
0
    def setUp(self):
        self.loop = asyncio.new_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
            name='test-cloud-account',
            account_type="mock",
        )

        # Define the VDUR to avoid division by zero
        self.vdur = make_vdur()
        self.vdur.vm_flavor.vcpu_count = 4
        self.vdur.vm_flavor.memory_mb = 100
        self.vdur.vm_flavor.storage_gb = 2
        self.vdur.vim_id = 'test-vim-id'

        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        self.cache = NfviMetricsCache(
            self.logger,
            self.loop,
            self.plugin_manager,
        )

        self.nfvi_interface = NfviInterface(self.loop, self.logger,
                                            self.plugin_manager, self.cache)
コード例 #3
0
    def setUp(self):
        # Reduce the sample interval so that test run quickly
        NfviMetrics.SAMPLE_INTERVAL = 0.1

        # Create a mock plugin to define the metrics retrieved. The plugin will
        # return a VCPU utilization of 0.5.
        class MockPlugin(object):
            def __init__(self):
                self.metrics = RwmonYang.NfviMetrics()

            def nfvi_metrics(self, account, vim_id):
                self.metrics.vcpu.utilization = 0.5
                return self.metrics

        self.loop = asyncio.get_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
            name='test-cloud-account',
            account_type="mock",
        )

        # Define the VDUR to avoid division by zero
        vdur = make_vdur()
        vdur.vm_flavor.vcpu_count = 4
        vdur.vm_flavor.memory_mb = 100
        vdur.vm_flavor.storage_gb = 2
        vdur.vim_id = 'test-vim-id'

        # Instantiate the mock plugin
        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        self.plugin = self.plugin_manager.plugin(self.account.name)
        self.plugin.set_impl(MockPlugin())

        self.cache = NfviMetricsCache(
            self.logger,
            self.loop,
            self.plugin_manager,
        )

        self.manager = NfviInterface(
            self.loop,
            self.logger,
            self.plugin_manager,
            self.cache,
        )

        self.metrics = NfviMetrics(
            self.logger,
            self.loop,
            self.account,
            self.plugin,
            vdur,
        )
コード例 #4
0
 def setUp(self):
     self.logger = logging.getLogger('test-logger')
     self.plugins = NfviMetricsPluginManager(self.logger)
     self.account = RwcalYang.CloudAccount(
             name='test-cloud-account',
             account_type="mock",
             )
コード例 #5
0
    def setUp(self):
        self.loop = asyncio.new_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
                name='test-cloud-account',
                account_type="mock",
                )

        # Define the VDUR to avoid division by zero
        self.vdur = make_vdur()
        self.vdur.vm_flavor.vcpu_count = 4
        self.vdur.vm_flavor.memory_mb = 100
        self.vdur.vm_flavor.storage_gb = 2
        self.vdur.vim_id = 'test-vim-id'

        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        self.cache = NfviMetricsCache(
                self.logger,
                self.loop,
                self.plugin_manager,
                )

        self.nfvi_interface = NfviInterface(
                self.loop,
                self.logger,
                self.plugin_manager,
                self.cache
                )
コード例 #6
0
    def setUp(self):
        # Reduce the sample interval so that test run quickly
        NfviMetrics.SAMPLE_INTERVAL = 0.1

        # Create a mock plugin to define the metrics retrieved. The plugin will
        # return a VCPU utilization of 0.5.
        class MockPlugin(object):
            def __init__(self):
                self.metrics = RwmonYang.NfviMetrics()

            def nfvi_metrics(self, account, vim_id):
                self.metrics.vcpu.utilization = 0.5
                return self.metrics

        self.loop = asyncio.get_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
                name='test-cloud-account',
                account_type="mock",
                )

        # Define the VDUR to avoid division by zero
        vdur = make_vdur()
        vdur.vm_flavor.vcpu_count = 4
        vdur.vm_flavor.memory_mb = 100
        vdur.vm_flavor.storage_gb = 2
        vdur.vim_id = 'test-vim-id'

        # Instantiate the mock plugin
        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        self.plugin = self.plugin_manager.plugin(self.account.name)
        self.plugin.set_impl(MockPlugin())

        self.cache = NfviMetricsCache(
                self.logger,
                self.loop,
                self.plugin_manager,
                )

        self.manager = NfviInterface(
                self.loop,
                self.logger,
                self.plugin_manager,
                self.cache,
                )

        self.metrics = NfviMetrics(
                self.logger,
                self.loop,
                self.account,
                self.plugin,
                vdur,
                )
コード例 #7
0
    def setUp(self):
        self.loop = asyncio.new_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
            name='test-cloud-account',
            account_type="mock",
        )

        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        mock = self.plugin_manager.plugin(self.account.name)
        mock.set_impl(TestNfviMetricsCache.Plugin())

        self.vdur = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vdur()
        self.vdur.id = "test-vdur-id"
        self.vdur.vim_id = "test-vim-id"
        self.vdur.vm_flavor.vcpu_count = 4
        self.vdur.vm_flavor.memory_mb = 1
        self.vdur.vm_flavor.storage_gb = 1
コード例 #8
0
    def setUp(self):
        self.loop = asyncio.new_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
                name='test-cloud-account',
                account_type="mock",
                )

        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        mock = self.plugin_manager.plugin(self.account.name)
        mock.set_impl(TestNfviMetricsCache.Plugin())

        self.vdur = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vdur()
        self.vdur.id = "test-vdur-id"
        self.vdur.vim_id = "test-vim-id"
        self.vdur.vm_flavor.vcpu_count = 4
        self.vdur.vm_flavor.memory_mb = 1
        self.vdur.vm_flavor.storage_gb = 1
コード例 #9
0
class TestNfviMetricsPluginManager(unittest.TestCase):
    def setUp(self):
        self.logger = logging.getLogger('test-logger')
        self.plugins = NfviMetricsPluginManager(self.logger)
        self.account = RwcalYang.CloudAccount(
            name='test-cloud-account',
            account_type="mock",
        )

    def test_mock_plugin(self):
        # Register an account name with a mock plugin. If successful, the
        # plugin manager should return a non-None object.
        self.plugins.register(self.account, 'mock')
        self.assertIsNotNone(self.plugins.plugin(self.account.name))

        # Now unregister the cloud account
        self.plugins.unregister(self.account.name)

        # Trying to retrieve a plugin for a cloud account that has not been
        # registered with the manager is expected to raise an exception.
        with self.assertRaises(KeyError):
            self.plugins.plugin(self.account.name)

    def test_multiple_registration(self):
        self.plugins.register(self.account, 'mock')

        # Attempting to register the account with another type of plugin will
        # also cause an exception to be raised.
        with self.assertRaises(AccountAlreadyRegisteredError):
            self.plugins.register(self.account, 'mock')

        # Attempting to register the account with 'openstack' again with cause
        # an exception to be raised.
        with self.assertRaises(AccountAlreadyRegisteredError):
            self.plugins.register(self.account, 'openstack')

    def test_unsupported_plugin(self):
        # If an attempt is made to register a cloud account with an unknown
        # type of plugin, a PluginNotSupportedError should be raised.
        with self.assertRaises(PluginNotSupportedError):
            self.plugins.register(self.account, 'unsupported-plugin')

    def test_anavailable_plugin(self):
        # Create a factory that always raises PluginUnavailableError
        class UnavailablePluginFactory(PluginFactory):
            PLUGIN_NAME = "unavailable-plugin"

            def create(self, cloud_account):
                raise PluginUnavailableError()

        # Register the factory
        self.plugins.register_plugin_factory(UnavailablePluginFactory())

        # Ensure that the correct exception propagates when the cloud account
        # is registered.
        with self.assertRaises(PluginUnavailableError):
            self.plugins.register(self.account, "unavailable-plugin")
コード例 #10
0
class TestVdurNfviMetrics(unittest.TestCase):
    def setUp(self):
        # Reduce the sample interval so that test run quickly
        NfviMetrics.SAMPLE_INTERVAL = 0.1

        # Create a mock plugin to define the metrics retrieved. The plugin will
        # return a VCPU utilization of 0.5.
        class MockPlugin(object):
            def __init__(self):
                self.metrics = RwmonYang.NfviMetrics()

            def nfvi_metrics(self, account, vim_id):
                self.metrics.vcpu.utilization = 0.5
                return self.metrics

        self.loop = asyncio.get_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
            name='test-cloud-account',
            account_type="mock",
        )

        # Define the VDUR to avoid division by zero
        vdur = make_vdur()
        vdur.vm_flavor.vcpu_count = 4
        vdur.vm_flavor.memory_mb = 100
        vdur.vm_flavor.storage_gb = 2
        vdur.vim_id = 'test-vim-id'

        # Instantiate the mock plugin
        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        self.plugin = self.plugin_manager.plugin(self.account.name)
        self.plugin.set_impl(MockPlugin())

        self.cache = NfviMetricsCache(
            self.logger,
            self.loop,
            self.plugin_manager,
        )

        self.manager = NfviInterface(
            self.loop,
            self.logger,
            self.plugin_manager,
            self.cache,
        )

        self.metrics = NfviMetrics(
            self.logger,
            self.loop,
            self.account,
            self.plugin,
            vdur,
        )

    def test_retrieval(self):
        metrics_a = None
        metrics_b = None

        # Define a coroutine that can be added to the asyncio event loop
        @asyncio.coroutine
        def update():
            # Output from the metrics calls with be written to these nonlocal
            # variables
            nonlocal metrics_a
            nonlocal metrics_b

            # This first call will return the current metrics values and
            # schedule a request to the NFVI to retrieve metrics from the data
            # source. All metrics will be zero at this point.
            metrics_a = self.metrics.retrieve()

            # Wait for the scheduled update to take effect
            yield from asyncio.sleep(0.2, loop=self.loop)

            # Retrieve the updated metrics
            metrics_b = self.metrics.retrieve()

        self.loop.run_until_complete(update())

        # Check that the metrics returned indicate that the plugin was queried
        # and returned the appropriate value, i.e. 0.5 utilization
        self.assertEqual(0.0, metrics_a.vcpu.utilization)
        self.assertEqual(0.5, metrics_b.vcpu.utilization)
コード例 #11
0
class TestNfviInterface(unittest.TestCase):
    class NfviPluginImpl(object):
        def __init__(self):
            self._alarms = set()

        def nfvi_metrics(self, account, vm_id):
            return rwmon.NfviMetrics()

        def nfvi_metrics_available(self, account):
            return True

        def alarm_create(self, account, vim_id, alarm):
            alarm.alarm_id = str(uuid.uuid4())
            self._alarms.add(alarm.alarm_id)
            return RwTypes.RwStatus.SUCCESS

        def alarm_delete(self, account, alarm_id):
            self._alarms.remove(alarm_id)
            return RwTypes.RwStatus.SUCCESS

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
            name='test-cloud-account',
            account_type="mock",
        )

        # Define the VDUR to avoid division by zero
        self.vdur = make_vdur()
        self.vdur.vm_flavor.vcpu_count = 4
        self.vdur.vm_flavor.memory_mb = 100
        self.vdur.vm_flavor.storage_gb = 2
        self.vdur.vim_id = 'test-vim-id'

        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        self.cache = NfviMetricsCache(
            self.logger,
            self.loop,
            self.plugin_manager,
        )

        self.nfvi_interface = NfviInterface(self.loop, self.logger,
                                            self.plugin_manager, self.cache)

    def test_nfvi_metrics_available(self):
        self.assertTrue(
            self.nfvi_interface.nfvi_metrics_available(self.account))

    def test_retrieve(self):
        pass

    def test_alarm_create_and_destroy(self):
        alarm = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vdur_Alarms()
        alarm.name = "test-alarm"
        alarm.description = "test-description"
        alarm.vdur_id = "test-vdur-id"
        alarm.metric = "CPU_UTILIZATION"
        alarm.statistic = "MINIMUM"
        alarm.operation = "GT"
        alarm.value = 0.1
        alarm.period = 10
        alarm.evaluations = 1

        plugin_impl = TestNfviInterface.NfviPluginImpl()
        plugin = self.plugin_manager.plugin(self.account.name)
        plugin.set_impl(plugin_impl)

        self.assertEqual(len(plugin_impl._alarms), 0)

        @asyncio.coroutine
        @wait_for_pending_tasks(self.loop)
        def wait_for_create():
            coro = self.nfvi_interface.alarm_create(
                self.account,
                "test-vim-id",
                alarm,
            )
            yield from asyncio.wait_for(
                coro,
                timeout=2,
                loop=self.loop,
            )

        self.loop.run_until_complete(wait_for_create())
        self.assertEqual(len(plugin_impl._alarms), 1)
        self.assertTrue(alarm.alarm_id is not None)

        @asyncio.coroutine
        @wait_for_pending_tasks(self.loop)
        def wait_for_destroy():
            coro = self.nfvi_interface.alarm_destroy(
                self.account,
                alarm.alarm_id,
            )
            yield from asyncio.wait_for(
                coro,
                timeout=2,
                loop=self.loop,
            )

        self.loop.run_until_complete(wait_for_destroy())
        self.assertEqual(len(plugin_impl._alarms), 0)
コード例 #12
0
class TestNfviMetricsCache(unittest.TestCase):
    class Plugin(object):
        def nfvi_metrics_available(self, cloud_account):
            return True

        def nfvi_metrics(self, account, vim_id):
            metrics = RwmonYang.NfviMetrics()
            metrics.vcpu.utilization = 0.5
            return metrics

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
            name='test-cloud-account',
            account_type="mock",
        )

        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        mock = self.plugin_manager.plugin(self.account.name)
        mock.set_impl(TestNfviMetricsCache.Plugin())

        self.vdur = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vdur()
        self.vdur.id = "test-vdur-id"
        self.vdur.vim_id = "test-vim-id"
        self.vdur.vm_flavor.vcpu_count = 4
        self.vdur.vm_flavor.memory_mb = 1
        self.vdur.vm_flavor.storage_gb = 1

    def test_create_destroy_entry(self):
        cache = NfviMetricsCache(self.logger, self.loop, self.plugin_manager)
        self.assertEqual(len(cache._nfvi_metrics), 0)

        cache.create_entry(self.account, self.vdur)
        self.assertEqual(len(cache._nfvi_metrics), 1)

        cache.destroy_entry(self.vdur.id)
        self.assertEqual(len(cache._nfvi_metrics), 0)

    def test_retrieve(self):
        NfviMetrics.SAMPLE_INTERVAL = 1

        cache = NfviMetricsCache(self.logger, self.loop, self.plugin_manager)
        cache.create_entry(self.account, self.vdur)

        @wait_for_pending_tasks(self.loop)
        @asyncio.coroutine
        def retrieve_metrics():
            metrics = cache.retrieve("test-vim-id")
            self.assertEqual(metrics.vcpu.utilization, 0.0)

            yield from asyncio.sleep(NfviMetrics.SAMPLE_INTERVAL,
                                     loop=self.loop)

            metrics = cache.retrieve("test-vim-id")
            self.assertEqual(metrics.vcpu.utilization, 0.5)

        self.loop.run_until_complete(retrieve_metrics())

    def test_id_mapping(self):
        cache = NfviMetricsCache(self.logger, self.loop, self.plugin_manager)

        cache.create_entry(self.account, self.vdur)

        self.assertEqual(cache.to_vim_id(self.vdur.id), self.vdur.vim_id)
        self.assertEqual(cache.to_vdur_id(self.vdur.vim_id), self.vdur.id)
        self.assertTrue(cache.contains_vdur_id(self.vdur.id))
        self.assertTrue(cache.contains_vim_id(self.vdur.vim_id))

        cache.destroy_entry(self.vdur.id)

        self.assertFalse(cache.contains_vdur_id(self.vdur.id))
        self.assertFalse(cache.contains_vim_id(self.vdur.vim_id))
コード例 #13
0
class TestNfviMetricsPluginManager(unittest.TestCase):
    def setUp(self):
        self.logger = logging.getLogger('test-logger')
        self.plugins = NfviMetricsPluginManager(self.logger)
        self.account = RwcalYang.CloudAccount(
                name='test-cloud-account',
                account_type="mock",
                )

    def test_mock_plugin(self):
        # Register an account name with a mock plugin. If successful, the
        # plugin manager should return a non-None object.
        self.plugins.register(self.account, 'mock')
        self.assertIsNotNone(self.plugins.plugin(self.account.name))

        # Now unregister the cloud account
        self.plugins.unregister(self.account.name)

        # Trying to retrieve a plugin for a cloud account that has not been
        # registered with the manager is expected to raise an exception.
        with self.assertRaises(KeyError):
            self.plugins.plugin(self.account.name)

    def test_multiple_registration(self):
        self.plugins.register(self.account, 'mock')

        # Attempting to register the account with another type of plugin will
        # also cause an exception to be raised.
        with self.assertRaises(AccountAlreadyRegisteredError):
            self.plugins.register(self.account, 'mock')

        # Attempting to register the account with 'openstack' again with cause
        # an exception to be raised.
        with self.assertRaises(AccountAlreadyRegisteredError):
            self.plugins.register(self.account, 'openstack')

    def test_unsupported_plugin(self):
        # If an attempt is made to register a cloud account with an unknown
        # type of plugin, a PluginNotSupportedError should be raised.
        with self.assertRaises(PluginNotSupportedError):
            self.plugins.register(self.account, 'unsupported-plugin')

    def test_anavailable_plugin(self):
        # Create a factory that always raises PluginUnavailableError
        class UnavailablePluginFactory(PluginFactory):
            PLUGIN_NAME = "unavailable-plugin"

            def create(self, cloud_account):
                raise PluginUnavailableError()

        # Register the factory
        self.plugins.register_plugin_factory(UnavailablePluginFactory())

        # Ensure that the correct exception propagates when the cloud account
        # is registered.
        with self.assertRaises(PluginUnavailableError):
            self.plugins.register(self.account, "unavailable-plugin")
コード例 #14
0
class TestVdurNfviMetrics(unittest.TestCase):
    def setUp(self):
        # Reduce the sample interval so that test run quickly
        NfviMetrics.SAMPLE_INTERVAL = 0.1

        # Create a mock plugin to define the metrics retrieved. The plugin will
        # return a VCPU utilization of 0.5.
        class MockPlugin(object):
            def __init__(self):
                self.metrics = RwmonYang.NfviMetrics()

            def nfvi_metrics(self, account, vim_id):
                self.metrics.vcpu.utilization = 0.5
                return self.metrics

        self.loop = asyncio.get_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
                name='test-cloud-account',
                account_type="mock",
                )

        # Define the VDUR to avoid division by zero
        vdur = make_vdur()
        vdur.vm_flavor.vcpu_count = 4
        vdur.vm_flavor.memory_mb = 100
        vdur.vm_flavor.storage_gb = 2
        vdur.vim_id = 'test-vim-id'

        # Instantiate the mock plugin
        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        self.plugin = self.plugin_manager.plugin(self.account.name)
        self.plugin.set_impl(MockPlugin())

        self.cache = NfviMetricsCache(
                self.logger,
                self.loop,
                self.plugin_manager,
                )

        self.manager = NfviInterface(
                self.loop,
                self.logger,
                self.plugin_manager,
                self.cache,
                )

        self.metrics = NfviMetrics(
                self.logger,
                self.loop,
                self.account,
                self.plugin,
                vdur,
                )

    def test_retrieval(self):
        metrics_a = None
        metrics_b = None

        # Define a coroutine that can be added to the asyncio event loop
        @asyncio.coroutine
        def update():
            # Output from the metrics calls with be written to these nonlocal
            # variables
            nonlocal metrics_a
            nonlocal metrics_b

            # This first call will return the current metrics values and
            # schedule a request to the NFVI to retrieve metrics from the data
            # source. All metrics will be zero at this point.
            metrics_a = self.metrics.retrieve()

            # Wait for the scheduled update to take effect
            yield from asyncio.sleep(0.2, loop=self.loop)

            # Retrieve the updated metrics
            metrics_b = self.metrics.retrieve()

        self.loop.run_until_complete(update())

        # Check that the metrics returned indicate that the plugin was queried
        # and returned the appropriate value, i.e. 0.5 utilization
        self.assertEqual(0.0, metrics_a.vcpu.utilization)
        self.assertEqual(0.5, metrics_b.vcpu.utilization)
コード例 #15
0
class TestNfviInterface(unittest.TestCase):
    class NfviPluginImpl(object):
        def __init__(self):
            self._alarms = set()

        def nfvi_metrics(self, account, vm_id):
            return rwmon.NfviMetrics()

        def nfvi_metrics_available(self, account):
            return True

        def alarm_create(self, account, vim_id, alarm):
            alarm.alarm_id = str(uuid.uuid4())
            self._alarms.add(alarm.alarm_id)
            return RwTypes.RwStatus.SUCCESS

        def alarm_delete(self, account, alarm_id):
            self._alarms.remove(alarm_id)
            return RwTypes.RwStatus.SUCCESS

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
                name='test-cloud-account',
                account_type="mock",
                )

        # Define the VDUR to avoid division by zero
        self.vdur = make_vdur()
        self.vdur.vm_flavor.vcpu_count = 4
        self.vdur.vm_flavor.memory_mb = 100
        self.vdur.vm_flavor.storage_gb = 2
        self.vdur.vim_id = 'test-vim-id'

        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        self.cache = NfviMetricsCache(
                self.logger,
                self.loop,
                self.plugin_manager,
                )

        self.nfvi_interface = NfviInterface(
                self.loop,
                self.logger,
                self.plugin_manager,
                self.cache
                )

    def test_nfvi_metrics_available(self):
        self.assertTrue(self.nfvi_interface.nfvi_metrics_available(self.account))

    def test_retrieve(self):
        pass

    def test_alarm_create_and_destroy(self):
        alarm = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vdur_Alarms()
        alarm.name = "test-alarm"
        alarm.description = "test-description"
        alarm.vdur_id = "test-vdur-id"
        alarm.metric = "CPU_UTILIZATION"
        alarm.statistic = "MINIMUM"
        alarm.operation = "GT"
        alarm.value = 0.1
        alarm.period = 10
        alarm.evaluations = 1

        plugin_impl = TestNfviInterface.NfviPluginImpl()
        plugin = self.plugin_manager.plugin(self.account.name)
        plugin.set_impl(plugin_impl)

        self.assertEqual(len(plugin_impl._alarms), 0)

        @asyncio.coroutine
        @wait_for_pending_tasks(self.loop)
        def wait_for_create():
            coro = self.nfvi_interface.alarm_create(
                    self.account,
                    "test-vim-id",
                    alarm,
                    )
            yield from asyncio.wait_for(
                    coro,
                    timeout=2,
                    loop=self.loop,
                    )

        self.loop.run_until_complete(wait_for_create())
        self.assertEqual(len(plugin_impl._alarms), 1)
        self.assertTrue(alarm.alarm_id is not None)

        @asyncio.coroutine
        @wait_for_pending_tasks(self.loop)
        def wait_for_destroy():
            coro = self.nfvi_interface.alarm_destroy(
                    self.account,
                    alarm.alarm_id,
                    )
            yield from asyncio.wait_for(
                    coro,
                    timeout=2,
                    loop=self.loop,
                    )

        self.loop.run_until_complete(wait_for_destroy())
        self.assertEqual(len(plugin_impl._alarms), 0)
コード例 #16
0
class TestNfviMetricsCache(unittest.TestCase):
    class Plugin(object):
        def nfvi_metrics_available(self, cloud_account):
            return True

        def nfvi_metrics(self, account, vim_id):
            metrics = RwmonYang.NfviMetrics()
            metrics.vcpu.utilization = 0.5
            return metrics

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        self.logger = logging.getLogger('test-logger')

        self.account = RwcalYang.CloudAccount(
                name='test-cloud-account',
                account_type="mock",
                )

        self.plugin_manager = NfviMetricsPluginManager(self.logger)
        self.plugin_manager.register(self.account, "mock")

        mock = self.plugin_manager.plugin(self.account.name)
        mock.set_impl(TestNfviMetricsCache.Plugin())

        self.vdur = VnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr_Vdur()
        self.vdur.id = "test-vdur-id"
        self.vdur.vim_id = "test-vim-id"
        self.vdur.vm_flavor.vcpu_count = 4
        self.vdur.vm_flavor.memory_mb = 1
        self.vdur.vm_flavor.storage_gb = 1

    def test_create_destroy_entry(self):
        cache = NfviMetricsCache(self.logger, self.loop, self.plugin_manager)
        self.assertEqual(len(cache._nfvi_metrics), 0)

        cache.create_entry(self.account, self.vdur)
        self.assertEqual(len(cache._nfvi_metrics), 1)

        cache.destroy_entry(self.vdur.id)
        self.assertEqual(len(cache._nfvi_metrics), 0)

    def test_retrieve(self):
        NfviMetrics.SAMPLE_INTERVAL = 1

        cache = NfviMetricsCache(self.logger, self.loop, self.plugin_manager)
        cache.create_entry(self.account, self.vdur)

        @wait_for_pending_tasks(self.loop)
        @asyncio.coroutine
        def retrieve_metrics():
            metrics = cache.retrieve("test-vim-id")
            self.assertEqual(metrics.vcpu.utilization, 0.0)

            yield from asyncio.sleep(NfviMetrics.SAMPLE_INTERVAL, loop=self.loop)

            metrics = cache.retrieve("test-vim-id")
            self.assertEqual(metrics.vcpu.utilization, 0.5)

        self.loop.run_until_complete(retrieve_metrics())

    def test_id_mapping(self):
        cache = NfviMetricsCache(self.logger, self.loop, self.plugin_manager)

        cache.create_entry(self.account, self.vdur)

        self.assertEqual(cache.to_vim_id(self.vdur.id), self.vdur.vim_id)
        self.assertEqual(cache.to_vdur_id(self.vdur.vim_id), self.vdur.id)
        self.assertTrue(cache.contains_vdur_id(self.vdur.id))
        self.assertTrue(cache.contains_vim_id(self.vdur.vim_id))

        cache.destroy_entry(self.vdur.id)

        self.assertFalse(cache.contains_vdur_id(self.vdur.id))
        self.assertFalse(cache.contains_vim_id(self.vdur.vim_id))