Skip to content

Commit

Permalink
[macsec-cli]: Fixing to config MACsec on the port will clear port att…
Browse files Browse the repository at this point in the history
…ributes in config db (#10903)

Why I did it
There is a bug that the Port attributes in CONFIG_DB will be cleared if using sudo config macsec port add Ethernet0 or sudo config macsec port del Ethernet0

How I did it
To fetch the port attributes before set/remove MACsec field in port table.

Signed-off-by: Ze Gan <ganze718@gmail.com>
  • Loading branch information
Pterosaur authored May 24, 2022
1 parent 8f7ef1e commit 0156c21
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions dockers/docker-macsec/cli-plugin-tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def mock_cfgdb():
CONFIG = {
'PORT': {
'Ethernet0': {
"admin_status": "up"
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion dockers/docker-macsec/cli-plugin-tests/test_config_macsec.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ def test_macsec_port(self, mock_cfgdb):
port_table = db.cfgdb.get_entry("PORT", "Ethernet0")
assert port_table
assert port_table["macsec"] == "test"
assert port_table["admin_status"] == "up"

result = runner.invoke(macsec.macsec.commands["profile"].commands["del"], ["test"], obj=db)
assert result.exit_code != 0

result = runner.invoke(macsec.macsec.commands["port"].commands["del"], ["Ethernet0"], obj=db)
assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
port_table = db.cfgdb.get_entry("PORT", "Ethernet0")
assert not port_table["macsec"]
assert "macsec" not in port_table or not port_table["macsec"]
assert port_table["admin_status"] == "up"


def test_macsec_invalid_operation(self, mock_cfgdb):
Expand Down
16 changes: 14 additions & 2 deletions dockers/docker-macsec/cli/config/plugins/macsec.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ def add_port(db, port, profile):
if len(profile_entry) == 0:
ctx.fail("profile {} doesn't exist".format(profile))

db.cfgdb.set_entry("PORT", port, {'macsec': profile})
port_entry = db.cfgdb.get_entry('PORT', port)
if len(port_entry) == 0:
ctx.fail("port {} doesn't exist".format(port))

port_entry['macsec'] = profile

db.cfgdb.set_entry("PORT", port, port_entry)


#
Expand All @@ -64,7 +70,13 @@ def del_port(db, port):
if port is None:
ctx.fail("cannot find port name for alias {}".format(alias))

db.cfgdb.set_entry("PORT", port, {'macsec': ""})
port_entry = db.cfgdb.get_entry('PORT', port)
if len(port_entry) == 0:
ctx.fail("port {} doesn't exist".format(port))

del port_entry['macsec']

db.cfgdb.set_entry("PORT", port, port_entry)


#
Expand Down

0 comments on commit 0156c21

Please sign in to comment.