]> git.proxmox.com Git - mirror_frr.git/blame - tests/bgp_mpath_test.c
bgp: Modify output to be a bit clearer
[mirror_frr.git] / tests / bgp_mpath_test.c
CommitLineData
42ea6851
JB
1/* $QuaggaId: Format:%an, %ai, %h$ $
2 *
3 * BGP Multipath Unit Test
4 * Copyright (C) 2010 Google Inc.
5 *
6 * This file is part of Quagga
7 *
8 * Quagga is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * Quagga is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Quagga; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24#include <zebra.h>
25
1bf9f027 26#include "qobj.h"
42ea6851
JB
27#include "vty.h"
28#include "stream.h"
29#include "privs.h"
30#include "linklist.h"
31#include "memory.h"
32#include "zclient.h"
3f9c7369 33#include "queue.h"
039f3a34 34#include "filter.h"
42ea6851
JB
35
36#include "bgpd/bgpd.h"
42ea6851
JB
37#include "bgpd/bgp_table.h"
38#include "bgpd/bgp_route.h"
96450faf 39#include "bgpd/bgp_attr.h"
37d361e7 40#include "bgpd/bgp_nexthop.h"
96450faf 41#include "bgpd/bgp_mpath.h"
42ea6851
JB
42
43#define VT100_RESET "\x1b[0m"
44#define VT100_RED "\x1b[31m"
45#define VT100_GREEN "\x1b[32m"
46#define VT100_YELLOW "\x1b[33m"
47#define OK VT100_GREEN "OK" VT100_RESET
48#define FAILED VT100_RED "failed" VT100_RESET
49
50#define TEST_PASSED 0
51#define TEST_FAILED -1
52
53#define EXPECT_TRUE(expr, res) \
54 if (!(expr)) \
55 { \
56 printf ("Test failure in %s line %u: %s\n", \
57 __FUNCTION__, __LINE__, #expr); \
58 (res) = TEST_FAILED; \
59 }
60
61typedef struct testcase_t__ testcase_t;
62
63typedef int (*test_setup_func)(testcase_t *);
64typedef int (*test_run_func)(testcase_t *);
65typedef int (*test_cleanup_func)(testcase_t *);
66
67struct testcase_t__ {
68 const char *desc;
69 void *test_data;
70 void *verify_data;
71 void *tmp_data;
72 test_setup_func setup;
73 test_run_func run;
74 test_cleanup_func cleanup;
75};
76
77/* need these to link in libbgp */
78struct thread_master *master = NULL;
79struct zclient *zclient;
80struct zebra_privs_t bgpd_privs =
81{
82 .user = NULL,
83 .group = NULL,
84 .vty_group = NULL,
85};
86
87static int tty = 0;
88
89/* Create fake bgp instance */
90static struct bgp *
91bgp_create_fake (as_t *as, const char *name)
92{
93 struct bgp *bgp;
94 afi_t afi;
95 safi_t safi;
96
97 if ( (bgp = XCALLOC (MTYPE_BGP, sizeof (struct bgp))) == NULL)
98 return NULL;
99
100 bgp_lock (bgp);
101 //bgp->peer_self = peer_new (bgp);
102 //bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static announcement");
103
104 bgp->peer = list_new ();
105 //bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp;
106
107 bgp->group = list_new ();
108 //bgp->group->cmp = (int (*)(void *, void *)) peer_group_cmp;
109
42ea6851
JB
110 for (afi = AFI_IP; afi < AFI_MAX; afi++)
111 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
112 {
113 bgp->route[afi][safi] = bgp_table_init (afi, safi);
114 bgp->aggregate[afi][safi] = bgp_table_init (afi, safi);
115 bgp->rib[afi][safi] = bgp_table_init (afi, safi);
d5b77cc2
DS
116 bgp->maxpaths[afi][safi].maxpaths_ebgp = MULTIPATH_NUM;
117 bgp->maxpaths[afi][safi].maxpaths_ibgp = MULTIPATH_NUM;
42ea6851
JB
118 }
119
37d361e7 120 bgp_scan_init (bgp);
42ea6851
JB
121 bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
122 bgp->default_holdtime = BGP_DEFAULT_HOLDTIME;
123 bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE;
124 bgp->restart_time = BGP_DEFAULT_RESTART_TIME;
125 bgp->stalepath_time = BGP_DEFAULT_STALEPATH_TIME;
126
127 bgp->as = *as;
128
129 if (name)
130 bgp->name = strdup (name);
131
132 return bgp;
133}
134
135/*=========================================================
136 * Testcase for maximum-paths configuration
137 */
138static int
139setup_bgp_cfg_maximum_paths (testcase_t *t)
140{
141 as_t asn = 1;
142 t->tmp_data = bgp_create_fake (&asn, NULL);
143 if (!t->tmp_data)
144 return -1;
145 return 0;
146}
147
148static int
149run_bgp_cfg_maximum_paths (testcase_t *t)
150{
151 afi_t afi;
152 safi_t safi;
153 struct bgp *bgp;
154 int api_result;
155 int test_result = TEST_PASSED;
156
157 bgp = t->tmp_data;
158 for (afi = AFI_IP; afi < AFI_MAX; afi++)
159 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
160 {
161 /* test bgp_maximum_paths_set */
5e242b0d 162 api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_EBGP, 10, 0);
42ea6851 163 EXPECT_TRUE (api_result == 0, test_result);
5e242b0d 164 api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_IBGP, 10, 0);
42ea6851
JB
165 EXPECT_TRUE (api_result == 0, test_result);
166 EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ebgp == 10, test_result);
167 EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ibgp == 10, test_result);
168
169 /* test bgp_maximum_paths_unset */
170 api_result = bgp_maximum_paths_unset (bgp, afi, safi, BGP_PEER_EBGP);
171 EXPECT_TRUE (api_result == 0, test_result);
172 api_result = bgp_maximum_paths_unset (bgp, afi, safi, BGP_PEER_IBGP);
173 EXPECT_TRUE (api_result == 0, test_result);
174 EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ebgp ==
d5b77cc2 175 MULTIPATH_NUM), test_result);
42ea6851 176 EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ibgp ==
d5b77cc2 177 MULTIPATH_NUM), test_result);
42ea6851
JB
178 }
179
180 return test_result;
181}
182
183static int
184cleanup_bgp_cfg_maximum_paths (testcase_t *t)
185{
186 return bgp_delete ((struct bgp *)t->tmp_data);
187}
188
189testcase_t test_bgp_cfg_maximum_paths = {
190 .desc = "Test bgp maximum-paths config",
191 .setup = setup_bgp_cfg_maximum_paths,
192 .run = run_bgp_cfg_maximum_paths,
193 .cleanup = cleanup_bgp_cfg_maximum_paths,
194};
195
96450faf
JB
196/*=========================================================
197 * Testcase for bgp_mp_list
198 */
de8d5dff
JB
199struct peer test_mp_list_peer[] = {
200 { .local_as = 1, .as = 2 },
201 { .local_as = 1, .as = 2 },
202 { .local_as = 1, .as = 2 },
203 { .local_as = 1, .as = 2 },
204 { .local_as = 1, .as = 2 },
205};
96450faf
JB
206int test_mp_list_peer_count = sizeof (test_mp_list_peer)/ sizeof (struct peer);
207struct attr test_mp_list_attr[4];
208struct bgp_info test_mp_list_info[] = {
209 { .peer = &test_mp_list_peer[0], .attr = &test_mp_list_attr[0] },
210 { .peer = &test_mp_list_peer[1], .attr = &test_mp_list_attr[1] },
211 { .peer = &test_mp_list_peer[2], .attr = &test_mp_list_attr[1] },
212 { .peer = &test_mp_list_peer[3], .attr = &test_mp_list_attr[2] },
213 { .peer = &test_mp_list_peer[4], .attr = &test_mp_list_attr[3] },
214};
215int test_mp_list_info_count =
216 sizeof (test_mp_list_info)/sizeof (struct bgp_info);
217
218static int
219setup_bgp_mp_list (testcase_t *t)
220{
221 test_mp_list_attr[0].nexthop.s_addr = 0x01010101;
222 test_mp_list_attr[1].nexthop.s_addr = 0x02020202;
223 test_mp_list_attr[2].nexthop.s_addr = 0x03030303;
224 test_mp_list_attr[3].nexthop.s_addr = 0x04040404;
225
226 if ((test_mp_list_peer[0].su_remote = sockunion_str2su ("1.1.1.1")) == NULL)
227 return -1;
228 if ((test_mp_list_peer[1].su_remote = sockunion_str2su ("2.2.2.2")) == NULL)
229 return -1;
230 if ((test_mp_list_peer[2].su_remote = sockunion_str2su ("3.3.3.3")) == NULL)
231 return -1;
232 if ((test_mp_list_peer[3].su_remote = sockunion_str2su ("4.4.4.4")) == NULL)
233 return -1;
234 if ((test_mp_list_peer[4].su_remote = sockunion_str2su ("5.5.5.5")) == NULL)
235 return -1;
236
237 return 0;
238}
239
240static int
241run_bgp_mp_list (testcase_t *t)
242{
243 struct list mp_list;
244 struct listnode *mp_node;
245 struct bgp_info *info;
246 int i;
247 int test_result = TEST_PASSED;
248 bgp_mp_list_init (&mp_list);
249 EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
250
251 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
252 bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
253 bgp_mp_list_add (&mp_list, &test_mp_list_info[2]);
254 bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
255 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
256
9fc3f9b3 257 for (i = 0, mp_node = mp_list.head; i < test_mp_list_info_count;
96450faf
JB
258 i++, mp_node = listnextnode(mp_node))
259 {
260 info = listgetdata(mp_node);
261 EXPECT_TRUE (info == &test_mp_list_info[i], test_result);
262 }
263
264 bgp_mp_list_clear (&mp_list);
265 EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
266
267 return test_result;
268}
269
270static int
271cleanup_bgp_mp_list (testcase_t *t)
272{
273 int i;
274
275 for (i = 0; i < test_mp_list_peer_count; i++)
276 sockunion_free (test_mp_list_peer[i].su_remote);
277
278 return 0;
279}
280
281testcase_t test_bgp_mp_list = {
282 .desc = "Test bgp_mp_list",
283 .setup = setup_bgp_mp_list,
284 .run = run_bgp_mp_list,
285 .cleanup = cleanup_bgp_mp_list,
286};
287
de8d5dff
JB
288/*=========================================================
289 * Testcase for bgp_info_mpath_update
290 */
291
292struct bgp_node test_rn;
293
294static int
295setup_bgp_info_mpath_update (testcase_t *t)
296{
297 int i;
298 str2prefix ("42.1.1.0/24", &test_rn.p);
299 setup_bgp_mp_list (t);
300 for (i = 0; i < test_mp_list_info_count; i++)
301 bgp_info_add (&test_rn, &test_mp_list_info[i]);
302 return 0;
303}
304
305static int
306run_bgp_info_mpath_update (testcase_t *t)
307{
308 struct bgp_info *new_best, *old_best, *mpath;
309 struct list mp_list;
310 struct bgp_maxpaths_cfg mp_cfg = { 3, 3 };
311 int test_result = TEST_PASSED;
312 bgp_mp_list_init (&mp_list);
313 bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
314 bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
315 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
316 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
317 new_best = &test_mp_list_info[3];
318 old_best = NULL;
319 bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, &mp_cfg);
320 bgp_mp_list_clear (&mp_list);
321 EXPECT_TRUE (bgp_info_mpath_count (new_best) == 2, test_result);
322 mpath = bgp_info_mpath_first (new_best);
323 EXPECT_TRUE (mpath == &test_mp_list_info[0], test_result);
324 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
325 mpath = bgp_info_mpath_next (mpath);
326 EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
327 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
328
329 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
330 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
331 new_best = &test_mp_list_info[0];
332 old_best = &test_mp_list_info[3];
333 bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, &mp_cfg);
334 bgp_mp_list_clear (&mp_list);
335 EXPECT_TRUE (bgp_info_mpath_count (new_best) == 1, test_result);
336 mpath = bgp_info_mpath_first (new_best);
337 EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
338 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
339 EXPECT_TRUE (!CHECK_FLAG (test_mp_list_info[0].flags, BGP_INFO_MULTIPATH),
340 test_result);
341
342 return test_result;
343}
344
345static int
346cleanup_bgp_info_mpath_update (testcase_t *t)
347{
348 int i;
349
350 for (i = 0; i < test_mp_list_peer_count; i++)
351 sockunion_free (test_mp_list_peer[i].su_remote);
352
353 return 0;
354}
355
356testcase_t test_bgp_info_mpath_update = {
357 .desc = "Test bgp_info_mpath_update",
358 .setup = setup_bgp_info_mpath_update,
359 .run = run_bgp_info_mpath_update,
360 .cleanup = cleanup_bgp_info_mpath_update,
361};
362
42ea6851
JB
363/*=========================================================
364 * Set up testcase vector
365 */
366testcase_t *all_tests[] = {
367 &test_bgp_cfg_maximum_paths,
96450faf 368 &test_bgp_mp_list,
de8d5dff 369 &test_bgp_info_mpath_update,
42ea6851
JB
370};
371
372int all_tests_count = (sizeof(all_tests)/sizeof(testcase_t *));
373
374/*=========================================================
375 * Test Driver Functions
376 */
377static int
378global_test_init (void)
379{
1bf9f027 380 qobj_init ();
42ea6851 381 master = thread_master_create ();
4140ca4d 382 zclient = zclient_new(master);
42ea6851 383 bgp_master_init ();
ebf00458 384 vrf_init ();
c9e4f862
PJ
385 bgp_option_set (BGP_OPT_NO_LISTEN);
386
42ea6851
JB
387 if (fileno (stdout) >= 0)
388 tty = isatty (fileno (stdout));
389 return 0;
390}
391
392static int
393global_test_cleanup (void)
394{
005b6bc0
LB
395 if (zclient != NULL)
396 zclient_free (zclient);
42ea6851
JB
397 thread_master_free (master);
398 return 0;
399}
400
401static void
402display_result (testcase_t *test, int result)
403{
404 if (tty)
405 printf ("%s: %s\n", test->desc, result == TEST_PASSED ? OK : FAILED);
406 else
407 printf ("%s: %s\n", test->desc, result == TEST_PASSED ? "OK" : "FAILED");
408}
409
410static int
411setup_test (testcase_t *t)
412{
413 int res = 0;
414 if (t->setup)
415 res = t->setup (t);
416 return res;
417}
418
419static int
420cleanup_test (testcase_t *t)
421{
422 int res = 0;
423 if (t->cleanup)
424 res = t->cleanup (t);
425 return res;
426}
427
428static void
429run_tests (testcase_t *tests[], int num_tests, int *pass_count, int *fail_count)
430{
431 int test_index, result;
432 testcase_t *cur_test;
433
434 *pass_count = *fail_count = 0;
435
436 for (test_index = 0; test_index < num_tests; test_index++)
437 {
438 cur_test = tests[test_index];
439 if (!cur_test->desc)
440 {
441 printf ("error: test %d has no description!\n", test_index);
442 continue;
443 }
444 if (!cur_test->run)
445 {
446 printf ("error: test %s has no run function!\n", cur_test->desc);
447 continue;
448 }
449 if (setup_test (cur_test) != 0)
450 {
451 printf ("error: setup failed for test %s\n", cur_test->desc);
452 continue;
453 }
454 result = cur_test->run (cur_test);
455 if (result == TEST_PASSED)
456 *pass_count += 1;
457 else
458 *fail_count += 1;
459 display_result (cur_test, result);
460 if (cleanup_test (cur_test) != 0)
461 {
462 printf ("error: cleanup failed for test %s\n", cur_test->desc);
463 continue;
464 }
465 }
466}
467
468int
469main (void)
470{
471 int pass_count, fail_count;
472 time_t cur_time;
473
474 time (&cur_time);
475 printf("BGP Multipath Tests Run at %s", ctime(&cur_time));
476 if (global_test_init () != 0)
477 {
478 printf("Global init failed. Terminating.\n");
479 exit(1);
480 }
481 run_tests (all_tests, all_tests_count, &pass_count, &fail_count);
482 global_test_cleanup ();
483 printf("Total pass/fail: %d/%d\n", pass_count, fail_count);
484 return fail_count;
485}