class PrometheusTest(TestCase):
    def setUp(self):
        self.backend = Prometheus("http://url.com", query="key=value")

    @patch("requests.get")
    def test_mem_max(self, get_mock):
        self.backend.mem_max()
        expected = self.backend.url
        expected += "/api/v1/query_range?"
        expected += "query=avg(container_memory_usage_bytes{key=value})/1024/1024"
        expected += "&start=1473209927.011&end=1473213527.011&step=14"
        get_mock.assert_called_with(expected)
    def test_date_range_by_week(self):
        backend = Prometheus(
            "http://url.com",
            date_range="1w",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=7))
        self.assertEqual(backend.end, datetime.now())

        backend = Prometheus(
            "http://url.com",
            date_range="2w",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=14))
        self.assertEqual(backend.end, datetime.now())
    def test_date_range_by_days(self):
        backend = Prometheus(
            "http://url.com",
            date_range="1d",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=1))
        self.assertEqual(backend.end, datetime.now())

        backend = Prometheus(
            "http://url.com",
            date_range="3d",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=3))
        self.assertEqual(backend.end, datetime.now())
    def test_hour_date_range(self):
        backend = Prometheus(
            "http://url.com",
            date_range="1h",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(hours=1))
        self.assertEqual(backend.end, datetime.now())

        backend = Prometheus(
            "http://url.com",
            date_range="3h",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(hours=3))
        self.assertEqual(backend.end, datetime.now())
示例#5
0
 def test_default_date_range(self):
     backend = Prometheus(
         "http://url.com",
         query="key=value",
     )
     self.assertEqual(backend.start, datetime.now() - timedelta(hours=1))
     self.assertEqual(backend.end, datetime.now())
 def test_resolution(self):
     backend = Prometheus(
         "http://url.com",
         date_range="1w",
     )
     resolution = (backend.end - backend.start).total_seconds() / 250
     self.assertEqual(backend.resolution, resolution)
示例#7
0
 def setUp(self):
     self.backend = Prometheus("http://url.com", query="key=value")
示例#8
0
class PrometheusTest(TestCase):
    def setUp(self):
        self.backend = Prometheus("http://url.com", query="key=value")

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    @patch("requests.get")
    def test_mem_max(self, get_mock):
        self.backend.mem_max()
        expected = self.backend.url
        expected += "/api/v1/query_range?"
        expected += "query=min(container_memory_usage_bytes{key=value})/1024/1024"
        expected += "&start=1333305135.0&end=1333308735.0&step={}".format(
            self.backend.resolution)
        get_mock.assert_called_with(expected)

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    def test_default_date_range(self):
        backend = Prometheus(
            "http://url.com",
            query="key=value",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(hours=1))
        self.assertEqual(backend.end, datetime.now())

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    def test_hour_date_range(self):
        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="1h",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(hours=1))
        self.assertEqual(backend.end, datetime.now())

        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="3h",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(hours=3))
        self.assertEqual(backend.end, datetime.now())

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    def test_date_range_by_days(self):
        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="1d",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=1))
        self.assertEqual(backend.end, datetime.now())

        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="3d",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=3))
        self.assertEqual(backend.end, datetime.now())

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    def test_date_range_by_week(self):
        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="1w",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=7))
        self.assertEqual(backend.end, datetime.now())

        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="2w",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=14))
        self.assertEqual(backend.end, datetime.now())

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    def test_resolution(self):
        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="1w",
        )
        resolution = (backend.end - backend.start).total_seconds() / 250
        self.assertEqual(backend.resolution, resolution)
 def setUp(self):
     self.backend = Prometheus("http://url.com")
 def setUp(self):
     self.backend = Prometheus("http://url.com", query="key=value")
class PrometheusTest(TestCase):
    def setUp(self):
        self.backend = Prometheus("http://url.com", query="key=value")

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    @patch("requests.get")
    def test_mem_max(self, get_mock):
        self.backend.mem_max()
        expected = self.backend.url
        expected += "/api/v1/query_range?"
        expected += "query=min(container_memory_usage_bytes{key=value})/1024/1024"
        expected += "&start=1333305135.0&end=1333308735.0&step={}".format(self.backend.resolution)
        get_mock.assert_called_with(expected)

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    def test_default_date_range(self):
        backend = Prometheus(
            "http://url.com",
            query="key=value",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(hours=1))
        self.assertEqual(backend.end, datetime.now())

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    def test_hour_date_range(self):
        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="1h",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(hours=1))
        self.assertEqual(backend.end, datetime.now())

        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="3h",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(hours=3))
        self.assertEqual(backend.end, datetime.now())

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    def test_date_range_by_days(self):
        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="1d",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=1))
        self.assertEqual(backend.end, datetime.now())

        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="3d",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=3))
        self.assertEqual(backend.end, datetime.now())

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    def test_date_range_by_week(self):
        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="1w",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=7))
        self.assertEqual(backend.end, datetime.now())

        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="2w",
        )
        self.assertEqual(backend.start, datetime.now() - timedelta(days=14))
        self.assertEqual(backend.end, datetime.now())

    @freeze_time("2012-04-01 16:32:15", tz_offset=0)
    def test_resolution(self):
        backend = Prometheus(
            "http://url.com",
            query="key=value",
            date_range="1w",
        )
        resolution = (backend.end - backend.start).total_seconds() / 250
        self.assertEqual(backend.resolution, resolution)