Example #1
0
    def setUp(self):

        self.opentsdb_host = os.environ.get('OPENTSDB_HOST')
        self.opentsdb_port = os.environ.get('OPENTSDB_PORT')

        if self.opentsdb_host is None or self.opentsdb_port is None:
            raise SkipTest(
                "OPENTSDB_HOST and OPENTSDB_PORT must be set for OpenTSDB backed tests"
            )

        self.backend = DjangoAuthorizingBackend(
            host=self.opentsdb_host,
            port=self.opentsdb_port,
            django_project_path=DJANGO_PROJECT_DIR,
            django_settings_module=DJANGO_SETTINGS_MODULE)
    def setUp(self):

        self.opentsdb_host = os.environ.get("OPENTSDB_HOST")
        self.opentsdb_port = os.environ.get("OPENTSDB_PORT")

        if self.opentsdb_host is None or self.opentsdb_port is None:
            raise SkipTest("OPENTSDB_HOST and OPENTSDB_PORT must be set for OpenTSDB backed tests")

        self.backend = DjangoAuthorizingBackend(
            host=self.opentsdb_host,
            port=self.opentsdb_port,
            django_project_path=DJANGO_PROJECT_DIR,
            django_settings_module=DJANGO_SETTINGS_MODULE,
        )
Example #3
0
class TestDjangoAuthorizingBackend(TestCase):
    def setUp(self):

        self.opentsdb_host = os.environ.get('OPENTSDB_HOST')
        self.opentsdb_port = os.environ.get('OPENTSDB_PORT')

        if self.opentsdb_host is None or self.opentsdb_port is None:
            raise SkipTest(
                "OPENTSDB_HOST and OPENTSDB_PORT must be set for OpenTSDB backed tests"
            )

        self.backend = DjangoAuthorizingBackend(
            host=self.opentsdb_host,
            port=self.opentsdb_port,
            django_project_path=DJANGO_PROJECT_DIR,
            django_settings_module=DJANGO_SETTINGS_MODULE)

    def test_messages(self):
        msg = "version\n"
        response = self.backend.handle(msg)
        self.assertIn(OPENTSDB_BUILT_STRING, response)

        now = int(time.time())
        metric = "test.my.value"
        value = choice(range(0, 42))
        good_username = "******"
        good_password = "******"
        bad_username = "******"
        bad_password = "******"

        # Ensure that messages with no u/p are filtered
        unique_tag = uuid.uuid4().hex
        msg = "put %s %s %s uuid=%s\n" % (metric, now, value, unique_tag)
        response = self.backend.handle(msg)

        time.sleep(1)
        self.assertIsNone(response)

        m = "avg:%s{uuid=%s}" % (metric, unique_tag)
        response = query_opentsdb(self.opentsdb_host, self.opentsdb_port, m)
        self.assertEqual(400, response.status)

        # Ensure that messages with bad u/p are filtered
        unique_tag = uuid.uuid4().hex
        msg = "put %s %s %s uuid=%s user=%s password=%s\n" % (
            metric, now, value, unique_tag, bad_username, bad_password)
        response = self.backend.handle(msg)

        time.sleep(1)
        self.assertIsNone(response)

        m = "avg:%s{uuid=%s}" % (metric, unique_tag)
        response = query_opentsdb(self.opentsdb_host, self.opentsdb_port, m)
        self.assertEqual(400, response.status)

        # Ensure that messages with good u bad p are filtered
        unique_tag = uuid.uuid4().hex
        msg = "put %s %s %s uuid=%s user=%s password=%s\n" % (
            metric, now, value, unique_tag, good_username, bad_password)
        response = self.backend.handle(msg)

        time.sleep(1)
        self.assertIsNone(response)

        m = "avg:%s{uuid=%s}" % (metric, unique_tag)
        response = query_opentsdb(self.opentsdb_host, self.opentsdb_port, m)
        self.assertEqual(400, response.status)

        # Ensure that messages with good u no p are filtered
        unique_tag = uuid.uuid4().hex
        msg = "put %s %s %s uuid=%s user=%s\n" % (metric, now, value,
                                                  unique_tag, good_username)
        response = self.backend.handle(msg)

        time.sleep(1)
        self.assertIsNone(response)

        m = "avg:%s{uuid=%s}" % (metric, unique_tag)
        response = query_opentsdb(self.opentsdb_host, self.opentsdb_port, m)
        self.assertEqual(400, response.status)

        # Ensure that messages with good u good p are passed through
        unique_tag = uuid.uuid4().hex
        msg = "put %s %s %s uuid=%s user=%s password=%s\n" % (
            metric, now, value, unique_tag, good_username, good_password)
        response = self.backend.handle(msg)

        time.sleep(1)
        self.assertIsNone(response)

        m = "avg:%s{uuid=%s}" % (metric, unique_tag)
        response = query_opentsdb(self.opentsdb_host, self.opentsdb_port, m)
        self.assertEqual(200, response.status)

        read_response = response.read()
        read_metric, ts, read_value, read_tag = read_response.split(" ", 3)

        self.assertEqual(read_metric, metric)
        self.assertEqual(read_value, str(value))
        self.assertIn("uuid=%s" % unique_tag, read_tag)
        self.assertIn("user=%s" % good_username, read_tag)
class TestDjangoAuthorizingBackend(TestCase):
    def setUp(self):

        self.opentsdb_host = os.environ.get("OPENTSDB_HOST")
        self.opentsdb_port = os.environ.get("OPENTSDB_PORT")

        if self.opentsdb_host is None or self.opentsdb_port is None:
            raise SkipTest("OPENTSDB_HOST and OPENTSDB_PORT must be set for OpenTSDB backed tests")

        self.backend = DjangoAuthorizingBackend(
            host=self.opentsdb_host,
            port=self.opentsdb_port,
            django_project_path=DJANGO_PROJECT_DIR,
            django_settings_module=DJANGO_SETTINGS_MODULE,
        )

    def test_messages(self):
        msg = "version\n"
        response = self.backend.handle(msg)
        self.assertIn(OPENTSDB_BUILT_STRING, response)

        now = int(time.time())
        metric = "test.my.value"
        value = choice(range(0, 42))
        good_username = "******"
        good_password = "******"
        bad_username = "******"
        bad_password = "******"

        # Ensure that messages with no u/p are filtered
        unique_tag = uuid.uuid4().hex
        msg = "put %s %s %s uuid=%s\n" % (metric, now, value, unique_tag)
        response = self.backend.handle(msg)

        time.sleep(1)
        self.assertIsNone(response)

        m = "avg:%s{uuid=%s}" % (metric, unique_tag)
        response = query_opentsdb(self.opentsdb_host, self.opentsdb_port, m)
        self.assertEqual(400, response.status)

        # Ensure that messages with bad u/p are filtered
        unique_tag = uuid.uuid4().hex
        msg = "put %s %s %s uuid=%s user=%s password=%s\n" % (
            metric,
            now,
            value,
            unique_tag,
            bad_username,
            bad_password,
        )
        response = self.backend.handle(msg)

        time.sleep(1)
        self.assertIsNone(response)

        m = "avg:%s{uuid=%s}" % (metric, unique_tag)
        response = query_opentsdb(self.opentsdb_host, self.opentsdb_port, m)
        self.assertEqual(400, response.status)

        # Ensure that messages with good u bad p are filtered
        unique_tag = uuid.uuid4().hex
        msg = "put %s %s %s uuid=%s user=%s password=%s\n" % (
            metric,
            now,
            value,
            unique_tag,
            good_username,
            bad_password,
        )
        response = self.backend.handle(msg)

        time.sleep(1)
        self.assertIsNone(response)

        m = "avg:%s{uuid=%s}" % (metric, unique_tag)
        response = query_opentsdb(self.opentsdb_host, self.opentsdb_port, m)
        self.assertEqual(400, response.status)

        # Ensure that messages with good u no p are filtered
        unique_tag = uuid.uuid4().hex
        msg = "put %s %s %s uuid=%s user=%s\n" % (metric, now, value, unique_tag, good_username)
        response = self.backend.handle(msg)

        time.sleep(1)
        self.assertIsNone(response)

        m = "avg:%s{uuid=%s}" % (metric, unique_tag)
        response = query_opentsdb(self.opentsdb_host, self.opentsdb_port, m)
        self.assertEqual(400, response.status)

        # Ensure that messages with good u good p are passed through
        unique_tag = uuid.uuid4().hex
        msg = "put %s %s %s uuid=%s user=%s password=%s\n" % (
            metric,
            now,
            value,
            unique_tag,
            good_username,
            good_password,
        )
        response = self.backend.handle(msg)

        time.sleep(1)
        self.assertIsNone(response)

        m = "avg:%s{uuid=%s}" % (metric, unique_tag)
        response = query_opentsdb(self.opentsdb_host, self.opentsdb_port, m)
        self.assertEqual(200, response.status)

        read_response = response.read()
        read_metric, ts, read_value, read_tag = read_response.split(" ", 3)

        self.assertEqual(read_metric, metric)
        self.assertEqual(read_value, str(value))
        self.assertIn("uuid=%s" % unique_tag, read_tag)
        self.assertIn("user=%s" % good_username, read_tag)