Skip to content

Commit dfb5e81

Browse files
author
Taras Keryk
committed
Merge branch 'tk_tof3_prof_nam_bf' of github.com:tkerykx/sonic-utilities into tk_tof3_prof_nam_bf
2 parents 27667cf + b97a299 commit dfb5e81

File tree

6 files changed

+444
-17
lines changed

6 files changed

+444
-17
lines changed

config/plugins/barefoot.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,33 @@ def profile(profile):
3232
chip_family = json.load(file)['chip_list'][0]['chip_family'].lower()
3333

3434
# Check if profile is supported
35-
if chip_family == 'tofino' and profile[0] == 'y' or \
36-
chip_family == 'tofino2' and profile[0] == 'x':
35+
if chip_family == 'tofino' and profile[0] != 'x' or \
36+
chip_family == 'tofino2' and profile[0] != 'y':
3737
click.echo('Specified profile is unsupported on the system')
3838
raise click.Abort()
39-
40-
# Check if profile exists
39+
40+
# Check if profile <profile_name>_<chip_family> exists
41+
no_arch_information = False
4142
completed_process = subprocess.run(['docker', 'exec', '-it', 'syncd',
42-
'test', '-d', '/opt/bfn/install_' + profile + '_profile'])
43+
'test', '-d', '/opt/bfn/install_' + profile + '_' + chip_family])
44+
45+
# Otherwise, check if profile <profile_name>_profile exists (only for tofino and tofino2)
4346
if completed_process.returncode != 0:
44-
click.echo('No profile with the provided name found')
47+
if chip_family == 'tofino' or chip_family == 'tofino2':
48+
completed_process = subprocess.run(['docker', 'exec', '-it', 'syncd',
49+
'test', '-d', '/opt/bfn/install_' + profile + '_profile'])
50+
no_arch_information = True
51+
52+
if completed_process.returncode != 0:
53+
click.echo('No profile with the provided name found for {}'.format(chip_family))
4554
raise click.Abort()
4655

4756
# Update configuration
4857
config_db = ConfigDBConnector()
4958
config_db.connect()
50-
config_db.mod_entry('DEVICE_METADATA', 'localhost',
51-
{'p4_profile': profile + '_profile'})
59+
profile += '_profile' if no_arch_information else '_' + chip_family
60+
config_db.mod_entry('DEVICE_METADATA', 'localhost', {'p4_profile': profile})
61+
5262
subprocess.run(['systemctl', 'restart', 'swss'], check=True)
5363

5464
def register(cli):

show/platform.py

+7
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,10 @@ def firmware(args):
154154
subprocess.check_call(cmd, shell=True)
155155
except subprocess.CalledProcessError as e:
156156
sys.exit(e.returncode)
157+
158+
# 'barefoot' subcommand ("show platform barefoot")
159+
@platform.command()
160+
def barefoot():
161+
"""Show Barefoot profile information"""
162+
cmd = "barefoot profile"
163+
clicommon.run_command(cmd)

show/plugins/barefoot.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,32 @@ def profile():
2222
hwsku_dir = device_info.get_path_to_hwsku_dir()
2323
with open(hwsku_dir + '/switch-tna-sai.conf') as file:
2424
chip_family = json.load(file)['chip_list'][0]['chip_family'].lower()
25-
25+
2626
# Print current profile
2727
click.echo('Current profile: ', nl=False)
2828
subprocess.run('docker exec -it syncd readlink /opt/bfn/install | sed '
29-
r's/install_\\\(.\*\\\)_profile/\\1/', check=True, shell=True)
30-
31-
# Exclude current and unsupported profiles
29+
r's/install_\\\(.\*\\\)_profile/\\1/'
30+
r' | sed s/install_\\\(.\*\\\)_tofino\\\(.\*\\\)/\\1/', check=True, shell=True)
31+
32+
# Check if profile naming format contains tofino family information
33+
output = subprocess.check_output(['docker', 'exec', '-it', 'syncd', 'ls', '/opt/bfn']).strip().decode()
34+
suffix = '_' + chip_family if '_tofino' in output else '_profile'
35+
36+
# Check supported profiles
3237
opts = ''
3338
if chip_family == 'tofino':
34-
opts = r'\! -name install_y\*_profile '
39+
opts = r' -name install_x\*' + suffix
3540
elif chip_family == 'tofino2':
36-
opts = r'\! -name install_x\*_profile '
37-
41+
opts = r' -name install_y\*' + suffix
42+
else:
43+
opts = r' -name \*' + suffix
44+
3845
# Print profile list
3946
click.echo('Available profile(s):')
4047
subprocess.run('docker exec -it syncd find /opt/bfn -mindepth 1 '
41-
r'-maxdepth 1 -type d -name install_\*_profile ' + opts + '| sed '
42-
r's%/opt/bfn/install_\\\(.\*\\\)_profile%\\1%', shell=True)
48+
r'-maxdepth 1 -type d,l ' + opts + '| sed '
49+
r's%/opt/bfn/install_\\\(.\*\\\)_profile%\\1%'
50+
r' | sed s%/opt/bfn/install_\\\(.\*\\\)_tofino\\\(.\*\\\)%\\1%', shell=True)
4351

4452
def register(cli):
4553
version_info = device_info.get_sonic_version_info()

tests/config_barefoot_test.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import os
2+
import sys
3+
import textwrap
4+
from unittest import mock
5+
6+
import pytest
7+
from click.testing import CliRunner
8+
import config.plugins.barefoot as bf
9+
import config.main as config_main
10+
11+
@pytest.fixture(scope='class')
12+
def config_env():
13+
os.environ["UTILITIES_UNIT_TESTING"] = "1"
14+
15+
yield
16+
17+
os.environ["UTILITIES_UNIT_TESTING"] = "0"
18+
19+
class TestConfigPlatformBarefoot(object):
20+
@classmethod
21+
def setup_class(cls):
22+
print("SETUP")
23+
24+
def test_profile(self):
25+
runner = CliRunner()
26+
expected_output = "Swss service will be restarted, continue? [y/N]: \nAborted!\n"
27+
#result = runner.invoke(show.cli.commands['platform'].commands['barefoot'], ['profile'])
28+
#result = runner.invoke(show.cli.commands['platform'].commands['barefoot'].commands['profile'])
29+
result = runner.invoke(bf.barefoot.commands['profile'], ['default'])
30+
#assert mock_run_command.call_count == 0
31+
#assert result.exit_code == 2
32+
#assert result.exit_code == 0
33+
print("result.exit_code:", result.exit_code)
34+
print("result.output:", result.output)
35+
assert result.output == expected_output
36+
#mock_run_command.assert_called_with('show platform barefoot profile', display_cmd=False)
37+
38+
def test_register(self):
39+
bf.register(config_main.config)

0 commit comments

Comments
 (0)