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

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