Skip to content

Commit

Permalink
[vlan][dhcp_relay] Fix error while delete vlan (sonic-net#2987)
Browse files Browse the repository at this point in the history
Why I did
Use static str to run_command would encounter error because change in this PR: sonic-net#2718.

How I did it
Change command from static str to list.

How to verify it
UT passed.
Build whl and install in testbed.

Signed-off-by: Yaqiang Zhu <[email protected]>
  • Loading branch information
yaqiangz authored Sep 20, 2023
1 parent 701994f commit 3df1f18
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
3 changes: 2 additions & 1 deletion config/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def set_dhcp_relay_table(table, config_db, vlan_name, value):


def is_dhcp_relay_running():
out, _ = clicommon.run_command("systemctl show dhcp_relay.service --property ActiveState --value", return_cmd=True)
out, _ = clicommon.run_command(["systemctl", "show", "dhcp_relay.service", "--property", "ActiveState", "--value"],
return_cmd=True)
return out.strip() == "active"

def is_dhcpv6_relay_config_exist(db, vlan_name):
Expand Down
5 changes: 5 additions & 0 deletions tests/mock_tables/config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -2723,5 +2723,10 @@
"alias": "Fabric2",
"isolateStatus": "False",
"lanes": "2"
},
"DHCP_RELAY|Vlan1000": {
"dhcpv6_servers": [
"fc02:2000::1"
]
}
}
34 changes: 34 additions & 0 deletions tests/vlan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,3 +1377,37 @@ def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
bgp_util.run_bgp_command = cls._old_run_bgp_command
print("TEARDOWN")

def test_config_vlan_del_dhcp_relay_restart(self):
runner = CliRunner()
db = Db()
obj = {"config_db": db.cfgdb}

# remove vlan IP`s
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"],
["Vlan1000", "192.168.0.1/21"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code != 0

result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"],
["Vlan1000", "fc02:1000::1/64"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code != 0

# remove vlan members
vlan_member = db.cfgdb.get_table("VLAN_MEMBER")
keys = [(k, v) for k, v in vlan_member if k == "Vlan{}".format(1000)]
for _, v in keys:
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["del"], ["1000", v], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0

origin_run_command_func = config.vlan.clicommon.run_command
config.vlan.clicommon.run_command = mock.MagicMock(return_value=("active", 0))
result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1000"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0

config.vlan.clicommon.run_command = origin_run_command_func
6 changes: 3 additions & 3 deletions utilities_common/dhcp_relay_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def restart_dhcp_relay_service():
Restart dhcp_relay service
"""
click.echo("Restarting DHCP relay service...")
clicommon.run_command("systemctl stop dhcp_relay", display_cmd=False)
clicommon.run_command("systemctl reset-failed dhcp_relay", display_cmd=False)
clicommon.run_command("systemctl start dhcp_relay", display_cmd=False)
clicommon.run_command(["systemctl", "stop", "dhcp_relay"], display_cmd=False)
clicommon.run_command(["systemctl", "reset-failed", "dhcp_relay"], display_cmd=False)
clicommon.run_command(["systemctl", "start", "dhcp_relay"], display_cmd=False)


def handle_restart_dhcp_relay_service():
Expand Down

0 comments on commit 3df1f18

Please sign in to comment.