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 10 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
29 changes: 13 additions & 16 deletions snapcraft/parts/plugins/matter_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,17 @@ def get_build_commands(self) -> List[str]:
# 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.

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

"""
Bootstrapping script for building Matter SDK with minimal "build" requirements
and setting up the environment.
"""
# 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"]
["set +u && source scripts/setup/bootstrap.sh --platform build && set -u"]
)

commands.extend(["echo 'Built Matter SDK'"])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# matter_hello

A new Matter project.

## Getting Started

This project is a starting point for a simple application based on [Matter SDK](https://github.com/project-chip/connectedhomeip).

Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
*
* Copyright (c) 2020 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.
*/

#include "LightingAppCommandDelegate.h"
#include "LightingManager.h"
#include <AppMain.h>

#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/ConcreteAttributePath.h>
#include <app/server/Server.h>
#include <lib/support/logging/CHIPLogging.h>

#if defined(CHIP_IMGUI_ENABLED) && CHIP_IMGUI_ENABLED
#include <imgui_ui/ui.h>
#include <imgui_ui/windows/light.h>
#include <imgui_ui/windows/occupancy_sensing.h>
#include <imgui_ui/windows/qrcode.h>

#include <iostream>

#endif

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;

namespace {

constexpr char kChipEventFifoPathPrefix[] = "/tmp/chip_lighting_fifo_";
NamedPipeCommands sChipNamedPipeCommands;
LightingAppCommandDelegate sLightingAppCommandDelegate;
} // namespace

void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size,
uint8_t * value)
{
if (attributePath.mClusterId == OnOff::Id && attributePath.mAttributeId == OnOff::Attributes::OnOff::Id)
{
LightingMgr().InitiateAction(*value ? LightingManager::ON_ACTION : LightingManager::OFF_ACTION);
}
}

/** @brief OnOff Cluster Init
*
* This function is called when a specific cluster is initialized. It gives the
* application an opportunity to take care of cluster initialization procedures.
* It is called exactly once for each endpoint where cluster is present.
*
* @param endpoint Ver.: always
*
* TODO Issue #3841
* emberAfOnOffClusterInitCallback happens before the stack initialize the cluster
* attributes to the default value.
* The logic here expects something similar to the deprecated Plugins callback
* emberAfPluginOnOffClusterServerPostInitCallback.
*
*/
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
void emberAfOnOffClusterInitCallback(EndpointId endpoint)
{
// TODO: implement any additional Cluster Server init actions
}

void ApplicationInit()
{
std::string path = kChipEventFifoPathPrefix + std::to_string(getpid());

if (sChipNamedPipeCommands.Start(path, &sLightingAppCommandDelegate) != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to start CHIP NamedPipeCommands");
sChipNamedPipeCommands.Stop();
}
}

void ApplicationShutdown()
{
if (sChipNamedPipeCommands.Stop() != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to stop CHIP NamedPipeCommands");
}
}

int main(int argc, char * argv[])
{
std::cout << "hello world" << std::endl;

return 0;
}
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: matter-hello
version: "1.0"
summary: simple matter application
description: build an example lighting application using the Matter SDK and say "hello world"
base: core22
confinement: strict

apps:
matter-hello:
command: bin/lighting-app

parts:
lighting-hello:
plugin: matter
matter-sdk-version: "master"
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
matter-zap-version: "v2023.11.13"
source: app
override-build: |
craftctl default
cd $CRAFT_PART_BUILD/matter

set +u && source scripts/setup/bootstrap.sh --platform build && set -u

# Copy and replace the application files so that it will say "hello world"
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
cp -vr $CRAFT_PART_BUILD/main.cpp examples/lighting-app/linux/main.cpp

# Build a lighting example
MonicaisHer marked this conversation as resolved.
Show resolved Hide resolved
cd examples/lighting-app/linux
gn gen out/build
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


Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ environment:
SNAP/colcon_ros2_humble: colcon-ros2-humble-hello
SNAP/colcon_ros2_wrapper: colcon-ros2-wrapper
SNAP/flutter: flutter-hello
SNAP/matter: matter-hello
SNAP/python: python-hello

prepare: |
Expand Down
115 changes: 115 additions & 0 deletions tests/unit/parts/plugins/test_matter_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2023 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 3 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import pytest
from craft_parts import Part, PartInfo, ProjectInfo

from snapcraft.parts.plugins import MatterPlugin

# The repository where the matter SDK resides.
MATTER_REPO = "https://github.com/project-chip/connectedhomeip.git"


@pytest.fixture(autouse=True)
def part_info(new_dir):
yield PartInfo(
project_info=ProjectInfo(
application_name="test", project_name="test-snap", cache_dir=new_dir
),
part=Part("my-part", {}),
)


def test_get_build_snaps(part_info):
properties = MatterPlugin.properties_class.unmarshal(
{"matter-sdk-version": "master", "matter-zap-version": "v2023.11.13"}
)
plugin = MatterPlugin(properties=properties, part_info=part_info)
assert plugin.get_build_snaps() == set()


def test_get_build_packages(part_info):
properties = MatterPlugin.properties_class.unmarshal(
{"matter-sdk-version": "master", "matter-zap-version": "v2023.11.13"}
)
plugin = MatterPlugin(properties=properties, part_info=part_info)
assert plugin.get_build_packages() == {
"clang",
"cmake",
"generate-ninja",
"git",
"libavahi-client-dev",
"libcairo2-dev",
"libdbus-1-dev",
"libgirepository1.0-dev",
"libglib2.0-dev",
"libreadline-dev",
"libssl-dev",
"ninja-build",
"pkg-config",
"python3-dev",
"python3-pip",
"python3-venv",
"unzip",
"wget",
}


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

assert plugin.get_build_environment() == {}


def test_get_build_commands(part_info):
properties = MatterPlugin.properties_class.unmarshal(
{"matter-sdk-version": "master", "matter-zap-version": "v2023.11.13"}
)
plugin = MatterPlugin(properties=properties, part_info=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"{properties.matter_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",
f" git clone --depth 1 -b {properties.matter_sdk_version} {MATTER_REPO} matter;",
"else",
" echo 'Matter repository already exists, skip clone';",
"fi",
"cd matter;",
"scripts/checkout_submodules.py --shallow --platform linux",
r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPLinuxStorage.h",
r"sed -i 's/\/tmp/\/mnt/g' src/platform/Linux/CHIPPlatformConfig.h",
"set +u && source scripts/setup/bootstrap.sh --platform build && set -u",
"echo 'Built Matter SDK'",
]
)

assert plugin.get_build_commands() == expected_commands