From 3c045460ef6c0bd5f65616ca31958f8e2c32b063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Walstr=C3=B6m?= Date: Fri, 30 Aug 2024 14:13:57 +0200 Subject: [PATCH] Add implementation to control test mode functions using an action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This intoduce a new sysrepo plugin to handle a new YANG model 'infix-test'. Today only contains the actions. This is required to run tests on infix using infamy. This is a part of #561 Signed-off-by: Mattias Walström --- configs/aarch64_defconfig | 1 + configs/r2s_defconfig | 1 + configs/riscv64_defconfig | 1 + configs/x86_64_defconfig | 1 + package/Config.in | 1 + package/test-mode/Config.in | 10 ++ package/test-mode/test-mode.mk | 32 +++++++ src/confd/bin/bootstrap | 2 +- src/test-mode/LICENSE | 27 ++++++ src/test-mode/Makefile.am | 4 + src/test-mode/autogen.sh | 3 + src/test-mode/configure.ac | 71 ++++++++++++++ src/test-mode/src/Makefile.am | 16 ++++ src/test-mode/src/test-mode.c | 93 +++++++++++++++++++ src/test-mode/yang/Makefile.am | 2 + src/test-mode/yang/infix-test@2024-08-16.yang | 33 +++++++ src/test-mode/yang/test-mode.inc | 4 + 17 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 package/test-mode/Config.in create mode 100644 package/test-mode/test-mode.mk create mode 100644 src/test-mode/LICENSE create mode 100644 src/test-mode/Makefile.am create mode 100755 src/test-mode/autogen.sh create mode 100644 src/test-mode/configure.ac create mode 100644 src/test-mode/src/Makefile.am create mode 100644 src/test-mode/src/test-mode.c create mode 100644 src/test-mode/yang/Makefile.am create mode 100644 src/test-mode/yang/infix-test@2024-08-16.yang create mode 100644 src/test-mode/yang/test-mode.inc diff --git a/configs/aarch64_defconfig b/configs/aarch64_defconfig index 9b387584..25df0287 100644 --- a/configs/aarch64_defconfig +++ b/configs/aarch64_defconfig @@ -159,6 +159,7 @@ BR2_PACKAGE_TETRIS=y BR2_PACKAGE_ROUSETTE=y BR2_PACKAGE_LIBINPUT=y BR2_PACKAGE_HOST_PYTHON_YANGDOC=y +BR2_PACKAGE_TEST_MODE=y DISK_IMAGE_BOOT_BIN=y TRUSTED_KEYS=y TRUSTED_KEYS_DEVELOPMENT=y diff --git a/configs/r2s_defconfig b/configs/r2s_defconfig index 124291a2..fa70356d 100644 --- a/configs/r2s_defconfig +++ b/configs/r2s_defconfig @@ -201,6 +201,7 @@ BR2_PACKAGE_PODMAN_DRIVER_VFS=y BR2_PACKAGE_TETRIS=y BR2_PACKAGE_ROUSETTE=y BR2_PACKAGE_HOST_PYTHON_YANGDOC=y +# BR2_PACKAGE_TEST_MODE is not set TRUSTED_KEYS=y TRUSTED_KEYS_DEVELOPMENT=y # GNS3_APPLIANCE is not set diff --git a/configs/riscv64_defconfig b/configs/riscv64_defconfig index 346a49c6..98221d89 100644 --- a/configs/riscv64_defconfig +++ b/configs/riscv64_defconfig @@ -192,6 +192,7 @@ BR2_PACKAGE_PODMAN_DRIVER_VFS=y BR2_PACKAGE_TETRIS=y BR2_PACKAGE_ROUSETTE=y BR2_PACKAGE_HOST_PYTHON_YANGDOC=y +# BR2_PACKAGE_TEST_MODE is not set TRUSTED_KEYS=y TRUSTED_KEYS_DEVELOPMENT=y # GNS3_APPLIANCE is not set diff --git a/configs/x86_64_defconfig b/configs/x86_64_defconfig index de7e0b3b..f5eca009 100644 --- a/configs/x86_64_defconfig +++ b/configs/x86_64_defconfig @@ -161,6 +161,7 @@ BR2_PACKAGE_PODMAN_DRIVER_VFS=y BR2_PACKAGE_TETRIS=y BR2_PACKAGE_ROUSETTE=y BR2_PACKAGE_HOST_PYTHON_YANGDOC=y +BR2_PACKAGE_TEST_MODE=y TRUSTED_KEYS=y TRUSTED_KEYS_DEVELOPMENT=y GNS3_APPLIANCE_RAM=512 diff --git a/package/Config.in b/package/Config.in index 5b03bf6e..aaaed42a 100644 --- a/package/Config.in +++ b/package/Config.in @@ -1,5 +1,6 @@ menu "Packages" source "$BR2_EXTERNAL_INFIX_PATH/package/confd/Config.in" +source "$BR2_EXTERNAL_INFIX_PATH/package/test-mode/Config.in" source "$BR2_EXTERNAL_INFIX_PATH/package/curios-httpd/Config.in" source "$BR2_EXTERNAL_INFIX_PATH/package/execd/Config.in" source "$BR2_EXTERNAL_INFIX_PATH/package/gencert/Config.in" diff --git a/package/test-mode/Config.in b/package/test-mode/Config.in new file mode 100644 index 00000000..4ebff412 --- /dev/null +++ b/package/test-mode/Config.in @@ -0,0 +1,10 @@ +config BR2_PACKAGE_TEST_MODE + bool "test-mode" + select BR2_PACKAGE_LIBITE + select BR2_PACKAGE_LIBYANG + select BR2_PACKAGE_SYSREPO + select BR2_PACKAGE_LIBSRX + select BR2_PACKAGE_CONFD + help + A plugin to sysrepo that provides functionality required to + run tests with infamy. diff --git a/package/test-mode/test-mode.mk b/package/test-mode/test-mode.mk new file mode 100644 index 00000000..d32a5161 --- /dev/null +++ b/package/test-mode/test-mode.mk @@ -0,0 +1,32 @@ +TEST_MODE_VERSION = 1.0 +TEST_MODE_SITE_METHOD = local +TEST_MODE_SITE = $(BR2_EXTERNAL_INFIX_PATH)/src/test-mode +TEST_MODE_LICENSE = BSD-3-Clause +TEST_MODE_LICENSE_FILES = LICENSE +TEST_MODE_REDISTRIBUTE = NO +TEST_MODE_DEPENDENCIES = sysrepo libite libyang confd +TEST_MODE_AUTORECONF = YES +TEST_MODE_SYSREPO_SHM_PREFIX = sr_buildroot$(subst /,_,$(CONFIG_DIR))_test_mode + +COMMON_SYSREPO_ENV = \ + SYSREPO_SHM_PREFIX=$(CONFD_SYSREPO_SHM_PREFIX) \ + SYSREPOCTL_EXECUTABLE="$(HOST_DIR)/bin/sysrepoctl" \ + SYSREPOCFG_EXECUTABLE="$(HOST_DIR)/bin/sysrepocfg" \ + SEARCH_PATH="$(TARGET_DIR)/usr/share/yang/modules/confd/" + +define TEST_MODE_INSTALL_YANG_MODULES + $(COMMON_SYSREPO_ENV) \ + SEARCH_PATH="$(TARGET_DIR)/usr/share/yang/modules/test-mode/" $(BR2_EXTERNAL_INFIX_PATH)/utils/sysrepo-load-modules.sh $(@D)/yang/test-mode.inc +endef +define TEST_MODE_PERMISSIONS + /etc/sysrepo/data/ r 660 root wheel - - - - - + /etc/sysrepo/data d 770 root wheel - - - - - +endef +define TEST_MODE_CLEANUP + rm -f /dev/shm/$(TEST_MODE_SYSREPO_SHM_PREFIX)* +endef + +TEST_MODE_PRE_INSTALL_TARGET_HOOKS += TEST_MODE_CLEANUP +TEST_MODE_POST_INSTALL_TARGET_HOOKS += TEST_MODE_INSTALL_YANG_MODULES + +$(eval $(autotools-package)) diff --git a/src/confd/bin/bootstrap b/src/confd/bin/bootstrap index 0106c9dd..684e0792 100755 --- a/src/confd/bin/bootstrap +++ b/src/confd/bin/bootstrap @@ -142,13 +142,13 @@ gen_failure_cfg if [ -f "/mnt/aux/test-mode" ]; then gen_test_cfg + sysrepoctl -c infix-test -e test-mode-enable fi if [ -n "$TESTING" ]; then echo "Done." exit 0 fi - mkdir -p /etc/sysrepo/ if [ -f "$FACTORY_CFG" ]; then cp "$FACTORY_CFG" "$INIT_DATA" diff --git a/src/test-mode/LICENSE b/src/test-mode/LICENSE new file mode 100644 index 00000000..7a17cba1 --- /dev/null +++ b/src/test-mode/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2023 The KernelKit Authors +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of copyright holders nor the names of + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/src/test-mode/Makefile.am b/src/test-mode/Makefile.am new file mode 100644 index 00000000..226487df --- /dev/null +++ b/src/test-mode/Makefile.am @@ -0,0 +1,4 @@ +SUBDIRS = src yang +dist_doc_DATA = LICENSE +DISTCLEANFILES = *~ *.d +ACLOCAL_AMFLAGS = -I m4 diff --git a/src/test-mode/autogen.sh b/src/test-mode/autogen.sh new file mode 100755 index 00000000..69ad0e18 --- /dev/null +++ b/src/test-mode/autogen.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +autoreconf -W portability -vifm diff --git a/src/test-mode/configure.ac b/src/test-mode/configure.ac new file mode 100644 index 00000000..accdfc92 --- /dev/null +++ b/src/test-mode/configure.ac @@ -0,0 +1,71 @@ +AC_PREREQ(2.61) +AC_INIT([test-mode], [1.0], [https://github.com/kernelkit/infix/issues]) +AM_INIT_AUTOMAKE(1.11 foreign subdir-objects) +AM_SILENT_RULES(yes) + +AC_CONFIG_FILES([ + Makefile + src/Makefile + yang/Makefile +]) + +AC_PROG_CC +AC_PROG_INSTALL +LT_INIT + +PKG_PROG_PKG_CONFIG + +PKG_CHECK_MODULES([sysrepo], [sysrepo >= 2.2.36]) +PKG_CHECK_MODULES([libite], [libite >= 2.5.0]) + +# Plugin installation path for sysrepo-plugind +PKG_CHECK_VAR([srpdplugindir], [sysrepo], [SRPD_PLUGINS_PATH]) +AC_SUBST(srpdplugindir) + +test "x$prefix" = xNONE && prefix=$ac_default_prefix +test "x$exec_prefix" = xNONE && exec_prefix='${prefix}' + +YANGDIR=`eval echo $datarootdir/yang/modules/test-mode` +AC_SUBST(YANGDIR) + +LIBDIR=`eval echo $libdir` +LIBDIR=`eval echo $LIBDIR` +AC_SUBST(LIBDIR) + +LOCALSTATEDIR=`eval echo $localstatedir` +LOCALSTATEDIR=`eval echo $LOCALSTATEDIR` +AC_SUBST(LOCALSTATEDIR) + +RUNSTATEDIR=`eval echo $runstatedir` +RUNSTATEDIR=`eval echo $RUNSTATEDIR` +AC_SUBST(RUNSTATEDIR) + +SYSCONFDIR=`eval echo $sysconfdir` +SYSCONFDIR=`eval echo $SYSCONFDIR` +AC_SUBST(SYSCONFDIR) + +AC_OUTPUT + +cat < +#include +#include +#include +#include +#include + +#include +#define SYSREPO_TIMEOUT 60000 /* 60s, this is the timeout we use in our frontends. */ + +sr_subscription_ctx_t *sub; +sr_conn_ctx_t *conn; + +#define TEST_CONFIG_PATH "/etc/test-config.cfg" + +static int test_reset(sr_session_ctx_t *session, uint32_t sub_id, const char *path, + const sr_val_t *input, const size_t input_cnt, + sr_event_t event, unsigned request_id, + sr_val_t **output, size_t *output_cnt, + void *priv) +{ + int rc = SR_ERR_OK; + struct lyd_node *tree = NULL; + const struct ly_ctx *ly_ctx; + sr_conn_ctx_t *conn = sr_session_get_connection(session); + + ly_ctx = sr_acquire_context(conn); + rc = lyd_parse_data_path(ly_ctx, TEST_CONFIG_PATH, LYD_JSON, LYD_PARSE_STRICT, LYD_VALIDATE_NO_STATE, &tree); + if (rc != LY_SUCCESS) + { + ERROR("Failed to parse new configuration data"); + return SR_ERR_INTERNAL; + } + rc = sr_session_switch_ds(session, SR_DS_RUNNING); + if(rc) + { + ERROR("Failed to switch datastore"); + goto out; + } + rc = sr_replace_config(session, NULL, tree, SYSREPO_TIMEOUT); + if (rc) { + ERROR("Failed to replace configuration: %s", sr_strerror(rc)); + goto out; + } +out: + return rc; +} + +static int test_override(sr_session_ctx_t *session, uint32_t sub_id, const char *path, + const sr_val_t *input, const size_t input_cnt, + sr_event_t event, unsigned request_id, + sr_val_t **output, size_t *output_cnt, + void *priv) +{ + touch("/mnt/aux/test-override-startup"); + + return 0; +} + +int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) +{ + int rc = SR_ERR_SYS; + + if (!fexist("/mnt/aux/test-mode")) + return SR_ERR_OK; + rc = sr_rpc_subscribe(session, "/infix-test:test/reset", test_reset, NULL, 0, SR_SUBSCR_DEFAULT, &sub); + if (rc) { + ERROR("Failed subscribe for test-reset"); + goto out; + } + rc = sr_rpc_subscribe(session, "/infix-test:test/override-startup", test_override, NULL, 0, SR_SUBSCR_DEFAULT, &sub); + if (rc) { + ERROR("Failed subscribe for test-override"); + goto out; + } + +out: + return rc; +} + +void sr_plugin_cleanup_cb(sr_session_ctx_t *session, void *priv) +{ + if (fexist("/mnt/aux/test-mode")) + sr_unsubscribe(sub); +} diff --git a/src/test-mode/yang/Makefile.am b/src/test-mode/yang/Makefile.am new file mode 100644 index 00000000..3e287aca --- /dev/null +++ b/src/test-mode/yang/Makefile.am @@ -0,0 +1,2 @@ +yangdir = $(YANGDIR) +yang_DATA = $(wildcard *.yang) diff --git a/src/test-mode/yang/infix-test@2024-08-16.yang b/src/test-mode/yang/infix-test@2024-08-16.yang new file mode 100644 index 00000000..3c96cd0e --- /dev/null +++ b/src/test-mode/yang/infix-test@2024-08-16.yang @@ -0,0 +1,33 @@ +module infix-test { + yang-version 1.1; + namespace "urn:infix:test:ns:yang:1.0"; + prefix infix-test; + + import ietf-netconf-acm { + prefix nacm; + } + + organization "KernelKit"; + contact "kernelkit@googlegroups.com"; + description "Infix model for system testing."; + + revision 2024-08-16 { + description "Initial revision."; + reference "internal"; + } + feature test-mode-enable { + description + "Enable test mode for infix tests"; + } + container test { + if-feature "test-mode-enable"; + action reset { + nacm:default-deny-all; + description "Reset the running-config datastore to test configuration"; + } + action override-startup { + nacm:default-deny-all; + description "Start from startup datastore once, instead of the default test configuration"; + } + } +} diff --git a/src/test-mode/yang/test-mode.inc b/src/test-mode/yang/test-mode.inc new file mode 100644 index 00000000..37999bf5 --- /dev/null +++ b/src/test-mode/yang/test-mode.inc @@ -0,0 +1,4 @@ +# -*- sh -*- +MODULES=( + "infix-test@2024-08-16.yang" +)