Skip to content

Commit

Permalink
t: add tests for common formatter
Browse files Browse the repository at this point in the history
Problem: flux-accounting does not have tests that use the common
formatter in the Python bindings.

Add some sharness tests for the list-bank command with multiple
customization options for the different fields and getting output as
both a table and as JSON. Add some unit tests for construction of
AccountingFormatter objects and ensuring the correct amount of data
is returned from executing a query.
  • Loading branch information
cmoussa1 committed Oct 22, 2024
1 parent eab605c commit e2ca7ab
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 1 deletion.
4 changes: 3 additions & 1 deletion t/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ TESTSCRIPTS = \
t1040-mf-priority-projects.t \
t1041-view-jobs-by-project.t \
t1042-issue508.t \
t1043-common-formatter.t \
t5000-valgrind.t \
python/t1000-example.py \
python/t1001_db.py \
python/t1002_user_cmds.py \
python/t1003_bank_cmds.py \
python/t1004_queue_cmds.py \
python/t1005_project_cmds.py \
python/t1006_job_archive.py
python/t1006_job_archive.py \
python/t1007_formatter.py

dist_check_SCRIPTS = \
$(TESTSCRIPTS) \
Expand Down
74 changes: 74 additions & 0 deletions t/python/t1007_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/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

from fluxacct.accounting import create_db as c
from fluxacct.accounting import bank_subcommands as b
from fluxacct.accounting import formatter as fmt


class TestAccountingCLI(unittest.TestCase):
@classmethod
def setUpClass(self):
# create test accounting database
c.create_db("TestFormatter.db")
global conn
global cur

conn = sqlite3.connect("TestFormatter.db")
cur = conn.cursor()

# initialize Formatter object with no data in table
def test_AccountingFormatter_empty(self):
cur.execute("SELECT * FROM bank_table")
formatter = fmt.AccountingFormatter(cur)

self.assertIsInstance(formatter, fmt.AccountingFormatter)

# add some data to a table and re-initialize formatter
def test_AccountingFormatter_with_banks(self):
b.add_bank(conn, bank="root", shares=1)
b.add_bank(conn, bank="A", shares=1, parent_bank="root")
b.add_bank(conn, bank="B", shares=1, parent_bank="root")
b.add_bank(conn, bank="C", shares=1, parent_bank="root")

cur.execute("SELECT * FROM bank_table")
formatter = fmt.AccountingFormatter(cur)

self.assertIsInstance(formatter, fmt.AccountingFormatter)

# ensure formatter has the correct number of rows
def test_formatter_get_rows(self):
cur.execute("SELECT * FROM bank_table")
formatter = fmt.AccountingFormatter(cur)

self.assertEqual(len(formatter.get_rows()), 4)

# remove database and log file
@classmethod
def tearDownClass(self):
conn.close()
os.remove("TestFormatter.db")


def suite():
suite = unittest.TestSuite()

return suite


if __name__ == "__main__":
from pycotap import TAPTestRunner

unittest.main(testRunner=TAPTestRunner())
80 changes: 80 additions & 0 deletions t/t1043-common-formatter.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

test_description='test using common formatter for Python bindings'

. `dirname $0`/sharness.sh

mkdir -p conf.d

ACCOUNTING_DB=$(pwd)/FluxAccountingTest.db

export TEST_UNDER_FLUX_SCHED_SIMPLE_MODE="limited=1"
test_under_flux 1 job -o,--config-path=$(pwd)/conf.d

flux setattr log-stderr-level 1

test_expect_success 'create flux-accounting DB, start flux-accounting service' '
flux account -p ${ACCOUNTING_DB} create-db &&
flux account-service -p ${ACCOUNTING_DB} -t
'

test_expect_success 'call list-banks with no data in bank_table' '
flux account list-banks
'

test_expect_success 'add some banks' '
flux account add-bank root 1 &&
flux account add-bank --parent-bank=root A 1 &&
flux account add-bank --parent-bank=root B 1 &&
flux account add-bank --parent-bank=root C 1
'

test_expect_success 'list banks in table format' '
flux account list-banks
'

test_expect_success 'bad field will result in ValueError' '
test_must_fail flux account list-banks --fields=foo > error.out 2>&1 &&
grep "invalid fields: foo" error.out
'

test_expect_success 'customize output in table format' '
flux account list-banks --fields=bank_id &&
flux account list-banks --fields=bank_id,bank &&
flux account list-banks --fields=bank_id,bank,active &&
flux account list-banks --fields=bank_id,bank,active,parent_bank &&
flux account list-banks --fields=bank_id,bank,active,parent_bank,shares &&
flux account list-banks --fields=bank_id,bank,active,parent_bank,shares,job_usage
'

test_expect_success 'include inactive banks in table format' '
flux account delete-bank A &&
flux account list-banks --inactive
'

test_expect_success 'list banks in JSON format' '
flux account list-banks --json
'

test_expect_success 'customize output in JSON format' '
flux account list-banks --json --fields=bank_id &&
flux account list-banks --json --fields=bank_id,bank &&
flux account list-banks --json --fields=bank_id,bank,active &&
flux account list-banks --json --fields=bank_id,bank,active,parent_bank &&
flux account list-banks --json --fields=bank_id,bank,active,parent_bank,shares &&
flux account list-banks --json --fields=bank_id,bank,active,parent_bank,shares,job_usage
'

test_expect_success 'include inactive banks in JSON format' '
flux account list-banks --json --inactive
'

test_expect_success 'shut down flux-accounting service' '
flux python -c "import flux; flux.Flux().rpc(\"accounting.shutdown_service\").get()"
'

test_expect_success 'remove flux-accounting DB' '
rm ${ACCOUNTING_DB}
'

test_done

0 comments on commit e2ca7ab

Please sign in to comment.