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

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