1+ import os
2+ import sys
3+ import pytest
4+ import mock
5+ from importlib import reload
6+
7+ from click .testing import CliRunner
8+
9+ from utilities_common .db import Db
10+
11+ modules_path = os .path .join (os .path .dirname (__file__ ), ".." )
12+ test_path = os .path .join (modules_path , "tests" )
13+ sys .path .insert (0 , modules_path )
14+ sys .path .insert (0 , test_path )
15+ mock_db_path = os .path .join (test_path , "int_ip_input" )
16+
17+
18+ class TestIntIp (object ):
19+ @pytest .fixture (scope = "class" , autouse = True )
20+ def setup_class (cls ):
21+ print ("SETUP" )
22+ os .environ ['UTILITIES_UNIT_TESTING' ] = "1"
23+ import config .main as config
24+ reload (config )
25+ yield
26+ print ("TEARDOWN" )
27+ os .environ ["UTILITIES_UNIT_TESTING" ] = "0"
28+ from .mock_tables import dbconnector
29+ dbconnector .dedicated_dbs = {}
30+
31+ @pytest .mark .parametrize ('setup_single_bgp_instance' ,
32+ ['ip_route_for_int_ip' ], indirect = ['setup_single_bgp_instance' ])
33+ def test_config_int_ip_rem (
34+ self ,
35+ get_cmd_module ,
36+ setup_single_bgp_instance ):
37+ (config , show ) = get_cmd_module
38+ jsonfile_config = os .path .join (mock_db_path , "config_db.json" )
39+ from .mock_tables import dbconnector
40+ dbconnector .dedicated_dbs ['CONFIG_DB' ] = jsonfile_config
41+
42+ runner = CliRunner ()
43+ db = Db ()
44+ obj = {'config_db' : db .cfgdb }
45+
46+ # remove vlan IP`s
47+ with mock .patch ('utilities_common.cli.run_command' ) as mock_run_command :
48+ result = runner .invoke (config .config .commands ["interface" ].commands ["ip" ].commands ["remove" ],
49+ ["Ethernet16" , "192.168.10.1/24" ], obj = obj )
50+ print (result .exit_code , result .output )
51+ assert result .exit_code == 0
52+ assert mock_run_command .call_count == 1
53+
54+ @pytest .mark .parametrize ('setup_single_bgp_instance' ,
55+ ['ip_route_for_int_ip' ], indirect = ['setup_single_bgp_instance' ])
56+ def test_config_int_ip_rem_static (
57+ self ,
58+ get_cmd_module ,
59+ setup_single_bgp_instance ):
60+ (config , show ) = get_cmd_module
61+ jsonfile_config = os .path .join (mock_db_path , "config_db" )
62+ from .mock_tables import dbconnector
63+ dbconnector .dedicated_dbs ['CONFIG_DB' ] = jsonfile_config
64+
65+ runner = CliRunner ()
66+ db = Db ()
67+ obj = {'config_db' : db .cfgdb }
68+
69+ with mock .patch ('utilities_common.cli.run_command' ) as mock_run_command :
70+ result = runner .invoke (config .config .commands ["interface" ].commands ["ip" ].commands ["remove" ],
71+ ["Ethernet2" , "192.168.0.1/24" ], obj = obj )
72+ print (result .exit_code , result .output )
73+ assert result .exit_code != 0
74+ assert "Error: Cannot remove the last IP entry of interface Ethernet2. A static ip route is still bound to the RIF." in result .output
75+ assert mock_run_command .call_count == 0
76+
77+ with mock .patch ('utilities_common.cli.run_command' ) as mock_run_command :
78+ result = runner .invoke (config .config .commands ["interface" ].commands ["ip" ].commands ["remove" ],
79+ ["Ethernet8" , "192.168.3.1/24" ], obj = obj )
80+ print (result .exit_code , result .output )
81+ assert result .exit_code != 0
82+ assert "Error: Cannot remove the last IP entry of interface Ethernet8. A static ipv6 route is still bound to the RIF." in result .output
83+ assert mock_run_command .call_count == 0
84+
85+ with mock .patch ('utilities_common.cli.run_command' ) as mock_run_command :
86+ result = runner .invoke (config .config .commands ["interface" ].commands ["ip" ].commands ["remove" ],
87+ ["Vlan2" , "192.168.1.1/21" ], obj = obj )
88+ print (result .exit_code , result .output )
89+ assert result .exit_code != 0
90+ assert "Error: Cannot remove the last IP entry of interface Vlan2. A static ip route is still bound to the RIF." in result .output
91+ assert mock_run_command .call_count == 0
92+
93+ with mock .patch ('utilities_common.cli.run_command' ) as mock_run_command :
94+ result = runner .invoke (config .config .commands ["interface" ].commands ["ip" ].commands ["remove" ],
95+ ["PortChannel2" , "10.0.0.56/31" ], obj = obj )
96+ print (result .exit_code , result .output )
97+ assert result .exit_code != 0
98+ assert "Error: Cannot remove the last IP entry of interface PortChannel2. A static ip route is still bound to the RIF." in result .output
99+ assert mock_run_command .call_count == 0
100+
101+ with mock .patch ('utilities_common.cli.run_command' ) as mock_run_command :
102+ result = runner .invoke (config .config .commands ["interface" ].commands ["ip" ].commands ["remove" ],
103+ ["Ethernet4" , "192.168.4.1/24" ], obj = obj )
104+ print (result .exit_code , result .output )
105+ assert result .exit_code == 0
106+ assert mock_run_command .call_count == 1
107+
108+ class TestIntIpMultiasic (object ):
109+ @pytest .fixture (scope = "class" , autouse = True )
110+ def setup_class (cls ):
111+ print ("SETUP" )
112+ os .environ ['UTILITIES_UNIT_TESTING' ] = "1"
113+ os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] = "multi_asic"
114+ from .mock_tables import dbconnector
115+ from .mock_tables import mock_multi_asic
116+ reload (mock_multi_asic )
117+ dbconnector .load_namespace_config ()
118+ yield
119+ print ("TEARDOWN" )
120+ os .environ ["UTILITIES_UNIT_TESTING" ] = "0"
121+ os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] = ""
122+ # change back to single asic config
123+ from .mock_tables import dbconnector
124+ from .mock_tables import mock_single_asic
125+ reload (mock_single_asic )
126+ dbconnector .dedicated_dbs = {}
127+ dbconnector .load_namespace_config ()
128+
129+ @pytest .mark .parametrize ('setup_multi_asic_bgp_instance' ,
130+ ['ip_route_for_int_ip' ], indirect = ['setup_multi_asic_bgp_instance' ])
131+ def test_config_int_ip_rem_static_multiasic (
132+ self ,
133+ get_cmd_module ,
134+ setup_multi_asic_bgp_instance ):
135+ (config , show ) = get_cmd_module
136+ jsonfile_config = os .path .join (mock_db_path , "config_db" )
137+ from .mock_tables import dbconnector
138+ dbconnector .dedicated_dbs ['CONFIG_DB' ] = jsonfile_config
139+
140+ runner = CliRunner ()
141+ db = Db ()
142+ obj = {'config_db' : db .cfgdb , 'namespace' : 'test_ns' }
143+
144+ with mock .patch ('utilities_common.cli.run_command' ) as mock_run_command :
145+ result = runner .invoke (config .config .commands ["interface" ].commands ["ip" ].commands ["remove" ],
146+ ["Ethernet2" , "192.168.0.1/24" ], obj = obj )
147+ print (result .exit_code , result .output )
148+ assert result .exit_code != 0
149+ assert "Error: Cannot remove the last IP entry of interface Ethernet2. A static ip route is still bound to the RIF." in result .output
150+ assert mock_run_command .call_count == 0
151+
152+ with mock .patch ('utilities_common.cli.run_command' ) as mock_run_command :
153+ result = runner .invoke (config .config .commands ["interface" ].commands ["ip" ].commands ["remove" ],
154+ ["Ethernet8" , "192.168.3.1/24" ], obj = obj )
155+ print (result .exit_code , result .output )
156+ assert result .exit_code != 0
157+ assert "Error: Cannot remove the last IP entry of interface Ethernet8. A static ipv6 route is still bound to the RIF." in result .output
158+ assert mock_run_command .call_count == 0
0 commit comments