From e94c02608a1f6349056fa066e8d05fdeb4079831 Mon Sep 17 00:00:00 2001 From: Ken Gaillot Date: Jan 22 2021 12:12:32 +0000 Subject: Test: libcrmcommon: add unit tests for stonith parameter name checker --- diff --git a/configure.ac b/configure.ac index 8b5018b..66faa47 100644 --- a/configure.ac +++ b/configure.ac @@ -2017,6 +2017,7 @@ AC_CONFIG_FILES(Makefile \ lib/pacemaker-cluster.pc \ lib/common/Makefile \ lib/common/tests/Makefile \ + lib/common/tests/agents/Makefile \ lib/common/tests/cmdline/Makefile \ lib/common/tests/flags/Makefile \ lib/common/tests/operations/Makefile \ diff --git a/lib/common/tests/Makefile.am b/lib/common/tests/Makefile.am index f3eaeec..2c33cc5 100644 --- a/lib/common/tests/Makefile.am +++ b/lib/common/tests/Makefile.am @@ -1 +1 @@ -SUBDIRS = cmdline flags operations strings utils +SUBDIRS = agents cmdline flags operations strings utils diff --git a/lib/common/tests/agents/Makefile.am b/lib/common/tests/agents/Makefile.am new file mode 100644 index 0000000..40cb5f7 --- /dev/null +++ b/lib/common/tests/agents/Makefile.am @@ -0,0 +1,29 @@ +# +# Copyright 2020 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. +# +AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include +LDADD = $(top_builddir)/lib/common/libcrmcommon.la + +include $(top_srcdir)/mk/glib-tap.mk + +# Add each test program here. Each test should be written as a little standalone +# program using the glib unit testing functions. See the documentation for more +# information. +# +# https://developer.gnome.org/glib/unstable/glib-Testing.html +# +# Add "_test" to the end of all test program names to simplify .gitignore. +test_programs = pcmk_stonith_param_test + +# If any extra data needs to be added to the source distribution, add it to the +# following list. +dist_test_data = + +# If any extra data needs to be used by tests but should not be added to the +# source distribution, add it to the following list. +test_data = diff --git a/lib/common/tests/agents/pcmk_stonith_param_test.c b/lib/common/tests/agents/pcmk_stonith_param_test.c new file mode 100644 index 0000000..bf509e9 --- /dev/null +++ b/lib/common/tests/agents/pcmk_stonith_param_test.c @@ -0,0 +1,58 @@ +/* + * Copyright 2020 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 + +static void +is_stonith_param(void) +{ + g_assert_cmpint(pcmk_stonith_param(NULL), ==, false); + g_assert_cmpint(pcmk_stonith_param(""), ==, false); + g_assert_cmpint(pcmk_stonith_param("unrecognized"), ==, false); + g_assert_cmpint(pcmk_stonith_param("pcmk_unrecognized"), ==, false); + g_assert_cmpint(pcmk_stonith_param("x" PCMK_STONITH_ACTION_LIMIT), ==, false); + g_assert_cmpint(pcmk_stonith_param(PCMK_STONITH_ACTION_LIMIT "x"), ==, false); + + g_assert_cmpint(pcmk_stonith_param(PCMK_STONITH_ACTION_LIMIT), ==, true); + g_assert_cmpint(pcmk_stonith_param(PCMK_STONITH_DELAY_BASE), ==, true); + g_assert_cmpint(pcmk_stonith_param(PCMK_STONITH_DELAY_MAX), ==, true); + g_assert_cmpint(pcmk_stonith_param(PCMK_STONITH_HOST_ARGUMENT), ==, true); + g_assert_cmpint(pcmk_stonith_param(PCMK_STONITH_HOST_CHECK), ==, true); + g_assert_cmpint(pcmk_stonith_param(PCMK_STONITH_HOST_LIST), ==, true); + g_assert_cmpint(pcmk_stonith_param(PCMK_STONITH_HOST_MAP), ==, true); + g_assert_cmpint(pcmk_stonith_param(PCMK_STONITH_PROVIDES), ==, true); + g_assert_cmpint(pcmk_stonith_param(PCMK_STONITH_STONITH_TIMEOUT), ==, true); +} + +static void +is_stonith_action_param(void) +{ + /* Currently, the function accepts any string not containing underbars as + * the action name, so we do not need to verify particular action names. + */ + g_assert_cmpint(pcmk_stonith_param("pcmk_on_unrecognized"), ==, false); + g_assert_cmpint(pcmk_stonith_param("pcmk_on_action"), ==, true); + g_assert_cmpint(pcmk_stonith_param("pcmk_on_timeout"), ==, true); + g_assert_cmpint(pcmk_stonith_param("pcmk_on_retries"), ==, true); +} + +int +main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/common/utils/parse_op_key/is_stonith_param", + is_stonith_param); + g_test_add_func("/common/utils/parse_op_key/is_stonith_action_param", + is_stonith_action_param); + return g_test_run(); +}