root/lib/common/tests/xml/pcmk__xe_copy_attrs_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. null_args
  2. no_source_attrs
  3. copy_one
  4. copy_multiple
  5. overwrite
  6. no_overwrite
  7. score_update
  8. no_score_update

   1  /*
   2  * Copyright 2022-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/common/unittest_internal.h>
  13 
  14 #include <glib.h>
  15 
  16 static void
  17 null_args(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  18 {
  19     // This test dumps core via CRM_CHECK()
  20     xmlNode *xml = pcmk__xe_create(NULL, "test");
  21 
  22     assert_int_equal(pcmk__xe_copy_attrs(NULL, NULL, pcmk__xaf_none), EINVAL);
  23     assert_int_equal(pcmk__xe_copy_attrs(NULL, xml, pcmk__xaf_none), EINVAL);
  24     assert_int_equal(pcmk__xe_copy_attrs(xml, NULL, pcmk__xaf_none), EINVAL);
  25     assert_ptr_equal(xml->properties, NULL);
  26 
  27     free_xml(xml);
  28 }
  29 
  30 static void
  31 no_source_attrs(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  32 {
  33     xmlNode *src = pcmk__xe_create(NULL, "test");
  34     xmlNode *target = pcmk__xe_create(NULL, "test");
  35 
  36     // Ensure copying from empty source doesn't create target properties
  37     assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
  38                      pcmk_rc_ok);
  39     assert_ptr_equal(target->properties, NULL);
  40 
  41     // Ensure copying from empty source doesn't delete target attributes
  42     crm_xml_add(target, "attr", "value");
  43     assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
  44                      pcmk_rc_ok);
  45     assert_string_equal(crm_element_value(target, "attr"), "value");
  46 
  47     free_xml(src);
  48     free_xml(target);
  49 }
  50 
  51 static void
  52 copy_one(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  53 {
  54     xmlNode *src = pcmk__xe_create(NULL, "test");
  55     xmlNode *target = pcmk__xe_create(NULL, "test");
  56 
  57     crm_xml_add(src, "attr", "value");
  58 
  59     assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
  60                      pcmk_rc_ok);
  61     assert_string_equal(crm_element_value(src, "attr"),
  62                         crm_element_value(target, "attr"));
  63 
  64     free_xml(src);
  65     free_xml(target);
  66 }
  67 
  68 static void
  69 copy_multiple(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  70 {
  71     xmlNode *src = pcmk__xe_create(NULL, "test");
  72     xmlNode *target = pcmk__xe_create(NULL, "test");
  73 
  74     pcmk__xe_set_props(src,
  75                        "attr1", "value1",
  76                        "attr2", "value2",
  77                        "attr3", "value3",
  78                        NULL);
  79 
  80     assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
  81                      pcmk_rc_ok);
  82     assert_string_equal(crm_element_value(src, "attr1"),
  83                         crm_element_value(target, "attr1"));
  84     assert_string_equal(crm_element_value(src, "attr2"),
  85                         crm_element_value(target, "attr2"));
  86     assert_string_equal(crm_element_value(src, "attr3"),
  87                         crm_element_value(target, "attr3"));
  88 
  89     free_xml(src);
  90     free_xml(target);
  91 }
  92 
  93 static void
  94 overwrite(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96     xmlNode *src = pcmk__xe_create(NULL, "test");
  97     xmlNode *target = pcmk__xe_create(NULL, "test");
  98 
  99     crm_xml_add(src, "attr", "src_value");
 100     crm_xml_add(target, "attr", "target_value");
 101 
 102     // Overwrite enabled by default
 103     assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
 104                      pcmk_rc_ok);
 105     assert_string_equal(crm_element_value(src, "attr"),
 106                         crm_element_value(target, "attr"));
 107     free_xml(src);
 108     free_xml(target);
 109 }
 110 
 111 static void
 112 no_overwrite(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 113 {
 114     xmlNode *src = pcmk__xe_create(NULL, "test");
 115     xmlNode *target = pcmk__xe_create(NULL, "test");
 116 
 117     crm_xml_add(src, "attr", "src_value");
 118     crm_xml_add(target, "attr", "target_value");
 119 
 120     assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_no_overwrite),
 121                      pcmk_rc_ok);
 122     assert_string_not_equal(crm_element_value(src, "attr"),
 123                             crm_element_value(target, "attr"));
 124 
 125     // no_overwrite doesn't prevent copy if there's no conflict
 126     pcmk__xe_remove_attr(target, "attr");
 127 
 128     assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_no_overwrite),
 129                      pcmk_rc_ok);
 130     assert_string_equal(crm_element_value(src, "attr"),
 131                         crm_element_value(target, "attr"));
 132 
 133     free_xml(src);
 134     free_xml(target);
 135 }
 136 
 137 static void
 138 score_update(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 139 {
 140     xmlNode *src = pcmk__xe_create(NULL, "test");
 141     xmlNode *target = pcmk__xe_create(NULL, "test");
 142 
 143     crm_xml_add(src, "plus_plus_attr", "plus_plus_attr++");
 144     crm_xml_add(src, "plus_two_attr", "plus_two_attr+=2");
 145     crm_xml_add(target, "plus_plus_attr", "1");
 146     crm_xml_add(target, "plus_two_attr", "1");
 147 
 148     assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_score_update),
 149                      pcmk_rc_ok);
 150     assert_string_equal(crm_element_value(target, "plus_plus_attr"), "2");
 151     assert_string_equal(crm_element_value(target, "plus_two_attr"), "3");
 152 
 153     free_xml(src);
 154     free_xml(target);
 155 }
 156 
 157 static void
 158 no_score_update(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 159 {
 160     xmlNode *src = pcmk__xe_create(NULL, "test");
 161     xmlNode *target = pcmk__xe_create(NULL, "test");
 162 
 163     crm_xml_add(src, "plus_plus_attr", "plus_plus_attr++");
 164     crm_xml_add(src, "plus_two_attr", "plus_two_attr+=2");
 165     crm_xml_add(target, "plus_plus_attr", "1");
 166     crm_xml_add(target, "plus_two_attr", "1");
 167 
 168     // Score update disabled by default
 169     assert_int_equal(pcmk__xe_copy_attrs(target, src, pcmk__xaf_none),
 170                      pcmk_rc_ok);
 171     assert_string_equal(crm_element_value(target, "plus_plus_attr"),
 172                         "plus_plus_attr++");
 173     assert_string_equal(crm_element_value(target, "plus_two_attr"),
 174                         "plus_two_attr+=2");
 175 
 176     free_xml(src);
 177     free_xml(target);
 178 }
 179 
 180 PCMK__UNIT_TEST(pcmk__xml_test_setup_group, NULL,
 181                 cmocka_unit_test(null_args),
 182                 cmocka_unit_test(no_source_attrs),
 183                 cmocka_unit_test(copy_one),
 184                 cmocka_unit_test(copy_multiple),
 185                 cmocka_unit_test(overwrite),
 186                 cmocka_unit_test(no_overwrite),
 187                 cmocka_unit_test(score_update),
 188                 cmocka_unit_test(no_score_update));

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