Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added scripts to get metadata (runner arguments) associated with the python_testing scripts for automating the process #32752

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
a895011
Create unit test to test metadata parser
vatsalghelani-csa Mar 27, 2024
e104297
Create test script metadata parser
vatsalghelani-csa Mar 27, 2024
b410193
Create test environment file
vatsalghelani-csa Mar 27, 2024
1eb41d5
Added Copyright Code
vatsalghelani-csa Mar 27, 2024
9a3152a
Added Copyright code
vatsalghelani-csa Mar 27, 2024
1150dce
Added copyright code
vatsalghelani-csa Mar 27, 2024
a1b740d
Added run arguments comment header
vatsalghelani-csa Mar 27, 2024
d0a8091
Created BUILD.gn for metadata parser
vatsalghelani-csa Mar 27, 2024
ab74101
Added reference to the metadata parser BUILD.gn
vatsalghelani-csa Mar 27, 2024
b24f090
Added TC_RR_1_1_Test.py for testing purpose
vatsalghelani-csa Mar 27, 2024
03ba790
Reverted back the testing changes done to the original file
vatsalghelani-csa Mar 27, 2024
cd35abd
Updated TC_RR_1_1_Test.py with comments as it was
vatsalghelani-csa Mar 27, 2024
e43ca09
Update test_metadata.py to correct the location for TC_RR* test script
vatsalghelani-csa Mar 27, 2024
c38ec91
Updated BUILD.gn with the correct naming tests
vatsalghelani-csa Mar 27, 2024
503f83f
Rename env.yaml to test_env.yaml as it is for testing only
vatsalghelani-csa Mar 27, 2024
02316a7
Updated test_metadata.py with the corrected test env file
vatsalghelani-csa Mar 27, 2024
fc38fb2
Updated and rename TC_RR_1_1_Test.py to simple_run_args.py
vatsalghelani-csa Mar 27, 2024
58acf15
Updated test_metadata.py to the new name reference of the testing file
vatsalghelani-csa Mar 27, 2024
e5b22ec
Renamed test method to test_run_metadata_parser
vatsalghelani-csa Mar 27, 2024
8eb9a98
Removed the unwanted print method
vatsalghelani-csa Mar 27, 2024
0e8135b
Removed handling for int/bool/hex args and trace args
vatsalghelani-csa Mar 27, 2024
e9559a5
Remove try-catch block
vatsalghelani-csa Mar 27, 2024
a7e8072
Replaced == to is for null comparsion
vatsalghelani-csa Mar 27, 2024
9e02172
Removed argument validation method
vatsalghelani-csa Mar 27, 2024
1a1746d
Added the dataclass to be optional
vatsalghelani-csa Mar 28, 2024
cf2768f
Updated the test script to be a .txt file and changed the designed to…
vatsalghelani-csa Mar 28, 2024
c80a00a
Rename simple_run_args.py to simple_run_args.txt
vatsalghelani-csa Mar 28, 2024
c827ea3
Updated BUILD.gn
vatsalghelani-csa Mar 28, 2024
ebbf641
Rename scripts/tests/py/test_env.yaml to env_example.yaml
vatsalghelani-csa Mar 28, 2024
650c9a3
Rename env_example.yaml to scripts/tests/py/env_example.yaml
vatsalghelani-csa Mar 28, 2024
98f0fec
Updated path of the environment file
vatsalghelani-csa Mar 28, 2024
7a3dd44
Change the test script file type
vatsalghelani-csa Mar 28, 2024
925d1d2
Updated metadata_parser reference
vatsalghelani-csa Mar 28, 2024
97974c9
Rename env_example.yaml to env_test.yaml
vatsalghelani-csa Mar 28, 2024
7d46bfb
Update environment file name
vatsalghelani-csa Mar 28, 2024
5d85612
Created __init__.py file
vatsalghelani-csa Mar 28, 2024
8cc0659
Added reference for __init__.py in the source
vatsalghelani-csa Mar 28, 2024
e7cc9de
Created pyproject.toml file
vatsalghelani-csa Mar 28, 2024
751241e
Created setup.py file
vatsalghelani-csa Mar 28, 2024
817b8d5
Created setup.cfg file
vatsalghelani-csa Mar 28, 2024
e0be5de
Changed the path of the folder where the scripts are located
vatsalghelani-csa Mar 28, 2024
6f1f3c2
Added print statement
vatsalghelani-csa Mar 28, 2024
421cb00
Updated print statement
vatsalghelani-csa Mar 28, 2024
3a054e1
Added inputs to the BUILD.gn file
vatsalghelani-csa Mar 28, 2024
8f9cc9d
Updated paths for file references
vatsalghelani-csa Mar 28, 2024
8c7555f
Added test script path to the inputs
vatsalghelani-csa Mar 28, 2024
425a88b
Added sources for the file not find error fix
vatsalghelani-csa Mar 29, 2024
7edbd42
Updated metadata.py changes as suggested in the PR comments review
vatsalghelani-csa Mar 29, 2024
e316132
Updated test script
vatsalghelani-csa Mar 29, 2024
34bdaac
Updated license comment
vatsalghelani-csa Mar 29, 2024
fdd73f2
Removed from pathlib import Path
vatsalghelani-csa Mar 29, 2024
09d871d
Added Print Statement
vatsalghelani-csa Mar 29, 2024
faac196
Removed the print debug line
vatsalghelani-csa Mar 29, 2024
12e43f4
Updated it to use relative path instead of absolute path
vatsalghelani-csa Mar 31, 2024
d1fa735
Changed copyright year and removed unwanted sources
vatsalghelani-csa Apr 2, 2024
0dc589d
Updated copyright year to be 2024
vatsalghelani-csa Apr 2, 2024
f8571b0
Updated copyright year to be 2024
vatsalghelani-csa Apr 2, 2024
92172c0
Updated copyright year to be 2024
vatsalghelani-csa Apr 2, 2024
83475ad
Updated copyright year to be 2024
vatsalghelani-csa Apr 2, 2024
1cffe7e
Updated copyright year to be 2024
vatsalghelani-csa Apr 2, 2024
99c89c2
Updated copyright year to be 2024
vatsalghelani-csa Apr 2, 2024
a212154
Update comment
vatsalghelani-csa Apr 2, 2024
0a1e665
Updated the class name to MetadataReader
vatsalghelani-csa Apr 2, 2024
cd5c5cf
Updated class name to MetadataReader
vatsalghelani-csa Apr 2, 2024
bfae930
Removed optional on factoryreset
vatsalghelani-csa Apr 3, 2024
5c19ad5
Renamed env_yaml_file_path and solved inline inside constructor
vatsalghelani-csa Apr 3, 2024
120825d
Update metadata.py
vatsalghelani-csa Apr 3, 2024
560ebaa
Update metadata.py
vatsalghelani-csa Apr 3, 2024
cb228d2
Delete scripts/tests/py/simple_run_args.txt
vatsalghelani-csa Apr 4, 2024
eb96d1b
Update BUILD.gn
vatsalghelani-csa Apr 4, 2024
292b9da
Updated test_metadata.py
vatsalghelani-csa Apr 4, 2024
cd9d060
Updated metadata.py
vatsalghelani-csa Apr 4, 2024
0fecac6
Create simple_run_args.txt
vatsalghelani-csa Apr 4, 2024
c01e5f2
Update BUILD.gn
vatsalghelani-csa Apr 4, 2024
ac3f02c
Delete scripts/tests/py/simple_run_args.txt
vatsalghelani-csa Apr 4, 2024
7bbc5c0
Update BUILD.gn
vatsalghelani-csa Apr 4, 2024
af21a8e
Updated test_metadata.py
vatsalghelani-csa Apr 4, 2024
19b1919
Restyled by whitespace
restyled-commits Apr 4, 2024
688b013
Restyled by gn
restyled-commits Apr 4, 2024
85468ea
Restyled by prettier-yaml
restyled-commits Apr 4, 2024
4b0942c
Restyled by autopep8
restyled-commits Apr 4, 2024
ba136bf
Restyled by isort
restyled-commits Apr 4, 2024
cb9be9a
Fixed the spell error
vatsalghelani-csa Apr 9, 2024
bd4cfb8
Fixed comments from the PR
vatsalghelani-csa Apr 9, 2024
fb9084e
Removed extra print line
vatsalghelani-csa Apr 9, 2024
a4e6e6d
Restyled by autopep8
restyled-commits Apr 9, 2024
c785304
Test via comment space
vatsalghelani-csa Apr 10, 2024
12600e3
Test via removed comment space
vatsalghelani-csa Apr 10, 2024
4d7e539
Test via comment space
vatsalghelani-csa Apr 10, 2024
274175d
Restyled by autopep8
restyled-commits Apr 10, 2024
b8251f1
Fixed mypy erros and test script line readability fixes
vatsalghelani-csa Apr 15, 2024
c38f337
Restyled by autopep8
restyled-commits Apr 15, 2024
2d60e92
Removed unawanted variables
vatsalghelani-csa Apr 15, 2024
6c532e8
Removing unused Union
vatsalghelani-csa Apr 15, 2024
ac5239f
Restyled by autopep8
restyled-commits Apr 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ if (current_toolchain != "${dir_pw_toolchain}/default:default") {
"//scripts/build:build_examples.tests",
"//scripts/py_matter_idl:matter_idl.tests",
"//scripts/py_matter_yamltests:matter_yamltests.tests",
"//scripts/tests/py:metadata_parser.tests",
"//src:tests_run",
]

Expand Down
36 changes: 36 additions & 0 deletions scripts/tests/py/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2024 Project CHIP Authors
#
# 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("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

import("//build_overrides/pigweed.gni")
import("$dir_pw_build/python.gni")

pw_python_package("metadata_parser") {
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
setup = [
"setup.py",
"setup.cfg",
"pyproject.toml",
]

inputs = [ "env_test.yaml" ]

sources = [
"__init__.py",
"metadata.py",
]

tests = [ "test_metadata.py" ]
}
Empty file added scripts/tests/py/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions scripts/tests/py/env_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2024 Project CHIP Authors
#
# 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.

# test environment argument definitions for metadata parser
discriminator: 1234
KVS: kvs1
trace-to-appjson: json:out/trace_data/app-{SCRIPT_BASE_NAME}.json
enable-key: 000102030405060708090a0b0c0d0e0f

storage-path: admin_storage.json
commissioning-method: on-network
passcode: 20202021
endpoint: 1
trace-to-testjson: json:out/trace_data/test-{SCRIPT_BASE_NAME}.json
trace-to-testperfetto: perfetto:out/trace_data/test-{SCRIPT_BASE_NAME}.perfetto
manual-code: 10054912339
PICS: src/app/tests/suites/certification/ci-pics-values

app:
all-clusters: out/linux-x64-all-clusters-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-all-clusters-app
lock: out/linux-x64-lock-ipv6only-no-ble-no-wifi-tsan-clang-test/chip-lock-app
217 changes: 217 additions & 0 deletions scripts/tests/py/metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#!/usr/bin/python3
# Copyright (c) 2024 Project CHIP Authors
#
# 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 re
from dataclasses import dataclass
from typing import Dict, List, Optional

import yaml


@dataclass
class Metadata:
py_script_path: Optional[str] = None
run: Optional[str] = None
app: Optional[str] = None
discriminator: Optional[str] = None
passcode: Optional[str] = None

def copy_from_dict(self, attr_dict: Dict[str, str]) -> None:
"""
Sets the value of the attributes from a dictionary.

Attributes:

attr_dict:
Dictionary that stores attributes value that should
be transferred to this class.
"""

if "app" in attr_dict:
self.app = attr_dict["app"]

if "run" in attr_dict:
self.run = attr_dict["run"]

if "discriminator" in attr_dict:
self.discriminator = attr_dict["discriminator"]

if "passcode" in attr_dict:
self.passcode = attr_dict["passcode"]

if "py_script_path" in attr_dict:
self.py_script_path = attr_dict["py_script_path"]

# TODO - set other attributes as well


class MetadataReader:
"""
A class to parse run arguments from the test scripts and
resolve them to environment specific values.
"""

def __init__(self, env_yaml_file_path: str):
"""
Reads the YAML file and Constructs the environment object

Parameters:

env_yaml_file_path:
Path to the environment file that contains the YAML configuration.
"""
with open(env_yaml_file_path) as stream:
self.env = yaml.safe_load(stream)

def __resolve_env_vals__(self, metadata_dict: Dict[str, str]) -> None:
"""
Resolves the argument defined in the test script to environment values.
For example, if a test script defines "all_clusters" as the value for app
name, we will check the environment configuration to see what raw value is
assocaited with the "all_cluster" variable and set the value for "app" option
to this raw value.

Parameter:

metadata_dict:
Dictionary where each key represent a particular argument and its value represent
the value for that argument defined in the test script.
"""

for run_arg, run_arg_val in metadata_dict.items():

if not type(run_arg_val) == str or run_arg == "run":
metadata_dict[run_arg] = run_arg_val
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
continue

if run_arg_val is None:
continue

sub_args = run_arg_val.split('/')

if len(sub_args) not in [1, 2]:
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
err = """The argument is not in the correct format.
The argument must follow the format of arg1 or arg1/arg2.
For example, arg1 represents the argument type and optionally arg2
represents a specific variable defined in the environment file whose
value should be used as the argument value. If arg2 is not specified,
we will just use the first value associated with arg1 in the environment file."""
raise Exception(err)

if len(sub_args) == 1:
run_arg_val = self.env.get(sub_args[0])

elif len(sub_args) == 2:
run_arg_val = self.env.get(sub_args[0]).get(sub_args[1])

# if a argument has been specified in the comment header
# but can't be found in the env file, consider it to be
# boolean value.
if run_arg_val is None:
run_arg_val = True
vatsalghelani-csa marked this conversation as resolved.
Show resolved Hide resolved

metadata_dict[run_arg] = run_arg_val

def __read_args__(self, run_args_lines: List[str]) -> Dict[str, str]:
"""
Parses a list of lines and extracts argument
values from it.

Parameters:

run_args_lines:
Line in test script header that contains run argument definition.
Each line will contain a list of run arguments separated by a space.
Line below is one example of what the run argument line will look like:
"app/all-clusters discriminator KVS storage-path"

In this case the line defines that app, discriminator, KVS, and storage-path
are the arguments that should be used with this run.

An argument can be defined multiple times in the same line or in different lines.
The last definition will override any previous definition. For example,
"KVS/kvs1 KVS/kvs2 KVS/kvs3" line will lead to KVS value of kvs3.
"""
metadata_dict = {}

for run_line in run_args_lines:
for run_arg_word in run_line.strip().split():
'''
We expect the run arg to be defined in one of the
following two formats:
1. run_arg
2. run_arg/run_arg_val

Examples: "discriminator" and "app/all_clusters"

'''
run_arg = run_arg_word.split('/', 1)[0]
metadata_dict[run_arg] = run_arg_word

return metadata_dict

def parse_script(self, py_script_path: str) -> List[Metadata]:
"""
Parses a script and returns a list of metadata object where
each element of that list representing run arguments associated
with a particular run.

Parameter:

py_script_path:
path to the python test script

Return:

List[Metadata]
List of Metadata object where each Metadata element represents
the run arguments associated with a particular run defined in
the script file.
"""

runs_def_ptrn = re.compile(r'^\s*#\s*test-runner-runs:\s*(.*)$')
args_def_ptrn = re.compile(r'^\s*#\s*test-runner-run/([a-zA-Z0-9_]+):\s*(.*)$')

runs_arg_lines: Dict[str, List[str]] = {}
runs_metadata = []

with open(py_script_path, 'r', encoding='utf8') as py_script:
for line in py_script.readlines():

runs_match = runs_def_ptrn.match(line.strip())
args_match = args_def_ptrn.match(line.strip())

if runs_match:
for run in runs_match.group(1).strip().split():
runs_arg_lines[run] = []

elif args_match:
runs_arg_lines[args_match.group(1)].append(args_match.group(2))

for run, lines in runs_arg_lines.items():
metadata_dict = self.__read_args__(lines)
self.__resolve_env_vals__(metadata_dict)

# store the run value and script location in the
# metadata object
metadata_dict['py_script_path'] = py_script_path
metadata_dict['run'] = run

metadata = Metadata()

metadata.copy_from_dict(metadata_dict)
runs_metadata.append(metadata)

return runs_metadata
17 changes: 17 additions & 0 deletions scripts/tests/py/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2024 Project CHIP Authors
#
# 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.

[build-system]
requires = ['setuptools', 'wheel']
build-backend = 'setuptools.build_meta'
19 changes: 19 additions & 0 deletions scripts/tests/py/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2024 Project CHIP Authors
#
# 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.

[metadata]
name = metadata_parser
version = 0.0.1
author = Project CHIP Authors
description = Scripts to get metadata (runner arguments) associated with the python_testing scripts
20 changes: 20 additions & 0 deletions scripts/tests/py/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2024 Project CHIP Authors
#
# 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.


"""The metadata_parser package."""

import setuptools # type: ignore

setuptools.setup() # Package definition in setup.cfg
Loading
Loading