diff --git a/devel/Makefile.am b/devel/Makefile.am
index 94581e1e9e..b4b211d6a1 100644
--- a/devel/Makefile.am
+++ b/devel/Makefile.am
@@ -1,295 +1,300 @@
#
# Copyright 2020-2023 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/common.mk
include $(top_srcdir)/mk/release.mk
# Coccinelle is a tool that takes special patch-like files (called semantic patches) and
# applies them throughout a source tree. This is useful when refactoring, changing APIs,
# catching dangerous or incorrect code, and other similar tasks. It's not especially
# easy to write a semantic patch but most users should only be concerned about running
# the target and inspecting the results.
#
# Documentation (including examples, which are the most useful):
# https://coccinelle.gitlabpages.inria.fr/website/docs/
#
# Run the "make cocci" target to just output what would be done, or "make cocci-inplace"
# to apply the changes to the source tree.
#
# COCCI_FILES may be set on the command line, if you want to test just a single file
# while it's under development. Otherwise, it is a list of all the files that are ready
# to be run.
#
# ref-passed-variables-inited.cocci seems to be returning some false positives around
# GHashTableIters, so it is disabled for the moment.
COCCI_FILES ?= coccinelle/string-any-of.cocci \
coccinelle/string-empty.cocci \
coccinelle/string-null-matches.cocci \
coccinelle/use-func.cocci
dist_noinst_SCRIPTS = coccinelle/test/testrunner.sh
EXTRA_DIST = README gdbhelpers $(COCCI_FILES) \
coccinelle/ref-passed-variables-inited.cocci \
coccinelle/rename-fn.cocci \
coccinelle/test/ref-passed-variables-inited.input.c \
coccinelle/test/ref-passed-variables-inited.output
# Any file in this list is allowed to use any of the pcmk__ internal functions.
# Coccinelle can use any transformation that depends on "internal" to rewrite
# code to use the internal functions.
MAY_USE_INTERNAL_FILES = $(shell find .. -path "../lib/*.c" -o -path "../lib/*private.h" -o -path "../tools/*.c" -o -path "../daemons/*.c" -o -path '../include/pcmki/*h' -o -name '*internal.h')
# And then any file in this list is public API, which may not use internal
# functions. Thus, only those transformations that do not depend on "internal"
# may be applied.
OTHER_FILES = $(shell find ../include -name '*h' -a \! -name '*internal.h' -a \! -path '../include/pcmki/*')
cocci:
-for cf in $(COCCI_FILES); do \
for f in $(MAY_USE_INTERNAL_FILES); do \
spatch $(_SPATCH_FLAGS) -D internal --very-quiet --local-includes --preprocess --sp-file $$cf $$f; \
done ; \
for f in $(OTHER_FILES); do \
spatch $(_SPATCH_FLAGS) --very-quiet --local-includes --preprocess --sp-file $$cf $$f; \
done ; \
done
cocci-inplace:
$(MAKE) $(AM_MAKEFLAGS) _SPATCH_FLAGS=--in-place cocci
cocci-test:
for f in coccinelle/test/*.c; do \
coccinelle/test/testrunner.sh $$f; \
done
#
# Static analysis
#
## clang
# See scan-build(1) for possible checkers (leave empty to use default set)
CLANG_checkers ?=
clang:
OUT=$$(cd $(top_builddir) \
&& scan-build $(CLANG_checkers:%=-enable-checker %) \
$(MAKE) $(AM_MAKEFLAGS) CFLAGS="-std=c99 $(CFLAGS)" \
clean all 2>&1); \
REPORT=$$(echo "$$OUT" \
| sed -n -e "s/.*'scan-view \(.*\)'.*/\1/p"); \
[ -z "$$REPORT" ] && echo "$$OUT" || scan-view "$$REPORT"
## coverity
# Aggressiveness (low, medium, or high)
COVLEVEL ?= low
# Generated outputs
COVERITY_DIR = $(abs_top_builddir)/coverity-$(TAG)
COVTAR = $(abs_top_builddir)/$(PACKAGE)-coverity-$(TAG).tgz
COVEMACS = $(abs_top_builddir)/$(TAG).coverity
COVHTML = $(COVERITY_DIR)/output/errors
# Coverity outputs are phony so they get rebuilt every invocation
.PHONY: $(COVERITY_DIR)
$(COVERITY_DIR): coverity-clean
$(MAKE) $(AM_MAKEFLAGS) -C $(top_builddir) init core-clean
$(AM_V_GEN)cd $(top_builddir) \
&& cov-build --dir "$@" $(MAKE) $(AM_MAKEFLAGS) core
# Public coverity instance
.PHONY: $(COVTAR)
$(COVTAR): $(COVERITY_DIR)
$(AM_V_GEN)tar czf "$@" --transform="s@.*$(TAG)@cov-int@" "$<"
.PHONY: coverity
coverity: $(COVTAR)
@echo "Now go to https://scan.coverity.com/users/sign_in and upload:"
@echo " $(COVTAR)"
@echo "then make clean at the top level"
# Licensed coverity instance
#
# The prerequisites are a little hacky; rather than actually required, some
# of them are designed so that things execute in the proper order (which is
# not the same as GNU make's order-only prerequisites).
.PHONY: coverity-analyze
coverity-analyze: $(COVERITY_DIR)
@echo ""
@echo "Analyzing (waiting for coverity license if necessary) ..."
cd $(top_builddir) && cov-analyze --dir "$<" --wait-for-license \
--security --aggressiveness-level "$(COVLEVEL)"
.PHONY: $(COVEMACS)
$(COVEMACS): coverity-analyze
$(AM_V_GEN)cd $(top_builddir) \
&& cov-format-errors --dir "$(COVERITY_DIR)" --emacs-style > "$@"
.PHONY: $(COVHTML)
$(COVHTML): $(COVEMACS)
$(AM_V_GEN)cd $(top_builddir) \
&& cov-format-errors --dir "$(COVERITY_DIR)" --html-output "$@"
.PHONY: coverity-corp
coverity-corp: $(COVHTML)
$(MAKE) $(AM_MAKEFLAGS) -C $(top_builddir) core-clean
@echo "Done. See:"
@echo " file://$(COVHTML)/index.html"
@echo "When no longer needed, make coverity-clean"
# Remove all outputs regardless of tag
.PHONY: coverity-clean
coverity-clean:
-rm -rf "$(abs_builddir)"/coverity-* \
"$(abs_builddir)"/$(PACKAGE)-coverity-*.tgz \
"$(abs_builddir)"/*.coverity
## cppcheck
# Use CPPCHECK_ARGS to pass extra cppcheck options, e.g.:
# --enable={warning,style,performance,portability,information,all}
# --inconclusive --std=posix
# -DBUILD_PUBLIC_LIBPACEMAKER -DDEFAULT_CONCURRENT_FENCING_TRUE
CPPCHECK_ARGS ?=
CPPCHECK_DIRS = replace lib daemons tools
CPPCHECK_OUT = $(abs_top_builddir)/cppcheck.out
cppcheck:
cppcheck $(CPPCHECK_ARGS) -I $(top_srcdir)/include \
--output-file=$(CPPCHECK_OUT) \
--max-configs=30 --inline-suppr -q \
--library=posix --library=gnu --library=gtk \
$(GLIB_CFLAGS) -D__GNUC__ \
$(foreach dir,$(CPPCHECK_DIRS),$(top_srcdir)/$(dir))
@echo "Done: See $(CPPCHECK_OUT)"
@echo "When no longer needed, make cppcheck-clean"
.PHONY: cppcheck-clean
cppcheck-clean:
-rm -f "$(CPPCHECK_OUT)"
#
# Coverage/profiling
#
COVERAGE_DIR = $(top_builddir)/coverage
# Check coverage of unit tests
.PHONY: coverage
coverage: coverage-partial-clean
cd $(top_builddir) \
- && $(MAKE) $(AM_MAKEFLAGS) core \
+ && $(MAKE) $(AM_MAKEFLAGS) \
&& lcov --no-external --exclude='*_test.c' -c -i -d . \
-o pacemaker_base.info \
&& $(MAKE) $(AM_MAKEFLAGS) check \
&& lcov --no-external --exclude='*_test.c' -c -d . \
-o pacemaker_test.info \
&& lcov -a pacemaker_base.info -a pacemaker_test.info \
- -o pacemaker_total.info
- genhtml $(top_builddir)/pacemaker_total.info -o $(COVERAGE_DIR) -s
+ -o pacemaker_total.info \
+ && lcov --remove pacemaker_total.info -o pacemaker_filtered.info\
+ "$(abs_top_builddir)/tools/*" \
+ "$(abs_top_builddir)/daemons/*/*" \
+ "$(abs_top_builddir)/replace/*" \
+ "$(abs_top_builddir)/lib/gnu/*"
+ genhtml $(top_builddir)/pacemaker_filtered.info -o $(COVERAGE_DIR) -s -t "Pacemaker code coverage"
# Check coverage of CLI regression tests
.PHONY: coverage-cts
coverage-cts: coverage-partial-clean
cd $(top_builddir) \
- && $(MAKE) $(AM_MAKEFLAGS) core \
+ && $(MAKE) $(AM_MAKEFLAGS) \
&& lcov --no-external -c -i -d tools -o pacemaker_base.info \
&& cts/cts-cli \
&& lcov --no-external -c -d tools -o pacemaker_test.info \
&& lcov -a pacemaker_base.info -a pacemaker_test.info \
-o pacemaker_total.info
genhtml $(top_builddir)/pacemaker_total.info -o $(COVERAGE_DIR) -s
# Remove coverage-related files that aren't needed across runs
.PHONY: coverage-partial-clean
coverage-partial-clean:
-rm -f $(top_builddir)/pacemaker_*.info
-rm -rf $(COVERAGE_DIR)
-find $(top_builddir) -name "*.gcda" -exec rm -f \{\} \;
# This target removes all coverage-related files. It is only to be run when
# done with coverage analysis and you are ready to go back to normal development,
# starting with re-running ./configure. It is not to be run in between
# "make coverage" runs.
#
# In particular, the *.gcno files are generated when the source is built.
# Removing those files will break "make coverage" until the whole source tree
# has been built and the *.gcno files generated again.
.PHONY: coverage-clean
coverage-clean: coverage-partial-clean
-find $(top_builddir) -name "*.gcno" -exec rm -f \{\} \;
#
# indent cannot cope with all our exceptions and needs heavy manual editing
#
# indent target: Limit indent to these directories
INDENT_DIRS ?= .
# indent target: Extra options to pass to indent
INDENT_OPTS ?=
INDENT_IGNORE_PATHS = daemons/controld/controld_fsa.h \
lib/gnu/*
INDENT_PACEMAKER_STYLE = --blank-lines-after-declarations \
--blank-lines-after-procedures \
--braces-after-func-def-line \
--braces-on-if-line \
--braces-on-struct-decl-line \
--break-before-boolean-operator \
--case-brace-indentation4 \
--case-indentation4 \
--comment-indentation0 \
--continuation-indentation4 \
--continue-at-parentheses \
--cuddle-do-while \
--cuddle-else \
--declaration-comment-column0 \
--declaration-indentation1 \
--else-endif-column0 \
--honour-newlines \
--indent-label0 \
--indent-level4 \
--line-comments-indentation0 \
--line-length80 \
--no-blank-lines-after-commas \
--no-comment-delimiters-on-blank-lines \
--no-space-after-function-call-names \
--no-space-after-parentheses \
--no-tabs \
--preprocessor-indentation2 \
--procnames-start-lines \
--space-after-cast \
--start-left-side-of-comments \
--swallow-optional-blank-lines \
--tab-size8
indent:
VERSION_CONTROL=none \
find $(INDENT_DIRS) -type f -name "*.[ch]" \
$(INDENT_IGNORE_PATHS:%= ! -path '%') \
-exec indent $(INDENT_PACEMAKER_STYLE) $(INDENT_OPTS) \{\} \;
#
# Scratch file for ad-hoc testing
#
EXTRA_PROGRAMS = scratch
nodist_scratch_SOURCES = scratch.c
scratch_LDADD = $(top_builddir)/lib/common/libcrmcommon.la
clean-local: coverage-clean coverity-clean cppcheck-clean
-rm -f $(EXTRA_PROGRAMS)
diff --git a/doc/sphinx/Pacemaker_Development/helpers.rst b/doc/sphinx/Pacemaker_Development/helpers.rst
index 3fcb48daf4..6bd19266b4 100644
--- a/doc/sphinx/Pacemaker_Development/helpers.rst
+++ b/doc/sphinx/Pacemaker_Development/helpers.rst
@@ -1,521 +1,520 @@
C Development Helpers
---------------------
.. index::
single: unit testing
Refactoring
###########
Pacemaker uses an optional tool called `coccinelle `_
to do automatic refactoring. coccinelle is a very complicated tool that can be
difficult to understand, and the existing documentation makes it pretty tough
to get started. Much of the documentation is either aimed at kernel developers
or takes the form of grammars.
However, it can apply very complex transformations across an entire source tree.
This is useful for tasks like code refactoring, changing APIs (number or type of
arguments, etc.), catching functions that should not be called, and changing
existing patterns.
coccinelle is driven by input scripts called `semantic patches `_
written in its own language. These scripts bear a passing resemblance to source
code patches and tell coccinelle how to match and modify a piece of source
code. They are stored in ``devel/coccinelle`` and each script either contains
a single source transformation or several related transformations. In general,
we try to keep these as simple as possible.
In Pacemaker development, we use a couple targets in ``devel/Makefile.am`` to
control coccinelle. The ``cocci`` target tries to apply each script to every
Pacemaker source file, printing out any changes it would make to the console.
The ``cocci-inplace`` target does the same but also makes those changes to the
source files. A variety of warnings might also be printed. If you aren't working
on a new script, these can usually be ignored.
If you are working on a new coccinelle script, it can be useful (and faster) to
skip everything else and only run the new script. The ``COCCI_FILES`` variable
can be used for this:
.. code-block:: none
$ make -C devel COCCI_FILES=coccinelle/new-file.cocci cocci
This variable is also used for preventing some coccinelle scripts in the Pacemaker
source tree from running. Some scripts are disabled because they are not currently
fully working or because they are there as templates. When adding a new script,
remember to add it to this variable if it should always be run.
One complication when writing coccinelle scripts is that certain Pacemaker source
files may not use private functions (those whose name starts with ``pcmk__``).
Handling this requires work in both the Makefile and in the coccinelle scripts.
The Makefile deals with this by maintaining two lists of source files: those that
may use private functions and those that may not. For those that may, a special
argument (``-D internal``) is added to the coccinelle command line. This creates
a virtual dependency named ``internal``.
In the coccinelle scripts, those transformations that modify source code to use
a private function also have a dependency on ``internal``. If that dependency
was given on the command line, the transformation will be run. Otherwise, it will
be skipped.
This means that not all instances of an older style of code will be changed after
running a given transformation. Some developer intervention is still necessary
to know whether a source code block should have been changed or not.
Probably the easiest way to learn how to use coccinelle is by following other
people's scripts. In addition to the ones in the Pacemaker source directory,
there's several others on the `coccinelle website `_.
Sanitizers
##########
gcc supports a variety of run-time checks called sanitizers. These can be used to
catch programming errors with memory, race conditions, various undefined behavior
conditions, and more. Because these are run-time checks, they should only be used
during development and not in compiled packages or production code.
Certain sanitizers cannot be combined with others because their run-time checks
cause interfere. Instead of trying to figure out which combinations work, it is
simplest to just enable one at a time.
Each supported sanitizer requires an installed libray. In addition to just
enabling the sanitizer, their use can be configured with environment variables.
For example:
.. code-block:: none
$ ASAN_OPTIONS=verbosity=1:replace_str=true crm_mon -1R
Pacemaker supports the following subset of gcc's sanitizers:
+--------------------+-------------------------+----------+----------------------+
| Sanitizer | Configure Option | Library | Environment Variable |
+====================+=========================+==========+======================+
| Address | --with-sanitizers=asan | libasan | ASAN_OPTIONS |
+--------------------+-------------------------+----------+----------------------+
| Threads | --with-sanitizers=tsan | libtsan | TSAN_OPTIONS |
+--------------------+-------------------------+----------+----------------------+
| Undefined behavior | --with-sanitizers=ubsan | libubsan | UBSAN_OPTIONS |
+--------------------+-------------------------+----------+----------------------+
The undefined behavior sanitizer further supports suboptions that need to be
given as CFLAGS when configuring pacemaker:
.. code-block:: none
$ CFLAGS=-fsanitize=integer-divide-by-zero ./configure --with-sanitizers=ubsan
For more information, see the `gcc documentation `_
which also provides links to more information on each sanitizer.
Unit Testing
############
Where possible, changes to the C side of Pacemaker should be accompanied by unit
tests. Much of Pacemaker cannot effectively be unit tested (and there are other
testing systems used for those parts), but the ``lib`` subdirectory is pretty easy
to write tests for.
Pacemaker uses the `cmocka unit testing framework `_ which looks
a lot like other unit testing frameworks for C and should be fairly familiar. In
addition to regular unit tests, cmocka also gives us the ability to use
`mock functions `_ for unit testing
functions that would otherwise be difficult to test.
Organization
____________
Pay close attention to the organization and naming of test cases to ensure the
unit tests continue to work as they should.
Tests are spread throughout the source tree, alongside the source code they test.
For instance, all the tests for the source code in ``lib/common/`` are in the
``lib/common/tests`` directory. If there is no ``tests`` subdirectory, there are no
tests for that library yet.
Under that directory, there is a ``Makefile.am`` and additional subdirectories. Each
subdirectory contains the tests for a single library source file. For instance,
all the tests for ``lib/common/strings.c`` are in the ``lib/common/tests/strings``
directory. Note that the test subdirectory does not have a ``.c`` suffix. If there
is no test subdirectory, there are no tests for that file yet.
Finally, under that directory, there is a ``Makefile.am`` and then various source
files. Each of these source files tests the single function that it is named
after. For instance, ``lib/common/tests/strings/pcmk__btoa_test.c`` tests the
``pcmk__btoa()`` function in ``lib/common/strings.c``. If there is no test
source file, there are no tests for that function yet.
The ``_test`` suffix on the test source file is important. All tests have this
suffix, which means all the compiled test cases will also end with this suffix.
That lets us ignore all the compiled tests with a single line in ``.gitignore``:
.. code-block:: none
/lib/*/tests/*/*_test
Adding a test
_____________
Testing a new function in an already testable source file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Follow these steps if you want to test a function in a source file where there
are already other tested functions. For the purposes of this example, we will
add a test for the ``pcmk__scan_port()`` function in ``lib/common/strings.c``. As
you can see, there are already tests for other functions in this same file in
the ``lib/common/tests/strings`` directory.
* cd into ``lib/common/tests/strings``
* Add the new file to the the ``check_PROGRAMS`` variable in ``Makefile.am``,
making it something like this:
.. code-block:: none
check_PROGRAMS = \
pcmk__add_word_test \
pcmk__btoa_test \
pcmk__scan_port_test
* Create a new ``pcmk__scan_port_test.c`` file, copying the copyright and include
boilerplate from another file in the same directory.
* Continue with the steps in `Writing the test`_.
Testing a function in a source file without tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Follow these steps if you want to test a function in a source file where there
are not already other tested functions, but there are tests for other files in
the same library. For the purposes of this example, we will add a test for the
``pcmk_acl_required()`` function in ``lib/common/acls.c``. At the time of this
documentation being written, no tests existed for that source file, so there
is no ``lib/common/tests/acls`` directory.
* Add to ``AC_CONFIG_FILES`` in the top-level ``configure.ac`` file so the build
process knows to use directory we're about to create. That variable would
now look something like:
.. code-block:: none
dnl Other files we output
AC_CONFIG_FILES(Makefile \
...
lib/common/tests/Makefile \
lib/common/tests/acls/Makefile \
lib/common/tests/agents/Makefile \
...
)
* cd into ``lib/common/tests``
* Add to the ``SUBDIRS`` variable in ``Makefile.am``, making it something like:
.. code-block:: none
SUBDIRS = agents acls cmdline flags operations strings utils xpath results
* Create a new ``acls`` directory, copying the ``Makefile.am`` from some other
directory. At this time, each ``Makefile.am`` is largely boilerplate with
very little that needs to change from directory to directory.
* cd into ``acls``
* Get rid of any existing values for ``check_PROGRAMS`` and set it to
``pcmk_acl_required_test`` like so:
.. code-block:: none
check_PROGRAMS = pcmk_acl_required_test
* Double check that ``$(top_srcdir)/mk/tap.mk`` and ``$(top_srcdir)/mk/unittest.mk``
are included in the ``Makefile.am``. These files contain all the flags necessary
for most unit tests. If necessary, individual settings can be overridden like so:
.. code-block:: none
AM_CPPFLAGS += -I$(top_srcdir)
LDADD += $(top_builddir)/lib/pengine/libpe_status_test.la
* Follow the steps in `Testing a new function in an already testable source file`_
to create the new ``pcmk_acl_required_test.c`` file.
Testing a function in a library without tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Adding a test case for a function in a library that doesn't have any test cases
to begin with is only slightly more complicated. In general, the steps are the
same as for the previous section, except with an additional layer of directory
creation.
For the purposes of this example, we will add a test case for the
``lrmd_send_resource_alert()`` function in ``lib/lrmd/lrmd_alerts.c``. Note that this
may not be a very good function or even library to write actual unit tests for.
* Add to ``AC_CONFIG_FILES`` in the top-level ``configure.ac`` file so the build
process knows to use directory we're about to create. That variable would
now look something like:
.. code-block:: none
dnl Other files we output
AC_CONFIG_FILES(Makefile \
...
lib/lrmd/Makefile \
lib/lrmd/tests/Makefile \
lib/services/Makefile \
...
)
* cd into ``lib/lrmd``
* Create a ``SUBDIRS`` variable in ``Makefile.am`` if it doesn't already exist.
Most libraries should not have this variable already.
.. code-block:: none
SUBDIRS = tests
* Create a new ``tests`` directory and add a ``Makefile.am`` with the following
contents:
.. code-block:: none
SUBDIRS = lrmd_alerts
* Follow the steps in `Testing a function in a source file without tests`_ to create
the rest of the new directory structure.
* Follow the steps in `Testing a new function in an already testable source file`_
to create the new ``lrmd_send_resource_alert_test.c`` file.
Adding to an existing test case
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If all you need to do is add additional test cases to an existing file, none of
the above work is necessary. All you need to do is find the test source file
with the name matching your function and add to it and then follow the
instructions in `Writing the test`_.
Writing the test
________________
A test case file contains a fair amount of boilerplate. For this reason, it's
usually easiest to just copy an existing file and adapt it to your needs. However,
here's the basic structure:
.. code-block:: c
/*
* Copyright 2021 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
* This source code is licensed under the GNU Lesser General Public License
* version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
*/
#include
#include
/* Put your test-specific includes here */
/* Put your test functions here */
PCMK__UNIT_TEST(NULL, NULL,
/* Register your test functions here */)
Each test-specific function should test one aspect of the library function,
though it can include many assertions if there are many ways of testing that
one aspect. For instance, there might be multiple ways of testing regular
expression matching:
.. code-block:: c
static void
regex(void **state) {
const char *s1 = "abcd";
const char *s2 = "ABCD";
assert_true(pcmk__strcmp(NULL, "a..d", pcmk__str_regex) < 0);
assert_true(pcmk__strcmp(s1, NULL, pcmk__str_regex) > 0);
assert_int_equal(pcmk__strcmp(s1, "a..d", pcmk__str_regex), 0);
}
Each test-specific function must also be registered or it will not be called.
This is done with ``cmocka_unit_test()`` in the ``PCMK__UNIT_TEST`` macro:
.. code-block:: c
PCMK__UNIT_TEST(NULL, NULL,
cmocka_unit_test(regex))
Most unit tests do not require a setup and teardown function to be executed
around the entire group of tests. On occassion, this may be necessary. Simply
pass those functions in as the first two parameters to ``PCMK__UNIT_TEST``
instead of using NULL.
Assertions
__________
In addition to the `assertions provided by `_,
``unittest_internal.h`` also provides ``pcmk__assert_asserts``. This macro takes an
expression and verifies that the expression aborts due to a failed call to
``CRM_ASSERT`` or some other similar function. It can be used like so:
.. code-block:: c
static void
null_input_variables(void **state)
{
long long start, end;
pcmk__assert_asserts(pcmk__parse_ll_range("1234", NULL, &end));
pcmk__assert_asserts(pcmk__parse_ll_range("1234", &start, NULL));
}
Here, ``pcmk__parse_ll_range`` expects non-NULL for its second and third
arguments. If one of those arguments is NULL, ``CRM_ASSERT`` will fail and
the program will abort. ``pcmk__assert_asserts`` checks that the code would
abort and the test passes. If the code does not abort, the test fails.
Running
_______
If you had to create any new files or directories, you will first need to run
``./configure`` from the top level of the source directory. This will regenerate
the Makefiles throughout the tree. If you skip this step, your changes will be
skipped and you'll be left wondering why the output doesn't match what you
expected.
To run the tests, simply run ``make check`` after previously building the source
with ``make``. The test cases in each directory will be built and then run.
This should not take long. If all the tests succeed, you will be back at the
prompt. Scrolling back through the history, you should see lines like the
following:
.. code-block:: none
PASS: pcmk__strcmp_test 1 - same_pointer
PASS: pcmk__strcmp_test 2 - one_is_null
PASS: pcmk__strcmp_test 3 - case_matters
PASS: pcmk__strcmp_test 4 - case_insensitive
PASS: pcmk__strcmp_test 5 - regex
============================================================================
Testsuite summary for pacemaker 2.1.0
============================================================================
# TOTAL: 33
# PASS: 33
# SKIP: 0
# XFAIL: 0
# FAIL: 0
# XPASS: 0
# ERROR: 0
============================================================================
make[7]: Leaving directory '/home/clumens/src/pacemaker/lib/common/tests/strings'
The testing process will quit on the first failed test, and you will see lines
like these:
.. code-block:: none
PASS: pcmk__scan_double_test 3 - trailing_chars
FAIL: pcmk__scan_double_test 4 - typical_case
PASS: pcmk__scan_double_test 5 - double_overflow
PASS: pcmk__scan_double_test 6 - double_underflow
ERROR: pcmk__scan_double_test - exited with status 1
PASS: pcmk__starts_with_test 1 - bad_input
============================================================================
Testsuite summary for pacemaker 2.1.0
============================================================================
# TOTAL: 56
# PASS: 54
# SKIP: 0
# XFAIL: 0
# FAIL: 1
# XPASS: 0
# ERROR: 1
============================================================================
See lib/common/tests/strings/test-suite.log
Please report to users@clusterlabs.org
============================================================================
make[7]: *** [Makefile:1218: test-suite.log] Error 1
make[7]: Leaving directory '/home/clumens/src/pacemaker/lib/common/tests/strings'
The failure is in ``lib/common/tests/strings/test-suite.log``:
.. code-block:: none
ERROR: pcmk__scan_double_test
=============================
1..6
ok 1 - empty_input_string
PASS: pcmk__scan_double_test 1 - empty_input_string
ok 2 - bad_input_string
PASS: pcmk__scan_double_test 2 - bad_input_string
ok 3 - trailing_chars
PASS: pcmk__scan_double_test 3 - trailing_chars
not ok 4 - typical_case
FAIL: pcmk__scan_double_test 4 - typical_case
# 0.000000 != 3.000000
# pcmk__scan_double_test.c:80: error: Failure!
ok 5 - double_overflow
PASS: pcmk__scan_double_test 5 - double_overflow
ok 6 - double_underflow
PASS: pcmk__scan_double_test 6 - double_underflow
# not ok - tests
ERROR: pcmk__scan_double_test - exited with status 1
At this point, you need to determine whether your test case is incorrect or
whether the code being tested is incorrect. Fix whichever is wrong and continue.
Code Coverage
#############
Figuring out what needs unit tests written is the purpose of a code coverage tool.
The Pacemaker build process uses ``lcov`` and special make targets to generate
an HTML coverage report that can be inspected with any web browser.
To start, you'll need to install the ``lcov`` package which is included in most
-distributions. Next, reconfigure and rebuild the source tree:
+distributions. Next, reconfigure the source tree:
.. code-block:: none
$ ./configure --with-coverage
- $ make
-Then simply run ``make coverage``. This will do the same thing as ``make check``,
+Then run ``make -C devel coverage``. This will do the same thing as ``make check``,
but will generate a bunch of intermediate files as part of the compiler's output.
Essentially, the coverage tools run all the unit tests and make a note if a given
line if code is executed as a part of some test program. This will include not
just things run as part of the tests but anything in the setup and teardown
functions as well.
Afterwards, the HTML report will be in ``coverage/index.html``. You can drill down
into individual source files to see exactly which lines are covered and which are
not, which makes it easy to target new unit tests. Note that sometimes, it is
impossible to achieve 100% coverage for a source file. For instance, how do you
test a function with a return type of void that simply returns on some condition?
Note that Pacemaker's overall code coverage numbers are very low at the moment.
One reason for this is the large amount of code in the ``daemons`` directory that
will be very difficult to write unit tests for. For now, it is best to focus
efforts on increasing the coverage on individual libraries.
Additionally, there is a ``coverage-cts`` target that does the same thing but
instead of testing ``make check``, it tests ``cts/cts-cli``. The idea behind this
target is to see what parts of our command line tools are covered by our regression
tests. It is probably best to clean and rebuild the source tree when switching
between these various targets.
Debugging
#########
gdb
___
If you use ``gdb`` for debugging, some helper functions are defined in
``devel/gdbhelpers``, which can be given to ``gdb`` using the ``-x`` option.
From within the debugger, you can then invoke the ``pcmk`` command that
will describe the helper functions available.
diff --git a/include/crm/common/unittest_internal.h b/include/crm/common/unittest_internal.h
index b8f78cf411..1fc8501cb1 100644
--- a/include/crm/common/unittest_internal.h
+++ b/include/crm/common/unittest_internal.h
@@ -1,84 +1,122 @@
/*
- * Copyright 2022 the Pacemaker project contributors
+ * Copyright 2022-2023 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
* This source code is licensed under the GNU Lesser General Public License
* version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifndef CRM_COMMON_UNITTEST_INTERNAL__H
#define CRM_COMMON_UNITTEST_INTERNAL__H
/* internal unit testing related utilities */
/*!
* \internal
* \brief Assert that a statement aborts through CRM_ASSERT().
*
* \param[in] stmt Statement to execute; can be an expression.
*
* A cmocka-like assert macro for use in unit testing. This one verifies that a
* statement aborts through CRM_ASSERT(), erroring out if that is not the case.
*
* This macro works by running the statement in a forked child process with core
* dumps disabled (CRM_ASSERT() calls \c abort(), which will write out a core
* dump). The parent waits for the child to exit and checks why. If the child
* received a \c SIGABRT, the test passes. For all other cases, the test fails.
*
* \note If cmocka's expect_*() or will_return() macros are called along with
* pcmk__assert_asserts(), they must be called within a block that is
* passed as the \c stmt argument. That way, the values are added only to
* the child's queue. Otherwise, values added to the parent's queue will
* never be popped, and the test will fail.
*/
#define pcmk__assert_asserts(stmt) \
do { \
pid_t p = fork(); \
if (p == 0) { \
struct rlimit cores = { 0, 0 }; \
setrlimit(RLIMIT_CORE, &cores); \
stmt; \
_exit(0); \
} else if (p > 0) { \
int wstatus = 0; \
if (waitpid(p, &wstatus, 0) == -1) { \
fail_msg("waitpid failed"); \
} \
if (!(WIFSIGNALED(wstatus) && WTERMSIG(wstatus) == SIGABRT)) { \
fail_msg("statement terminated in child without asserting"); \
} \
} else { \
fail_msg("unable to fork for assert test"); \
} \
} while (0);
+/*!
+ * \internal
+ * \brief Assert that a statement exits with the expected exit status.
+ *
+ * \param[in] stmt Statement to execute; can be an expression.
+ * \param[in] rc The expected exit status.
+ *
+ * This functions just like \c pcmk__assert_asserts, except that it tests for
+ * an expected exit status. Abnormal termination or incorrect exit status is
+ * treated as a failure of the test.
+ *
+ * In the event that stmt does not exit at all, the special code \c CRM_EX_NONE
+ * will be returned. It is expected that this code is not used anywhere, thus
+ * always causing an error.
+ */
+#define pcmk__assert_exits(rc, stmt) \
+ do { \
+ pid_t p = fork(); \
+ if (p == 0) { \
+ struct rlimit cores = { 0, 0 }; \
+ setrlimit(RLIMIT_CORE, &cores); \
+ stmt; \
+ _exit(CRM_EX_NONE); \
+ } else if (p > 0) { \
+ int wstatus = 0; \
+ if (waitpid(p, &wstatus, 0) == -1) { \
+ fail_msg("waitpid failed"); \
+ } \
+ if (!WIFEXITED(wstatus)) { \
+ fail_msg("statement terminated abnormally"); \
+ } else if (WEXITSTATUS(wstatus) != rc) { \
+ fail_msg("statement exited with %d, not expected %d", WEXITSTATUS(wstatus), rc); \
+ } \
+ } else { \
+ fail_msg("unable to fork for assert test"); \
+ } \
+ } while (0);
+
/* Generate the main function of most unit test files. Typically, group_setup
* and group_teardown will be NULL. The rest of the arguments are a list of
* calls to cmocka_unit_test or cmocka_unit_test_setup_teardown to run the
* individual unit tests.
*/
#define PCMK__UNIT_TEST(group_setup, group_teardown, ...) \
int \
main(int argc, char **argv) \
{ \
const struct CMUnitTest t[] = { \
__VA_ARGS__ \
}; \
cmocka_set_message_output(CM_OUTPUT_TAP); \
return cmocka_run_group_tests(t, group_setup, group_teardown); \
}
#endif /* CRM_COMMON_UNITTEST_INTERNAL__H */
diff --git a/lib/common/tests/agents/crm_parse_agent_spec_test.c b/lib/common/tests/agents/crm_parse_agent_spec_test.c
index cfd75f05c1..1d44459eb3 100644
--- a/lib/common/tests/agents/crm_parse_agent_spec_test.c
+++ b/lib/common/tests/agents/crm_parse_agent_spec_test.c
@@ -1,87 +1,95 @@
/*
- * Copyright 2022 the Pacemaker project contributors
+ * Copyright 2022-2023 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
#include
#include
static void
all_params_null(void **state) {
assert_int_equal(crm_parse_agent_spec(NULL, NULL, NULL, NULL), -EINVAL);
assert_int_equal(crm_parse_agent_spec("", NULL, NULL, NULL), -EINVAL);
assert_int_equal(crm_parse_agent_spec(":", NULL, NULL, NULL), -EINVAL);
assert_int_equal(crm_parse_agent_spec("::", NULL, NULL, NULL), -EINVAL);
}
static void
no_prov_or_type(void **state) {
- assert_int_equal(crm_parse_agent_spec("ocf", NULL, NULL, NULL), -EINVAL);
- assert_int_equal(crm_parse_agent_spec("ocf:", NULL, NULL, NULL), -EINVAL);
- assert_int_equal(crm_parse_agent_spec("ocf::", NULL, NULL, NULL), -EINVAL);
+ char *std = NULL;
+ char *prov = NULL;
+ char *ty = NULL;
+
+ assert_int_equal(crm_parse_agent_spec("ocf", &std, &prov, &ty), -EINVAL);
+ assert_int_equal(crm_parse_agent_spec("ocf:", &std, &prov, &ty), -EINVAL);
+ assert_int_equal(crm_parse_agent_spec("ocf::", &std, &prov, &ty), -EINVAL);
}
static void
no_type(void **state) {
- assert_int_equal(crm_parse_agent_spec("ocf:pacemaker:", NULL, NULL, NULL), -EINVAL);
+ char *std = NULL;
+ char *prov = NULL;
+ char *ty = NULL;
+
+ assert_int_equal(crm_parse_agent_spec("ocf:pacemaker:", &std, &prov, &ty), -EINVAL);
}
static void
get_std_and_ty(void **state) {
char *std = NULL;
char *prov = NULL;
char *ty = NULL;
assert_int_equal(crm_parse_agent_spec("stonith:fence_xvm", &std, &prov, &ty), pcmk_ok);
assert_string_equal(std, "stonith");
assert_null(prov);
assert_string_equal(ty, "fence_xvm");
free(std);
free(ty);
}
static void
get_all_values(void **state) {
char *std = NULL;
char *prov = NULL;
char *ty = NULL;
assert_int_equal(crm_parse_agent_spec("ocf:pacemaker:ping", &std, &prov, &ty), pcmk_ok);
assert_string_equal(std, "ocf");
assert_string_equal(prov, "pacemaker");
assert_string_equal(ty, "ping");
free(std);
free(prov);
free(ty);
}
static void
get_systemd_values(void **state) {
char *std = NULL;
char *prov = NULL;
char *ty = NULL;
assert_int_equal(crm_parse_agent_spec("systemd:UNIT@A:B", &std, &prov, &ty), pcmk_ok);
assert_string_equal(std, "systemd");
assert_null(prov);
assert_string_equal(ty, "UNIT@A:B");
free(std);
free(ty);
}
PCMK__UNIT_TEST(NULL, NULL,
cmocka_unit_test(all_params_null),
cmocka_unit_test(no_prov_or_type),
cmocka_unit_test(no_type),
cmocka_unit_test(get_std_and_ty),
cmocka_unit_test(get_all_values),
cmocka_unit_test(get_systemd_values))
diff --git a/lib/common/tests/cmdline/Makefile.am b/lib/common/tests/cmdline/Makefile.am
index d781ed5e96..c778f7d370 100644
--- a/lib/common/tests/cmdline/Makefile.am
+++ b/lib/common/tests/cmdline/Makefile.am
@@ -1,17 +1,18 @@
#
-# Copyright 2020-2022 the Pacemaker project contributors
+# Copyright 2020-2023 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__cmdline_preproc_test \
+ pcmk__new_common_args_test \
pcmk__quote_cmdline_test
TESTS = $(check_PROGRAMS)
diff --git a/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c b/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c
index 863fbb947b..299fec6869 100644
--- a/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c
+++ b/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c
@@ -1,156 +1,167 @@
/*
- * Copyright 2020-2022 the Pacemaker project contributors
+ * Copyright 2020-2023 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
#include
#include
#include
#include
#define LISTS_EQ(a, b) { \
assert_int_equal(g_strv_length((gchar **) (a)), g_strv_length((gchar **) (b))); \
for (int i = 0; i < g_strv_length((a)); i++) { \
assert_string_equal((a)[i], (b)[i]); \
} \
}
static void
empty_input(void **state) {
assert_null(pcmk__cmdline_preproc(NULL, ""));
}
static void
no_specials(void **state) {
const char *argv[] = { "crm_mon", "-a", "-b", "-c", "-d", "-1", NULL };
const gchar *expected[] = { "crm_mon", "-a", "-b", "-c", "-d", "-1", NULL };
gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
LISTS_EQ(processed, expected);
g_strfreev(processed);
processed = pcmk__cmdline_preproc((char **) argv, "");
LISTS_EQ(processed, expected);
g_strfreev(processed);
}
static void
single_dash(void **state) {
const char *argv[] = { "crm_mon", "-", NULL };
const gchar *expected[] = { "crm_mon", "-", NULL };
gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
LISTS_EQ(processed, expected);
g_strfreev(processed);
}
static void
double_dash(void **state) {
const char *argv[] = { "crm_mon", "-a", "--", "-bc", NULL };
const gchar *expected[] = { "crm_mon", "-a", "--", "-bc", NULL };
gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
LISTS_EQ(processed, expected);
g_strfreev(processed);
}
static void
special_args(void **state) {
const char *argv[] = { "crm_mon", "-aX", "-Fval", NULL };
const gchar *expected[] = { "crm_mon", "-a", "X", "-F", "val", NULL };
gchar **processed = pcmk__cmdline_preproc((char **) argv, "aF");
LISTS_EQ(processed, expected);
g_strfreev(processed);
}
static void
special_arg_at_end(void **state) {
const char *argv[] = { "crm_mon", "-a", NULL };
const gchar *expected[] = { "crm_mon", "-a", NULL };
gchar **processed = pcmk__cmdline_preproc((char **) argv, "a");
LISTS_EQ(processed, expected);
g_strfreev(processed);
}
static void
long_arg(void **state) {
const char *argv[] = { "crm_mon", "--blah=foo", NULL };
const gchar *expected[] = { "crm_mon", "--blah=foo", NULL };
gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
LISTS_EQ(processed, expected);
g_strfreev(processed);
}
static void
negative_score(void **state) {
const char *argv[] = { "crm_mon", "-v", "-1000", NULL };
const gchar *expected[] = { "crm_mon", "-v", "-1000", NULL };
gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
LISTS_EQ(processed, expected);
g_strfreev(processed);
}
static void
negative_score_2(void **state) {
const char *argv[] = { "crm_mon", "-1i3", NULL };
const gchar *expected[] = { "crm_mon", "-1", "-i", "-3", NULL };
gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
LISTS_EQ(processed, expected);
g_strfreev(processed);
}
+static void
+negative_score_3(void **state) {
+ const char *argv[] = { "crm_attribute", "-p", "-v", "-INFINITY", NULL };
+ const gchar *expected[] = { "crm_attribute", "-p", "-v", "-INFINITY", NULL };
+
+ gchar **processed = pcmk__cmdline_preproc((char **) argv, "pv");
+ LISTS_EQ(processed, expected);
+ g_strfreev(processed);
+}
+
static void
string_arg_with_dash(void **state) {
const char *argv[] = { "crm_mon", "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL };
const gchar *expected[] = { "crm_mon", "-n", "crm_mon_options", "-v", "--opt1 --opt2", NULL };
gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
LISTS_EQ(processed, expected);
g_strfreev(processed);
}
static void
string_arg_with_dash_2(void **state) {
const char *argv[] = { "crm_mon", "-n", "crm_mon_options", "-v", "-1i3", NULL };
const gchar *expected[] = { "crm_mon", "-n", "crm_mon_options", "-v", "-1i3", NULL };
gchar **processed = pcmk__cmdline_preproc((char **) argv, "v");
LISTS_EQ(processed, expected);
g_strfreev(processed);
}
static void
string_arg_with_dash_3(void **state) {
const char *argv[] = { "crm_mon", "-abc", "-1i3", NULL };
const gchar *expected[] = { "crm_mon", "-a", "-b", "-c", "-1i3", NULL };
gchar **processed = pcmk__cmdline_preproc((char **) argv, "c");
LISTS_EQ(processed, expected);
g_strfreev(processed);
}
PCMK__UNIT_TEST(NULL, NULL,
cmocka_unit_test(empty_input),
cmocka_unit_test(no_specials),
cmocka_unit_test(single_dash),
cmocka_unit_test(double_dash),
cmocka_unit_test(special_args),
cmocka_unit_test(special_arg_at_end),
cmocka_unit_test(long_arg),
cmocka_unit_test(negative_score),
cmocka_unit_test(negative_score_2),
+ cmocka_unit_test(negative_score_3),
cmocka_unit_test(string_arg_with_dash),
cmocka_unit_test(string_arg_with_dash_2),
cmocka_unit_test(string_arg_with_dash_3))
diff --git a/lib/common/tests/cmdline/pcmk__new_common_args_test.c b/lib/common/tests/cmdline/pcmk__new_common_args_test.c
new file mode 100644
index 0000000000..6b70465629
--- /dev/null
+++ b/lib/common/tests/cmdline/pcmk__new_common_args_test.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2023 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
+
+#include
+#include
+
+#include "mock_private.h"
+
+#include
+
+static void
+calloc_fails(void **state)
+{
+ pcmk__assert_exits(CRM_EX_OSERR,
+ {
+ pcmk__mock_calloc = true; // calloc() will return NULL
+ expect_value(__wrap_calloc, nmemb, 1);
+ expect_value(__wrap_calloc, size, sizeof(pcmk__common_args_t));
+ pcmk__new_common_args("boring summary");
+ pcmk__mock_calloc = false; // Use real calloc()
+ }
+ );
+}
+
+static void
+strdup_fails(void **state)
+{
+ pcmk__assert_exits(CRM_EX_OSERR,
+ {
+ pcmk__mock_strdup = true; // strdup() will return NULL
+ expect_string(__wrap_strdup, s, "boring summary");
+ pcmk__new_common_args("boring summary");
+ pcmk__mock_strdup = false; // Use the real strdup()
+ }
+ );
+}
+
+static void
+success(void **state)
+{
+ pcmk__common_args_t *args = pcmk__new_common_args("boring summary");
+ assert_string_equal(args->summary, "boring summary");
+ assert_null(args->output_as_descr);
+ assert_false(args->version);
+ assert_false(args->quiet);
+ assert_int_equal(args->verbosity, 0);
+ assert_null(args->output_ty);
+ assert_null(args->output_dest);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(calloc_fails),
+ cmocka_unit_test(strdup_fails),
+ cmocka_unit_test(success))
diff --git a/lib/common/tests/scores/pcmk__add_scores_test.c b/lib/common/tests/scores/pcmk__add_scores_test.c
index 85ac232909..1309659fb3 100644
--- a/lib/common/tests/scores/pcmk__add_scores_test.c
+++ b/lib/common/tests/scores/pcmk__add_scores_test.c
@@ -1,74 +1,76 @@
/*
- * Copyright 2022 the Pacemaker project contributors
+ * Copyright 2022-2023 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
#include
static void
score1_minus_inf(void **state)
{
assert_int_equal(pcmk__add_scores(-CRM_SCORE_INFINITY, -CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(-CRM_SCORE_INFINITY, -1), -CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(-CRM_SCORE_INFINITY, 0), -CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(-CRM_SCORE_INFINITY, 1), -CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(-CRM_SCORE_INFINITY, CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
}
static void
score2_minus_inf(void **state)
{
assert_int_equal(pcmk__add_scores(-1, -CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(0, -CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(1, -CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(CRM_SCORE_INFINITY, -CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
}
static void
score1_pos_inf(void **state)
{
assert_int_equal(pcmk__add_scores(CRM_SCORE_INFINITY, CRM_SCORE_INFINITY), CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(CRM_SCORE_INFINITY, -1), CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(CRM_SCORE_INFINITY, 0), CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(CRM_SCORE_INFINITY, 1), CRM_SCORE_INFINITY);
}
static void
score2_pos_inf(void **state)
{
assert_int_equal(pcmk__add_scores(-1, CRM_SCORE_INFINITY), CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(0, CRM_SCORE_INFINITY), CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(1, CRM_SCORE_INFINITY), CRM_SCORE_INFINITY);
}
static void
result_infinite(void **state)
{
assert_int_equal(pcmk__add_scores(INT_MAX, INT_MAX), CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(INT_MIN, INT_MIN), -CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(2000000, 50), CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(CRM_SCORE_INFINITY/2, CRM_SCORE_INFINITY/2), CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(-CRM_SCORE_INFINITY/2, -CRM_SCORE_INFINITY/2), -CRM_SCORE_INFINITY);
assert_int_equal(pcmk__add_scores(-4000000, 50), -CRM_SCORE_INFINITY);
}
static void
result_finite(void **state)
{
assert_int_equal(pcmk__add_scores(0, 0), 0);
assert_int_equal(pcmk__add_scores(0, 100), 100);
assert_int_equal(pcmk__add_scores(200, 0), 200);
assert_int_equal(pcmk__add_scores(200, -50), 150);
}
PCMK__UNIT_TEST(NULL, NULL,
cmocka_unit_test(score1_minus_inf),
cmocka_unit_test(score2_minus_inf),
cmocka_unit_test(score1_pos_inf),
cmocka_unit_test(score2_pos_inf),
cmocka_unit_test(result_infinite),
cmocka_unit_test(result_finite))
diff --git a/lib/common/tests/strings/Makefile.am b/lib/common/tests/strings/Makefile.am
index 9abb8e9a18..01b69fa57c 100644
--- a/lib/common/tests/strings/Makefile.am
+++ b/lib/common/tests/strings/Makefile.am
@@ -1,41 +1,42 @@
#
-# Copyright 2020-2022 the Pacemaker project contributors
+# Copyright 2020-2023 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 = \
crm_get_msec_test \
crm_is_true_test \
crm_str_to_boolean_test \
pcmk__add_word_test \
pcmk__btoa_test \
pcmk__char_in_any_str_test \
pcmk__compress_test \
pcmk__ends_with_test \
pcmk__g_strcat_test \
pcmk__guint_from_hash_test \
pcmk__numeric_strcasecmp_test \
pcmk__parse_ll_range_test \
pcmk__s_test \
pcmk__scan_double_test \
+ pcmk__scan_ll_test \
pcmk__scan_min_int_test \
pcmk__scan_port_test \
pcmk__starts_with_test \
pcmk__str_any_of_test \
pcmk__str_in_list_test \
pcmk__str_table_dup_test \
pcmk__str_update_test \
pcmk__strcmp_test \
pcmk__strkey_table_test \
pcmk__strikey_table_test \
pcmk__trim_test
TESTS = $(check_PROGRAMS)
diff --git a/lib/common/tests/strings/pcmk__guint_from_hash_test.c b/lib/common/tests/strings/pcmk__guint_from_hash_test.c
index e2b476217b..225c5b36c5 100644
--- a/lib/common/tests/strings/pcmk__guint_from_hash_test.c
+++ b/lib/common/tests/strings/pcmk__guint_from_hash_test.c
@@ -1,76 +1,80 @@
/*
* Copyright 2022 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
#include
#include
static void
null_args(void **state)
{
GHashTable *tbl = pcmk__strkey_table(free, free);
guint result;
assert_int_equal(pcmk__guint_from_hash(NULL, "abc", 123, &result), EINVAL);
assert_int_equal(pcmk__guint_from_hash(tbl, NULL, 123, &result), EINVAL);
g_hash_table_destroy(tbl);
}
static void
missing_key(void **state)
{
GHashTable *tbl = pcmk__strkey_table(free, free);
guint result;
assert_int_equal(pcmk__guint_from_hash(tbl, "abc", 123, &result), pcmk_rc_ok);
assert_int_equal(result, 123);
g_hash_table_destroy(tbl);
}
static void
standard_usage(void **state)
{
GHashTable *tbl = pcmk__strkey_table(free, free);
guint result;
g_hash_table_insert(tbl, strdup("abc"), strdup("123"));
assert_int_equal(pcmk__guint_from_hash(tbl, "abc", 456, &result), pcmk_rc_ok);
assert_int_equal(result, 123);
g_hash_table_destroy(tbl);
}
static void
conversion_errors(void **state)
{
GHashTable *tbl = pcmk__strkey_table(free, free);
guint result;
g_hash_table_insert(tbl, strdup("negative"), strdup("-3"));
g_hash_table_insert(tbl, strdup("toobig"), strdup("20000000000000000"));
+ g_hash_table_insert(tbl, strdup("baddata"), strdup("asdf"));
assert_int_equal(pcmk__guint_from_hash(tbl, "negative", 456, &result), ERANGE);
assert_int_equal(result, 456);
assert_int_equal(pcmk__guint_from_hash(tbl, "toobig", 456, &result), ERANGE);
assert_int_equal(result, 456);
+ assert_int_equal(pcmk__guint_from_hash(tbl, "baddata", 456, &result), EINVAL);
+ assert_int_equal(result, 456);
+
g_hash_table_destroy(tbl);
}
PCMK__UNIT_TEST(NULL, NULL,
cmocka_unit_test(null_args),
cmocka_unit_test(missing_key),
cmocka_unit_test(standard_usage),
cmocka_unit_test(conversion_errors))
diff --git a/lib/common/tests/strings/pcmk__scan_ll_test.c b/lib/common/tests/strings/pcmk__scan_ll_test.c
new file mode 100644
index 0000000000..645ecb4f62
--- /dev/null
+++ b/lib/common/tests/strings/pcmk__scan_ll_test.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2023 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
+
+#include
+
+static void
+empty_input_string(void **state)
+{
+ long long result;
+
+ assert_int_equal(pcmk__scan_ll(NULL, &result, 47), pcmk_rc_ok);
+ assert_int_equal(result, 47);
+}
+
+static void
+bad_input_string(void **state)
+{
+ long long result;
+
+ assert_int_equal(pcmk__scan_ll("asdf", &result, 47), EINVAL);
+ assert_int_equal(result, 47);
+ assert_int_equal(pcmk__scan_ll("as12", &result, 47), EINVAL);
+ assert_int_equal(result, 47);
+}
+
+static void
+trailing_chars(void **state)
+{
+ long long result;
+
+ assert_int_equal(pcmk__scan_ll("12as", &result, 47), pcmk_rc_ok);
+ assert_int_equal(result, 12);
+}
+
+static void
+no_result_variable(void **state)
+{
+ assert_int_equal(pcmk__scan_ll("1234", NULL, 47), pcmk_rc_ok);
+ assert_int_equal(pcmk__scan_ll("asdf", NULL, 47), EINVAL);
+}
+
+static void
+typical_case(void **state)
+{
+ long long result;
+
+ assert_int_equal(pcmk__scan_ll("1234", &result, 47), pcmk_rc_ok);
+ assert_int_equal(result, 1234);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(empty_input_string),
+ cmocka_unit_test(bad_input_string),
+ cmocka_unit_test(trailing_chars),
+ cmocka_unit_test(no_result_variable),
+ cmocka_unit_test(typical_case))
diff --git a/lib/common/tests/utils/Makefile.am b/lib/common/tests/utils/Makefile.am
index edccf0941a..9bb66f2e55 100644
--- a/lib/common/tests/utils/Makefile.am
+++ b/lib/common/tests/utils/Makefile.am
@@ -1,28 +1,31 @@
#
-# Copyright 2020-2022 the Pacemaker project contributors
+# Copyright 2020-2023 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 = \
compare_version_test \
crm_meta_name_test \
crm_meta_value_test \
crm_user_lookup_test \
pcmk_daemon_user_test \
pcmk_str_is_infinity_test \
pcmk_str_is_minus_infinity_test \
- pcmk__getpid_s_test
+ pcmk__fail_attr_name_test \
+ pcmk__failcount_name_test \
+ pcmk__getpid_s_test \
+ pcmk__lastfailure_name_test
if WRAPPABLE_UNAME
check_PROGRAMS += pcmk_hostname_test
endif
TESTS = $(check_PROGRAMS)
diff --git a/lib/common/tests/utils/pcmk__fail_attr_name_test.c b/lib/common/tests/utils/pcmk__fail_attr_name_test.c
new file mode 100644
index 0000000000..c6c25fcbf2
--- /dev/null
+++ b/lib/common/tests/utils/pcmk__fail_attr_name_test.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2023 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
+
+#include
+
+static void
+null_arguments(void **state)
+{
+ assert_null(pcmk__fail_attr_name(NULL, NULL, NULL, 30000));
+ assert_null(pcmk__fail_attr_name(NULL, "myrsc", "monitor", 30000));
+ assert_null(pcmk__fail_attr_name("xyz", NULL, "monitor", 30000));
+ assert_null(pcmk__fail_attr_name("xyz", "myrsc", NULL, 30000));
+}
+
+static void
+standard_usage(void **state)
+{
+ char *s = NULL;
+
+ assert_string_equal(pcmk__fail_attr_name("xyz", "myrsc", "monitor", 30000),
+ "xyz-myrsc#monitor_30000");
+
+ free(s);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(null_arguments),
+ cmocka_unit_test(standard_usage))
diff --git a/lib/common/tests/utils/pcmk__failcount_name_test.c b/lib/common/tests/utils/pcmk__failcount_name_test.c
new file mode 100644
index 0000000000..a801f4da2f
--- /dev/null
+++ b/lib/common/tests/utils/pcmk__failcount_name_test.c
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2023 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
+
+#include
+
+static void
+null_arguments(void **state)
+{
+ assert_null(pcmk__failcount_name(NULL, NULL, 30000));
+ assert_null(pcmk__failcount_name("myrsc", NULL, 30000));
+ assert_null(pcmk__failcount_name(NULL, "monitor", 30000));
+}
+
+static void
+standard_usage(void **state)
+{
+ char *s = NULL;
+
+ assert_string_equal(pcmk__failcount_name("myrsc", "monitor", 30000),
+ "fail-count-myrsc#monitor_30000");
+
+ free(s);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(null_arguments),
+ cmocka_unit_test(standard_usage))
diff --git a/lib/common/tests/utils/pcmk__lastfailure_name_test.c b/lib/common/tests/utils/pcmk__lastfailure_name_test.c
new file mode 100644
index 0000000000..eab01f2fd4
--- /dev/null
+++ b/lib/common/tests/utils/pcmk__lastfailure_name_test.c
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2023 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
+
+#include
+
+static void
+null_arguments(void **state)
+{
+ assert_null(pcmk__lastfailure_name(NULL, NULL, 30000));
+ assert_null(pcmk__lastfailure_name("myrsc", NULL, 30000));
+ assert_null(pcmk__lastfailure_name(NULL, "monitor", 30000));
+}
+
+static void
+standard_usage(void **state)
+{
+ char *s = NULL;
+
+ assert_string_equal(pcmk__lastfailure_name("myrsc", "monitor", 30000),
+ "last-failure-myrsc#monitor_30000");
+
+ free(s);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(null_arguments),
+ cmocka_unit_test(standard_usage))