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

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

DEFINITIONS

This source file includes following definitions.
  1. setup_test
  2. teardown_test
  3. bad_arguments
  4. unknown_ticket
  5. verify_results
  6. attribute_exists
  7. default_no_ticket
  8. default_no_attribute

   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 int
  20 setup_test(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  21 {
  22     cib_path = pcmk__cib_test_copy_cib("tickets.xml");
  23 
  24     if (cib_path == NULL) {
  25         return -1;
  26     }
  27 
  28     return 0;
  29 }
  30 
  31 static int
  32 teardown_test(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  33 {
  34     pcmk__cib_test_cleanup(cib_path);
  35     cib_path = NULL;
  36     return 0;
  37 }
  38 
  39 static void
  40 bad_arguments(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  41 {
  42     xmlNode *xml = NULL;
  43 
  44     assert_int_equal(pcmk_ticket_get_attr(NULL, "ticketA", "XYZ", NULL), EINVAL);
  45 
  46     assert_int_equal(pcmk_ticket_get_attr(&xml, NULL, "attrA", NULL), EINVAL);
  47     pcmk__assert_validates(xml);
  48     free_xml(xml);
  49     xml = NULL;
  50 
  51     assert_int_equal(pcmk_ticket_get_attr(&xml, "ticketA", NULL, NULL), EINVAL);
  52     pcmk__assert_validates(xml);
  53     free_xml(xml);
  54 }
  55 
  56 static void
  57 unknown_ticket(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  58 {
  59     xmlNode *xml = NULL;
  60 
  61     /* Both an unknown ticket and an unknown attribute on a known ticket
  62      * return ENXIO so we can't really differentiate between the two here.
  63      * Still, we'd better test both.
  64      */
  65     assert_int_equal(pcmk_ticket_get_attr(&xml, "XYZ", "attrA", NULL), ENXIO);
  66     pcmk__assert_validates(xml);
  67     free_xml(xml);
  68     xml = NULL;
  69 
  70     assert_int_equal(pcmk_ticket_get_attr(&xml, "ticketA", "XYZ", NULL), ENXIO);
  71     pcmk__assert_validates(xml);
  72     free_xml(xml);
  73 }
  74 
  75 static void
  76 verify_results(xmlNode *xml, const char *ticket_id, const char *attr_name,
     /* [previous][next][first][last][top][bottom][index][help] */
  77                const char *attr_value)
  78 {
  79     xmlNode *node = NULL;
  80     xmlXPathObject *xpath_obj = NULL;
  81 
  82     /* Verify that the XML result has only one <ticket>, and that its ID is
  83      * what we asked for.
  84      */
  85     xpath_obj = xpath_search(xml, "//" PCMK_XE_PACEMAKER_RESULT "/" PCMK_XE_TICKETS "/" PCMK_XE_TICKET);
  86     assert_int_equal(numXpathResults(xpath_obj), 1);
  87 
  88     node = getXpathResult(xpath_obj, 0);
  89     assert_string_equal(crm_element_value(node, PCMK_XA_ID), ticket_id);
  90     freeXpathObject(xpath_obj);
  91 
  92     /* Verify that it has an <attribute> child whose name and value are what
  93      * we expect.
  94      */
  95     xpath_obj = xpath_search(xml, "//" PCMK_XE_PACEMAKER_RESULT "/" PCMK_XE_TICKETS "/" PCMK_XE_TICKET
  96                                   "/" PCMK_XE_ATTRIBUTE);
  97     assert_int_equal(numXpathResults(xpath_obj), 1);
  98 
  99     node = getXpathResult(xpath_obj, 0);
 100     assert_string_equal(crm_element_value(node, PCMK_XA_NAME), attr_name);
 101     assert_string_equal(crm_element_value(node, PCMK_XA_VALUE), attr_value);
 102 
 103     freeXpathObject(xpath_obj);
 104 }
 105 
 106 static void
 107 attribute_exists(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 108 {
 109     xmlNode *xml = NULL;
 110 
 111     assert_int_equal(pcmk_ticket_get_attr(&xml, "ticketA", "owner", NULL), pcmk_rc_ok);
 112     pcmk__assert_validates(xml);
 113 
 114     verify_results(xml, "ticketA", "owner", "1");
 115 
 116     free_xml(xml);
 117 }
 118 
 119 static void
 120 default_no_ticket(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 121 {
 122     xmlNode *xml = NULL;
 123 
 124     assert_int_equal(pcmk_ticket_get_attr(&xml, "ticketX", "ABC", "DEFAULT"), pcmk_rc_ok);
 125     pcmk__assert_validates(xml);
 126 
 127     verify_results(xml, "ticketX", "ABC", "DEFAULT");
 128 
 129     free_xml(xml);
 130 }
 131 
 132 static void
 133 default_no_attribute(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 134 {
 135     xmlNode *xml = NULL;
 136 
 137     assert_int_equal(pcmk_ticket_get_attr(&xml, "ticketA", "ABC", "DEFAULT"), pcmk_rc_ok);
 138     pcmk__assert_validates(xml);
 139 
 140     verify_results(xml, "ticketA", "ABC", "DEFAULT");
 141 
 142     free_xml(xml);
 143 }
 144 
 145 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, NULL,
 146                 cmocka_unit_test_setup_teardown(bad_arguments, setup_test, teardown_test),
 147                 cmocka_unit_test_setup_teardown(unknown_ticket, setup_test, teardown_test),
 148                 cmocka_unit_test_setup_teardown(attribute_exists, setup_test, teardown_test),
 149                 cmocka_unit_test_setup_teardown(default_no_ticket, setup_test, teardown_test),
 150                 cmocka_unit_test_setup_teardown(default_no_attribute, setup_test, teardown_test))

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