blob: c8febc93381fdc7c08608547bc3c4447988dec85 [file] [log] [blame]
#
# Copyright (c) 2024 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import importlib
import os
import sys
from pathlib import Path
from unittest.mock import MagicMock
from chip.clusters import Attribute
try:
from matter_testing_support import MatterStackState, MatterTestConfig, run_tests_no_exit
except ImportError:
sys.path.append(os.path.abspath(
os.path.join(os.path.dirname(__file__), '..')))
from matter_testing_support import MatterStackState, MatterTestConfig, run_tests_no_exit
class AsyncMock(MagicMock):
async def __call__(self, *args, **kwargs):
return super(AsyncMock, self).__call__(*args, **kwargs)
class MockTestRunner():
def __init__(self, filename: str, classname: str, test: str, endpoint: int = 0, pics: dict[str, bool] = None):
self.test = test
self.endpoint = endpoint
self.pics = pics
self.set_test(filename, classname, test)
self.stack = MatterStackState(self.config)
self.default_controller = self.stack.certificate_authorities[0].adminList[0].NewController(
nodeId=self.config.controller_node_id,
paaTrustStorePath=str(self.config.paa_trust_store_path),
catTags=self.config.controller_cat_tags
)
def set_test(self, filename: str, classname: str, test: str):
self.test = test
self.set_test_config()
module = importlib.import_module(Path(os.path.basename(filename)).stem)
self.test_class = getattr(module, classname)
def set_test_config(self, test_config: MatterTestConfig = MatterTestConfig()):
self.config = test_config
self.config.tests = [self.test]
self.config.endpoint = self.endpoint
if not self.config.dut_node_ids:
self.config.dut_node_ids = [1]
if self.pics:
self.config.pics = self.pics
def Shutdown(self):
self.stack.Shutdown()
def run_test_with_mock_read(self, read_cache: Attribute.AsyncReadTransaction.ReadResponse, hooks=None):
self.default_controller.Read = AsyncMock(return_value=read_cache)
# This doesn't need to do anything since we are overriding the read anyway
self.default_controller.FindOrEstablishPASESession = AsyncMock(return_value=None)
return run_tests_no_exit(self.test_class, self.config, hooks, self.default_controller, self.stack)