Skip to content

Commit

Permalink
Fix import statement in mclag.py (#2073)
Browse files Browse the repository at this point in the history
#### What I did
Fixed import error
Added unit test for that case

#### How I did it
Used relative import to import file from the current package.

#### How to verify it
Try to configure mclag with Port as peer interface 
```
sudo config mclag add 1 192.168.3.1 192.168.3.2 Ethernet12
```

#### Previous command output (if the output of a command-line utility has changed)
```
admin@sonic:~$ sudo config mclag add 1 192.168.3.1 192.168.3.2 Ethernet12
Traceback (most recent call last):
  File "/usr/local/bin/config", line 8, in <module>
    sys.exit(config())
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/dist-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/config/mclag.py", line 140, in add_mclag_domain
    if (peer_ifname.startswith("Ethernet") is True) and (check_if_interface_is_valid(db, peer_ifname) is False):
  File "/usr/local/lib/python3.9/dist-packages/config/mclag.py", line 89, in check_if_interface_is_valid
    from main import interface_name_is_valid
ModuleNotFoundError: No module named 'main'
```
#### New command output (if the output of a command-line utility has changed)
```
admin@sonic:~$ sudo config mclag add 1 192.168.3.1 192.168.3.2 Ethernet12
admin@sonic:~$
```
  • Loading branch information
Myron Sosyak authored and bratashX committed May 13, 2022
1 parent c69d900 commit 4626a72
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/mclag.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def is_ipv4_addr_valid(addr):


def check_if_interface_is_valid(db, interface_name):
from main import interface_name_is_valid
from .main import interface_name_is_valid
if interface_name_is_valid(db,interface_name) is False:
ctx.fail("Interface name is invalid. Please enter a valid interface name!!")

Expand Down
20 changes: 20 additions & 0 deletions tests/mclag_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
MCLAG_INVALID_SESSION_TMOUT_LBOUND = "2"
MCLAG_INVALID_SESSION_TMOUT_UBOUND = "4000"

MCLAG_VALID_PEER_LINK_PORT = "Ethernet0"
MCLAG_VALID_PEER_LINK_PORTCHANNEL = "PortChannel1000"

MCLAG_INVALID_MCLAG_MEMBER = "Ethernet4"
MCLAG_INVALID_PORTCHANNEL1 = "portchannel"
MCLAG_INVALID_PORTCHANNEL2 = "PortChannelabcd"
Expand Down Expand Up @@ -142,6 +145,23 @@ def test_add_mclag_with_invalid_peer_mcast_ip(self):
result = runner.invoke(config.config.commands["mclag"].commands["add"], [MCLAG_DOMAIN_ID, MCLAG_SRC_IP, MCLAG_INVALID_PEER_IP2, MCLAG_PEER_LINK], obj=obj)
assert result.exit_code != 0, "mclag invalid peer ip mcast test caase with code {}:{} Output:{}".format(type(result.exit_code), result.exit_code, result.output)

def test_add_mclag_with_valid_peer_link(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# add mclag with valid port peer link
result = runner.invoke(config.config.commands["mclag"].commands["add"], [MCLAG_DOMAIN_ID, MCLAG_SRC_IP, MCLAG_PEER_IP, MCLAG_VALID_PEER_LINK_PORT], obj=obj)
assert result.exit_code == 0, "mclag valid peer link test case with code {}:{} Output:{}".format(type(result.exit_code), result.exit_code, result.output)

runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# add mclag with valid portchannel peer link
result = runner.invoke(config.config.commands["mclag"].commands["add"], [MCLAG_DOMAIN_ID, MCLAG_SRC_IP, MCLAG_PEER_IP, MCLAG_VALID_PEER_LINK_PORTCHANNEL], obj=obj)
assert result.exit_code == 0, "mclag valid peer link test case with code {}:{} Output:{}".format(type(result.exit_code), result.exit_code, result.output)

def test_add_mclag_with_invalid_peer_link(self):
runner = CliRunner()
db = Db()
Expand Down

0 comments on commit 4626a72

Please sign in to comment.