From 3995adc8cd98badb7b549de4da0aa197181d1ce5 Mon Sep 17 00:00:00 2001 From: "Mark A. Grondona" Date: Mon, 17 Jun 2024 16:54:23 -0700 Subject: [PATCH] job-list/test: add unit tests for ranks constraint Problem: There are no unit tests for the 'ranks' constraint operator. Add them. --- src/modules/job-list/test/match.c | 172 ++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) diff --git a/src/modules/job-list/test/match.c b/src/modules/job-list/test/match.c index 6c2f0307fecd..b69cdfdbba50 100644 --- a/src/modules/job-list/test/match.c +++ b/src/modules/job-list/test/match.c @@ -103,6 +103,7 @@ static struct job *setup_job (uint32_t userid, const char *name, const char *queue, const char *nodelist, + const char *ranks, flux_job_state_t state, flux_job_result_t result, double t_submit, @@ -125,6 +126,11 @@ static struct job *setup_job (uint32_t userid, if (!(job->nodelist = strdup (nodelist))) BAIL_OUT ("failed to strdup nodelist"); } + if (ranks) { + /* N.B. internally is not const, so strdup it */ + if (!(job->ranks = strdup (ranks))) + BAIL_OUT ("failed to strdup ranks"); + } job->state = state; if (state) { /* Assume all jobs run, we don't skip any states, so add bitmask @@ -172,6 +178,7 @@ static void test_basic_special_cases (void) NULL, NULL, NULL, + NULL, 0, 0, 0.0, @@ -265,6 +272,7 @@ static void test_basic_userid (void) NULL, NULL, NULL, + NULL, 0, 0, 0.0, @@ -353,6 +361,7 @@ static void test_basic_name (void) tests->name, NULL, NULL, + NULL, 0, 0, 0.0, @@ -441,6 +450,7 @@ static void test_basic_queue (void) NULL, tests->queue, NULL, + NULL, 0, 0, 0.0, @@ -533,6 +543,7 @@ static void test_basic_states (void) NULL, NULL, NULL, + NULL, tests->state, 0, 0.0, @@ -628,6 +639,7 @@ static void test_basic_results (void) NULL, NULL, NULL, + NULL, tests->state, tests->result, 0.0, @@ -755,6 +767,7 @@ static void test_basic_hostlist (void) NULL, NULL, tests->nodelist, + NULL, 0, 0, 0.0, @@ -777,6 +790,115 @@ static void test_basic_hostlist (void) } } + +struct basic_ranks_test { + const char *ranks; + bool expected; + bool end; /* ranks can be NULL */ +}; + +struct basic_ranks_constraint_test { + const char *constraint; + struct basic_ranks_test tests[9]; +} basic_ranks_tests[] = { + { + "{ \"ranks\": [ ] }", + { + /* N.B. ranks can potentially be NULL */ + { NULL, false, false, }, + { NULL, false, true, }, + }, + }, + { + "{ \"ranks\": [ \"1\" ] }", + { + /* N.B. ranks can potentially be NULL */ + { NULL, false, false, }, + { "1", true, false, }, + { "2", false, false, }, + { "1-2", true, false, }, + { "2-3", false, false, }, + { NULL, false, true, }, + }, + }, + { + "{ \"ranks\": [ \"1-2\" ] }", + { + /* N.B. ranks can potentially be NULL */ + { NULL, false, false, }, + { "1", true, false, }, + { "2", true, false, }, + { "1-2", true, false, }, + { "2-3", true, false, }, + { "3-4", false, false, }, + { NULL, false, true, }, + }, + }, + { + "{ \"ranks\": [ \"1\", \"2\", \"3\" ] }", + { + /* N.B. ranks can potentially be NULL */ + { NULL, false, false, }, + { "1", true, false, }, + { "2", true, false, }, + { "1-2", true, false, }, + { "2-3", true, false, }, + { "3-4", true, false, }, + { "4", false, false, }, + { "4-5", false, false, }, + { NULL, false, true, }, + }, + }, + { + NULL, + { + { NULL, false, true, }, + }, + }, +}; + +static void test_basic_ranks (void) +{ + struct basic_ranks_constraint_test *ctests = basic_ranks_tests; + int index = 0; + + while (ctests->constraint) { + struct basic_ranks_test *tests = ctests->tests; + struct list_constraint *c; + int index2 = 0; + + c = create_list_constraint (ctests->constraint); + while (!tests->end) { + struct job *job; + flux_error_t error; + int rv; + job = setup_job (0, + NULL, + NULL, + NULL, + tests->ranks, + 0, + 0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0); + rv = job_match (job, c, &error); + ok (rv == tests->expected, + "basic rank job match test #%d/#%d job->ranks = %s (expected %d got %d)", + index, index2, tests->ranks, tests->expected, rv); + job_destroy (job); + index2++; + tests++; + } + + index++; + list_constraint_destroy (c); + ctests++; + } +} + struct basic_timestamp_test { flux_job_state_t state; int submit_version; @@ -1056,6 +1178,7 @@ static void test_basic_timestamp (void) NULL, NULL, NULL, + NULL, tests->state, 0, tests->t_submit, @@ -1264,6 +1387,7 @@ static void test_basic_conditionals (void) tests->name, NULL, NULL, + NULL, 0, 0, 0.0, @@ -1292,6 +1416,7 @@ struct realworld_test { const char *name; const char *queue; const char *nodelist; + const char *ranks; flux_job_state_t state; flux_job_result_t result; double t_run; @@ -1317,6 +1442,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_DEPEND, 0, 0.0, @@ -1328,6 +1454,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_RUN, 0, 0.0, @@ -1339,6 +1466,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 0.0, @@ -1350,6 +1478,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 0.0, @@ -1361,6 +1490,7 @@ struct realworld_constraint_test { NULL, NULL, NULL, + NULL, 0, 0, 0.0, @@ -1383,6 +1513,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_FAILED, 0.0, @@ -1394,6 +1525,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_CANCELED, 0.0, @@ -1405,6 +1537,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_TIMEOUT, 0.0, @@ -1416,6 +1549,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_FAILED, 0.0, @@ -1427,6 +1561,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_DEPEND, 0, 0.0, @@ -1438,6 +1573,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_RUN, 0, 0.0, @@ -1449,6 +1585,7 @@ struct realworld_constraint_test { NULL, NULL, NULL, + NULL, 0, 0, 0.0, @@ -1472,6 +1609,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_DEPEND, 0, 0.0, @@ -1483,6 +1621,7 @@ struct realworld_constraint_test { "foo", "debug", NULL, + NULL, FLUX_JOB_STATE_DEPEND, 0, 0.0, @@ -1494,6 +1633,7 @@ struct realworld_constraint_test { "foo", "debug", NULL, + NULL, FLUX_JOB_STATE_RUN, 0, 0.0, @@ -1505,6 +1645,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_DEPEND, 0, 0.0, @@ -1516,6 +1657,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 0.0, @@ -1527,6 +1669,7 @@ struct realworld_constraint_test { "foo", "gpu", NULL, + NULL, FLUX_JOB_STATE_DEPEND, 0, 0.0, @@ -1538,6 +1681,7 @@ struct realworld_constraint_test { NULL, NULL, NULL, + NULL, 0, 0, 0.0, @@ -1562,6 +1706,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_RUN, 0, 0.0, @@ -1573,6 +1718,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_CLEANUP, 0, 0.0, @@ -1584,6 +1730,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_RUN, 0, 0.0, @@ -1595,6 +1742,7 @@ struct realworld_constraint_test { "foo", "debug", NULL, + NULL, FLUX_JOB_STATE_RUN, 0, 0.0, @@ -1606,6 +1754,7 @@ struct realworld_constraint_test { "bar", "batch", NULL, + NULL, FLUX_JOB_STATE_RUN, 0, 0.0, @@ -1617,6 +1766,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 0.0, @@ -1628,6 +1778,7 @@ struct realworld_constraint_test { NULL, NULL, NULL, + NULL, 0, 0, 0.0, @@ -1650,6 +1801,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_SCHED, 0, 0.0, @@ -1661,6 +1813,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_RUN, 0, 0.0, @@ -1672,6 +1825,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 0.0, @@ -1683,6 +1837,7 @@ struct realworld_constraint_test { "foo", "batch", NULL, + NULL, FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 0.0, @@ -1694,6 +1849,7 @@ struct realworld_constraint_test { NULL, NULL, NULL, + NULL, 0, 0, 0.0, @@ -1716,6 +1872,7 @@ struct realworld_constraint_test { "foo", "batch", "node[1-3]", + "0-2", FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 0.0, @@ -1727,6 +1884,7 @@ struct realworld_constraint_test { "foo", "batch", "node[1-3]", + "0-2", FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 0.0, @@ -1738,6 +1896,7 @@ struct realworld_constraint_test { "foo", "batch", "node[2-4]", + "1-3", FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 0.0, @@ -1749,6 +1908,7 @@ struct realworld_constraint_test { "foo", "batch", "node[3-4]", + "2-3", FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 0.0, @@ -1760,6 +1920,7 @@ struct realworld_constraint_test { NULL, NULL, NULL, + NULL, 0, 0, 0.0, @@ -1784,6 +1945,7 @@ struct realworld_constraint_test { "foo", "batch", "node[1-3]", + "0-2", FLUX_JOB_STATE_RUN, 0, 1000.0, @@ -1795,6 +1957,7 @@ struct realworld_constraint_test { "foo", "batch", "node[1-3]", + "0-2", FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 1000.0, @@ -1806,6 +1969,7 @@ struct realworld_constraint_test { "foo", "batch", "node[2-3]", + "1-2", FLUX_JOB_STATE_RUN, 0, 1000.0, @@ -1817,6 +1981,7 @@ struct realworld_constraint_test { "foo", "batch", "node[2-3]", + "1-2", FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 1000.0, @@ -1828,6 +1993,7 @@ struct realworld_constraint_test { "foo", "batch", "node[2-3]", + "1-2", FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 1000.0, @@ -1839,6 +2005,7 @@ struct realworld_constraint_test { "foo", "batch", "node[3-4]", + "2-3", FLUX_JOB_STATE_RUN, 0, 1000.0, @@ -1850,6 +2017,7 @@ struct realworld_constraint_test { "foo", "batch", "node[3-4]", + "2-3", FLUX_JOB_STATE_INACTIVE, FLUX_JOB_RESULT_COMPLETED, 1000.0, @@ -1861,6 +2029,7 @@ struct realworld_constraint_test { NULL, NULL, NULL, + NULL, 0, 0, 0.0, @@ -1877,6 +2046,7 @@ struct realworld_constraint_test { NULL, NULL, NULL, + NULL, 0, 0, 0.0, @@ -1906,6 +2076,7 @@ static void test_realworld (void) tests->name, tests->queue, tests->nodelist, + tests->ranks, tests->state, tests->result, 0.0, @@ -1941,6 +2112,7 @@ int main (int argc, char *argv[]) test_basic_results (); test_corner_case_hostlist (); test_basic_hostlist (); + test_basic_ranks (); test_basic_timestamp (); test_basic_conditionals (); test_realworld ();