root/lib/pacemaker/tests/pcmk_resource/pcmk_resource_delete_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_input
  5. find_rsc
  6. incorrect_type
  7. correct_type
  8. unknown_resource

   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_resource_delete 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_resource_delete(&xml, "rsc", "primitive"), ENOTCONN);
  29     pcmk__assert_validates(xml);
  30     pcmk__xml_free(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("crm_mon.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_input(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  55 {
  56     xmlNode *xml = NULL;
  57 
  58     /* There is a primitive resource named "Fencing", so we're just checking
  59      * that it returns EINVAL if both parameters aren't given.
  60      */
  61     assert_int_equal(pcmk_resource_delete(&xml, "Fencing", NULL), EINVAL);
  62     pcmk__assert_validates(xml);
  63     pcmk__xml_free(xml);
  64     xml = NULL;
  65 
  66     assert_int_equal(pcmk_resource_delete(&xml, NULL, "primitive"), EINVAL);
  67     pcmk__assert_validates(xml);
  68     pcmk__xml_free(xml);
  69 }
  70 
  71 static xmlNode *
  72 find_rsc(const char *rsc)
     /* [previous][next][first][last][top][bottom][index][help] */
  73 {
  74     GString *xpath = g_string_sized_new(1024);
  75     xmlNode *xml_search = NULL;
  76     cib_t *cib = NULL;
  77 
  78     cib = cib_new();
  79     cib->cmds->signon(cib, crm_system_name, cib_command);
  80 
  81     pcmk__g_strcat(xpath,
  82                    pcmk_cib_xpath_for(PCMK_XE_RESOURCES),
  83                    "//*[@" PCMK_XA_ID "=\"", rsc, "\"]", NULL);
  84 
  85     cib->cmds->query(cib, (const char *) xpath->str, &xml_search, cib_xpath);
  86 
  87     g_string_free(xpath, TRUE);
  88     cib__clean_up_connection(&cib);
  89     return xml_search;
  90 }
  91 
  92 static void
  93 incorrect_type(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  94 {
  95     xmlNode *xml = NULL;
  96     xmlNode *result = NULL;
  97 
  98     /* cib_process_delete returns pcmk_ok even if given the wrong type so
  99      * we have to do an xpath query of the CIB to make sure it's still
 100      * there.
 101      */
 102     assert_int_equal(pcmk_resource_delete(&xml, "Fencing", "clone"), pcmk_rc_ok);
 103     pcmk__assert_validates(xml);
 104     pcmk__xml_free(xml);
 105 
 106     result = find_rsc("Fencing");
 107     assert_non_null(result);
 108 
 109     pcmk__xml_free(result);
 110 }
 111 
 112 static void
 113 correct_type(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 114 {
 115     xmlNode *xml = NULL;
 116     xmlNode *result = NULL;
 117 
 118     assert_int_equal(pcmk_resource_delete(&xml, "Fencing", "primitive"), pcmk_rc_ok);
 119     pcmk__assert_validates(xml);
 120     pcmk__xml_free(xml);
 121 
 122     result = find_rsc("Fencing");
 123     assert_null(result);
 124 
 125     pcmk__xml_free(result);
 126 }
 127 
 128 static void
 129 unknown_resource(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
 130 {
 131     xmlNode *xml = NULL;
 132 
 133     /* cib_process_delete returns pcmk_ok even if asked to delete something
 134      * that doesn't exist.
 135      */
 136     assert_int_equal(pcmk_resource_delete(&xml, "no_such_resource", "primitive"), pcmk_rc_ok);
 137     pcmk__assert_validates(xml);
 138     pcmk__xml_free(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, pcmk__xml_test_teardown_group,
 151                 cmocka_unit_test(cib_not_connected),
 152                 cmocka_unit_test_setup_teardown(bad_input, setup_test, teardown_test),
 153                 cmocka_unit_test_setup_teardown(incorrect_type, setup_test, teardown_test),
 154                 cmocka_unit_test_setup_teardown(correct_type, setup_test, teardown_test),
 155                 cmocka_unit_test_setup_teardown(unknown_resource, setup_test, teardown_test))

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