Commit | Line | Data |
---|---|---|
73e9abbe | 1 | /* |
4942c256 | 2 | * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
73e9abbe | 3 | * |
9d16b343 | 4 | * SPDX-License-Identifier: GPL-2.0-only |
73e9abbe | 5 | * |
73e9abbe JR |
6 | */ |
7 | ||
28ab034a JG |
8 | #include "backward-compatibility-group-by.hpp" |
9 | ||
10 | #include <common/time.hpp> | |
11 | ||
73e9abbe JR |
12 | #include <stdbool.h> |
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <string.h> | |
e30c79d2 MJ |
16 | #include <tap/tap.h> |
17 | ||
73e9abbe JR |
18 | /* Number of TAP tests in this file */ |
19 | #define NUM_TESTS_PER_TEST 1 | |
20 | ||
f1494934 | 21 | namespace { |
73e9abbe | 22 | struct test { |
b53d4e59 SM |
23 | const char *stream_path; |
24 | const char *session_name; | |
25 | const char *hostname; | |
26 | const char *creation_time; | |
27 | const char *extra_path; | |
28 | const char *leftover; | |
73e9abbe JR |
29 | bool is_valid; |
30 | }; | |
f1494934 | 31 | } /* namespace */ |
73e9abbe JR |
32 | |
33 | int lttng_opt_quiet; | |
34 | int lttng_opt_mi; | |
35 | int lttng_opt_verbose; | |
36 | ||
37 | struct test tests[] = { | |
28ab034a JG |
38 | /* Default name session streaming. */ |
39 | { "hostname/auto-20190918-164429/ust/uid/1000/64-bit", | |
40 | "auto-20190918-164429", | |
41 | "hostname", | |
42 | "20190918-164429", | |
43 | "", | |
44 | "ust/uid/1000/64-bit", | |
45 | true }, | |
46 | /* Custom default name session */ | |
47 | { "hostname/custom_auto-20190319-120000/ust/uid/1000/64-bit", | |
48 | "custom_auto-20190319-120000", | |
49 | "hostname", | |
50 | "20190319-120000", | |
51 | "", | |
52 | "ust/uid/1000/64-bit", | |
53 | true }, | |
54 | /* Named session streaming */ | |
55 | { "hostname/test-20190918-164709/ust/uid/1000/64-bit", | |
56 | "test", | |
57 | "hostname", | |
58 | "20190918-164709", | |
59 | "", | |
60 | "ust/uid/1000/64-bit", | |
61 | true }, | |
62 | /* Default session snapshot streaming */ | |
63 | { "hostname//snapshot-1-20190918-164829-0/ust//uid/1000/64-bit", | |
64 | "my_session", | |
65 | "hostname", | |
66 | "", | |
67 | "", | |
68 | "snapshot-1-20190918-164829-0/ust//uid/1000/64-bit", | |
69 | true }, | |
70 | /* Named session snapshot streaming */ | |
71 | { "hostname//snapshot-1-20190918-175919-0/ust//uid/1000/64-bit", | |
72 | "my_session", | |
73 | "hostname", | |
74 | "", | |
75 | "", | |
76 | "snapshot-1-20190918-175919-0/ust//uid/1000/64-bit", | |
77 | true }, | |
78 | /* Default name session, live */ | |
79 | { "hostname//auto-20190918-171641/ust/uid/1000/64-bit", | |
80 | "auto-20190918-171641", | |
81 | "hostname", | |
82 | "20190918-171641", | |
83 | "", | |
84 | "ust/uid/1000/64-bit", | |
85 | true }, | |
86 | /* Named session, live */ | |
87 | { "hostname//test-20190918-180333//ust/uid/1000/64-bit", | |
88 | "test-20190918-180333", | |
89 | "hostname", | |
90 | "20190918-180333", | |
91 | "", | |
92 | "/ust/uid/1000/64-bit", | |
93 | true }, | |
94 | /* Default name session, streaming & live , extra path */ | |
95 | { "hostname/extra/path/ust/uid/1000/64-bit", | |
96 | "auto-20190919-122110", | |
97 | "hostname", | |
98 | "20190919-122110", | |
99 | "extra", | |
100 | "path/ust/uid/1000/64-bit", | |
101 | true }, | |
102 | /* Named session, live, extra path */ | |
103 | { "hostname/extra/path/ust/uid/1000/64-bit", | |
104 | "test", | |
105 | "hostname", | |
106 | "", | |
107 | "extra", | |
108 | "path/ust/uid/1000/64-bit", | |
109 | true }, | |
110 | /* Named session, snapshot, extra path */ | |
111 | { "hostname/extra/path/snapshot-1-20190919-140702-0/ust//uid/1000/64-bit", | |
112 | "test", | |
113 | "hostname", | |
114 | "", | |
115 | "extra", | |
116 | "path/snapshot-1-20190919-140702-0/ust//uid/1000/64-bit", | |
117 | true }, | |
118 | /* Corner cases*/ | |
119 | /* Named session with valid datetime in it */ | |
120 | /* Default name session, extra path with session name in it*/ | |
121 | { "hostname/test-20190319-120000-20190918-180921/ust/uid/1000/64-bit", | |
122 | "test-20190319-120000", | |
123 | "hostname", | |
124 | "20190918-180921", | |
125 | "", | |
126 | "ust/uid/1000/64-bit", | |
127 | true }, | |
128 | /* Empty path */ | |
129 | { "", "test", "", "", "", "", false }, | |
130 | /* Path without second token */ | |
131 | { "hostname", "test", "hostname", "", "", "", false }, | |
132 | /* No leftover */ | |
133 | { "hostname/test", "test", "hostname", "", "", "", true }, | |
134 | /* Path with ession name but no datetime */ | |
135 | { "hostname/test/ust/uid/1000/64-bit", | |
136 | "test", | |
137 | "hostname", | |
138 | "", | |
139 | "", | |
140 | "ust/uid/1000/64-bit", | |
141 | true }, | |
73e9abbe JR |
142 | }; |
143 | ||
d2cb4a90 | 144 | static char *craft_expected(struct test *test, time_t relay_session_creation_time) |
73e9abbe JR |
145 | { |
146 | int ret; | |
147 | char *result = NULL; | |
d2cb4a90 JG |
148 | char relay_session_creation_datetime[DATETIME_STR_LEN]; |
149 | ||
150 | ret = time_to_datetime_str(relay_session_creation_time, | |
28ab034a JG |
151 | relay_session_creation_datetime, |
152 | sizeof(relay_session_creation_datetime)); | |
d2cb4a90 JG |
153 | if (ret < 0) { |
154 | result = NULL; | |
155 | goto end; | |
156 | } | |
73e9abbe | 157 | |
28ab034a JG |
158 | ret = asprintf(&result, |
159 | "%s/%s-%s/%s%s%s", | |
160 | test->session_name, | |
161 | test->hostname, | |
162 | test->creation_time[0] == '\0' ? relay_session_creation_datetime : | |
163 | test->creation_time, | |
164 | test->extra_path, | |
165 | test->extra_path[0] != '\0' ? "/" : "", | |
166 | test->leftover); | |
73e9abbe JR |
167 | if (ret < 0) { |
168 | result = NULL; | |
d2cb4a90 | 169 | goto end; |
73e9abbe | 170 | } |
d2cb4a90 | 171 | end: |
73e9abbe JR |
172 | return result; |
173 | } | |
174 | ||
f46376a1 | 175 | int main(void) |
73e9abbe | 176 | { |
9df6c82a | 177 | int i; |
73e9abbe | 178 | int num_test = sizeof(tests) / sizeof(struct test); |
d2cb4a90 | 179 | const time_t test_time = time(NULL); |
73e9abbe JR |
180 | |
181 | plan_tests(NUM_TESTS_PER_TEST * num_test); | |
182 | diag("Backward compatibility utils for lttng-relayd --group-by-session"); | |
d2cb4a90 JG |
183 | |
184 | if (test_time == (time_t) -1) { | |
185 | perror("Failed to sample time"); | |
186 | return exit_status(); | |
187 | } | |
188 | ||
9df6c82a | 189 | for (i = 0; i < num_test; i++) { |
73e9abbe JR |
190 | char *expected = NULL; |
191 | char *result = NULL; | |
192 | ||
d2cb4a90 | 193 | expected = craft_expected(&tests[i], test_time); |
73e9abbe JR |
194 | if (!expected) { |
195 | fprintf(stderr, "Failed to craft expected output\n"); | |
196 | goto loop; | |
197 | } | |
198 | ||
28ab034a JG |
199 | result = backward_compat_group_by_session( |
200 | tests[i].stream_path, tests[i].session_name, test_time); | |
73e9abbe JR |
201 | if (!result && tests[i].is_valid) { |
202 | fprintf(stderr, "Failed to get result\n"); | |
203 | goto loop; | |
204 | } else if (!result && tests[i].is_valid == false) { | |
205 | pass("Returned null as expected"); | |
206 | goto loop; | |
207 | } | |
208 | ||
209 | ok(strncmp(expected, result, strlen(expected)) == 0, | |
28ab034a JG |
210 | "In: %s, out: %s, expected: %s", |
211 | tests[i].stream_path, | |
212 | result, | |
213 | expected); | |
73e9abbe JR |
214 | loop: |
215 | free(expected); | |
216 | free(result); | |
217 | } | |
218 | return exit_status(); | |
219 | } |