1
+ from unittest .mock import MagicMock , patch
2
+
3
+ import os
4
+ from bgpcfgd .directory import Directory
5
+ from bgpcfgd .template import TemplateFabric
6
+ from . import swsscommon_test
7
+ from swsscommon import swsscommon
8
+
9
+ from bgpcfgd .managers_prefix_list import PrefixListMgr
10
+
11
+ TEMPLATE_PATH = os .path .abspath ('../../dockers/docker-fpm-frr/frr' )
12
+
13
+ def constructor ():
14
+ cfg_mgr = MagicMock ()
15
+ common_objs = {
16
+ 'directory' : Directory (),
17
+ 'cfg_mgr' : cfg_mgr ,
18
+ 'tf' : TemplateFabric (TEMPLATE_PATH ),
19
+ 'constants' : {},
20
+ }
21
+
22
+ m = PrefixListMgr (common_objs , "CONFIG_DB" , "PREFIX_LIST" )
23
+ m .directory .put ("CONFIG_DB" , swsscommon .CFG_DEVICE_METADATA_TABLE_NAME , "localhost" , {"bgp_asn" : "65100" , "type" : "SpineRouter" })
24
+
25
+ return m
26
+
27
+ def set_handler_test (manager , key , value ):
28
+ res = manager .set_handler (key , value )
29
+ assert res , "Returns always True"
30
+
31
+ def del_handler_test (manager , key ):
32
+ res = manager .del_handler (key )
33
+ assert res , "Returns always True"
34
+
35
+ # test if the ipv4 radian configs are set correctly
36
+ @patch ('bgpcfgd.managers_prefix_list.log_debug' )
37
+ def test_generate_prefix_list_config_ipv4 (mocked_log_debug ):
38
+ m = constructor ()
39
+ set_handler_test (m , "ANCHOR_PREFIX|192.168.0.0/24" , {})
40
+ mocked_log_debug .assert_called_with ("PrefixListMgr:: Anchor prefix 192.168.0.0/24 added to radian configuration" )
41
+
42
+ # test if the ipv6 radian configs are set correctly
43
+ @patch ('bgpcfgd.managers_prefix_list.log_debug' )
44
+ def test_generate_prefix_list_config_ipv6 (mocked_log_debug ):
45
+ m = constructor ()
46
+ set_handler_test (m , "ANCHOR_PREFIX|fc02:100::/64" , {})
47
+ mocked_log_debug .assert_called_with ("PrefixListMgr:: Anchor prefix fc02:100::/64 added to radian configuration" )
48
+
49
+ # test if invalid prefix is handled correctly
50
+ @patch ('bgpcfgd.managers_prefix_list.log_warn' )
51
+ def test_generate_prefix_list_config_invalid_prefix (mocked_log_warn ):
52
+ m = constructor ()
53
+ set_handler_test (m , "ANCHOR_PREFIX|invalid_prefix" , {})
54
+ mocked_log_warn .assert_called_with ("PrefixListMgr:: Prefix 'invalid_prefix' format is wrong for prefix list 'ANCHOR_PREFIX'" )
55
+
56
+ # test if the ipv4 radian configs are deleted correctly
57
+ @patch ('bgpcfgd.managers_prefix_list.log_debug' )
58
+ def test_del_handler_ipv4 (mocked_log_debug ):
59
+ m = constructor ()
60
+ set_handler_test (m , "ANCHOR_PREFIX|192.168.0.0/24" , {})
61
+ del_handler_test (m , "ANCHOR_PREFIX|192.168.0.0/24" )
62
+ mocked_log_debug .assert_called_with ("PrefixListMgr:: Anchor prefix 192.168.0.0/24 removed from radian configuration" )
63
+
64
+ # test if the ipv6 radian configs are deleted correctly
65
+ @patch ('bgpcfgd.managers_prefix_list.log_debug' )
66
+ def test_del_handler_ipv6 (mocked_log_debug ):
67
+ m = constructor ()
68
+ set_handler_test (m , "ANCHOR_PREFIX|fc02:100::/64" , {})
69
+ del_handler_test (m , "ANCHOR_PREFIX|fc02:100::/64" )
70
+ mocked_log_debug .assert_called_with ("PrefixListMgr:: Anchor prefix fc02:100::/64 removed from radian configuration" )
0 commit comments