-
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: 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
Showing
3 changed files
with
157 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,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()) |
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,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 |