pacemaker 2.1.4-dc6eb4362e
Scalable High-Availability cluster resource manager
pcmk_hostname_test.c
Go to the documentation of this file.
1/*
2 * Copyright 2021 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 Lesser General Public License
7 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11#include "mock_private.h"
12
13#include <stdarg.h>
14#include <stddef.h>
15#include <stdint.h>
16#include <stdlib.h>
17#include <string.h>
18#include <setjmp.h>
19#include <cmocka.h>
20
21#include <sys/utsname.h>
22
23int
24__wrap_uname(struct utsname *buf)
25{
26 int retval = mock_type(int);
27
28 if (retval == 0) {
29 strcpy(buf->nodename, mock_ptr_type(char *));
30 }
31
32 return retval;
33}
34
35static void
36uname_succeeded_test(void **state)
37{
38 char *retval;
39
40 will_return(__wrap_uname, 0); // uname() return value
41 will_return(__wrap_uname, "somename"); // uname() buf->nodename
42
43 retval = pcmk_hostname();
44 assert_non_null(retval);
45 assert_string_equal("somename", retval);
46
47 free(retval);
48}
49
50static void
51uname_failed_test(void **state)
52{
53 char *retval;
54
55 will_return(__wrap_uname, -1); // uname() return value
56
57 retval = pcmk_hostname();
58 assert_null(retval);
59}
60
61int
62main(int argc, char **argv)
63{
64 const struct CMUnitTest tests[] = {
65 cmocka_unit_test(uname_succeeded_test),
66 cmocka_unit_test(uname_failed_test),
67 };
68
69 cmocka_set_message_output(CM_OUTPUT_TAP);
70 return cmocka_run_group_tests(tests, NULL, NULL);
71}
char * pcmk_hostname(void)
Get the local hostname.
Definition: utils.c:505
int main(int argc, char **argv)
int __wrap_uname(struct utsname *buf)