Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rosswhitfield committed Dec 3, 2024
1 parent 71877d4 commit 145c29d
Show file tree
Hide file tree
Showing 5 changed files with 542 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ jobs:
- name: install
run: |
python -m pip install -e .
- name: Stand up docker containers for ActiveMQ and PostgreSQL
run: |
docker-compose up -d
sleep 1
- name: run unit tests
run: |
echo "running unit tests"
python -m pytest --cov=src --cov-report=xml --cov-report=term-missing tests/
- name: Stand down docker containers for ActiveMQ and PostgreSQL
run: |
docker-compose down
- name: upload coverage to codecov
uses: codecov/codecov-action@v5
if:
Expand Down
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:

db:
restart: always
image: postgres:14
environment:
POSTGRES_DB: workflow
POSTGRES_USER: workflow
POSTGRES_PASSWORD: workflow
ports:
- 5432:5432

activemq:
image: apache/activemq-artemis:latest-alpine
volumes:
- ./tests/data/broker.xml:/var/lib/artemis-instance/etc-override/broker.xml
ports:
- 8161:8161
- 61613:61613
38 changes: 19 additions & 19 deletions src/artemis_data_collector/artemis_data_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
logger = logging.getLogger("AtremisDataCollector")


def initialize_database_tables(db_hostname, db_port, db_user, db_password, db_name):
def initialize_database_tables(config):
"""Initializes the tables in the database from sql files. This will fail if the tables already exist.
WebMon should have already created the tables so this is mostly for testing."""
logger.info("Initializing tables")
with psycopg.connect(dbname=db_name, host=db_hostname, port=db_port, user=db_user, password=db_password) as conn:
with psycopg.connect(
dbname=config.database_name,
host=config.database_hostname,
port=config.database_port,
user=config.database_user,
password=config.database_password,
) as conn:
with conn.cursor() as cur:
cur.execute(files("artemis_data_collector.sql").joinpath("report_statusqueue.sql").read_text())
conn.commit()
Expand Down Expand Up @@ -90,11 +96,8 @@ def request_activemq(self, query):
"""Make a request to ActiveMQ Artemis Jolokia API"""
try:
response = self.session.get(self.base_url + query)
except requests.exceptions.Timeout as e:
logger.error(f"Timeout: {e}")
return None
except requests.exceptions.RequestException as e:
logger.error(f"Error: {e}")
logger.error(e)
return None

if response.status_code != 200:
Expand Down Expand Up @@ -164,7 +167,7 @@ def get_database_statusqueues(self):
return queue_map


def main():
def parse_args(args):
parser = argparse.ArgumentParser(description="Collect data from Artemis")
parser.add_argument("--version", action="version", version="%(prog)s 1.0")
parser.add_argument(
Expand All @@ -187,32 +190,29 @@ def main():
parser.add_argument("--interval", type=int, default=600, help="Interval to collect data (seconds)")
parser.add_argument("--log_level", default="INFO", help="Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)")
parser.add_argument("--log_file", help="Log file. If not specified, log to stdout")
config = parser.parse_args()
return parser.parse_args(args)


def main():
config = parse_args(sys.argv[1:])
# setup logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=config.log_level, filename=config.log_file
)

try:
if config.initialize_db:
initialize_database_tables(
config.database_hostname,
config.database_port,
config.database_user,
config.database_password,
config.database_name,
)
return 0
if config.initialize_db:
initialize_database_tables(config)
return 0

try:
adc = ArtemisDataCollector(config)
adc.run()
except KeyboardInterrupt:
logger.info("Exiting")
return 0
except Exception as e:
# catch any unhandled exception and log it before exiting
logger.exception(f"Error: {e}")
logger.exception(e)
return 1


Expand Down
Loading

0 comments on commit 145c29d

Please sign in to comment.