|
| 1 | +import json |
| 2 | +import click |
| 3 | +import pytest |
| 4 | +from click.testing import CliRunner |
| 5 | +import show.plugins.barefoot as show |
| 6 | +from unittest.mock import call, patch, mock_open, MagicMock |
| 7 | + |
| 8 | + |
| 9 | +class TestShowBarefoot(object): |
| 10 | + def setup(self): |
| 11 | + print('SETUP') |
| 12 | + |
| 13 | + @patch('subprocess.run') |
| 14 | + def test_default_profile(self, mock_run): |
| 15 | + mock_run.return_value.returncode = 1 |
| 16 | + runner = CliRunner() |
| 17 | + result = runner.invoke(show.profile, []) |
| 18 | + assert result.exit_code == 0 |
| 19 | + assert result.output == 'Current profile: default\n' |
| 20 | + mock_run.assert_called_once_with(['docker', 'exec', '-it', 'syncd', 'test', '-h', '/opt/bfn/install']) |
| 21 | + |
| 22 | + @patch('show.plugins.barefoot.getstatusoutput_noshell_pipe') |
| 23 | + @patch('show.plugins.barefoot.device_info.get_path_to_hwsku_dir', MagicMock(return_value='/usr/share/sonic/hwsku_dir')) |
| 24 | + @patch('subprocess.run') |
| 25 | + def test_nondefault_profile(self, mock_run, mock_cmd): |
| 26 | + mock_run.return_value.returncode = 0 |
| 27 | + chip_list = [{'chip_family': 'TOFINO'}] |
| 28 | + mock_open_args = mock_open(read_data=json.dumps({'chip_list': chip_list})) |
| 29 | + expected_calls = [ |
| 30 | + call( |
| 31 | + ['docker', 'exec', '-it', 'syncd', 'readlink', '/opt/bfn/install'], |
| 32 | + ['sed', 's/install_\\\\\\(.\\*\\\\\\)_profile/\\\\1/'] |
| 33 | + ), |
| 34 | + |
| 35 | + call( |
| 36 | + ['docker', 'exec', '-it', 'syncd', 'find', '/opt/bfn', '-mindepth', '1',\ |
| 37 | + '-maxdepth', '1', '-type', 'd', '-name', r'install_\*_profile', r'\! -name install_y\*_profile'], |
| 38 | + ["sed", r's%/opt/bfn/install_\\\(.\*\\\)_profile%\\1%'] |
| 39 | + ) |
| 40 | + ] |
| 41 | + |
| 42 | + with patch("builtins.open", mock_open_args) as mock_open_file: |
| 43 | + runner = CliRunner() |
| 44 | + result = runner.invoke(show.profile) |
| 45 | + assert result.exit_code == 0 |
| 46 | + |
| 47 | + mock_run.assert_called_once_with(['docker', 'exec', '-it', 'syncd', 'test', '-h', '/opt/bfn/install']) |
| 48 | + mock_open_file.assert_called_once_with('/usr/share/sonic/hwsku_dir/switch-tna-sai.conf') |
| 49 | + assert mock_cmd.call_args_list == expected_calls |
| 50 | + |
| 51 | + def teardown(self): |
| 52 | + print('TEARDOWN') |
| 53 | + |
0 commit comments