Skip to content

Commit

Permalink
t: add tests for --scrub
Browse files Browse the repository at this point in the history
Problem: There exists no tests for the scrub_old_jobs() function.

Add some tests.
  • Loading branch information
cmoussa1 committed Jun 11, 2024
1 parent f8c2ae6 commit b98595b
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
3 changes: 2 additions & 1 deletion t/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ TESTSCRIPTS = \
t1033-mf-priority-update-job.t \
t1034-mf-priority-config.t \
t5000-valgrind.t \
python/t1000-example.py
python/t1000-example.py \
python/t1001-scrub-old-jobs.py

dist_check_SCRIPTS = \
$(TESTSCRIPTS) \
Expand Down
130 changes: 130 additions & 0 deletions t/python/t1001-scrub-old-jobs.py
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

0 comments on commit b98595b

Please sign in to comment.