Skip to content

Commit 114817d

Browse files
Copilotqkalsky
andcommitted
Fix: Skip removing 'switchport trunk allowed vlan 1' as it's the default state
Co-authored-by: qkalsky <[email protected]>
1 parent cc8c22d commit 114817d

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

cloudshell/networking/cisco/command_actions/iface_actions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ def clean_interface_switchport_config(
114114

115115
for line in current_config.splitlines():
116116
if line.strip(" ").startswith("switchport "):
117+
# Skip removing "switchport trunk allowed vlan 1" as it's the default state
118+
if re.match(
119+
r"^\s*switchport\s+trunk\s+allowed\s+vlan\s+1\s*$",
120+
line,
121+
re.IGNORECASE,
122+
):
123+
continue
117124
line_to_remove = re.sub(r"\s+\d+[-\d+,]+", "", line).strip(" ")
118125
CommandTemplateExecutor(
119126
self._cli_service,

tests/networking/cisco/command_actions/test_iface_actions.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,71 @@ def test_get_no_l2_protocol_tunnel_cmd(self, vlan_templates_mock, cte_mock):
5858
error_map=None,
5959
)
6060
self.assertEqual(result, cte_mock.return_value)
61+
62+
@patch(
63+
"cloudshell.networking.cisco.command_actions.iface_actions"
64+
".CommandTemplateExecutor"
65+
)
66+
def test_clean_interface_switchport_config_preserves_vlan_1(self, cte_mock):
67+
"""Test that switchport trunk allowed vlan 1 is not removed (default state)."""
68+
current_config = """Building configuration...
69+
70+
Current configuration : 144 bytes
71+
!
72+
interface GigabitEthernet110/1/0/6
73+
description KG-255X-06-PT
74+
switchport
75+
switchport trunk allowed vlan 1
76+
switchport mode dynamic auto
77+
end
78+
"""
79+
executor_mock = MagicMock()
80+
cte_mock.return_value = executor_mock
81+
82+
self._handler.clean_interface_switchport_config(current_config)
83+
84+
# Verify that execute_command was called for other switchport lines but not for vlan 1
85+
calls = executor_mock.execute_command.call_args_list
86+
# Should be called twice: once for "switchport" and once for "switchport mode dynamic auto"
87+
# but NOT for "switchport trunk allowed vlan 1"
88+
self.assertEqual(len(calls), 2)
89+
90+
# Verify the commands that were issued
91+
called_commands = [call[1]["command"] for call in calls]
92+
self.assertIn("switchport", called_commands)
93+
self.assertIn("switchport mode dynamic auto", called_commands)
94+
# Ensure vlan 1 command was NOT called
95+
self.assertNotIn("switchport trunk allowed vlan", called_commands)
96+
97+
@patch(
98+
"cloudshell.networking.cisco.command_actions.iface_actions"
99+
".CommandTemplateExecutor"
100+
)
101+
def test_clean_interface_switchport_config_removes_other_vlans(self, cte_mock):
102+
"""Test that switchport trunk allowed vlan with other VLANs are removed."""
103+
current_config = """Building configuration...
104+
105+
Current configuration : 144 bytes
106+
!
107+
interface GigabitEthernet110/1/0/6
108+
description KG-255X-06-PT
109+
switchport
110+
switchport trunk allowed vlan 100
111+
switchport mode trunk
112+
end
113+
"""
114+
executor_mock = MagicMock()
115+
cte_mock.return_value = executor_mock
116+
117+
self._handler.clean_interface_switchport_config(current_config)
118+
119+
# Verify that execute_command was called for all switchport lines including vlan 100
120+
calls = executor_mock.execute_command.call_args_list
121+
# Should be called three times: "switchport", "switchport trunk allowed vlan", "switchport mode trunk"
122+
self.assertEqual(len(calls), 3)
123+
124+
# Verify the commands that were issued
125+
called_commands = [call[1]["command"] for call in calls]
126+
self.assertIn("switchport", called_commands)
127+
self.assertIn("switchport trunk allowed vlan", called_commands)
128+
self.assertIn("switchport mode trunk", called_commands)

0 commit comments

Comments
 (0)