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 1 commit
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
90 changes: 51 additions & 39 deletions snapcraft/parts/plugins/matter_sdk_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

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


class MatterSdkPluginProperties(plugins.PluginProperties, plugins.PluginModel):
Expand Down Expand Up @@ -78,6 +79,38 @@ def __init__(
self.matter_sdk_dir = part_info.part_build_dir
self.snap_arch = os.getenv("SNAP_ARCH")

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

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

# Clone Matter SDK repository
commands.extend(
[
"if [ ! -d matter ]; then",
" git init",
f" git remote add origin {MATTER_SDK_REPO}",
f" git fetch --depth 1 origin {options.matter_sdk_version}",
" git checkout FETCH_HEAD",
"fi",
]
)

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

return commands

@overrides
def get_build_packages(self) -> Set[str]:
return {
Expand Down Expand Up @@ -114,34 +147,6 @@ def get_build_commands(self) -> List[str]:
options = cast(MatterSdkPluginProperties, self._options)
commands = []

if self.snap_arch == "arm64":
commands.extend(
[
"wget --no-verbose https://github.com/project-chip/zap/releases/download/"
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'",
]
)

# Clone Matter SDK repository if not present
commands.extend(
[
"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;",
"fi",]
)

# 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.

# Setting TMPDIR env var when running the app isn't sufficient as
Expand All @@ -151,8 +156,8 @@ def get_build_commands(self) -> List[str]:

# 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.
# The consumer snap needs to bind mount a persisted directory within
# the confined snap space on /mnt.

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

# Capture initial environment variables
commands.extend(["initial_environment=$(printenv)"])

# 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(
["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
# Capture updated environment variables after the bootstrapping
commands.extend(["updated_environment=$(printenv)"])

# Compare and output environment variable differences to matter_sdk_env env file
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'",
f"environment_differences=$(comm -3 <(echo $initial_environment | tr ' ' '\n' | sort) \\",
f" <(echo $updated_environment | tr ' ' '\n' | sort) | awk '{{print $1}}')",
"for env in $environment_differences; do",
f' echo "export $env" >> matter_sdk_env',
"done",
"echo 'Environment variables differences exported to matter_sdk_env file'",
]
)

Expand Down
11 changes: 6 additions & 5 deletions tests/spread/plugins/craft-parts/matter-sdk/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ parts:
lighting:
plugin: nil
after: [matter-sdk]
build-environment:
- MATTER_SDK_ENV: ../../matter-sdk/build/matter_sdk_env
override-build: |
cd ../../matter-sdk/build/matter-sdk
# Source the Matter SDK environment variables
source $MATTER_SDK_ENV

source matter_sdk_env

# Build the lighting app
cd examples/lighting-app/linux
# Build the lighting app for snapcraft spread testing purposes
cd ../../matter-sdk/build/examples/lighting-app/linux
gn gen out/build
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
ninja -C out/build

Expand Down
2 changes: 1 addition & 1 deletion tests/spread/plugins/craft-parts/matter-sdk/task.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
summary: Craft Parts matter plugin test
summary: Craft Parts matter SDK plugin test
manual: true
kill-timeout: 180m

Expand Down
87 changes: 53 additions & 34 deletions tests/unit/parts/plugins/test_matter_sdk_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,47 @@ def part_info(new_dir):
)


def test_get_pull_commands(part_info):
properties = MatterSdkPlugin.properties_class.unmarshal(
{"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"}
)
plugin = MatterSdkPlugin(properties=properties, part_info=part_info)

sdk_version = properties.matter_sdk_version # type: ignore
zap_version = properties.matter_sdk_zap_version # type: ignore

expected_commands = []

if plugin.snap_arch == "arm64":
expected_commands.extend(
[
f"wget --no-verbose {ZAP_REPO}/releases/download/"
f"{zap_version}/zap-linux-arm64.zip",
"unzip -o zap-linux-arm64.zip",
"echo 'export ZAP_INSTALL_PATH=$PWD'",
]
)

expected_commands.extend(
[
"if [ ! -d matter ]; then",
" git init",
f" git remote add origin {MATTER_SDK_REPO}",
f" git fetch --depth 1 origin {sdk_version}",
" git checkout FETCH_HEAD",
"fi",
]
)

expected_commands.extend(
[
"scripts/checkout_submodules.py --shallow --platform linux",
]
)

assert plugin.get_pull_commands() == expected_commands


def test_get_build_snaps(part_info):
properties = MatterSdkPlugin.properties_class.unmarshal(
{"matter-sdk-version": "master", "matter-sdk-zap-version": "v2023.11.13"}
Expand Down Expand Up @@ -89,52 +130,30 @@ def test_get_build_commands(part_info):

expected_commands = []

if plugin.snap_arch == "arm64":
expected_commands.extend(
[
f"wget --no-verbose https://github.com/project-chip/zap/releases/download/"
f"{zap_version}/zap-linux-arm64.zip",
"unzip -o zap-linux-arm64.zip",
"echo 'export ZAP_INSTALL_PATH=$PWD'",
]
)

expected_commands.extend(
[
"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 {sdk_version}",
" git checkout FETCH_HEAD",
"else",
" echo 'Matter SDK repository already exists, skip clone'",
" cd matter-sdk;",
"fi",]
)

expected_commands.extend(["scripts/checkout_submodules.py --shallow --platform linux",])

expected_commands.extend(
[
r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPLinuxStorage.h",
r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPPlatformConfig.h",
]
)

expected_commands.extend(["initial_environment=$(printenv)"])

expected_commands.extend(
[
"set +u && source scripts/setup/bootstrap.sh --platform build && set -u",
"echo 'Built Matter SDK'",
]
["set +u && source scripts/setup/bootstrap.sh --platform build && set -u"]
)
expected_commands.extend(["echo 'Built Matter SDK'"])

expected_commands.extend(["updated_environment=$(printenv)"])

expected_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'",
f"environment_differences=$(comm -3 <(echo $initial_environment | tr ' ' '\n' | sort) \\",
f" <(echo $updated_environment | tr ' ' '\n' | sort) | awk '{{print $1}}')",
"for env in $environment_differences; do",
f' echo "export $env" >> matter_sdk_env',
"done",
"echo 'Environment variables differences exported to matter_sdk_env file'",
]
)

Expand Down
Loading