root/lib/pacemaker/tests/pcmk_ticket/pcmk__get_ticket_state_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. cib_not_connected
  2. setup_test
  3. teardown_test
  4. bad_arguments
  5. unknown_ticket
  6. ticket_exists
  7. multiple_tickets
  8. duplicate_tickets

   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 General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <crm/cib/internal.h>
  13 #include <crm/common/unittest_internal.h>
  14 #include <crm/common/xml.h>
  15 #include <pacemaker.h>
  16 
  17 #include <libxml/xpath.h>                   // xmlXPathObject, etc.
  18 
  19 #include <pacemaker-internal.h>
  20 
  21 static char *cib_path = NULL;
  22 
  23 static void
  24 cib_not_connected(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26     xmlNode *xml = NULL;
  27     cib_t *cib = cib_new();
  28 
  29     /* Without any special setup, cib_new() here will use the native CIB which
  30      * means IPC calls.  But there's nothing listening for those calls, so
  31      * signon() will return ENOTCONN.  Check that we handle that.
  32      */
  33     assert_int_equal(pcmk__get_ticket_state(cib, "ticketA", &xml), ENOTCONN);
  34     cib__clean_up_connection(&cib);
  35 }
  36 
  37 static int
  38 setup_test(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  39 {
  40     cib_path = pcmk__cib_test_copy_cib("tickets.xml");
  41 
  42     if (cib_path == NULL) {
  43         return -1;
  44     }
  45 
  46     return 0;
  47 }
  48 
  49 static int
  50 teardown_test(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52     pcmk__cib_test_cleanup(cib_path);
  53     cib_path = NULL;
  54     return 0;
  55 }
  56 
  57 static void
  58 bad_arguments(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60     xmlNode *xml = NULL;
  61     cib_t *cib = cib_new();
  62 
  63     cib->cmds->signon(cib, crm_system_name, cib_command);
  64 
  65     pcmk__assert_asserts(pcmk__get_ticket_state(NULL, "ticketA", &xml));
  66     pcmk__assert_asserts(pcmk__get_ticket_state(cib, "ticketA", NULL));
  67 
  68     cib__clean_up_connection(&cib);
  69 }
  70 
  71 static void
  72 unknown_ticket(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  73 {
  74     xmlNode *xml = NULL;
  75     cib_t *cib = cib_new();
  76 
  77     cib->cmds->signon(cib, crm_system_name, cib_command);
  78 
  79     assert_int_equal(pcmk__get_ticket_state(cib, "XYZ", &xml), ENXIO);
  80 
  81     pcmk__xml_free(xml);
  82     cib__clean_up_connection(&cib);
  83 }
  84 
  85 static void
  86 ticket_exists(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  87 {
  88     xmlNode *xml = NULL;
  89     xmlXPathObject *xpath_obj = NULL;
  90     cib_t *cib = cib_new();
  91 
  92     cib->cmds->signon(cib, crm_system_name, cib_command);
  93 
  94     assert_int_equal(pcmk__get_ticket_state(cib, "ticketA", &xml), pcmk_rc_ok);
  95 
  96     /* Verify that the XML result has only one <ticket>, and that its ID is
  97      * what we asked for.
  98      */
  99     xpath_obj = pcmk__xpath_search(xml->doc,
 100                                    "//" PCMK__XE_TICKET_STATE
 101                                    "[@" PCMK_XA_ID "=\"ticketA\"]");
 102     assert_int_equal(pcmk__xpath_num_results(xpath_obj), 1);
 103 
 104     xmlXPathFreeObject(xpath_obj);
 105     pcmk__xml_free(xml);
 106     cib__clean_up_connection(&cib);
 107 }
 108 
 109 static void
 110 multiple_tickets(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 111 {
 112     xmlNode *xml = NULL;
 113     xmlNode *ticket_node = NULL;
 114     xmlXPathObject *xpath_obj = NULL;
 115     cib_t *cib = cib_new();
 116 
 117     cib->cmds->signon(cib, crm_system_name, cib_command);
 118 
 119     assert_int_equal(pcmk__get_ticket_state(cib, NULL, &xml), pcmk_rc_ok);
 120 
 121     /* Verify that the XML result has four <ticket> elements, and that their
 122      * IDs are as expected.
 123      */
 124     xpath_obj = pcmk__xpath_search(xml->doc, "//" PCMK__XE_TICKET_STATE);
 125 
 126     assert_int_equal(pcmk__xpath_num_results(xpath_obj), 4);
 127 
 128     ticket_node = pcmk__xpath_result(xpath_obj, 0);
 129     assert_non_null(ticket_node);
 130     assert_string_equal(crm_element_value(ticket_node, PCMK_XA_ID), "ticketA");
 131 
 132     ticket_node = pcmk__xpath_result(xpath_obj, 1);
 133     assert_non_null(ticket_node);
 134     assert_string_equal(crm_element_value(ticket_node, PCMK_XA_ID), "ticketB");
 135 
 136     ticket_node = pcmk__xpath_result(xpath_obj, 2);
 137     assert_non_null(ticket_node);
 138     assert_string_equal(crm_element_value(ticket_node, PCMK_XA_ID), "ticketC");
 139 
 140     ticket_node = pcmk__xpath_result(xpath_obj, 3);
 141     assert_non_null(ticket_node);
 142     assert_string_equal(crm_element_value(ticket_node, PCMK_XA_ID), "ticketC");
 143 
 144     xmlXPathFreeObject(xpath_obj);
 145     pcmk__xml_free(xml);
 146     cib__clean_up_connection(&cib);
 147 }
 148 
 149 static void
 150 duplicate_tickets(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 151 {
 152     xmlNode *xml = NULL;
 153     xmlXPathObject *xpath_obj = NULL;
 154     cib_t *cib = cib_new();
 155 
 156     cib->cmds->signon(cib, crm_system_name, cib_command);
 157 
 158     assert_int_equal(pcmk__get_ticket_state(cib, "ticketC", &xml), pcmk_rc_duplicate_id);
 159 
 160     /* Verify that the XML result has two <ticket> elements, and that their
 161      * IDs are as expected.
 162      */
 163     xpath_obj = pcmk__xpath_search(xml->doc,
 164                                    "//" PCMK__XE_TICKET_STATE
 165                                    "[@" PCMK_XA_ID "=\"ticketC\"]");
 166 
 167     assert_int_equal(pcmk__xpath_num_results(xpath_obj), 2);
 168     xmlXPathFreeObject(xpath_obj);
 169     pcmk__xml_free(xml);
 170     cib__clean_up_connection(&cib);
 171 }
 172 
 173 /* There are two kinds of tests in this file:
 174  *
 175  * (1) Those that test what happens if the CIB is not set up correctly, and
 176  * (2) Those that test what happens when run against a CIB.
 177  *
 178  * Therefore, we need two kinds of setup/teardown functions.  We only do
 179  * minimal overall setup for the entire group, and then setup the CIB for
 180  * those tests that need it.
 181  */
 182 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group,
 183                 cmocka_unit_test_setup_teardown(cib_not_connected, setup_test, teardown_test),
 184                 cmocka_unit_test_setup_teardown(bad_arguments, setup_test, teardown_test),
 185                 cmocka_unit_test_setup_teardown(unknown_ticket, setup_test, teardown_test),
 186                 cmocka_unit_test_setup_teardown(ticket_exists, setup_test, teardown_test),
 187                 cmocka_unit_test_setup_teardown(multiple_tickets, setup_test, teardown_test),
 188                 cmocka_unit_test_setup_teardown(duplicate_tickets, setup_test, teardown_test))

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