-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Manimaran R <[email protected]>
- Loading branch information
Manimaran R
committed
Sep 25, 2024
1 parent
5e601b7
commit 2e0ac71
Showing
10 changed files
with
1,022 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
load("@fbcode_macros//build_defs:python_library.bzl", "python_library") | ||
load("@fbcode_macros//build_defs:python_unittest.bzl", "python_unittest") | ||
|
||
oncall("rtp_tools_automation") | ||
|
||
python_library( | ||
name = "autoval_cfg", | ||
resources = glob(["*.json"]) + glob(["**/*.json"]) + glob(["**/*.yaml"]), | ||
) | ||
|
||
python_library( | ||
name = "mock_libs", | ||
srcs = [ | ||
"mock/lib/mock_autoval.py", | ||
"mock/lib/mock_connection_dispatcher.py", | ||
"mock/lib/mock_host.py", | ||
"mock/lib/mock_openbmc.py", | ||
"mock/lib/mock_test_base_init.py", | ||
"mock/lib/mock_threadpool_executor.py", | ||
], | ||
deps = [ | ||
"//autoval/lib:test_args", | ||
"//autoval/lib/connection:connection", | ||
"//autoval/lib/host:host", | ||
"//autoval/lib/transport:transport", | ||
"//autoval/lib/utils:autoval_utils", | ||
"//autoval/plugins:plugin_manager", # @manual | ||
], | ||
) | ||
|
||
python_unittest( | ||
name = "test_manifest", | ||
srcs = ["test_manifest.py"], | ||
deps = [ | ||
"//autoval/lib/utils:autoval_utils", | ||
"//autoval/plugins:plugin_manager", # @manual | ||
], | ||
) | ||
|
||
python_unittest( | ||
name = "test_test_base", | ||
srcs = [ | ||
"test_test_base.py", | ||
], | ||
deps = [ | ||
":mock_libs", | ||
"//autoval/lib:test_args", | ||
"//autoval/lib:test_base", | ||
"//autoval/lib/host:host", | ||
"//autoval/lib/test_utils:bg_runner", | ||
"//autoval/lib/utils:autoval_utils", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# pyre-unsafe | ||
|
||
import csv | ||
import logging | ||
import unittest.mock as mock | ||
|
||
from autoval.lib.transport.ssh import SSHConn | ||
from autoval.lib.utils.autoval_exceptions import CmdError | ||
from autoval.lib.utils.autoval_log import AutovalLog | ||
from autoval.lib.utils.autoval_utils import AutovalUtils, CmdResult | ||
from autoval.lib.utils.file_actions import FileActions | ||
from autoval.unittest.mock.lib.mock_host import MockHost | ||
|
||
MOCK_INPUT_PATH = "autoval/unittest/mock/util_outputs/" | ||
MOCK_COMMAND_MAP_PATH = "autoval/unittest/mock/testbed/cmd_map" | ||
|
||
|
||
class MockAutovalUtils(MockHost): | ||
def __init__(self, cmd_map=None): | ||
self.autovalLog = AutovalLog._init_logs() | ||
self.logger = logging.getLogger("cmdlog") | ||
self.cmd_output_dict = None | ||
self.get_result_obj_rc = 0 | ||
if cmd_map: | ||
self.cmd_map = cmd_map | ||
else: | ||
self.cmd_map = self.generate_cmp_map() | ||
super(MockAutovalUtils, self).__init__(self.cmd_map) | ||
|
||
@staticmethod | ||
def generate_cmp_map(): | ||
"""This function will convert the cmd_map file into the list of | ||
dict with in format [{"cmd"="cmd","file"="file_path"},..]""" | ||
try: | ||
file_path = FileActions.get_resource_file_path(MOCK_COMMAND_MAP_PATH[14:]) | ||
with open(file_path, "r") as file_context: | ||
cmd_map_reader = csv.reader( | ||
file_context, delimiter=":", quoting=csv.QUOTE_ALL | ||
) | ||
"""in case cmd has the delimiter part of it, csv reader | ||
will consider the last element as "file" and will join | ||
the rest of elements to command""" | ||
cmd_map = [ | ||
{ | ||
"cmd": ":".join(each_cmd_map[0:-1]).strip(), | ||
"file": each_cmd_map[-1].strip(), | ||
} | ||
for each_cmd_map in cmd_map_reader | ||
] | ||
return cmd_map | ||
except Exception: | ||
raise Exception( | ||
f"Failed to generate the cmd_map from file {MOCK_COMMAND_MAP_PATH}" | ||
) | ||
|
||
def run(self, *params, **kparams): | ||
"""Function is side effect of mocking run method and | ||
will be give a mock output based on the cmd_map | ||
*params will contain values of cmd from run method | ||
**kparams will contain the key argument values of get_result_obj, | ||
ignore_status,custom_logfile cmd_output_dict is used | ||
in case cmd_map is not to be referred which would be and optimised way | ||
in case we have single line output instead of creating file | ||
get_result_obj_rc return code of command run by default set to 0 | ||
""" | ||
data = None | ||
cmd = params[0] | ||
get_result_obj = kparams.get("get_result_obj") | ||
ignore_status = kparams.get("ignore_status") | ||
if self.cmd_output_dict and cmd in self.cmd_output_dict: | ||
data = self.cmd_output_dict[cmd] | ||
if isinstance(data, Exception): | ||
raise data | ||
else: | ||
if get_result_obj: | ||
data = self.run_get_result(cmd, ignore_status) | ||
else: | ||
data = super(MockAutovalUtils, self).run(cmd, ignore_status) | ||
if get_result_obj: | ||
data = CmdResult(cmd, data, "", self.get_result_obj_rc) | ||
if self.get_result_obj_rc and not ignore_status: | ||
raise CmdError(cmd, data, "command failed") | ||
return data | ||
|
||
def get_mock_data(self, funct, *args, **kwargs): | ||
"""Function will mock the methods which should run on Dut | ||
such as run""" | ||
self.cmd_output_dict = kwargs.pop("cmd_output_dict", None) | ||
self.get_result_obj_rc = kwargs.pop("get_result_obj_rc", 0) | ||
with mock.patch.object( | ||
SSHConn, "scp_file", return_value="pass" | ||
), mock.patch.object(SSHConn, "run", side_effect=self.run), mock.patch.object( | ||
AutovalUtils, "run_get_output", side_effect=self.run | ||
), mock.patch.object( | ||
AutovalLog, | ||
"log_info", | ||
side_effect=self.logger.info, | ||
): | ||
return funct(*args, **kwargs) |
25 changes: 25 additions & 0 deletions
25
src/autoval/unittest/mock/lib/mock_connection_dispatcher.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# pyre-unsafe | ||
|
||
from autoval.lib.transport.ssh import SSHConn | ||
|
||
MOCK_HOSTS = { | ||
"hostname": "using.fake.host", | ||
"ipv6": "abcd:db00:0012:700e:face:0000:0023:0000", | ||
"oob_addr": "using-oob.fake.host", | ||
"rack_sub_position_slot": 1, | ||
"is_container": False, | ||
} | ||
|
||
|
||
class MockConnectionDispatcher: | ||
def __init__(self): | ||
self.oob_only = None | ||
self.host_connection = SSHConn(None) | ||
self.bmc_connections = [SSHConn(None)] | ||
self._bmc_connections = [SSHConn(None)] | ||
self.oob_addr = MOCK_HOSTS.get("oob_addr") | ||
self.rack_sub_position = MOCK_HOSTS.get("rack_sub_position") | ||
self.rack_sub_position_slot = MOCK_HOSTS.get("rack_sub_position_slot") | ||
self.hostname = MOCK_HOSTS.get("hostname") | ||
self.localhost = None | ||
self.host_dict = MOCK_HOSTS |
Oops, something went wrong.