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

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