root/lib/common/unittest.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pcmk__assert_validates
  2. pcmk__xml_test_setup_group
  3. pcmk__xml_test_teardown_group
  4. pcmk__cib_test_copy_cib
  5. pcmk__cib_test_cleanup
  6. pcmk__test_init_logging

   1 /*
   2  * Copyright 2024-2025 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <crm/common/unittest_internal.h>
  13 
  14 #include <stdlib.h>
  15 #include <unistd.h>
  16 
  17 #include <libxml/parser.h>                  // xmlCleanupParser()
  18 
  19 // LCOV_EXCL_START
  20 
  21 void
  22 pcmk__assert_validates(xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
  23 {
  24     const char *schema_dir = NULL;
  25     char *cmd = NULL;
  26     gchar *out = NULL;
  27     gchar *err = NULL;
  28     gint status;
  29     GError *gerr = NULL;
  30     char *xmllint_input = crm_strdup_printf("%s/test-xmllint.XXXXXX",
  31                                             pcmk__get_tmpdir());
  32     int fd;
  33     int rc;
  34 
  35     fd = mkstemp(xmllint_input);
  36     if (fd < 0) {
  37         fail_msg("Could not create temp file: %s", strerror(errno));
  38     }
  39 
  40     rc = pcmk__xml2fd(fd, xml);
  41     if (rc != pcmk_rc_ok) {
  42         unlink(xmllint_input);
  43         fail_msg("Could not write temp file: %s", pcmk_rc_str(rc));
  44     }
  45 
  46     close(fd);
  47 
  48     /* This should be set as part of AM_TESTS_ENVIRONMENT in Makefile.am. */
  49     schema_dir = getenv("PCMK_schema_directory");
  50     if (schema_dir == NULL) {
  51         unlink(xmllint_input);
  52         fail_msg("PCMK_schema_directory is not set in test environment");
  53     }
  54 
  55     cmd = crm_strdup_printf("xmllint --relaxng %s/api/api-result.rng %s",
  56                             schema_dir, xmllint_input);
  57 
  58     if (!g_spawn_command_line_sync(cmd, &out, &err, &status, &gerr)) {
  59         unlink(xmllint_input);
  60         fail_msg("Error occurred when performing validation: %s", gerr->message);
  61     }
  62 
  63     if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
  64         unlink(xmllint_input);
  65         fail_msg("XML validation failed: %s\n%s\n", out, err);
  66     }
  67 
  68     free(cmd);
  69     g_free(out);
  70     g_free(err);
  71     unlink(xmllint_input);
  72     free(xmllint_input);
  73 }
  74 
  75 /*!
  76  * \internal
  77  * \brief Perform setup for a group of unit tests that manipulate XML
  78  *
  79  * This function is suitable for being passed as the first argument to the
  80  * \c PCMK__UNIT_TEST macro.
  81  *
  82  * \param[in] state  Ignored
  83  *
  84  * \return 0
  85  */
  86 int
  87 pcmk__xml_test_setup_group(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  88 {
  89     pcmk__schema_init();
  90     return 0;
  91 }
  92 
  93 /*!
  94  * \internal
  95  * \brief Perform teardown for a group of unit tests that manipulate XML
  96  *
  97  * This function is suitable for being passed as the second argument to the
  98  * \c PCMK__UNIT_TEST macro.
  99  *
 100  * \param[in] state  Ignored
 101  *
 102  * \return 0
 103  */
 104 int
 105 pcmk__xml_test_teardown_group(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 106 {
 107     pcmk__schema_cleanup();
 108     xmlCleanupParser();
 109     return 0;
 110 }
 111 
 112 char *
 113 pcmk__cib_test_copy_cib(const char *in_file)
     /* [previous][next][first][last][top][bottom][index][help] */
 114 {
 115     char *in_path = crm_strdup_printf("%s/%s", getenv("PCMK_CTS_CLI_DIR"), in_file);
 116     char *out_path = NULL;
 117     char *contents = NULL;
 118     int fd;
 119 
 120     /* Copy the CIB over to a temp location so we can modify it. */
 121     out_path = crm_strdup_printf("%s/test-cib.XXXXXX", pcmk__get_tmpdir());
 122 
 123     fd = mkstemp(out_path);
 124     if (fd < 0) {
 125         free(out_path);
 126         return NULL;
 127     }
 128 
 129     if (pcmk__file_contents(in_path, &contents) != pcmk_rc_ok) {
 130         free(out_path);
 131         close(fd);
 132         return NULL;
 133     }
 134 
 135     if (pcmk__write_sync(fd, contents) != pcmk_rc_ok) {
 136         free(out_path);
 137         free(in_path);
 138         free(contents);
 139         close(fd);
 140         return NULL;
 141     }
 142 
 143     setenv("CIB_file", out_path, 1);
 144     return out_path;
 145 }
 146 
 147 void
 148 pcmk__cib_test_cleanup(char *out_path)
     /* [previous][next][first][last][top][bottom][index][help] */
 149 {
 150     unlink(out_path);
 151     free(out_path);
 152     unsetenv("CIB_file");
 153 }
 154 
 155 /*!
 156  * \internal
 157  * \brief Initialize logging for unit testing purposes
 158  *
 159  * \param[in] name      What to use as system name for logging
 160  * \param[in] filename  If not NULL, enable debug logs to this file (intended
 161  *                      for debugging during development rather than committed
 162  *                      unit tests)
 163  */
 164 void
 165 pcmk__test_init_logging(const char *name, const char *filename)
     /* [previous][next][first][last][top][bottom][index][help] */
 166 {
 167     pcmk__cli_init_logging(name, 0);
 168     if (filename != NULL) {
 169         pcmk__add_logfile(filename);
 170         set_crm_log_level(LOG_DEBUG);
 171     }
 172 }
 173 
 174 // LCOV_EXCL_STOP

/* [previous][next][first][last][top][bottom][index][help] */