-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem: There exists no tests for the scrub_old_jobs() function. Add some tests.
- Loading branch information
Showing
2 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#!/usr/bin/env python3 | ||
|
||
############################################################### | ||
# Copyright 2024 Lawrence Livermore National Security, LLC | ||
# (c.f. AUTHORS, NOTICE.LLNS, COPYING) | ||
# | ||
# This file is part of the Flux resource manager framework. | ||
# For details, see https://github.com/flux-framework. | ||
# | ||
# SPDX-License-Identifier: LGPL-3.0 | ||
############################################################### | ||
import unittest | ||
import os | ||
import sqlite3 | ||
import sys | ||
import time | ||
|
||
from unittest import mock | ||
|
||
from fluxacct.accounting import job_archive_interface as jobs | ||
from fluxacct.accounting import create_db as c | ||
|
||
|
||
class TestAccountingCLI(unittest.TestCase): | ||
# create accounting database and populate the jobs table with job records | ||
@classmethod | ||
def setUpClass(self): | ||
c.create_db("FluxAccountingTest.db") | ||
|
||
def setUp(self): | ||
global conn | ||
global cur | ||
try: | ||
conn = sqlite3.connect("file:FluxAccountingTest.db?mode=rw", uri=True) | ||
cur = conn.cursor() | ||
except sqlite3.OperationalError: | ||
print(f"Unable to open test database file", file=sys.stderr) | ||
sys.exit(-1) | ||
|
||
# clear the jobs table before each test | ||
cur.execute("DELETE FROM jobs") | ||
conn.commit() | ||
|
||
userid = 9999 | ||
t_submit = t_run = 0 | ||
t_inactive_one_week = time.time() - 604801 # 1 second more than a week | ||
t_inactive_two_weeks = time.time() - (604801 * 2) # more than 2 weeks old | ||
ranks = r = jobspec = "" | ||
insert_stmt = "INSERT INTO jobs VALUES (?, ?, ?, ?, ?, ?, ?, ?)" | ||
|
||
cur.execute( | ||
insert_stmt, | ||
( | ||
"1", | ||
userid, | ||
t_submit, | ||
t_run, | ||
t_inactive_one_week, | ||
ranks, | ||
r, | ||
jobspec, | ||
), | ||
) | ||
cur.execute( | ||
insert_stmt, | ||
( | ||
"2", | ||
userid, | ||
t_submit, | ||
t_run, | ||
t_inactive_two_weeks, | ||
ranks, | ||
r, | ||
jobspec, | ||
), | ||
) | ||
cur.execute( | ||
insert_stmt, | ||
( | ||
"3", | ||
userid, | ||
t_submit, | ||
t_run, | ||
t_inactive_two_weeks, | ||
ranks, | ||
r, | ||
jobspec, | ||
), | ||
) | ||
|
||
conn.commit() | ||
|
||
# ensure jobs are added to jobs table | ||
def test_01_check_jobs_table(self): | ||
cur.execute("SELECT * FROM jobs") | ||
rows = cur.fetchall() | ||
|
||
self.assertEqual(len(rows), 3) | ||
|
||
# only remove the job records older than our cutoff date | ||
# (should remove two jobs) | ||
def test_02_scrub_old_jobs_two_found(self): | ||
jobs.scrub_old_jobs(conn, cur, num_weeks=2) | ||
cur.execute("SELECT * FROM jobs") | ||
rows = cur.fetchall() | ||
|
||
self.assertEqual(len(rows), 1) | ||
|
||
# remove last job in table since it is older than 1 week old | ||
def test_03_scrub_old_jobs_all_jobs(self): | ||
jobs.scrub_old_jobs(conn, cur, num_weeks=1) | ||
cur.execute("SELECT * FROM jobs") | ||
rows = cur.fetchall() | ||
|
||
self.assertEqual(len(rows), 0) | ||
|
||
# remove database and log file | ||
@classmethod | ||
def tearDownClass(self): | ||
conn.close() | ||
os.remove("FluxAccountingTest.db") | ||
|
||
|
||
if __name__ == "__main__": | ||
from pycotap import TAPTestRunner | ||
|
||
unittest.main(testRunner=TAPTestRunner()) | ||
|
||
|
||
# vi: ts=4 sw=4 expandtab |