From 79ed8df3c36f03ae2f2b930e417c7978a6fb12e5 Mon Sep 17 00:00:00 2001 From: isabelmsft Date: Wed, 19 Apr 2023 23:41:20 +0000 Subject: [PATCH] add UT --- .../field_operation_validators.py | 4 +- .../field_operation_validator_test.py | 68 ++++++++++++++++--- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/generic_config_updater/field_operation_validators.py b/generic_config_updater/field_operation_validators.py index ff7f0e356f..fd291e79db 100644 --- a/generic_config_updater/field_operation_validators.py +++ b/generic_config_updater/field_operation_validators.py @@ -86,8 +86,8 @@ def rdma_config_update_validator(path, operation): if operation not in scenario["operations"]: return False - if platform in scenario["platforms"]: - if version < scenario["platforms"][platform]: + if asic in scenario["platforms"]: + if branch_version < scenario["platforms"][asic]: return False else: return False diff --git a/tests/generic_config_updater/field_operation_validator_test.py b/tests/generic_config_updater/field_operation_validator_test.py index 9ff3987954..d117e06478 100644 --- a/tests/generic_config_updater/field_operation_validator_test.py +++ b/tests/generic_config_updater/field_operation_validator_test.py @@ -1,17 +1,76 @@ +import io import unittest import mock +import json import subprocess import generic_config_updater import generic_config_updater.field_operation_validators as fov import generic_config_updater.gu_common as gu_common -from unittest.mock import MagicMock, Mock +from unittest.mock import MagicMock, Mock, mock_open from mock import patch from sonic_py_common.device_info import get_hwsku, get_sonic_version_info class TestValidateFieldOperation(unittest.TestCase): + @patch("sonic_py_common.device_info.get_sonic_version_info", mock.Mock(return_value={"build_version": "SONiC.20181131"})) + @patch("generic_config_updater.field_operation_validators.get_asic_name", mock.Mock(return_value="unknown")) + def test_rdma_config_update_validator_unknown_asic(self): + path = "/PFC_WD/GLOBAL/POLL_INTERVAL" + operation = "replace" + assert generic_config_updater.field_operation_validators.rdma_config_update_validator(path, operation) == False + + @patch("sonic_py_common.device_info.get_sonic_version_info", mock.Mock(return_value={"build_version": "SONiC.20171131"})) + @patch("generic_config_updater.field_operation_validators.get_asic_name", mock.Mock(return_value="spc1")) + @patch("os.path.exists", mock.Mock(return_value=True)) + @patch("builtins.open", mock_open(read_data="sample")) + @patch('json.load') + def test_rdma_config_update_validator_spc_asic_invalid_version(self, mock_json_config): + path = "/PFC_WD/Ethernet8/action" + operation = "replace" + mock_json_config.return_value = {"tables": {"pfc_wd": {"validator_data": {"rdma_config_update_validator": {"PFCWD enable/disable": {"fields": ["detection_time", "action"], "operations": ["remove", "replace", "add"], "platforms": {"spc1": "20181100"}}}}}}} + assert generic_config_updater.field_operation_validators.rdma_config_update_validator(path, operation) == False + + @patch("sonic_py_common.device_info.get_sonic_version_info", mock.Mock(return_value={"build_version": "SONiC.20181131"})) + @patch("generic_config_updater.field_operation_validators.get_asic_name", mock.Mock(return_value="spc1")) + @patch("os.path.exists", mock.Mock(return_value=True)) + @patch("builtins.open", mock_open(read_data="sample")) + @patch('json.load') + def test_rdma_config_update_validator_spc_asic_valid_version(self, mock_json_config): + path = "/PFC_WD/Ethernet8/action" + operation = "replace" + mock_json_config.return_value = {"tables": {"pfc_wd": {"validator_data": {"rdma_config_update_validator": {"PFCWD enable/disable": {"fields": ["detection_time", "action"], "operations": ["remove", "replace", "add"], "platforms": {"spc1": "20181100"}}}}}}} + assert generic_config_updater.field_operation_validators.rdma_config_update_validator(path, operation) == True + + @patch("sonic_py_common.device_info.get_sonic_version_info", mock.Mock(return_value={"build_version": "SONiC.20181131"})) + @patch("generic_config_updater.field_operation_validators.get_asic_name", mock.Mock(return_value="spc1")) + @patch("os.path.exists", mock.Mock(return_value=True)) + @patch("builtins.open", mock_open(read_data="sample")) + @patch('json.load') + def test_rdma_config_update_validator_spc_asic_invalid_op(self, mock_json_config): + path = "/PFC_WD/Ethernet8/action" + operation = "invalid-op" + mock_json_config.return_value = {"tables": {"pfc_wd": {"validator_data": {"rdma_config_update_validator": {"PFCWD enable/disable": {"fields": ["detection_time", "action"], "operations": ["remove", "replace", "add"], "platforms": {"spc1": "20181100"}}}}}}} + assert generic_config_updater.field_operation_validators.rdma_config_update_validator(path, operation) == False + + @patch("sonic_py_common.device_info.get_sonic_version_info", mock.Mock(return_value={"build_version": "SONiC.20181131"})) + @patch("generic_config_updater.field_operation_validators.get_asic_name", mock.Mock(return_value="spc1")) + @patch("os.path.exists", mock.Mock(return_value=True)) + @patch("builtins.open", mock_open(read_data="sample")) + @patch('json.load') + def test_rdma_config_update_validator_spc_asic_other_field(self, mock_json_config): + path = "/PFC_WD/Ethernet8/other_field" + operation = "invalid-op" + mock_json_config.return_value = {"tables": {"pfc_wd": {"validator_data": {"rdma_config_update_validator": {"PFCWD enable/disable": {"fields": ["detection_time", "action"], "operations": ["remove", "replace", "add"], "platforms": {"spc1": "20181100"}}}}}}} + assert generic_config_updater.field_operation_validators.rdma_config_update_validator(path, operation) == False + + @patch("sonic_py_common.device_info.get_sonic_version_info", mock.Mock(return_value={"build_version": "SONiC.20181131"})) + @patch("generic_config_updater.field_operation_validators.get_asic_name", mock.Mock(return_value="unknown")) + def test_rdma_config_update_validator_unknown_asic(self): + path = "/PFC_WD/GLOBAL/POLL_INTERVAL" + operation = "replace" + @patch("sonic_py_common.device_info.get_sonic_version_info", mock.Mock(return_value={"asic_type": "mellanox", "build_version": "SONiC.20181131"})) def test_validate_field_operation_legal__pfcwd(self): old_config = {"PFC_WD": {"GLOBAL": {"POLL_INTERVAL": "60"}}} @@ -25,13 +84,6 @@ def test_validate_field_operation_illegal__pfcwd(self): config_wrapper = gu_common.ConfigWrapper() self.assertRaises(gu_common.IllegalPatchOperationError, config_wrapper.validate_field_operation, old_config, target_config) - @patch("sonic_py_common.device_info.get_sonic_version_info", mock.Mock(return_value={"build_version": "SONiC.20181131"})) - @patch("generic_config_updater.field_operation_validators.get_asic_name", mock.Mock(return_value="unknown")) - def test_validate_field_modification_illegal__pfcwd(self): - path = "/PFC_WD/GLOBAL/POLL_INTERVAL" - operation = "replace" - assert generic_config_updater.field_operation_validators.rdma_config_update_validator(path, operation) == False - def test_validate_field_operation_legal__rm_loopback1(self): old_config = { "LOOPBACK_INTERFACE": {