From 7ad1f14c6513463c5ac5c03acceb3fff8f1284c8 Mon Sep 17 00:00:00 2001 From: Christopher Moussa Date: Wed, 2 Mar 2022 10:55:19 -0800 Subject: [PATCH] test: add tests for plugin_factor commands --- .../python/fluxacct/accounting/Makefile.am | 3 +- .../test/test_plugin_factor_subcommands.py | 68 ++++++++++++++ t/Makefile.am | 3 +- t/t1007-flux-account.t | 22 +++++ t/t1015-mf-priority-weights.t | 92 +++++++++++++++++++ 5 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 src/bindings/python/fluxacct/accounting/test/test_plugin_factor_subcommands.py create mode 100755 t/t1015-mf-priority-weights.t diff --git a/src/bindings/python/fluxacct/accounting/Makefile.am b/src/bindings/python/fluxacct/accounting/Makefile.am index fdfffb13..c1f56df3 100644 --- a/src/bindings/python/fluxacct/accounting/Makefile.am +++ b/src/bindings/python/fluxacct/accounting/Makefile.am @@ -17,7 +17,8 @@ TESTSCRIPTS = \ test/test_example.py \ test/test_job_archive_interface.py \ test/test_user_subcommands.py \ - test/test_queue_subcommands.py + test/test_queue_subcommands.py \ + test/test_plugin_factor_subcommands.py dist_check_SCRIPTS = \ $(TESTSCRIPTS) diff --git a/src/bindings/python/fluxacct/accounting/test/test_plugin_factor_subcommands.py b/src/bindings/python/fluxacct/accounting/test/test_plugin_factor_subcommands.py new file mode 100644 index 00000000..4cb0024d --- /dev/null +++ b/src/bindings/python/fluxacct/accounting/test/test_plugin_factor_subcommands.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 + +############################################################### +# Copyright 2020 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 plugin_factor_subcommands as p + + +class TestAccountingCLI(unittest.TestCase): + @classmethod + def setUpClass(self): + # create test accounting database + c.create_db("TestPluginFactorSubcommands.db") + global acct_conn + global cur + + acct_conn = sqlite3.connect("TestPluginFactorSubcommands.db") + cur = acct_conn.cursor() + + # edit the weight for the fairshare factor + def test_01_edit_fairshare_factor_successfully(self): + p.edit_factor(acct_conn, factor="fairshare", weight=1500) + cur.execute("SELECT weight FROM plugin_factor_table WHERE factor='fairshare'") + row = cur.fetchone() + + self.assertEqual(row[0], 1500) + + # edit the weight for the queue factor + def test_02_edit_queue_factor_successfully(self): + p.edit_factor(acct_conn, factor="queue", weight=200) + cur.execute("SELECT weight FROM plugin_factor_table WHERE factor='queue'") + row = cur.fetchone() + + self.assertEqual(row[0], 200) + + # try to edit a factor with a bad type + def test_03_edit_factor_bad_type(self): + with self.assertRaises(ValueError): + p.edit_factor(acct_conn, factor="fairshare", weight="foo") + + # remove database and log file + @classmethod + def tearDownClass(self): + acct_conn.close() + os.remove("TestPluginFactorSubcommands.db") + + +def suite(): + suite = unittest.TestSuite() + + return suite + + +if __name__ == "__main__": + from pycotap import TAPTestRunner + + unittest.main(testRunner=TAPTestRunner()) diff --git a/t/Makefile.am b/t/Makefile.am index 1252baea..e01b9c6f 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -15,7 +15,8 @@ TESTSCRIPTS = \ t1011-job-archive-interface.t \ t1012-mf-priority-load.t \ t1013-mf-priority-queues.t \ - t1014-mf-priority-dne.t + t1014-mf-priority-dne.t \ + t1015-mf-priority-weights.t dist_check_SCRIPTS = \ $(TESTSCRIPTS) \ diff --git a/t/t1007-flux-account.t b/t/t1007-flux-account.t index a6a9d7fe..17f0e007 100755 --- a/t/t1007-flux-account.t +++ b/t/t1007-flux-account.t @@ -167,6 +167,28 @@ test_expect_success 'remove a queue from the queue_table' ' grep "Queue not found in queue_table" deleted_queue.out ' +test_expect_success 'view plugin factor information' ' + flux account -p ${DB_PATH} view-plugin-factor fairshare > fshare_factor.test && + grep "fairshare 100000" fshare_factor.test +' + +test_expect_success 'edit a plugin factor successfully' ' + flux account -p ${DB_PATH} edit-plugin-factor fairshare --weight=1234 && + flux account -p ${DB_PATH} view-plugin-factor fairshare > fshare_factor_edited.test && + grep "fairshare 1234" fshare_factor_edited.test +' + +test_expect_success 'try to edit a plugin factor with a bad type' ' + test_must_fail flux account -p ${DB_PATH} edit-plugin-factor fairshare --weight=foo > bad_type.out 2>&1 && + test_debug "bad_type.out" && + grep "edit-plugin-factor: error: argument --weight: invalid int value:" bad_type.out +' + +test_expect_success 'try to view a plugin factor that does not exist' ' + flux account -p ${DB_PATH} view-plugin-factor foo > no_such_factor.out && + grep "Factor not found in plugin_factor_table" no_such_factor.out +' + test_expect_success 'remove flux-accounting DB' ' rm $(pwd)/FluxAccountingTest.db ' diff --git a/t/t1015-mf-priority-weights.t b/t/t1015-mf-priority-weights.t new file mode 100755 index 00000000..de778aad --- /dev/null +++ b/t/t1015-mf-priority-weights.t @@ -0,0 +1,92 @@ +#!/bin/bash + +test_description='Test configuring plugin weights and their effects on priority calculation' + +. `dirname $0`/sharness.sh +MULTI_FACTOR_PRIORITY=${FLUX_BUILD_DIR}/src/plugins/.libs/mf_priority.so +DB_PATH=$(pwd)/FluxAccountingTest.db + +export TEST_UNDER_FLUX_NO_JOB_EXEC=y +export TEST_UNDER_FLUX_SCHED_SIMPLE_MODE="limited=1" +test_under_flux 1 job + +flux setattr log-stderr-level 1 + +test_expect_success 'load multi-factor priority plugin' ' + flux jobtap load -r .priority-default ${MULTI_FACTOR_PRIORITY} +' + +test_expect_success 'check that mf_priority plugin is loaded' ' + flux jobtap list | grep mf_priority +' + +test_expect_success 'create flux-accounting DB' ' + flux account -p $(pwd)/FluxAccountingTest.db create-db +' + +test_expect_success 'add some banks to the DB' ' + flux account -p ${DB_PATH} add-bank root 1 && + flux account -p ${DB_PATH} add-bank --parent-bank=root account1 1 +' + +test_expect_success 'add a default queue to the DB' ' + flux account -p ${DB_PATH} add-queue default --priority=100 +' + +test_expect_success 'add a user to the DB' ' + username=$(whoami) && + uid=$(id -u) && + flux account -p ${DB_PATH} add-user --username=$username --userid=$uid --bank=account1 --shares=1 +' + +test_expect_success 'view queue information' ' + flux account -p ${DB_PATH} view-plugin-factor fairshare > fshare_weight.test && + grep "fairshare 100000" fshare_weight.test && + flux account -p ${DB_PATH} view-plugin-factor queue > queue_weight.test && + grep "queue 10000" queue_weight.test +' + +test_expect_success 'send the user, queue, and plugin factor weight information to the plugin' ' + flux account-priority-update -p $(pwd)/FluxAccountingTest.db +' + +test_expect_success 'submit a job using default fshare and queue factor weights' ' + jobid1=$(flux mini submit -n1 hostname) && + flux job wait-event -f json $jobid1 priority | jq '.context.priority' > job1.test && + cat <<-EOF >job1.expected && + 1050000 + EOF + test_cmp job1.expected job1.test +' + +test_expect_success 'edit plugin factor weights to give fairshare all the weight' ' + flux account -p ${DB_PATH} edit-plugin-factor fairshare --weight=1000 && + flux account -p ${DB_PATH} edit-plugin-factor queue --weight=0 && + flux account-priority-update -p $(pwd)/FluxAccountingTest.db +' + +test_expect_success 'submit a job using the new fshare and queue factor weights' ' + jobid2=$(flux mini submit -n1 hostname) && + flux job wait-event -f json $jobid2 priority | jq '.context.priority' > job2.test && + cat <<-EOF >job2.expected && + 500 + EOF + test_cmp job2.expected job2.test +' + +test_expect_success 'edit plugin factor weights to give queue all the weight' ' + flux account -p ${DB_PATH} edit-plugin-factor fairshare --weight=0 && + flux account -p ${DB_PATH} edit-plugin-factor queue --weight=1000 && + flux account-priority-update -p $(pwd)/FluxAccountingTest.db +' + +test_expect_success 'submit a job using the new fshare and queue factor weights' ' + jobid3=$(flux mini submit -n1 hostname) && + flux job wait-event -f json $jobid3 priority | jq '.context.priority' > job3.test && + cat <<-EOF >job3.expected && + 100000 + EOF + test_cmp job3.expected job3.test +' + +test_done