|
| 1 | +/************************************************************\ |
| 2 | + * Copyright 2024 Lawrence Livermore National Security, LLC |
| 3 | + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) |
| 4 | + * |
| 5 | + * This file is part of the Flux resource manager framework. |
| 6 | + * For details, see https://github.com/flux-framework. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: LGPL-3.0 |
| 9 | +\************************************************************/ |
| 10 | + |
| 11 | +#if HAVE_CONFIG_H |
| 12 | +#include "config.h" |
| 13 | +#endif |
| 14 | +#include <jansson.h> |
| 15 | +#include <errno.h> |
| 16 | +#include <string.h> |
| 17 | + |
| 18 | +#include <flux/core.h> |
| 19 | + |
| 20 | +#include "src/common/libtap/tap.h" |
| 21 | +#include "ccan/str/str.h" |
| 22 | + |
| 23 | +#include "src/modules/resource/drainset.h" |
| 24 | + |
| 25 | +static void check_drainset (struct drainset *ds, |
| 26 | + const char *json_str) |
| 27 | +{ |
| 28 | + char *s; |
| 29 | + json_t *o = drainset_to_json (ds); |
| 30 | + json_t *expected = json_loads (json_str, 0, NULL); |
| 31 | + if (!o || !expected) |
| 32 | + BAIL_OUT ("drainset_to_json failed"); |
| 33 | + if (!(s = json_dumps (o, JSON_COMPACT))) |
| 34 | + BAIL_OUT ("json_dumps failed"); |
| 35 | + diag ("drainset_to_json = %s", s); |
| 36 | + diag ("expected = %s", json_str); |
| 37 | + ok (json_equal (expected, o), |
| 38 | + "drainset_to_json got expected result"); |
| 39 | + json_decref (expected); |
| 40 | + json_decref (o); |
| 41 | + free (s); |
| 42 | +} |
| 43 | + |
| 44 | +static void test_empty () |
| 45 | +{ |
| 46 | + struct drainset *ds = drainset_create (); |
| 47 | + if (!ds) |
| 48 | + BAIL_OUT ("drainset_create failed"); |
| 49 | + diag ("empty drainset should return empty JSON object"); |
| 50 | + check_drainset (ds, "{}"); |
| 51 | + drainset_destroy (ds); |
| 52 | +} |
| 53 | + |
| 54 | +static void test_basic () |
| 55 | +{ |
| 56 | + struct drainset *ds = drainset_create (); |
| 57 | + if (!ds) |
| 58 | + BAIL_OUT ("drainset_create failed"); |
| 59 | + |
| 60 | + ok (drainset_drain_rank (NULL, 0, 1234.0, NULL) < 0 && errno == EINVAL, |
| 61 | + "drainset_drain_rank (NULL, ...) returns EINVAL"); |
| 62 | + |
| 63 | + for (unsigned int i = 0; i < 8; i++) { |
| 64 | + ok (drainset_drain_rank (ds, i, 1234.0, "test") == 0, |
| 65 | + "drainset_drain_rank: rank=%u", i); |
| 66 | + } |
| 67 | + check_drainset (ds, |
| 68 | + "{\"0-7\":{\"timestamp\":1234.0,\"reason\":\"test\"}}"); |
| 69 | + drainset_destroy (ds); |
| 70 | +} |
| 71 | + |
| 72 | +static void test_multiple () |
| 73 | +{ |
| 74 | + struct drainset *ds = drainset_create (); |
| 75 | + |
| 76 | + if (!ds) |
| 77 | + BAIL_OUT ("drainset_create failed"); |
| 78 | + |
| 79 | + ok (drainset_drain_rank (ds, 0, 1234.0, "test") == 0, |
| 80 | + "drainset_drain_rank: rank=0"); |
| 81 | + ok (drainset_drain_rank (ds, 1, 2345.0, "test") == 0, |
| 82 | + "drainset_drain_rank: rank=1"); |
| 83 | + ok (drainset_drain_rank (ds, 2, 1234.0, "test1") == 0, |
| 84 | + "drainset_drain_rank: rank=1"); |
| 85 | + ok (drainset_drain_rank (ds, 3, 1234.0, "test") == 0, |
| 86 | + "drainset_drain_rank: rank=0"); |
| 87 | + ok (drainset_drain_rank (ds, 4, 1234.0, NULL) == 0, |
| 88 | + "drainset_drain_rank: rank=1"); |
| 89 | + |
| 90 | + check_drainset (ds, |
| 91 | + "{\"0,3\":{\"timestamp\":1234.0,\"reason\":\"test\"}," |
| 92 | + "\"1\":{\"timestamp\":2345.0,\"reason\":\"test\"}," |
| 93 | + "\"2\":{\"timestamp\":1234.0,\"reason\":\"test1\"}," |
| 94 | + "\"4\":{\"timestamp\":1234.0,\"reason\":\"\"}}"); |
| 95 | + drainset_destroy (ds); |
| 96 | +} |
| 97 | + |
| 98 | +int main (int argc, char *argv[]) |
| 99 | +{ |
| 100 | + plan (NO_PLAN); |
| 101 | + test_empty (); |
| 102 | + test_basic (); |
| 103 | + test_multiple (); |
| 104 | + done_testing (); |
| 105 | + return (0); |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +/* |
| 110 | + * vi:ts=4 sw=4 expandtab |
| 111 | + */ |
0 commit comments