Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[unit test][CLI][pfcwd] Added pfcwd config tests for single and multi ASIC platform. #1248

Merged
merged 9 commits into from
Dec 9, 2020

Conversation

smaheshm
Copy link
Contributor

@smaheshm smaheshm commented Nov 18, 2020

- What I did
Added pfcwd config tests for single and multi ASIC platform.

Changed the pfcwd CLI to access the DB object as a parameter.

**- How I did it **
Updated DB object that creates DB client connections for Config DBs and other redis DBs to include db/config clients for all namespaces on a multi ASIC platform and for default namespace on a single ASIC platform.

'run_on_multi_asic' decorator uses this object to reuse the DB client connections, thus always reading the mock redis DB in the memory, instead of loading config DB from file.

- How to verify it
Unit tests

samaddik@e3cf370f3adb:/sonic/src/sonic-utilities/tests$ sudo pytest -k pfcwd_test.py                                                                                                  
================================================================================ test session starts =================================================================================
platform linux2 -- Python 2.7.16, pytest-3.10.1, py-1.7.0, pluggy-0.8.0                                                                                                               
rootdir: /sonic/src/sonic-utilities/tests, inifile: pytest.ini                                                                                                                        
plugins: cov-2.6.0                                                                                                                                                                    
collected 339 items / 323 deselected                                                                                                                                                  
                                                                                                                                                                                      
pfcwd_test.py ................                                                                                                                                                 [100%] 
                                                                                                                                                                                      
===================================================================== 16 passed, 323 deselected in 1.65 seconds ======================================================================
Exception TypeError: "'NoneType' object is not callable" in <bound method Logger.__del__ of <sonic_py_common.logger.Logger object at 0x7ff9d2baead0>> ignored                         
Exception TypeError: "'NoneType' object is not callable" in <bound method Logger.__del__ of <sonic_py_common.logger.Logger object at 0x7ff9d3832b10>> ignored                         
Exception TypeError: "'NoneType' object is not callable" in <bound method Logger.__del__ of <sonic_py_common.logger.Logger object at 0x7ff9d3ddbc50>> ignored     


Last login: Mon Dec  7 16:09:14 2020 from 100.127.20.23
admin@str-s6000-acs-9:~$ sudo pfcwd start --action drop --restoration-time 199 all 199
admin@str-s6000-acs-9:~$ show pfcwd config
       PORT    ACTION    DETECTION TIME    RESTORATION TIME
-----------  --------  ----------------  ------------------
  Ethernet0      drop               199                 199
  Ethernet4      drop               199                 199
  Ethernet8      drop               199                 199
 Ethernet12      drop               199                 199
 Ethernet16      drop               199                 199
 Ethernet20      drop               199                 199
 Ethernet24      drop               199                 199
 Ethernet28      drop               199                 199
 Ethernet32      drop               199                 199
 Ethernet36      drop               199                 199
 Ethernet40      drop               199                 199
 Ethernet44      drop               199                 199
 Ethernet48      drop               199                 199
 Ethernet52      drop               199                 199
 Ethernet56      drop               199                 199
 Ethernet60      drop               199                 199
 Ethernet64      drop               199                 199
 Ethernet68      drop               199                 199
 Ethernet72      drop               199                 199
 Ethernet76      drop               199                 199
 Ethernet80      drop               199                 199
 Ethernet84      drop               199                 199
 Ethernet88      drop               199                 199
 Ethernet92      drop               199                 199
 Ethernet96      drop               199                 199
Ethernet100      drop               199                 199
Ethernet104      drop               199                 199
Ethernet108      drop               199                 199
Ethernet112      drop               199                 199
Ethernet116      drop               199                 199
Ethernet120      drop               199                 199
Ethernet124      drop               199                 199
admin@str-s6000-acs-9:~$ 


- Previous command output (if the output of a command-line utility has changed)

- New command output (if the output of a command-line utility has changed)

@smaheshm smaheshm changed the title Configdb [unit test][CLI][pfcwd] Added pfcwd config tests for single and multi ASIC platform. Nov 18, 2020
@smaheshm
Copy link
Contributor Author

smaheshm commented Dec 2, 2020

👋

@smaheshm
Copy link
Contributor Author

smaheshm commented Dec 3, 2020

retest this please

2 similar comments
@smaheshm
Copy link
Contributor Author

smaheshm commented Dec 3, 2020

retest this please

@smaheshm
Copy link
Contributor Author

smaheshm commented Dec 4, 2020

retest this please

@smaheshm
Copy link
Contributor Author

smaheshm commented Dec 5, 2020

@arlakshm Thanks for providing inputs. Updated as per discussion.

pfcwd/main.py Outdated Show resolved Hide resolved
@smaheshm
Copy link
Contributor Author

smaheshm commented Dec 7, 2020

retest this please

arlakshm
arlakshm previously approved these changes Dec 7, 2020
Copy link
Contributor

@arlakshm arlakshm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

tests/pfcwd_test.py Outdated Show resolved Hide resolved
@neethajohn
Copy link
Contributor

Also can you fix the description since you are not creating a new multi asic db object but extending the existing one?

@smaheshm
Copy link
Contributor Author

smaheshm commented Dec 8, 2020

retest this please

1 similar comment
@smaheshm
Copy link
Contributor Author

smaheshm commented Dec 8, 2020

retest this please

@stephenxs
Copy link
Collaborator

Hi @smaheshm,
I'm encountering the following errors well compiling the sonic-utilities.
Looks like it's related to this PR. Could you please check it?

=================================== FAILURES ===================================
____________________ TestPfcwd.test_pfcwd_start_ports_valid ____________________

self = <tests.pfcwd_test.TestPfcwd object at 0x7f1222030b00>

    def test_pfcwd_start_ports_valid(self):
        # pfcwd start --action drop --restoration-time 200 Ethernet0 200
        import pfcwd.main as pfcwd
        runner = CliRunner()
        db = Db()
    
        # get initial config
        result = runner.invoke(
            pfcwd.cli.commands["show"].commands["config"],
            obj=db
        )
        print(result.output)
        assert result.output == pfcwd_show_config_output
    
        result = runner.invoke(
            pfcwd.cli.commands["start"],
            [
                "--action", "forward", "--restoration-time", "101",
                "Ethernet0", "102"
            ],
            obj=db
        )
        print(result.output)
>       assert result.exit_code == 0
E       AssertionError: assert 1 == 0
E        +  where 1 = <Result SystemExit('Root privileges are required for this operation')>.exit_code

tests/pfcwd_test.py:104: AssertionError
----------------------------- Captured stdout call -----------------------------
Changed polling interval to 600ms
     PORT    ACTION    DETECTION TIME    RESTORATION TIME
---------  --------  ----------------  ------------------
Ethernet0      drop               600                 600
Ethernet4      drop               600                 600
Ethernet8      drop               600                 600

Root privileges are required for this operation

______________________ TestPfcwd.test_pfcwd_start_actions ______________________

self = <tests.pfcwd_test.TestPfcwd object at 0x7f12229b2048>

    def test_pfcwd_start_actions(self):
        # pfcwd start --action fwd --restoration-time 200 Ethernet0 200
        import pfcwd.main as pfcwd
        runner = CliRunner()
        db = Db()
    
        # get initial config
        result = runner.invoke(
            pfcwd.cli.commands["show"].commands["config"],
            obj=db
        )
        print(result.output)
        assert result.output == pfcwd_show_config_output
    
        result = runner.invoke(
            pfcwd.cli.commands["start"],
            [
                "--action", "forward", "--restoration-time", "301",
                "all", "302"
            ],
            obj=db
        )
        print(result.output)
>       assert result.exit_code == 0
E       AssertionError: assert 1 == 0
E        +  where 1 = <Result SystemExit('Root privileges are required for this operation')>.exit_code

tests/pfcwd_test.py:138: AssertionError
----------------------------- Captured stdout call -----------------------------
Changed polling interval to 600ms
     PORT    ACTION    DETECTION TIME    RESTORATION TIME
---------  --------  ----------------  ------------------
Ethernet0      drop               600                 600
Ethernet4      drop               600                 600
Ethernet8      drop               600                 600

Root privileges are required for this operation

__________ TestMultiAsicPfcwdShow.test_pfcwd_start_ports_masic_valid ___________

self = <tests.pfcwd_test.TestMultiAsicPfcwdShow object at 0x7f12221c2d30>

    def test_pfcwd_start_ports_masic_valid(self):
        # pfcwd start --action forward --restoration-time 200 Ethernet0 200
        import pfcwd.main as pfcwd
        runner = CliRunner()
        db = Db()
        # get initial config
        result = runner.invoke(
            pfcwd.cli.commands["show"].commands["config"],
            obj=db
        )
        print(result.output)
        assert result.output == show_pfc_config_all
    
        result = runner.invoke(
            pfcwd.cli.commands["start"],
            [
                "--action", "forward", "--restoration-time", "101",
                "Ethernet0", "Ethernet-BP4", "102"
            ],
            obj=db
        )
        print(result.output)
>       assert result.exit_code == 0
E       AssertionError: assert 1 == 0
E        +  where 1 = <Result SystemExit('Root privileges are required for this operation')>.exit_code

tests/pfcwd_test.py:292: AssertionError
----------------------------- Captured stdout call -----------------------------
Changed polling interval to 199ms on asic0
BIG_RED_SWITCH status is enable on asic0
Changed polling interval to 199ms on asic1
BIG_RED_SWITCH status is enable on asic1
          PORT    ACTION    DETECTION TIME    RESTORATION TIME
--------------  --------  ----------------  ------------------
     Ethernet0      drop               200                 200
     Ethernet4      drop               200                 200
  Ethernet-BP0      drop               200                 200
  Ethernet-BP4      drop               200                 200
Ethernet-BP256      drop               200                 200
Ethernet-BP260      drop               200                 200

Root privileges are required for this operation

____________ TestMultiAsicPfcwdShow.test_pfcwd_start_actions_masic _____________

self = <tests.pfcwd_test.TestMultiAsicPfcwdShow object at 0x7f1221eb7400>

    def test_pfcwd_start_actions_masic(self):
        # pfcwd start --action drop --restoration-time 200 Ethernet0 200
        import pfcwd.main as pfcwd
        runner = CliRunner()
        db = Db()
        # get initial config
        result = runner.invoke(
            pfcwd.cli.commands["show"].commands["config"],
            obj=db
        )
        print(result.output)
        assert result.output == show_pfc_config_all
    
        result = runner.invoke(
            pfcwd.cli.commands["start"],
            [
                "--action", "drop", "--restoration-time", "301",
                "all", "302"
            ],
            obj=db
        )
        print(result.output)
>       assert result.exit_code == 0
E       AssertionError: assert 1 == 0
E        +  where 1 = <Result SystemExit('Root privileges are required for this operation')>.exit_code

tests/pfcwd_test.py:325: AssertionError

@smaheshm
Copy link
Contributor Author

Hi @smaheshm,
I'm encountering the following errors well compiling the sonic-utilities.
Looks like it's related to this PR. Could you please check it?

=================================== FAILURES ===================================
____________________ TestPfcwd.test_pfcwd_start_ports_valid ____________________

self = <tests.pfcwd_test.TestPfcwd object at 0x7f1222030b00>

    def test_pfcwd_start_ports_valid(self):
        # pfcwd start --action drop --restoration-time 200 Ethernet0 200
        import pfcwd.main as pfcwd
        runner = CliRunner()
        db = Db()
    
        # get initial config
        result = runner.invoke(
            pfcwd.cli.commands["show"].commands["config"],
            obj=db
        )
        print(result.output)
        assert result.output == pfcwd_show_config_output
    
        result = runner.invoke(
            pfcwd.cli.commands["start"],
            [
                "--action", "forward", "--restoration-time", "101",
                "Ethernet0", "102"
            ],
            obj=db
        )
        print(result.output)
>       assert result.exit_code == 0
E       AssertionError: assert 1 == 0
E        +  where 1 = <Result SystemExit('Root privileges are required for this operation')>.exit_code

tests/pfcwd_test.py:104: AssertionError
----------------------------- Captured stdout call -----------------------------
Changed polling interval to 600ms
     PORT    ACTION    DETECTION TIME    RESTORATION TIME
---------  --------  ----------------  ------------------
Ethernet0      drop               600                 600
Ethernet4      drop               600                 600
Ethernet8      drop               600                 600

Root privileges are required for this operation

______________________ TestPfcwd.test_pfcwd_start_actions ______________________

self = <tests.pfcwd_test.TestPfcwd object at 0x7f12229b2048>

    def test_pfcwd_start_actions(self):
        # pfcwd start --action fwd --restoration-time 200 Ethernet0 200
        import pfcwd.main as pfcwd
        runner = CliRunner()
        db = Db()
    
        # get initial config
        result = runner.invoke(
            pfcwd.cli.commands["show"].commands["config"],
            obj=db
        )
        print(result.output)
        assert result.output == pfcwd_show_config_output
    
        result = runner.invoke(
            pfcwd.cli.commands["start"],
            [
                "--action", "forward", "--restoration-time", "301",
                "all", "302"
            ],
            obj=db
        )
        print(result.output)
>       assert result.exit_code == 0
E       AssertionError: assert 1 == 0
E        +  where 1 = <Result SystemExit('Root privileges are required for this operation')>.exit_code

tests/pfcwd_test.py:138: AssertionError
----------------------------- Captured stdout call -----------------------------
Changed polling interval to 600ms
     PORT    ACTION    DETECTION TIME    RESTORATION TIME
---------  --------  ----------------  ------------------
Ethernet0      drop               600                 600
Ethernet4      drop               600                 600
Ethernet8      drop               600                 600

Root privileges are required for this operation

__________ TestMultiAsicPfcwdShow.test_pfcwd_start_ports_masic_valid ___________

self = <tests.pfcwd_test.TestMultiAsicPfcwdShow object at 0x7f12221c2d30>

    def test_pfcwd_start_ports_masic_valid(self):
        # pfcwd start --action forward --restoration-time 200 Ethernet0 200
        import pfcwd.main as pfcwd
        runner = CliRunner()
        db = Db()
        # get initial config
        result = runner.invoke(
            pfcwd.cli.commands["show"].commands["config"],
            obj=db
        )
        print(result.output)
        assert result.output == show_pfc_config_all
    
        result = runner.invoke(
            pfcwd.cli.commands["start"],
            [
                "--action", "forward", "--restoration-time", "101",
                "Ethernet0", "Ethernet-BP4", "102"
            ],
            obj=db
        )
        print(result.output)
>       assert result.exit_code == 0
E       AssertionError: assert 1 == 0
E        +  where 1 = <Result SystemExit('Root privileges are required for this operation')>.exit_code

tests/pfcwd_test.py:292: AssertionError
----------------------------- Captured stdout call -----------------------------
Changed polling interval to 199ms on asic0
BIG_RED_SWITCH status is enable on asic0
Changed polling interval to 199ms on asic1
BIG_RED_SWITCH status is enable on asic1
          PORT    ACTION    DETECTION TIME    RESTORATION TIME
--------------  --------  ----------------  ------------------
     Ethernet0      drop               200                 200
     Ethernet4      drop               200                 200
  Ethernet-BP0      drop               200                 200
  Ethernet-BP4      drop               200                 200
Ethernet-BP256      drop               200                 200
Ethernet-BP260      drop               200                 200

Root privileges are required for this operation

____________ TestMultiAsicPfcwdShow.test_pfcwd_start_actions_masic _____________

self = <tests.pfcwd_test.TestMultiAsicPfcwdShow object at 0x7f1221eb7400>

    def test_pfcwd_start_actions_masic(self):
        # pfcwd start --action drop --restoration-time 200 Ethernet0 200
        import pfcwd.main as pfcwd
        runner = CliRunner()
        db = Db()
        # get initial config
        result = runner.invoke(
            pfcwd.cli.commands["show"].commands["config"],
            obj=db
        )
        print(result.output)
        assert result.output == show_pfc_config_all
    
        result = runner.invoke(
            pfcwd.cli.commands["start"],
            [
                "--action", "drop", "--restoration-time", "301",
                "all", "302"
            ],
            obj=db
        )
        print(result.output)
>       assert result.exit_code == 0
E       AssertionError: assert 1 == 0
E        +  where 1 = <Result SystemExit('Root privileges are required for this operation')>.exit_code

tests/pfcwd_test.py:325: AssertionError

can you run with "sudo" option.

What command are you using?

@stephenxs
Copy link
Collaborator

I just tried building the sonic utilities python wheel package. Something like “make target/python-wheels/sonic-utilities-xx”(the full path name of the package).
The unit test script will be executed during compiling.
We don’t use “sudo” when compiling the code.

@smaheshm
Copy link
Contributor Author

"sudo" is required to run the 'pfcwd config' tests. Id suggest to add a sudo check in the test script and skip if not root.

if os.geteuid() != 0:
     pytest.skip("need sudo access")

@stephenxs
Copy link
Collaborator

I think to skip it if it's not prilivege user is a work around for now. But I think it's better to support testing it without "sudo".
Probably you can refer how unit test is execucted on "config" command?

@smaheshm
Copy link
Contributor Author

We should not add any checks to skip 'root' privilege in CLI code, since any user can simulate a test environment on a production switch and bypass the 'root' privilege to make config changes. I'm open for any other suggestions.

@stephenxs
Copy link
Collaborator

sounds reasonable...

@stephenxs
Copy link
Collaborator

Is it possible to use something like fakeroot during the compiling in order to simulate a root user?

@smaheshm
Copy link
Contributor Author

Is it possible to use something like fakeroot during the compiling in order to simulate a root user?

FYI.

#1340

@smaheshm smaheshm deleted the configdb branch February 26, 2021 22:33
anand-kumar-subramanian pushed a commit to anand-kumar-subramanian/sonic-utilities that referenced this pull request Mar 2, 2021
… ASIC platform. (sonic-net#1248)

* Update Db object to include multi ASIC db clients.
* Updated pfcwd CLI commands to use decorator to pass Db object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants