summaryrefslogtreecommitdiffstats
path: root/lib/common/unittest.c
blob: 0e63915f7b7a724ce7327798219a7a7d6b72eef0 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright 2024 the Pacemaker project contributors
 *
 * The version control history for this file may have further details.
 *
 * This source code is licensed under the GNU Lesser General Public License
 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
 */

#include <crm_internal.h>

#include <crm/common/unittest_internal.h>

#include <stdlib.h>
#include <unistd.h>

// LCOV_EXCL_START

void
pcmk__assert_validates(xmlNode *xml)
{
    const char *schema_dir = NULL;
    char *cmd = NULL;
    gchar *out = NULL;
    gchar *err = NULL;
    gint status;
    GError *gerr = NULL;
    char *xmllint_input = crm_strdup_printf("%s/test-xmllint.XXXXXX",
                                            pcmk__get_tmpdir());
    int fd;
    int rc;

    fd = mkstemp(xmllint_input);
    if (fd < 0) {
        fail_msg("Could not create temp file: %s", strerror(errno));
    }

    rc = pcmk__xml2fd(fd, xml);
    if (rc != pcmk_rc_ok) {
        unlink(xmllint_input);
        fail_msg("Could not write temp file: %s", pcmk_rc_str(rc));
    }

    close(fd);

    /* This should be set as part of AM_TESTS_ENVIRONMENT in Makefile.am. */
    schema_dir = getenv("PCMK_schema_directory");
    if (schema_dir == NULL) {
        unlink(xmllint_input);
        fail_msg("PCMK_schema_directory is not set in test environment");
    }

    cmd = crm_strdup_printf("xmllint --relaxng %s/api/api-result.rng %s",
                            schema_dir, xmllint_input);

    if (!g_spawn_command_line_sync(cmd, &out, &err, &status, &gerr)) {
        unlink(xmllint_input);
        fail_msg("Error occurred when performing validation: %s", gerr->message);
    }

    if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
        unlink(xmllint_input);
        fail_msg("XML validation failed: %s\n%s\n", out, err);
    }

    free(cmd);
    g_free(out);
    g_free(err);
    unlink(xmllint_input);
    free(xmllint_input);
}

int
pcmk__xml_test_setup_group(void **state)
{
    /* This needs to be run before we run unit tests that manipulate XML.
     * Without it, document private data won't get created, which can cause
     * segmentation faults or assertions in functions related to change
     * tracking and ACLs. There's no harm in doing this before all tests.
     */
    crm_xml_init();
    return 0;
}

char *
pcmk__cib_test_copy_cib(const char *in_file)
{
    char *in_path = crm_strdup_printf("%s/%s", getenv("PCMK_CTS_CLI_DIR"), in_file);
    char *out_path = NULL;
    char *contents = NULL;
    int fd;

    /* Copy the CIB over to a temp location so we can modify it. */
    out_path = crm_strdup_printf("%s/test-cib.XXXXXX", pcmk__get_tmpdir());

    fd = mkstemp(out_path);
    if (fd < 0) {
        free(out_path);
        return NULL;
    }

    if (pcmk__file_contents(in_path, &contents) != pcmk_rc_ok) {
        free(out_path);
        close(fd);
        return NULL;
    }

    if (pcmk__write_sync(fd, contents) != pcmk_rc_ok) {
        free(out_path);
        free(in_path);
        free(contents);
        close(fd);
        return NULL;
    }

    setenv("CIB_file", out_path, 1);
    return out_path;
}

void
pcmk__cib_test_cleanup(char *out_path)
{
    unlink(out_path);
    free(out_path);
    unsetenv("CIB_file");
}

// LCOV_EXCL_STOP