def test_collect_metrics_converts_jsonfile_to_correct_jql(mock_query_jira): mock_query_jira.return_value ="42" jc = JiraCollector("simple_test.json") result = jc.collect() assert mock_query_jira.call_count == 2 mock_query_jira.assert_any_call('project = BIP AND status IN ("10002")', 0) mock_query_jira.assert_any_call('project = DAPLA AND status IN ("10002")', 0)
def test_collect_metrics_converts_jql_results_to_correct_dictionary(mock_query_jira): mock_query_jira.return_value ="42" jc = JiraCollector("simple_test.json") result = jc.collect() expectedMetricsDict ={ "jira_total_done{project_name=\"BIP\"}":"42", "jira_total_done{project_name=\"DAPLA\"}":"42" } assert result == expectedMetricsDict
def test_that_getJiraConnection_raises_exception(mock_apikey, mock_jiraconnection): mock_apikey.return_value = "test_apikey" with pytest.raises(HTTPError): assert JiraCollector("NA")
def test_open_empty_metricsdescriptions_file(mock_apikey, mock_jiraconnection): mock_apikey.return_value = "test_apikey" mock_jiraconnection.return_value = "test_jiraconnection" open("empty_file.json", 'a').close() with pytest.raises(FileNotFoundError): assert JiraCollector("empty_file.json")
def test_metricsdescriptions_file_does_not_exist(mock_apikey, mock_jiraconnection): mock_apikey.return_value = "test_apikey" mock_jiraconnection.return_value = "test_jiraconnection" with pytest.raises(FileNotFoundError): assert JiraCollector("filedoesnotexist.json")
def test_constructor_raises_exception_when_getApiKey_raises_exception( mock_google_api): with pytest.raises(Exception): assert JiraCollector("This is not a relevant input")
from os import getenv import sys import http.server import signal from livenessserver import LivenessServer from prometheus_client import start_http_server from prometheus_client.core import REGISTRY from jiracollector import JiraCollector if __name__ == '__main__': REGISTRY.register(JiraCollector()) # First we collect the environment variables that were set in either # the Dockerfile or the Kubernetes Pod specification. listen_port = int(getenv('LISTEN_PORT', 8090)) prom_listen_port = int(getenv('PROM_LISTEN_PORT', 8080)) # Let the Prometheus client export its metrics on a separate port. start_http_server(prom_listen_port) # Let our web application run and listen on the specified port. httpd = http.server.HTTPServer(('localhost', listen_port), LivenessServer) # Make sure you have the webserver signal when it's done. httpd.ready = True # Simple handler function to show that we we're handling the SIGTERM def do_shutdown(signum, frame): global httpd log = {'jira-metrics': {'message': 'Graceful shutdown.'}} print(json.dumps(log))