def _get(self, *args, **kwargs): c = Cube(settings.CUBE_HOST) stop = kwargs.get('stop', datetime.now()) start = kwargs.get('start', stop-timedelta(hours=1)) if not isinstance(start, datetime): start = datetime.fromtimestamp(float(start)) step = kwargs.get('step', None) if not step: step = time_utils.STEP_5_MIN metrics = {} for f in args: m = self._get_metric_expr(f, **kwargs) metrics[f] = c.get_metric(m, start=start, stop=stop, step=step) return metrics
def setUp(self): self.c = Cube('testing.com')
class TestCube(unittest.TestCase): def setUp(self): self.c = Cube('testing.com') def test_init(self): self.assertEqual(self.c.hostname, 'testing.com') self.assertEqual(self.c.port, 1081) self.assertEqual(self.c.api_version, "1.0") def test_url(self): self.assertEqual(self.c.get_base_url(), "http://testing.com:1081/1.0") def test_no_matching_events(self): mock_response = MockResponse(ok=True, status_code='200', content="[]", json=[]) Query.get = mock_get(mock_response) event = EventExpression('test') response = self.c.get_event(event, limit=10) self.assertEqual(len(response), 0) def test_single_matching_event(self): timestamp = datetime.utcnow() expected_content = '[{"time":"' + timestamp.isoformat() + '"}]' mock_response = MockResponse(ok=True, status_code='200', content=expected_content, json=json.loads(expected_content)) Query.get = mock_get(mock_response) event = EventExpression('test') response = self.c.get_event(event, limit=1) self.assertEqual(len(response), 1) self.assertTrue(isinstance(response[0], Event)) self.assertEqual(response[0].time, timestamp) def test_no_matching_metrics(self): mock_response = MockResponse(ok=True, status_code='200', content="[]", json=[]) Query.get = mock_get(mock_response) event = EventExpression('test') metric = Sum(event) response = self.c.get_metric(metric, limit=10) self.assertEqual(len(response), 0) def test_single_matching_metric(self): timestamp = datetime.utcnow() expected_content = '[{"time":"' + timestamp.isoformat() + '", '\ '"value":100}]' mock_response = MockResponse(ok=True, status_code='200', content=expected_content, json=json.loads(expected_content)) Query.get = mock_get(mock_response) event = EventExpression('test') metric = Sum(event) response = self.c.get_metric(metric, limit=1) self.assertEqual(len(response), 1) self.assertTrue(isinstance(response[0], Metric)) self.assertEqual(response[0].time, timestamp) self.assertEqual(response[0].value, 100)
def setUp(self): self.c = Cube('unittest') self.e = EventExpression('request')
def setUp(self): self.c = Cube('unittest')