@@ -406,6 +406,114 @@ def test_pfcwd_enable_history_ports_invalid(self):
406406 # same as original config
407407 assert result .output == test_vectors .pfcwd_show_config_output
408408
409+ def test_pfcwd_show_stats_check_storm_no_storms (self ):
410+ """ Test --check-storm flag when no storms are present """
411+ import pfcwd .main as pfcwd
412+ from unittest .mock import patch
413+ runner = CliRunner ()
414+ db = Db ()
415+
416+ # Mock the collect_stats method to simulate no storms
417+ def mock_collect_stats_no_storm (self , empty , queues , storm_only = False ):
418+ # Create fake table data with only operational queues
419+ self .table = [
420+ ['Ethernet0:3' , 'operational' , '2/2' , '100/100' , '100/100' , '0/0' , '0/0' ],
421+ ['Ethernet4:3' , 'operational' , '3/3' , '150/150' , '150/150' , '0/0' , '0/0' ],
422+ ['Ethernet8:4' , 'operational' , '1/1' , '50/50' , '50/50' , '0/0' , '0/0' ]
423+ ]
424+
425+ with patch .object (pfcwd .PfcwdCli , 'collect_stats' , mock_collect_stats_no_storm ):
426+ # Test with no storms - should exit 0
427+ result = runner .invoke (
428+ pfcwd .cli .commands ["show" ].commands ["stats" ],
429+ ["--check-storm" ],
430+ obj = db
431+ )
432+ print ("No storms test - exit code:" , result .exit_code )
433+ print ("No storms test - output:" , result .output )
434+ assert result .exit_code == 0
435+ assert result .output == "" # Should be silent when checking storms
436+
437+ def test_pfcwd_show_stats_check_storm_with_storms (self ):
438+ """ Test --check-storm flag when storms are present """
439+ import pfcwd .main as pfcwd
440+ from unittest .mock import patch
441+ runner = CliRunner ()
442+ db = Db ()
443+
444+ # Mock the collect_stats method to simulate storm detection
445+ def mock_collect_stats_with_storm (self , empty , queues , storm_only = False ):
446+ # Create fake table data with a stormed queue
447+ self .table = [
448+ ['Ethernet0:3' , 'stormed' , '1/0' , '100/300' , '100/300' , '0/200' , '0/200' ]
449+ ]
450+
451+ with patch .object (pfcwd .PfcwdCli , 'collect_stats' , mock_collect_stats_with_storm ):
452+ result = runner .invoke (
453+ pfcwd .cli .commands ["show" ].commands ["stats" ],
454+ ["--check-storm" ],
455+ obj = db
456+ )
457+ print ("With storms test - exit code:" , result .exit_code )
458+ print ("With storms test - output:" , result .output )
459+ assert result .exit_code == 1
460+ assert result .output == "" # Should be silent when checking storms
461+
462+ def test_pfcwd_show_stats_check_storm_mixed_status (self ):
463+ """ Test --check-storm flag with mixed operational and stormed queues """
464+ import pfcwd .main as pfcwd
465+ from unittest .mock import patch
466+ runner = CliRunner ()
467+ db = Db ()
468+
469+ # Mock the collect_stats method to simulate mixed queue states
470+ def mock_collect_stats_mixed (self , empty , queues , storm_only = False ):
471+ # Create fake table data with mixed states - should still exit 1 if ANY storm detected
472+ self .table = [
473+ ['Ethernet0:3' , 'operational' , '2/2' , '100/100' , '100/100' , '0/0' , '0/0' ],
474+ ['Ethernet4:3' , 'stormed' , '1/0' , '100/300' , '100/300' , '0/200' , '0/200' ],
475+ ['Ethernet8:4' , 'operational' , '0/0' , '50/50' , '50/50' , '0/0' , '0/0' ]
476+ ]
477+
478+ with patch .object (pfcwd .PfcwdCli , 'collect_stats' , mock_collect_stats_mixed ):
479+ result = runner .invoke (
480+ pfcwd .cli .commands ["show" ].commands ["stats" ],
481+ ["--check-storm" ],
482+ obj = db
483+ )
484+ print ("Mixed states test - exit code:" , result .exit_code )
485+ print ("Mixed states test - output:" , result .output )
486+ assert result .exit_code == 1 # Should exit 1 if ANY storms detected
487+ assert result .output == ""
488+
489+ def test_pfcwd_show_stats_normal_output_unchanged (self ):
490+ """ Test that normal stats output is unchanged when not using --check-storm """
491+ import pfcwd .main as pfcwd
492+ from unittest .mock import patch
493+ runner = CliRunner ()
494+ db = Db ()
495+
496+ # Mock the collect_stats method to ensure consistent test output
497+ def mock_collect_stats_normal (self , empty , queues , storm_only = False ):
498+ # Create fake table data with mixed states (like normal operation)
499+ self .table = [
500+ ['Ethernet0:3' , 'operational' , '2/2' , '100/100' , '100/100' , '0/0' , '0/0' ],
501+ ['Ethernet4:3' , 'operational' , '3/3' , '150/150' , '150/150' , '0/0' , '0/0' ]
502+ ]
503+
504+ with patch .object (pfcwd .PfcwdCli , 'collect_stats' , mock_collect_stats_normal ):
505+ # Test normal stats command (without --check-storm) - should work as before
506+ result = runner .invoke (
507+ pfcwd .cli .commands ["show" ].commands ["stats" ],
508+ obj = db
509+ )
510+ print ("Normal output test - exit code:" , result .exit_code )
511+ print ("Normal output test - output length:" , len (result .output ))
512+ assert result .exit_code == 0
513+ # Should have normal tabulated output (not empty)
514+ assert len (result .output ) > 0
515+ assert "QUEUE" in result .output # Should contain table headers
516+
409517 @classmethod
410518 def teardown_class (cls ):
411519 os .environ ["PATH" ] = os .pathsep .join (os .environ ["PATH" ].split (os .pathsep )[:- 1 ])
0 commit comments