Skip to content

Commit

Permalink
tests: add topotest for mgmtd rpc processing
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Ryzhov <[email protected]>
  • Loading branch information
idryzhov committed Mar 22, 2024
1 parent d7e621c commit 137b2ac
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
34 changes: 34 additions & 0 deletions mgmtd/mgmt_testc.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/* ---------------- */

static void async_notification(struct nb_cb_notify_args *args);
static int rpc_callback(struct nb_cb_rpc_args *args);

static void sigusr1(void);
static void sigint(void);
Expand Down Expand Up @@ -86,6 +87,10 @@ static const struct frr_yang_module_info frr_ripd_info = {
.xpath = "/frr-ripd:authentication-failure",
.cbs.notify = async_notification,
},
{
.xpath = "/frr-ripd:clear-rip-route",
.cbs.rpc = rpc_callback,
},
{
.xpath = NULL,
}
Expand Down Expand Up @@ -113,6 +118,7 @@ FRR_DAEMON_INFO(mgmtd_testc, MGMTD_TESTC,
/* clang-format on */

const char **__notif_xpaths;
const char **__rpc_xpaths;

struct mgmt_be_client_cbs __client_cbs = {};
struct event *event_timeout;
Expand All @@ -134,6 +140,7 @@ static void quit(int exit_code)
{
EVENT_OFF(event_timeout);
darr_free(__client_cbs.notif_xpaths);
darr_free(__client_cbs.rpc_xpaths);

frr_fini();

Expand All @@ -152,6 +159,12 @@ static void timeout(struct event *event)
quit(1);
}

static void success(struct event *event)
{
zlog_notice("Success, exiting");
quit(0);
}

static void async_notification(struct nb_cb_notify_args *args)
{
zlog_notice("Received YANG notification");
Expand All @@ -163,6 +176,23 @@ static void async_notification(struct nb_cb_notify_args *args)
quit(0);
}

static int rpc_callback(struct nb_cb_rpc_args *args)
{
const char *vrf = NULL;

zlog_notice("Received YANG RPC");

if (yang_dnode_exists(args->input, "vrf"))
vrf = yang_dnode_get_string(args->input, "vrf");

printf("{\"frr-ripd:clear-rip-route\": {\"vrf\": \"%s\"}}\n", vrf);

event_cancel(&event_timeout);
event_add_timer(master, success, NULL, 1, NULL);

return 0;
}

int main(int argc, char **argv)
{
int f_listen = 0;
Expand Down Expand Up @@ -217,6 +247,10 @@ int main(int argc, char **argv)
__client_cbs.nnotif_xpaths = darr_len(__notif_xpaths);
}

darr_push(__rpc_xpaths, "/frr-ripd:clear-rip-route");
__client_cbs.rpc_xpaths = __rpc_xpaths;
__client_cbs.nrpc_xpaths = darr_len(__rpc_xpaths);

mgmt_be_client = mgmt_be_client_create("mgmtd-testc", &__client_cbs, 0,
master);

Expand Down
Empty file.
74 changes: 74 additions & 0 deletions tests/topotests/mgmt_rpc/test_rpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8 eval: (blacken-mode 1) -*-
# SPDX-License-Identifier: ISC
#
# March 21 2024, Igor Ryzhov <[email protected]>
#
# Copyright (c) 2024, NFWare Inc.
#

"""
Test YANG Notifications
"""
import json
import os
import threading

import pytest
from lib.topogen import Topogen
from lib.topotest import json_cmp

pytestmark = [pytest.mark.ripd, pytest.mark.mgmtd]

CWD = os.path.dirname(os.path.realpath(__file__))


@pytest.fixture(scope="module")
def tgen(request):
"Setup/Teardown the environment and provide tgen argument to tests"

topodef = {"s1": ("r1",)}

tgen = Topogen(topodef, request.module.__name__)
tgen.start_topology()

router_list = tgen.routers()
for rname, router in router_list.items():
router.load_frr_config("frr.conf")

tgen.start_router()
yield tgen
tgen.stop_topology()


def test_backend_rpc(tgen):
if tgen.routers_have_failure():
pytest.skip(tgen.errors)

r1 = tgen.gears["r1"]

be_client_path = "/usr/lib/frr/mgmtd_testc"
rc, _, _ = r1.net.cmd_status(be_client_path + " --help")

if rc:
pytest.skip("No mgmtd_testc")

out = []

def run_testc():
output = r1.net.cmd_raises(
be_client_path + " --timeout 10 --log file:mgmt_testc.log"
)
out.append(json.loads(output))

t = threading.Thread(target=run_testc)
t.start()

r1.vtysh_cmd("clear ip rip vrf testname")

t.join()

jsout = out[0]

expected = {"frr-ripd:clear-rip-route": {"vrf": "testname"}}
result = json_cmp(jsout, expected)
assert result is None

0 comments on commit 137b2ac

Please sign in to comment.