File: pcmk__register_format_test.c

package info (click to toggle)
pacemaker 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 68,576 kB
  • sloc: xml: 160,564; ansic: 143,744; python: 5,670; sh: 2,969; makefile: 2,426
file content (63 lines) | stat: -rw-r--r-- 2,003 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * Copyright 2022 the Pacemaker project contributors
 *
 * The version control history for this file may have further details.
 *
 * This source code is licensed under the GNU General Public License version 2
 * or later (GPLv2+) WITHOUT ANY WARRANTY.
 */

#include <crm_internal.h>

#include <crm/common/unittest_internal.h>
#include <crm/common/output_internal.h>

static pcmk__output_t *
null_create_fn(char **argv) {
    return NULL;
}

static pcmk__output_t *
null_create_fn_2(char **argv) {
    return NULL;
}

static void
invalid_params(void **state) {
    pcmk__assert_asserts(pcmk__register_format(NULL, "fake", NULL, NULL));
    pcmk__assert_asserts(pcmk__register_format(NULL, "", null_create_fn, NULL));
    pcmk__assert_asserts(pcmk__register_format(NULL, NULL, null_create_fn, NULL));
}

static void
add_format(void **state) {
    GHashTable *formatters = NULL;
    gpointer value;

    /* For starters, there should be no formatters defined. */
    assert_null(pcmk__output_formatters());

    /* Add a fake formatter and check that it's the only item in the hash table. */
    assert_int_equal(pcmk__register_format(NULL, "fake", null_create_fn, NULL), pcmk_rc_ok);
    formatters = pcmk__output_formatters();
    assert_int_equal(g_hash_table_size(formatters), 1);

    value = g_hash_table_lookup(formatters, "fake");
    assert_ptr_equal(value, null_create_fn);

    /* Add a second fake formatter which should overwrite the first one, leaving
     * only one item in the hash table but pointing at the new function.
     */
    assert_int_equal(pcmk__register_format(NULL, "fake", null_create_fn_2, NULL), pcmk_rc_ok);
    formatters = pcmk__output_formatters();
    assert_int_equal(g_hash_table_size(formatters), 1);

    value = g_hash_table_lookup(formatters, "fake");
    assert_ptr_equal(value, null_create_fn_2);

    pcmk__unregister_formats();
}

PCMK__UNIT_TEST(NULL, NULL,
                cmocka_unit_test(invalid_params),
                cmocka_unit_test(add_format))