|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import textwrap |
| 4 | +import json |
| 5 | +from unittest.mock import patch, mock_open |
| 6 | + |
| 7 | +import pytest |
| 8 | +from click.testing import CliRunner |
| 9 | +import utilities_common.cli as cli |
| 10 | + |
| 11 | +import show.main as show |
| 12 | +import config.main as config |
| 13 | +import show.plugins.barefoot as bfshow |
| 14 | +import config.plugins.barefoot as bfconfig |
| 15 | + |
| 16 | +@pytest.fixture(scope='class') |
| 17 | +def config_env(): |
| 18 | + os.environ["UTILITIES_UNIT_TESTING"] = "1" |
| 19 | + yield |
| 20 | + os.environ["UTILITIES_UNIT_TESTING"] = "0" |
| 21 | + |
| 22 | +class TestStdout: |
| 23 | + stdout = "" |
| 24 | + |
| 25 | +class TestReturncode: |
| 26 | + returncode = False |
| 27 | + |
| 28 | +class TestShowPlatformBarefoot(object): |
| 29 | + |
| 30 | + def test_config_barefoot(self): |
| 31 | + runner = CliRunner() |
| 32 | + expected_output = "" |
| 33 | + result = CliRunner().invoke(show.cli.commands["platform"], ["barefoot"]) |
| 34 | + assert result.output == expected_output |
| 35 | + |
| 36 | + def test_config_profile(self): |
| 37 | + runner = CliRunner() |
| 38 | + expected_output = "Swss service will be restarted, continue? [y/N]: \nAborted!\n" |
| 39 | + result = runner.invoke(bfconfig.barefoot.commands['profile'], ['x1']) |
| 40 | + print("result.exit_code:", result.exit_code) |
| 41 | + print("result.output:", result.output) |
| 42 | + assert result.output == expected_output |
| 43 | + |
| 44 | + def test_check_profile_naming_tf3(self): |
| 45 | + with patch('config.plugins.barefoot.subprocess.run', return_value=0): |
| 46 | + result = bfconfig.check_profile_naming_tf3("y2", "tofino3") |
| 47 | + assert result == 0 |
| 48 | + |
| 49 | + def test_check_profile_naming_tf2(self): |
| 50 | + with patch('config.plugins.barefoot.subprocess.run', return_value=0): |
| 51 | + result = bfconfig.check_profile_naming_tf2("y2") |
| 52 | + assert result == 0 |
| 53 | + |
| 54 | + def test_check_profile_naming_tf3_t(self): |
| 55 | + with patch('config.plugins.barefoot.subprocess.run', return_value=1): |
| 56 | + result = bfconfig.check_profile_naming_tf3("y2", "tofino3") |
| 57 | + assert result == 1 |
| 58 | + |
| 59 | + def test_check_profile_naming_tf2_t(self): |
| 60 | + with patch('config.plugins.barefoot.subprocess.run', return_value=1): |
| 61 | + result = bfconfig.check_profile_naming_tf2("y2") |
| 62 | + assert result == 1 |
| 63 | + |
| 64 | + def test_check_profile_exist1(self): |
| 65 | + completed_process = TestReturncode() |
| 66 | + completed_process.returncode = 0 |
| 67 | + with patch('config.plugins.barefoot.check_profile_naming_tf3', return_value=completed_process): |
| 68 | + result = bfconfig.check_profile_exist("x1", "tofino") |
| 69 | + assert result == 0 |
| 70 | + |
| 71 | + def test_check_profile_exist2(self): |
| 72 | + completed_process1 = TestReturncode() |
| 73 | + completed_process2 = TestReturncode() |
| 74 | + completed_process1.returncode = 1 |
| 75 | + completed_process2.returncode = 0 |
| 76 | + with patch('config.plugins.barefoot.check_profile_naming_tf3', return_value=completed_process1): |
| 77 | + with patch('config.plugins.barefoot.check_profile_naming_tf2', return_value=completed_process2): |
| 78 | + result = bfconfig.check_profile_exist("x1", "tofino") |
| 79 | + assert result == 1 |
| 80 | + |
| 81 | + def test_show_profile(self): |
| 82 | + runner = CliRunner() |
| 83 | + expected_output = "Current profile: default\n" |
| 84 | + result = runner.invoke(bfshow.barefoot.commands['profile'], []) |
| 85 | + print("result.exit_code:", result.exit_code) |
| 86 | + print("result.output:", result.output) |
| 87 | + assert result.output == expected_output |
| 88 | + |
| 89 | + def test_get_chip_family1(self): |
| 90 | + with patch('show.plugins.barefoot.device_info.get_path_to_hwsku_dir', return_value=""): |
| 91 | + chip_family = json.dumps({"chip_list": [{"instance": 0,"chip_family": "tofino3"}]}) |
| 92 | + with patch('show.plugins.barefoot.open', mock_open(read_data=chip_family)): |
| 93 | + result = bfshow.get_chip_family() |
| 94 | + assert result == "tofino3" |
| 95 | + |
| 96 | + def test_get_chip_family2(self): |
| 97 | + with patch('config.plugins.barefoot.device_info.get_path_to_hwsku_dir', return_value=""): |
| 98 | + chip_family = json.dumps({"chip_list": [{"instance": 0,"chip_family": "tofino3"}]}) |
| 99 | + with patch('show.plugins.barefoot.open', mock_open(read_data=chip_family)): |
| 100 | + result = bfconfig.get_chip_family() |
| 101 | + assert result == "tofino3" |
| 102 | + |
| 103 | + def test_show_profile_default(self): |
| 104 | + runner = CliRunner() |
| 105 | + expected_output = "Current profile: default\n" |
| 106 | + with patch("show.plugins.barefoot.check_profile", return_value=1): |
| 107 | + print(show.plugins.barefoot.check_profile()) |
| 108 | + result = runner.invoke(bfshow.barefoot.commands['profile'], []) |
| 109 | + print("result.exit_code:", result.exit_code) |
| 110 | + print("result.output:", result.output) |
| 111 | + assert result.output == expected_output |
| 112 | + |
| 113 | + def test_check_profile1(self): |
| 114 | + ret = TestReturncode() |
| 115 | + ret.returncode = 1 |
| 116 | + with patch('show.plugins.barefoot.subprocess.run', return_value=ret): |
| 117 | + result = bfshow.check_profile() |
| 118 | + print(result) |
| 119 | + assert result == True |
| 120 | + |
| 121 | + def test_check_profile2(self): |
| 122 | + ret = TestReturncode() |
| 123 | + ret.returncode = 1 |
| 124 | + with patch('config.plugins.barefoot.subprocess.run', return_value=ret): |
| 125 | + result = bfconfig.check_profile() |
| 126 | + print(result) |
| 127 | + assert result == True |
| 128 | + |
| 129 | + def test_check_profile3(self): |
| 130 | + ret = TestReturncode() |
| 131 | + ret.returncode = 0 |
| 132 | + with patch('show.plugins.barefoot.subprocess.run', return_value=ret): |
| 133 | + result = bfshow.check_profile() |
| 134 | + print(result) |
| 135 | + assert result == False |
| 136 | + |
| 137 | + def test_check_profile4(self): |
| 138 | + ret = TestReturncode() |
| 139 | + ret.returncode = 0 |
| 140 | + with patch('config.plugins.barefoot.subprocess.run', return_value=ret): |
| 141 | + result = bfconfig.check_profile() |
| 142 | + print(result) |
| 143 | + assert result == False |
| 144 | + |
| 145 | + def test_check_supported_profile1(self): |
| 146 | + result = bfconfig.check_supported_profile("x1", "tofino") |
| 147 | + print(result) |
| 148 | + assert result == True |
| 149 | + |
| 150 | + def test_check_supported_profile2(self): |
| 151 | + result = bfconfig.check_supported_profile("y2", "tofino2") |
| 152 | + print(result) |
| 153 | + assert result == True |
| 154 | + |
| 155 | + def test_check_supported_profile3(self): |
| 156 | + result = bfconfig.check_supported_profile("x1", "tofino2") |
| 157 | + print(result) |
| 158 | + assert result == False |
| 159 | + |
| 160 | + def test_check_supported_profile4(self): |
| 161 | + result = bfconfig.check_supported_profile("y2", "tofino") |
| 162 | + print(result) |
| 163 | + assert result == False |
| 164 | + |
| 165 | + def test_get_current_profile(self): |
| 166 | + with patch('show.plugins.barefoot.subprocess.run', return_value="y2"): |
| 167 | + result = bfshow.get_current_profile() |
| 168 | + assert result == "y2" |
| 169 | + |
| 170 | + def test_get_profile_format(self): |
| 171 | + with patch('show.plugins.barefoot.subprocess') as subprocess: |
| 172 | + subprocess.return_value = 0 |
| 173 | + result = bfshow.get_profile_format("tofino") |
| 174 | + assert result == "_profile" |
| 175 | + |
| 176 | + def test_get_available_profiles(self): |
| 177 | + with patch('show.plugins.barefoot.subprocess.run', return_value="x2"): |
| 178 | + result = bfshow.get_available_profiles("install_x1_tofino") |
| 179 | + assert result == "x2" |
| 180 | + |
| 181 | + def test_show_profile(self): |
| 182 | + runner = CliRunner() |
| 183 | + expected_output = """\ |
| 184 | +Current profile: y2 |
| 185 | +Available profile(s): |
| 186 | +x1 |
| 187 | +x2 |
| 188 | +y2 |
| 189 | +y3 |
| 190 | +""" |
| 191 | + with patch("show.plugins.barefoot.check_profile", return_value=False): |
| 192 | + with patch("show.plugins.barefoot.get_chip_family", return_value="tofino"): |
| 193 | + current_profile = TestStdout() |
| 194 | + current_profile.stdout = "y2\n" |
| 195 | + with patch("show.plugins.barefoot.get_current_profile", return_value=current_profile): |
| 196 | + with patch("show.plugins.barefoot.get_profile_format", return_value="_profile"): |
| 197 | + available_profile = TestStdout() |
| 198 | + available_profile.stdout = "x1\nx2\ny2\ny3\n" |
| 199 | + with patch("show.plugins.barefoot.get_available_profiles", return_value=available_profile): |
| 200 | + result = runner.invoke(bfshow.barefoot.commands['profile'], []) |
| 201 | + print("result.exit_code:", result.exit_code) |
| 202 | + print("result.output:", result.output) |
| 203 | + assert result.output == expected_output |
0 commit comments