Skip to content

Commit

Permalink
API: libcrmcommon: add pcmk_set_scheduler_cib()
Browse files Browse the repository at this point in the history
This is intended to replace direct access to scheduler->input
  • Loading branch information
kgaillot committed May 1, 2024
1 parent d6ca6f6 commit 810e858
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 3 deletions.
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,7 @@ AC_CONFIG_FILES(Makefile \
lib/common/tests/resources/Makefile \
lib/common/tests/results/Makefile \
lib/common/tests/rules/Makefile \
lib/common/tests/scheduler/Makefile \
lib/common/tests/schemas/Makefile \
lib/common/tests/scores/Makefile \
lib/common/tests/strings/Makefile \
Expand Down
4 changes: 3 additions & 1 deletion include/crm/common/scheduler.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2023 the Pacemaker project contributors
* Copyright 2004-2024 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
Expand Down Expand Up @@ -246,6 +246,8 @@ extern gboolean was_processing_error;
extern gboolean was_processing_warning;
//!@}

int pcmk_set_scheduler_cib(pcmk_scheduler_t *scheduler, xmlNode *cib);

#ifdef __cplusplus
}
#endif
Expand Down
29 changes: 27 additions & 2 deletions lib/common/scheduler.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2023 the Pacemaker project contributors
* Copyright 2004-2024 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
Expand All @@ -9,10 +9,35 @@

#include <crm_internal.h>

#include <glib.h> // gboolean
#include <stdint.h> // uint32_t
#include <errno.h> // EINVAL
#include <glib.h> // gboolean, FALSE
#include <libxml/tree.h> // xmlNode

#include <crm/common/scheduler.h>

uint32_t pcmk__warnings = 0;

gboolean was_processing_error = FALSE;
gboolean was_processing_warning = FALSE;

/*!
* \internal
* \brief Set CIB XML as scheduler input in scheduler data
*
* \param[out] scheduler Scheduler data
* \param[in] cib CIB XML to set as scheduler input
*
* \return Standard Pacemaker return code (EINVAL if \p scheduler is NULL,
* otherwise pcmk_rc_ok)
* \note This will not free any previously set scheduler CIB.
*/
int
pcmk_set_scheduler_cib(pcmk_scheduler_t *scheduler, xmlNode *cib)
{
if (scheduler == NULL) {
return EINVAL;
}
scheduler->input = cib;
return pcmk_rc_ok;
}
1 change: 1 addition & 0 deletions lib/common/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SUBDIRS = \
resources \
results \
rules \
scheduler \
schemas \
scores \
strings \
Expand Down
16 changes: 16 additions & 0 deletions lib/common/tests/scheduler/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright 2024 the Pacemaker project contributors
#
# The version control history for this file may have further details.
#
# This source code is licensed under the GNU General Public License version 2
# or later (GPLv2+) WITHOUT ANY WARRANTY.
#

include $(top_srcdir)/mk/tap.mk
include $(top_srcdir)/mk/unittest.mk

# Add "_test" to the end of all test program names to simplify .gitignore.
check_PROGRAMS = pcmk_set_scheduler_cib_test

TESTS = $(check_PROGRAMS)
71 changes: 71 additions & 0 deletions lib/common/tests/scheduler/pcmk_set_scheduler_cib_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2024 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
* This source code is licensed under the GNU General Public License version 2
* or later (GPLv2+) WITHOUT ANY WARRANTY.
*/

#include <crm_internal.h>

#include <crm/common/scheduler.h>
#include <crm/common/unittest_internal.h>

static void
null_scheduler(void **state)
{
xmlNode *cib = pcmk__xe_create(NULL, "test");

assert_int_equal(pcmk_set_scheduler_cib(NULL, NULL), EINVAL);
assert_int_equal(pcmk_set_scheduler_cib(NULL, cib), EINVAL);

free_xml(cib);
}

static void
null_cib(void **state)
{
pcmk_scheduler_t scheduler = {
.input = NULL,
};

assert_int_equal(pcmk_set_scheduler_cib(&scheduler, NULL), pcmk_rc_ok);
assert_null(scheduler.input);
}

static void
previous_cib_null(void **state)
{
pcmk_scheduler_t scheduler = {
.input = NULL,
};
xmlNode *cib = pcmk__xe_create(NULL, "test");

assert_int_equal(pcmk_set_scheduler_cib(&scheduler, cib), pcmk_rc_ok);
assert_ptr_equal(scheduler.input, cib);

free_xml(cib);
}

static void
previous_cib_nonnull(void **state)
{
xmlNode *old_cib = pcmk__xe_create(NULL, "old");
xmlNode *new_cib = pcmk__xe_create(NULL, "new");
pcmk_scheduler_t scheduler = {
.input = old_cib,
};

assert_int_equal(pcmk_set_scheduler_cib(&scheduler, new_cib), pcmk_rc_ok);
assert_ptr_equal(scheduler.input, new_cib);

free_xml(old_cib);
free_xml(new_cib);
}

PCMK__UNIT_TEST(NULL, NULL,
cmocka_unit_test(null_scheduler),
cmocka_unit_test(null_cib),
cmocka_unit_test(previous_cib_null),
cmocka_unit_test(previous_cib_nonnull))

0 comments on commit 810e858

Please sign in to comment.