def __init__(self): super(PowerVMInspector, self).__init__() # Build the adapter to the PowerVM API. self.adpt = pvm_adpt.Adapter( pvm_adpt.Session(), helpers=[log_hlp.log_helper, vio_hlp.vios_busy_retry_helper]) # Get the host system UUID host_uuid = self._get_host_uuid(self.adpt) # Ensure that metrics gathering is running for the host. pvm_mon_util.ensure_ltm_monitors(self.adpt, host_uuid) # Get the VM Metric Utility self.vm_metrics = pvm_mon_util.LparMetricCache(self.adpt, host_uuid)
def __init__(self, conf): super(PowerVMInspector, self).__init__(conf) # Build the adapter. May need to attempt the connection multiple times # in case the REST server is starting. session = pvm_adpt.Session(conn_tries=300) self.adpt = pvm_adpt.Adapter( session, helpers=[log_hlp.log_helper, vio_hlp.vios_busy_retry_helper]) # Get the host system UUID host_uuid = self._get_host_uuid(self.adpt) # Ensure that metrics gathering is running for the host. pvm_mon_util.ensure_ltm_monitors(self.adpt, host_uuid) # Get the VM Metric Utility self.vm_metrics = pvm_mon_util.LparMetricCache(self.adpt, host_uuid)
def test_refresh(self, mock_ensure_monitor, mock_stats, mock_vm_metrics): ret1 = None ret2 = {'lpar_uuid': 2} ret3 = {'lpar_uuid': 3} date_ret1 = datetime.datetime.now() date_ret2 = date_ret1 + datetime.timedelta(milliseconds=250) date_ret3 = date_ret2 + datetime.timedelta(milliseconds=250) mock_stats.side_effect = [ (date_ret1, mock.Mock(), mock.Mock(), mock.Mock()), (date_ret2, mock.Mock(), mock.Mock(), mock.Mock()), (date_ret3, mock.Mock(), mock.Mock(), mock.Mock())] mock_vm_metrics.side_effect = [ret1, ret2, ret3] # Creation invokes the refresh once automatically. metric_cache = pvm_t_mon.LparMetricCache(self.adpt, 'host_uuid', refresh_delta=.25) # Make sure the current and prev are none. self.assertEqual(date_ret1, metric_cache.cur_date) self.assertIsNone(metric_cache.cur_metric) self.assertIsNone(metric_cache.prev_date) self.assertIsNone(metric_cache.prev_metric) # The current metric should detect that it hasn't been enough time # and pass us none. cur_date, cur_metric = metric_cache.get_latest_metric('lpar_uuid') self.assertEqual(date_ret1, cur_date) self.assertIsNone(cur_metric) prev_date, prev_metric = metric_cache.get_previous_metric('lpar_uuid') self.assertIsNone(prev_date) self.assertIsNone(prev_metric) # Force the update by stating we're older than we are. pre_date = metric_cache.cur_date - datetime.timedelta(milliseconds=250) metric_cache.cur_date = pre_date # Verify that we've incremented cur_date, cur_metric = metric_cache.get_latest_metric('lpar_uuid') self.assertEqual(date_ret2, cur_date) self.assertEqual(2, cur_metric) prev_date, prev_metric = metric_cache.get_previous_metric('lpar_uuid') self.assertEqual(pre_date, prev_date) self.assertIsNone(prev_metric) # Verify that if we set the date to now, we don't increment metric_cache.cur_date = datetime.datetime.now() cur_date, cur_metric = metric_cache.get_latest_metric('lpar_uuid') self.assertEqual(2, cur_metric) prev_date, prev_metric = metric_cache.get_previous_metric('lpar_uuid') self.assertEqual(pre_date, prev_date) self.assertIsNone(prev_metric) # Delay one more time. Make sure the previous values are now set. pre_date = metric_cache.cur_date - datetime.timedelta(milliseconds=250) metric_cache.cur_date = pre_date cur_date, cur_metric = metric_cache.get_latest_metric('lpar_uuid') self.assertEqual(date_ret3, cur_date) self.assertEqual(3, cur_metric) prev_date, prev_metric = metric_cache.get_previous_metric('lpar_uuid') self.assertEqual(pre_date, prev_date) self.assertEqual(2, prev_metric)