def test(self): """ This test uses a simple, mock implementation of the alarm interface to check that create, update, delete, and list work correctly. """ # In the beginning, there were no alarms _, alarms = self.plugin.do_alarm_list(self.account) self.assertEqual(0, len(alarms)) # Create two alarms self.plugin.do_alarm_create(self.account, self.vim_id, RwmonYang.Alarm()) self.plugin.do_alarm_create(self.account, self.vim_id, RwmonYang.Alarm()) _, alarms = self.plugin.do_alarm_list(self.account) self.assertEqual(2, len(alarms)) # The alarms should have no names alarms.sort(key=lambda a: a.alarm_id) self.assertEqual('test-alarm-id-1', alarms[0].alarm_id) self.assertEqual('test-alarm-id-2', alarms[1].alarm_id) self.assertTrue(all(a.name is None for a in alarms)) # Give names to the alarms alarms[0].name = 'test-alarm' alarms[1].name = 'test-alarm' self.plugin.do_alarm_update(self.account, alarms[0]) self.plugin.do_alarm_update(self.account, alarms[1]) self.assertTrue(all(a.name == 'test-alarm' for a in alarms)) # Delete the alarms self.plugin.do_alarm_delete(self.account, alarms[0].alarm_id) self.plugin.do_alarm_delete(self.account, alarms[1].alarm_id) _, alarms = self.plugin.do_alarm_list(self.account) self.assertEqual(0, len(alarms))
def test_null_data_source(self): """ By default, the NFVI metrics plugin mock installs a 'null' implementation that simply returns empty NFVI structures. """ status, metrics = self.plugin.nfvi_metrics(self.account, self.vim_id) self.assertEqual(status, RwTypes.RwStatus.SUCCESS) self.assertEqual(metrics, RwmonYang.YangData_RwProject_Project_NfviMetrics()) status, metrics = self.plugin.nfvi_vcpu_metrics( self.account, self.vim_id) self.assertEqual(status, RwTypes.RwStatus.SUCCESS) self.assertEqual( metrics, RwmonYang.YangData_RwProject_Project_NfviMetrics_Vcpu()) status, metrics = self.plugin.nfvi_memory_metrics( self.account, self.vim_id) self.assertEqual(status, RwTypes.RwStatus.SUCCESS) self.assertEqual( metrics, RwmonYang.YangData_RwProject_Project_NfviMetrics_Memory()) status, metrics = self.plugin.nfvi_storage_metrics( self.account, self.vim_id) self.assertEqual(status, RwTypes.RwStatus.SUCCESS) self.assertEqual( metrics, RwmonYang.YangData_RwProject_Project_NfviMetrics_Storage()) status, result = self.plugin.nfvi_metrics_available(self.account) self.assertEqual(status, RwTypes.RwStatus.SUCCESS) self.assertTrue(result)
def test_mock_data_source(self): """ This test installs a mock data source implementation in the plugin, which returns known values. This test simply checks the expected values are indeed returned. """ expected_vcpu_metrics = RwmonYang.NfviMetrics_Vcpu() expected_vcpu_metrics.utilization = 50.0 expected_vcpu_metrics.total = 100 status, metrics = self.plugin.nfvi_vcpu_metrics(self.account, self.vim_id) self.assertEqual(status, RwTypes.RwStatus.SUCCESS) self.assertEqual(metrics.total, expected_vcpu_metrics.total) self.assertEqual(metrics.utilization, expected_vcpu_metrics.utilization) expected_memory_metrics = RwmonYang.NfviMetrics_Memory() expected_memory_metrics.used = 90 expected_memory_metrics.total = 100 expected_memory_metrics.utilization = 90/100 status, metrics = self.plugin.nfvi_memory_metrics(self.account, self.vim_id) self.assertEqual(status, RwTypes.RwStatus.SUCCESS) self.assertEqual(metrics.used, expected_memory_metrics.used) self.assertEqual(metrics.total, expected_memory_metrics.total) self.assertEqual(metrics.utilization, expected_memory_metrics.utilization) expected_storage_metrics = RwmonYang.NfviMetrics_Storage() expected_storage_metrics.used = 300 expected_storage_metrics.total = 500 expected_storage_metrics.utilization = 300/500 status, metrics = self.plugin.nfvi_storage_metrics(self.account, self.vim_id) self.assertEqual(status, RwTypes.RwStatus.SUCCESS) self.assertEqual(metrics.used, expected_storage_metrics.used) self.assertEqual(metrics.total, expected_storage_metrics.total) self.assertEqual(metrics.utilization, expected_storage_metrics.utilization) status, metrics = self.plugin.nfvi_metrics(self.account, self.vim_id) self.assertEqual(status, RwTypes.RwStatus.SUCCESS) self.assertEqual(metrics.vcpu.total, expected_vcpu_metrics.total) self.assertEqual(metrics.vcpu.utilization, expected_vcpu_metrics.utilization) self.assertEqual(metrics.storage.used, expected_storage_metrics.used) self.assertEqual(metrics.storage.total, expected_storage_metrics.total) self.assertEqual(metrics.storage.utilization, expected_storage_metrics.utilization) self.assertEqual(metrics.memory.used, expected_memory_metrics.used) self.assertEqual(metrics.memory.total, expected_memory_metrics.total) self.assertEqual(metrics.memory.utilization, expected_memory_metrics.utilization) status, result = self.plugin.nfvi_metrics_available(self.account) self.assertEqual(status, RwTypes.RwStatus.SUCCESS) self.assertTrue(result)
def do_nfvi_metrics(self, account, vm_id): try: samples = self._get_driver(account).ceilo_nfvi_metrics(vm_id) metrics = RwmonYang.YangData_RwProject_Project_NfviMetrics() vcpu = samples.get("cpu_util", {}) memory = samples.get("memory_usage", {}) storage = samples.get("disk_usage", {}) metrics.vcpu.utilization = vcpu.get("volume", 0) metrics.memory.used = memory.get("volume", 0) metrics.storage.used = storage.get("volume", 0) def convert_timestamp(t): return dateutil.parser.parse(t).timestamp() timestamps = [] if 'timestamp' in vcpu: timestamps.append(convert_timestamp(vcpu['timestamp'])) if 'timestamp' in memory: timestamps.append(convert_timestamp(memory['timestamp'])) if 'timestamp' in storage: timestamps.append(convert_timestamp(storage['timestamp'])) metrics.timestamp = max(timestamps) if timestamps else 0.0 return metrics except Exception as e: logger.exception(e)
def setUp(self): plugin = rw_peas.PeasPlugin("rwmon_mock", 'RwMon-1.0') self.mock = MockAlarmInterface() self.plugin = plugin.get_interface("Monitoring") self.plugin.set_impl(self.mock) self.account = RwcalYang.CloudAccount() self.alarm = RwmonYang.Alarm(name='test-alarm') self.vim_id = 'test-vim-id'
def do_nfvi_memory_metrics(self, account, vm_id): try: samples = self._get_driver(account).ceilo_nfvi_metrics(vm_id) metrics = RwmonYang.YangData_RwProject_Project_NfviMetrics_Memory() metrics.used = samples.get("memory_usage", 0) return metrics except Exception as e: logger.exception(e)
def do_nfvi_vcpu_metrics(self, account, vm_id): try: samples = self._get_driver(account).ceilo_nfvi_metrics(vm_id) metrics = RwmonYang.YangData_RwProject_Project_NfviMetrics_Vcpu() metrics.utilization = samples.get("cpu_util", 0) return metrics except Exception as e: logger.exception(e)
def do_nfvi_storage_metrics(self, account, vm_id): try: samples = self._get_driver(account).ceilo_nfvi_metrics(vm_id) metrics = RwmonYang.NfviMetrics_Storage() metrics.used = samples.get("disk_usage", 0) return metrics except Exception as e: logger.exception(e)
def nfvi_metrics(self, account, vm_id): metrics = RwmonYang.YangData_RwProject_Project_NfviMetrics() metrics.vcpu = self.nfvi_vcpu_metrics(account, vm_id) metrics.memory = self.nfvi_memory_metrics(account, vm_id) metrics.storage = self.nfvi_storage_metrics(account, vm_id) return metrics
def nfvi_storage_metrics(self, account, vm_id): return rwmon.YangData_RwProject_Project_NfviMetrics_Storage()
def nfvi_storage_metrics(self, account, vm_id): metrics = RwmonYang.YangData_RwProject_Project_NfviMetrics_Storage() metrics.used = 300 metrics.total = 500 metrics.utilization = 300 / 500 return metrics
def nfvi_memory_metrics(self, account, vm_id): return rwmon.YangData_RwProject_Project_NfviMetrics_Memory()
def __init__(self): self.metrics = RwmonYang.NfviMetrics()
def __init__(self): self._metrics = dict() self._metrics['test-vim-id-1'] = RwmonYang.YangData_RwProject_Project_NfviMetrics() self._metrics['test-vim-id-2'] = RwmonYang.YangData_RwProject_Project_NfviMetrics()
def nfvi_metrics(self, account, vim_id): metrics = RwmonYang.YangData_RwProject_Project_NfviMetrics() metrics.vcpu.utilization = 0.5 return metrics
def nfvi_storage_metrics(self, account, vm_id): return rwmon.NfviMetrics_Storage()
def nfvi_memory_metrics(self, account, vm_id): return rwmon.NfviMetrics_Memory()
def nfvi_vcpu_metrics(self, account, vm_id): return rwmon.NfviMetrics_Vcpu()
def __init__(self): self.metrics = RwmonYang.YangData_RwProject_Project_NfviMetrics()
def nfvi_vcpu_metrics(self, account, vm_id): metrics = RwmonYang.YangData_RwProject_Project_NfviMetrics_Vcpu() metrics.total = 100 metrics.utilization = 50.0 return metrics
def nfvi_metrics(self, account, vim_id): metrics = RwmonYang.NfviMetrics() metrics.vcpu.utilization = 0.5 return metrics
def nfvi_memory_metrics(self, account, vm_id): metrics = RwmonYang.YangData_RwProject_Project_NfviMetrics_Memory() metrics.used = 90 metrics.total = 100 metrics.utilization = 90 / 100 return metrics
def __init__(self): self._metrics = dict() self._metrics['test-vim-id-1'] = RwmonYang.NfviMetrics() self._metrics['test-vim-id-2'] = RwmonYang.NfviMetrics()
def nfvi_vcpu_metrics(self, account, vm_id): return rwmon.YangData_RwProject_Project_NfviMetrics_Vcpu()