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

Add tests for Matter plugin #3

Merged
merged 33 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e9b88ce
Update format to pass lint-pylint check
MonicaisHer Jan 3, 2024
2a72dd0
Add matter plugin unit test
MonicaisHer Jan 3, 2024
11bcf3b
Correct plugin properties names
MonicaisHer Jan 9, 2024
d4f6935
Fix bootstrap path
MonicaisHer Jan 9, 2024
c944a99
Add matter hello world snap for spread test
MonicaisHer Jan 10, 2024
68f1b73
Build hello world snap based on Matter lighting app
MonicaisHer Jan 11, 2024
95eb444
Overwrite the original main.app
MonicaisHer Jan 12, 2024
67cad0f
Update app name for spread test
MonicaisHer Jan 12, 2024
eebead2
Correct bootstrap path in unit test, fix typo in readme
MonicaisHer Jan 12, 2024
63f096c
Remove obsolete file
MonicaisHer Jan 12, 2024
1b2da56
Update comments
MonicaisHer Jan 12, 2024
698debd
Build lighting app from gn_build_example.sh
MonicaisHer Jan 16, 2024
ac0f29f
Update member type
MonicaisHer Jan 16, 2024
1e5909b
Fix pyright errors
MonicaisHer Jan 16, 2024
4751257
Use specific commit for matter sdk
MonicaisHer Jan 17, 2024
e139b7f
working code
MonicaisHer Jan 17, 2024
bb1a3d4
Add craft parts matter plugin test
MonicaisHer Jan 18, 2024
5702233
Update craft parts matter plugin test
MonicaisHer Jan 19, 2024
62c6d51
Test app initialization, remove hello-world snap
MonicaisHer Jan 22, 2024
648e712
Use matter commit with ptpython version fix
MonicaisHer Jan 22, 2024
32688df
Update unit test and spread test
MonicaisHer Jan 22, 2024
b9ede83
Store plugin environment, source environment in override-build
MonicaisHer Jan 22, 2024
664b81d
Remove hello-world snap related code
MonicaisHer Jan 22, 2024
1a274ac
Remove snapcraft part related environment variables
MonicaisHer Jan 22, 2024
a609bc2
Rename the plugin to matter sdk
MonicaisHer Jan 22, 2024
6d50ff8
Remove snapcraft part related env vars in plugin
MonicaisHer Jan 22, 2024
2a1cfe9
Update plugin keyword's name and file names
MonicaisHer Jan 23, 2024
1894129
Seperate pull and build commands, source only environment differences
MonicaisHer Jan 24, 2024
7d07d4c
Export environment variable PATH, improve formatting
MonicaisHer Jan 25, 2024
25973ab
Export zap and pigweed environment variables
MonicaisHer Jan 25, 2024
b50c90a
Export zap environment variable
MonicaisHer Jan 25, 2024
3a51c1a
Fix tox linter
MonicaisHer Jan 25, 2024
d63e5c0
Improve formatting
MonicaisHer Jan 25, 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
4 changes: 2 additions & 2 deletions snapcraft/parts/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
from .conda_plugin import CondaPlugin
from .flutter_plugin import FlutterPlugin
from .kernel_plugin import KernelPlugin
from .matter_plugin import MatterPlugin
from .matter_sdk_plugin import MatterSdkPlugin
from .python_plugin import PythonPlugin
from .register import register

__all__ = [
"ColconPlugin",
"CondaPlugin",
"FlutterPlugin",
"MatterPlugin",
"MatterSdkPlugin",
"KernelPlugin",
"PythonPlugin",
"register",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""The matter plugin."""
"""The matter SDK plugin."""
import os
from typing import Any, Dict, List, Set, cast

from craft_parts import infos, plugins
from overrides import overrides

# The repository where the matter SDK resides.
MATTER_REPO = "https://github.com/project-chip/connectedhomeip.git"
MATTER_SDK_REPO = "https://github.com/project-chip/connectedhomeip.git"
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved


class MatterPluginProperties(plugins.PluginProperties, plugins.PluginModel):
"""The part properties used by the matter plugin."""
class MatterSdkPluginProperties(plugins.PluginProperties, plugins.PluginModel):
"""The part properties used by the matter SDK plugin."""

matter_sdk_version: str
matter_zap_version: str
matter_sdk_zap_version: str

@classmethod
@overrides
def unmarshal(cls, data: Dict[str, Any]) -> "MatterPluginProperties":
def unmarshal(cls, data: Dict[str, Any]) -> "MatterSdkPluginProperties":
"""Populate class attributes from the part specification.

:param data: A dictionary containing part properties.
Expand All @@ -44,14 +44,14 @@ def unmarshal(cls, data: Dict[str, Any]) -> "MatterPluginProperties":
"""
plugin_data = plugins.extract_plugin_properties(
data,
plugin_name="matter",
required=["matter_sdk_version", "matter_zap_version"],
plugin_name="matter-sdk",
required=["matter_sdk_version", "matter_sdk_zap_version"],
)
return cls(**plugin_data)


class MatterPlugin(plugins.Plugin):
"""A plugin for matter project.
class MatterSdkPlugin(plugins.Plugin):
"""A plugin for matter SDK project.

This plugin uses the common plugin keywords.
For more information check the 'plugins' topic.
Expand All @@ -60,12 +60,12 @@ class MatterPlugin(plugins.Plugin):
- matter-sdk-version
(str, no default)
The matter SDK version to use for the build.
- matter-zap-version
- matter-sdk-zap-version
(str, no default)
The zap version to use for the build.
"""

properties_class = MatterPluginProperties
properties_class = MatterSdkPluginProperties

def __init__(
self,
Expand All @@ -75,7 +75,7 @@ def __init__(
) -> None:
super().__init__(properties=properties, part_info=part_info)

self.matter_dir = part_info.part_build_dir
self.matter_sdk_dir = part_info.part_build_dir
self.snap_arch = os.getenv("SNAP_ARCH")

@overrides
Expand Down Expand Up @@ -111,47 +111,48 @@ def get_build_snaps(self) -> Set[str]:

@overrides
def get_build_commands(self) -> List[str]:
options = cast(MatterPluginProperties, self._options)
options = cast(MatterSdkPluginProperties, self._options)
commands = []

if self.snap_arch == "arm64":
commands.extend(
[
f"wget --no-verbose https://github.com/project-chip/zap/releases/download/"
f"{options.matter_zap_version}/zap-linux-{self.snap_arch}.zip",
"wget --no-verbose https://github.com/project-chip/zap/releases/download/"
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
f"{options.matter_sdk_zap_version}/zap-linux-{self.snap_arch}.zip",
f"unzip -o zap-linux-{self.snap_arch}.zip",
"echo 'export ZAP_INSTALL_PATH=$PWD'",
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
]
)

# Clone Matter repository if not present
# Clone Matter SDK repository if not present
commands.extend(
[
"if [ ! -d matter ]; then",
f" git clone --depth 1 -b {options.matter_sdk_version} {MATTER_REPO} matter;",
"else",
" echo 'Matter repository already exists, skip clone';",
"fi",
"cd matter;",
]
"if [ ! -d matter ]; then",
" git init matter-sdk",
" cd matter-sdk",
f" git remote add origin {MATTER_SDK_REPO}",
f" git fetch --depth 1 origin {options.matter_sdk_version}",
" git checkout FETCH_HEAD",
"else",
" echo 'Matter SDK repository already exists, skip clone'",
" cd matter-sdk;",
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
"fi",]
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
)

# Checkout submodules for Linux platform
commands.extend(["scripts/checkout_submodules.py --shallow --platform linux"])

"""
The project writes its data to /tmp which isn't persisted.
# The project writes its data to /tmp which isn't persisted.

Setting TMPDIR env var when running the app isn't sufficient as
chip_[config,counter,factory,kvs].ini still get written under /tmp.
The chip-tool currently has no way of overriding the default paths to
storage and security config files.
# Setting TMPDIR env var when running the app isn't sufficient as
# chip_[config,counter,factory,kvs].ini still get written under /tmp.
# The chip-tool currently has no way of overriding the default paths to
# storage and security config files.

Snap does not allow bind mounting a persistent directory on /tmp,
so we need to replace it in the source with another path, e.g. /mnt.
See the top-level layout definition which bind mounts a persisted
directory within the confined snap space on /mnt.
"""
# Snap does not allow bind mounting a persistent directory on /tmp,
# so we need to replace it in the source with another path, e.g. /mnt.
# See the top-level layout definition which bind mounts a persisted
# directory within the confined snap space on /mnt.
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved

# Replace storage paths
commands.extend(
Expand All @@ -161,14 +162,22 @@ def get_build_commands(self) -> List[str]:
]
)

"""
Bootstrapping script for building Matter SDK with minimal "build" requirements
and setting up the environment.
"""
commands.extend(
["set +u && source setup/bootstrap.sh --platform build && set -u"]
)
# Bootstrapping script for building Matter SDK with minimal "build" requirements
# and setting up the environment.

commands.extend(["set +u && source scripts/setup/bootstrap.sh --platform build && set -u"])

commands.extend(["echo 'Built Matter SDK'"])

# Output environment variables to env file
# Remove environment variables related to snapcraft part
commands.extend(
[
"env > matter_sdk_env",
r"sed -i '/^CRAFT_PART_/d' matter_sdk_env",
r"sed -i '/^SNAPCRAFT_PART_/d' matter_sdk_env",
"echo 'Environment variables exported to matter_sdk_env file'",
]
)

MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
return commands
4 changes: 2 additions & 2 deletions snapcraft/parts/plugins/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .conda_plugin import CondaPlugin
from .flutter_plugin import FlutterPlugin
from .kernel_plugin import KernelPlugin
from .matter_plugin import MatterPlugin
from .matter_sdk_plugin import MatterSdkPlugin
from .python_plugin import PythonPlugin


Expand All @@ -33,4 +33,4 @@ def register() -> None:
craft_parts.plugins.register({"flutter": FlutterPlugin})
craft_parts.plugins.register({"python": PythonPlugin})
craft_parts.plugins.register({"kernel": KernelPlugin})
craft_parts.plugins.register({"matter": MatterPlugin})
craft_parts.plugins.register({"matter-sdk": MatterSdkPlugin})
50 changes: 50 additions & 0 deletions tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: matter-lighting
summary: Matter plugin test
description: An lighting application to test the matter plugin.
version: "1.0"

base: core22

grade: stable
build-base: core22
confinement: strict

layout:
/mnt:
bind: $SNAP_COMMON/mnt

apps:
matter-lighting:
daemon: simple
command: bin/lighting-app
install-mode: disable
plugs:
- network
- network-bind
- bluez
- avahi-control

parts:
matter-sdk:
plugin: matter-sdk
matter-sdk-version: "1536ca20c5917578ca40ce509400e97b52751788" # use this commit with ptpython version fix; needs to be updated once matter sdk have a stable release
matter-sdk-zap-version: "v2023.11.13"

lighting:
plugin: nil
after: [matter-sdk]
override-build: |
cd ../../matter-sdk/build/matter-sdk

source matter_sdk_env

# Build the lighting app
cd examples/lighting-app/linux
gn gen out/build
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
ninja -C out/build
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved

ldd out/build/chip-lighting-app

mkdir -p $CRAFT_PART_INSTALL/bin
cp out/build/chip-lighting-app $CRAFT_PART_INSTALL/bin/lighting-app

51 changes: 51 additions & 0 deletions tests/spread/plugins/craft-parts/matter-sdk/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
summary: Craft Parts matter plugin test
manual: true
kill-timeout: 180m

systems:
- ubuntu-22.04-64

prepare: |
#shellcheck source=tests/spread/tools/snapcraft-yaml.sh
. "$TOOLS_DIR/snapcraft-yaml.sh"
set_base "$SNAP/snap/snapcraft.yaml"

restore: |
cd "$SNAP"
snapcraft clean
rm -f ./*.snap

#shellcheck source=tests/spread/tools/snapcraft-yaml.sh
. "$TOOLS_DIR/snapcraft-yaml.sh"
restore_yaml "snap/snapcraft.yaml"

execute: |
cd "$SNAP"

# Build and install the snap
snapcraft
snap install "${SNAP}"*.snap --dangerous

start_time=$(date +"%Y-%m-%d %H:%M:%S")
snap start matter-lighting

# Check if storage path replacement from /tmp to SNAP_COMMON/mnt works
for file in /tmp/chip_*; do
if [ -e "$file" ]; then
echo "Error: $file should not exist."
exit 1
fi
done

if [ ! -e "${SNAP_COMMON/mnt/chip_*}" ]; then
echo "Error: ${SNAP_COMMON}/mnt/chip_* does not exist."
exit 1
fi

# Check if server initialization is complete for matter-lighting
if ! journalctl --since "$start_time" | grep matter-lighting | grep "CHIP:SVR: Server initialization complete"; then
echo "Error: matter-lighting initialization failed."
exit 1
fi


Loading
Loading