root/lib/pacemaker/tests/pcmk_ticket/pcmk_ticket_constraints_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. invalid_argument
  5. unknown_ticket
  6. ticket_exists
  7. multiple_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_constraints 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_constraints(&xml, NULL), 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 invalid_argument(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  55 {
  56     assert_int_equal(pcmk_ticket_constraints(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_constraints(&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_constraints(&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_constraints(&xml, NULL), pcmk_rc_ok);
  97     pcmk__assert_validates(xml);
  98 
  99     /* Verify that the XML result has two <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), 2);
 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     freeXpathObject(xpath_obj);
 113     free_xml(xml);
 114 }
 115 
 116 /* There are two kinds of tests in this file:
 117  *
 118  * (1) Those that test what happens if the CIB is not set up correctly, and
 119  * (2) Those that test what happens when run against a CIB.
 120  *
 121  * Therefore, we need two kinds of setup/teardown functions.  We only do
 122  * minimal overall setup for the entire group, and then setup the CIB for
 123  * those tests that need it.
 124  */
 125 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, pcmk__xml_test_teardown_group,
 126                 cmocka_unit_test(cib_not_connected),
 127                 cmocka_unit_test_setup_teardown(invalid_argument, setup_test, teardown_test),
 128                 cmocka_unit_test_setup_teardown(unknown_ticket, setup_test, teardown_test),
 129                 cmocka_unit_test_setup_teardown(ticket_exists, setup_test, teardown_test),
 130                 cmocka_unit_test_setup_teardown(multiple_tickets, setup_test, teardown_test))

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