|
| 1 | +# pyre-unsafe |
| 2 | + |
| 3 | +import os |
| 4 | +import unittest.mock as mock # noqa |
| 5 | +from typing import List, Optional |
| 6 | + |
| 7 | +from autoval.lib.connection.connection_abstract import ConnectionAbstract |
| 8 | +from autoval.lib.connection.connection_utils import CmdResult |
| 9 | +from autoval.lib.host.bmc import BMC |
| 10 | +from autoval.lib.host.system import System |
| 11 | +from autoval.lib.utils.autoval_exceptions import CmdError |
| 12 | +from autoval.lib.utils.file_actions import FileActions |
| 13 | +from autoval.unittest.mock.lib.mock_connection_dispatcher import ( |
| 14 | + MOCK_HOSTS, |
| 15 | + MockConnectionDispatcher, |
| 16 | +) |
| 17 | +from autoval.unittest.mock.lib.mock_openbmc import MockOpenBMC |
| 18 | + |
| 19 | + |
| 20 | +MOCK_INPUT_PATH = "autoval/unittest/mock/util_outputs/" |
| 21 | +MOCK_INPUT_RELATIVE_PATH = "autoval/unittest/mock/util_outputs/" |
| 22 | + |
| 23 | + |
| 24 | +class MockHost(ConnectionAbstract): |
| 25 | + def __init__(self, cmd_map, run_return_plain_stdout=False): |
| 26 | + self.run_return_plain_stdout = run_return_plain_stdout |
| 27 | + self.cmd_map = cmd_map |
| 28 | + self.bmc_type = "Openbmc" |
| 29 | + self.connection_obj = MockConnectionDispatcher() |
| 30 | + self.hostname = self.connection_obj.hostname |
| 31 | + self.connection = self.connection_obj.host_connection |
| 32 | + self.localhost = self |
| 33 | + self.oob_addr = self.connection_obj.oob_addr |
| 34 | + self.oob_only = self.connection_obj.oob_only |
| 35 | + self.rack_sub_position = self.connection_obj.rack_sub_position |
| 36 | + self.rack_sub_position_slot = self.connection_obj.rack_sub_position_slot |
| 37 | + self.product_obj = self._get_product_obj() |
| 38 | + self.host.product_obj.product_name = "TestPlatform V2" |
| 39 | + self._openbmc_obj = MockOpenBMC(self, self.cmd_map)._get_openbmc() |
| 40 | + self.host_dict = MOCK_HOSTS |
| 41 | + self.oobs = [self.oob] |
| 42 | + # storage for iterator over run() results |
| 43 | + self._iter_result = {} |
| 44 | + |
| 45 | + @property |
| 46 | + def openbmc_obj(self): |
| 47 | + return self._openbmc_obj |
| 48 | + |
| 49 | + @openbmc_obj.setter |
| 50 | + def openbmc_obj(self, cls_obj): |
| 51 | + if isinstance(cls_obj, BMC): |
| 52 | + self._openbmc_obj = cls_obj |
| 53 | + else: |
| 54 | + raise Exception("cls_obj is not an openBMC obj") |
| 55 | + |
| 56 | + # override run just like for sshpass |
| 57 | + def run( |
| 58 | + self, |
| 59 | + cmd: str, |
| 60 | + ignore_status: bool = False, |
| 61 | + timeout: int = 600, |
| 62 | + working_directory: Optional[str] = None, |
| 63 | + custom_logfile: Optional[str] = None, |
| 64 | + get_pty: bool = False, |
| 65 | + sudo: bool = False, |
| 66 | + sudo_options: Optional[List[str]] = None, |
| 67 | + connection_timeout: int = 60, |
| 68 | + background: bool = False, |
| 69 | + keepalive: int = 0, |
| 70 | + forward_ssh_agent=False, |
| 71 | + path_env=None, |
| 72 | + ) -> str: |
| 73 | + if self.run_return_plain_stdout: |
| 74 | + return self.run_get_result( |
| 75 | + cmd, |
| 76 | + ignore_status, |
| 77 | + timeout, |
| 78 | + working_directory, |
| 79 | + custom_logfile, |
| 80 | + get_pty, |
| 81 | + sudo, |
| 82 | + sudo_options, |
| 83 | + connection_timeout, |
| 84 | + background, |
| 85 | + keepalive, |
| 86 | + forward_ssh_agent, |
| 87 | + path_env, |
| 88 | + ).stdout |
| 89 | + |
| 90 | + return super(MockHost, self).run( |
| 91 | + cmd, |
| 92 | + ignore_status, |
| 93 | + timeout, |
| 94 | + working_directory, |
| 95 | + custom_logfile, |
| 96 | + get_pty, |
| 97 | + sudo, |
| 98 | + sudo_options, |
| 99 | + connection_timeout, |
| 100 | + background, |
| 101 | + keepalive, |
| 102 | + forward_ssh_agent, |
| 103 | + path_env, |
| 104 | + ) |
| 105 | + |
| 106 | + def run_get_result( |
| 107 | + self, |
| 108 | + cmd: str, |
| 109 | + ignore_status: bool = False, |
| 110 | + timeout: int = 600, |
| 111 | + working_directory: Optional[str] = None, |
| 112 | + custom_logfile: Optional[str] = None, |
| 113 | + get_pty: bool = False, |
| 114 | + sudo: bool = False, |
| 115 | + sudo_options: Optional[List[str]] = None, |
| 116 | + connection_timeout: int = 60, |
| 117 | + background: bool = False, |
| 118 | + keepalive: int = 0, |
| 119 | + forward_ssh_agent=False, |
| 120 | + path_env=None, |
| 121 | + ) -> CmdResult: |
| 122 | + # whether to iterate over multiple result values |
| 123 | + iterate = False |
| 124 | + data = None |
| 125 | + return_code = 0 |
| 126 | + for c in self.cmd_map: |
| 127 | + if cmd == c["cmd"]: |
| 128 | + if "return_code" in c: |
| 129 | + return_code = c["return_code"] |
| 130 | + if "result" in c: |
| 131 | + data = c["result"] |
| 132 | + else: |
| 133 | + data = self._read_file(c["file"]) |
| 134 | + if "iterate" in c and c["iterate"]: |
| 135 | + iterate = True |
| 136 | + break |
| 137 | + if iterate and type(data) in (list, tuple): |
| 138 | + if cmd not in self._iter_result: |
| 139 | + self._iter_result[cmd] = iter(data) |
| 140 | + if isinstance(data, CmdError): |
| 141 | + raise data |
| 142 | + if not data: |
| 143 | + data = "" |
| 144 | + if cmd in self._iter_result: |
| 145 | + try: |
| 146 | + return next(self._iter_result[cmd]) |
| 147 | + except StopIteration: |
| 148 | + return None # pyre-fixme[7] |
| 149 | + return CmdResult(cmd, str(data), "", return_code, 1) |
| 150 | + |
| 151 | + @classmethod |
| 152 | + def read_file(cls, file_path, **kwargs): |
| 153 | + return cls._read_file(file_path, **kwargs) |
| 154 | + |
| 155 | + def get_file(self, file_path, target, **kwargs): |
| 156 | + pass |
| 157 | + |
| 158 | + def put_file(self, file_path, target, **kwargs): |
| 159 | + pass |
| 160 | + |
| 161 | + def scp_file( |
| 162 | + self, source_location: str, file_tocopy: str, destination_location: str |
| 163 | + ) -> str: |
| 164 | + pass |
| 165 | + |
| 166 | + def get_host_arch(self) -> str: |
| 167 | + pass |
| 168 | + |
| 169 | + def _connect(self): |
| 170 | + pass |
| 171 | + |
| 172 | + def _get_product_obj(self): |
| 173 | + system = System(self) |
| 174 | + return system |
| 175 | + |
| 176 | + @staticmethod |
| 177 | + def _read_file(_file, json_file=False): |
| 178 | + _file = os.path.join(MOCK_INPUT_RELATIVE_PATH, _file) |
| 179 | + # file_path = FileActions.get_resource_file_path(_file[14:]) |
| 180 | + return FileActions.read_data(path=_file, json_file=json_file) |
| 181 | + |
| 182 | + def update_cmd_map( |
| 183 | + self, |
| 184 | + cmd: str, |
| 185 | + mock_output: str, |
| 186 | + is_file: bool = False, |
| 187 | + return_code: str = "0", |
| 188 | + ) -> None: |
| 189 | + """This method will update the command map with the cmd values |
| 190 | + in case if the command is already present it would update the file |
| 191 | + or result value of the command.""" |
| 192 | + for each_cmd_map in self.cmd_map: |
| 193 | + if each_cmd_map["cmd"] == cmd: |
| 194 | + each_cmd_map["return_code"] = return_code |
| 195 | + if is_file: |
| 196 | + each_cmd_map["file"] = mock_output |
| 197 | + else: |
| 198 | + each_cmd_map["result"] = mock_output |
| 199 | + return |
| 200 | + cmd_map_dict = {"cmd": cmd} |
| 201 | + cmd_map_dict["return_code"] = return_code |
| 202 | + if is_file: |
| 203 | + cmd_map_dict["file"] = mock_output |
| 204 | + else: |
| 205 | + cmd_map_dict["result"] = mock_output |
| 206 | + self.cmd_map.append(cmd_map_dict) |
| 207 | + |
| 208 | + def add_test_start_msg(self, test_name): |
| 209 | + |
| 210 | + pass |
| 211 | + |
| 212 | + def add_test_end_msg(self, test_name): |
| 213 | + pass |
| 214 | + |
| 215 | + def deploy_tool(self, tool) -> str: |
| 216 | + return "" |
| 217 | + |
| 218 | + def __getattr__(self, attr): |
| 219 | + if attr == "oob": |
| 220 | + _oob = MockOpenBMC(self, self.openbmc_obj)._get_openbmc() |
| 221 | + setattr(self, "oob", _oob) # noqa |
| 222 | + return _oob |
| 223 | + return getattr(self.product_obj, attr) |
| 224 | + |
| 225 | + def revert_cmd_map(self, cmd_map: List): |
| 226 | + for each_cmd_map in cmd_map: |
| 227 | + cmd = each_cmd_map["cmd"] |
| 228 | + if "result" in each_cmd_map: |
| 229 | + self.update_cmd_map(cmd, each_cmd_map["result"], is_file=False) |
| 230 | + else: |
| 231 | + self.update_cmd_map(cmd, each_cmd_map["file"], is_file=True) |
0 commit comments