Skip to content

Commit

Permalink
Adding config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Azfaar Qureshi committed Nov 20, 2020
1 parent fd01931 commit 7f668f8
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,79 @@ def __init__(
bearer_token_file: str = None,
headers: Dict = None,
):
pass
self.endpoint = endpoint
if basic_auth:
self.basic_auth = basic_auth
if bearer_token:
self.bearer_token = bearer_token
if bearer_token_file:
self.bearer_token_file = bearer_token_file
if headers:
self.headers = headers

@property
def endpoint(self):
return self._endpoint

@endpoint.setter
def endpoint(self, endpoint: str):
if endpoint == "":
raise ValueError("endpoint required")
self._endpoint = endpoint

@property
def basic_auth(self):
return self._basic_auth

@basic_auth.setter
def basic_auth(self, basic_auth: Dict):
if hasattr(self, "bearer_token") or hasattr(self, "bearer_token_file"):
raise ValueError("cannot contain basic_auth and bearer_token")
if "username" not in basic_auth:
raise ValueError("username required in basic_auth")
if "password" not in basic_auth and "password_file" not in basic_auth:
raise ValueError("password required in basic_auth")
if "password" in basic_auth and "password_file" in basic_auth:
raise ValueError(
"basic_auth cannot contain password and password_file"
)
self._basic_auth = basic_auth

@property
def bearer_token(self):
return self._bearer_token

@bearer_token.setter
def bearer_token(self, bearer_token: str):
if hasattr(self, "basic_auth"):
raise ValueError("cannot contain basic_auth and bearer_token")
if hasattr(self, "bearer_token_file"):
raise ValueError(
"cannot contain bearer_token and bearer_token_file"
)
self._bearer_token = bearer_token

@property
def bearer_token_file(self):
return self._bearer_token_file

@bearer_token_file.setter
def bearer_token_file(self, bearer_token_file: str):
if hasattr(self, "basic_auth"):
raise ValueError("cannot contain basic_auth and bearer_token")
if hasattr(self, "bearer_token"):
raise ValueError(
"cannot contain bearer_token and bearer_token_file"
)
self._bearer_token_file = bearer_token_file

@property
def headers(self):
return self._headers

@headers.setter
def headers(self, headers: Dict):
self._headers = headers

def export(
self, export_records: Sequence[ExportRecord]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,89 @@

import unittest

from opentelemetry.exporter.prometheus_remote_write import (
PrometheusRemoteWriteMetricsExporter,
)


class TestValidation(unittest.TestCase):
# Test cases to ensure exporter parameter validation works as intended
def test_valid_standard_param(self):
pass
try:
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
)
except ValueError:
self.fail("failed to instantiate exporter with valid params)")

def test_valid_basic_auth_param(self):
pass
try:
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
basic_auth={
"username": "test_username",
"password": "test_password",
},
)
except ValueError:
self.fail("failed to instantiate exporter with valid params)")

def test_valid_bearer_token_param(self):
pass
try:
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
bearer_token="test_bearer_token",
)
except ValueError:
self.fail("failed to instantiate exporter with valid params)")

def test_invalid_no_endpoint_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter("")

def test_invalid_no_username_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
basic_auth={"password": "test_password"},
)

def test_invalid_no_password_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
basic_auth={"username": "test_username"},
)

def test_invalid_conflicting_passwords_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
basic_auth={
"username": "test_username",
"password": "test_password",
"password_file": "test_file",
},
)

def test_invalid_conflicting_bearer_tokens_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
bearer_token="test_bearer_token",
bearer_token_file="test_file",
)

def test_invalid_conflicting_auth_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
basic_auth={
"username": "test_username",
"password": "test_password",
},
bearer_token="test_bearer_token",
)


class TestConversion(unittest.TestCase):
Expand Down

0 comments on commit 7f668f8

Please sign in to comment.