File: pack_event_cond-test.c

package info (click to toggle)
slurm-wlm-contrib 24.11.5-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 50,596 kB
  • sloc: ansic: 529,598; exp: 64,795; python: 17,051; sh: 9,411; javascript: 6,528; makefile: 4,030; perl: 3,762; pascal: 131
file content (176 lines) | stat: -rw-r--r-- 4,716 bytes parent folder | download | duplicates (5)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <check.h>
#include <stdio.h>
#include <stdlib.h>

#include "src/common/slurmdb_pack.h"
#include "src/common/xmalloc.h"
#include "src/common/slurm_protocol_common.h"
#include "src/common/slurm_protocol_defs.h"
#include "src/common/list.h"
#include "src/common/pack.h"

START_TEST(invalid_protocol)
{
	int rc;
	uint32_t x;

	slurmdb_event_cond_t *cond_rec = xmalloc(sizeof(slurmdb_event_cond_t));
	buf_t *buf = init_buf(1024);

	pack32(22, buf);
	set_buf_offset(buf, 0);

	slurmdb_event_cond_t *acr;

	slurmdb_pack_event_cond((void **)&cond_rec, 0, buf);
	unpack32(&x, buf);
	rc = slurmdb_unpack_event_cond((void **)&acr, 0, buf);
	ck_assert_int_eq(rc, SLURM_ERROR);
	ck_assert(x == 22);
	free_buf(buf);
	slurmdb_destroy_event_cond(cond_rec);
}
END_TEST

static void _init_event_cond(slurmdb_event_cond_t *pack)
{
	pack->cpus_max = 1;
	pack->cpus_min = 2;
	pack->event_type = 3;
	pack->period_end = 5;
	pack->period_start = 6;

	pack->node_list = xstrdup("node1,node2");

	pack->cluster_list = list_create(xfree_ptr);
	list_append(pack->cluster_list, "cluster1");
	list_append(pack->cluster_list, "cluster2");

	pack->format_list = list_create(xfree_ptr);
	list_append(pack->format_list, "format1");
	list_append(pack->format_list, "format2");

	pack->reason_list = list_create(xfree_ptr);
	list_append(pack->reason_list, "reason1");
	list_append(pack->reason_list, "reason2");

	pack->reason_uid_list = list_create(xfree_ptr);
	list_append(pack->reason_uid_list, "uid1");
	list_append(pack->reason_uid_list, "uid2");

	pack->state_list = list_create(xfree_ptr);
	list_append(pack->state_list, "state1");
	list_append(pack->state_list, "state2");
}

static void _test_list_str_eq(list_t *a, list_t *b)
{
	char *str;

	if (!a && !b)
		return;

	ck_assert(a != NULL);
	ck_assert(b != NULL);

	ck_assert(list_count(a) == list_count(b));

	list_itr_t *itr_a = list_iterator_create(a);
	while ((str = list_next(itr_a)))
		ck_assert(list_find_first(b, slurm_find_char_in_list, str)
			  != NULL);
}

static void _test_cond_eq(uint16_t protocol_version,
			 slurmdb_event_cond_t *pack)
{
	int rc;
	buf_t *buf = init_buf(1024);
	slurmdb_pack_event_cond(pack, protocol_version, buf);
	set_buf_offset(buf, 0);

	slurmdb_event_cond_t *unpack;
	rc = slurmdb_unpack_event_cond((void **)&unpack, protocol_version, buf);
	ck_assert(rc == SLURM_SUCCESS);
	ck_assert(pack->cpus_max == unpack->cpus_max);
	ck_assert(pack->cpus_min == unpack->cpus_min);
	ck_assert(pack->event_type == unpack->event_type);
	ck_assert(pack->period_end == unpack->period_end);
	ck_assert(pack->period_start == unpack->period_start);

	ck_assert_str_eq(pack->node_list, unpack->node_list);

	_test_list_str_eq(pack->cluster_list, unpack->cluster_list);
	/**
	 * The unpack for format_list actually won't create a list if there are
	 * 0 count in the list.
	 */
	_test_list_str_eq(pack->format_list, unpack->format_list);
	_test_list_str_eq(pack->reason_list, unpack->reason_list);
	_test_list_str_eq(pack->reason_uid_list, unpack->reason_uid_list);
	_test_list_str_eq(pack->state_list, unpack->state_list);

	free_buf(buf);
	slurmdb_destroy_event_cond(unpack);
}

static void _run_test(uint16_t protocol_version)
{
	slurmdb_event_cond_t pack = {0};

	_init_event_cond(&pack);
	_test_cond_eq(protocol_version, &pack);
}

START_TEST(pack_current_event_cond)
{
	_run_test(SLURM_PROTOCOL_VERSION);
}
END_TEST

START_TEST(pack_last_event_cond)
{
	_run_test(SLURM_ONE_BACK_PROTOCOL_VERSION);
}
END_TEST

START_TEST(pack_min_event_cond)
{
	_run_test(SLURM_MIN_PROTOCOL_VERSION);
}
END_TEST

/*****************************************************************************
 * TEST SUITE                                                                *
 ****************************************************************************/

Suite *suite(void)
{
	Suite *s = suite_create("Pack slurmdb_event_cond_t");
	TCase *tc_core = tcase_create("Pack slurmdb_event_cond_t");
	tcase_add_test(tc_core, invalid_protocol);
	tcase_add_test(tc_core, pack_current_event_cond);
	tcase_add_test(tc_core, pack_last_event_cond);
	tcase_add_test(tc_core, pack_min_event_cond);
	suite_add_tcase(s, tc_core);
	return s;
}

/*****************************************************************************
 * TEST RUNNER                                                               *
 ****************************************************************************/

int main(void)
{
	int number_failed;
	SRunner *sr = srunner_create(suite());

	//srunner_set_fork_status(sr, CK_NOFORK);

	srunner_run_all(sr, CK_VERBOSE);
	//srunner_run_all(sr, CK_NORMAL);
	number_failed = srunner_ntests_failed(sr);
	srunner_free(sr);

	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}