From 694f8b3f0dccf8ad4b9be799c092a616d0904eb2 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Wed, 16 Feb 2022 14:42:19 +0100 Subject: [PATCH] Test for https://github.com/clicon/clixon/issues/301 --- test/test_cli_union.sh | 158 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100755 test/test_cli_union.sh diff --git a/test/test_cli_union.sh b/test/test_cli_union.sh new file mode 100755 index 00000000..c8357de2 --- /dev/null +++ b/test/test_cli_union.sh @@ -0,0 +1,158 @@ +#!/usr/bin/env bash +# test of unions in autocli for leafs and keys +# See eg: +# keys: https://github.com/clicon/clixon/issues/301 +# reftree: https://github.com/clicon/cligen/issues/73 + +# Magic line must be first in script (see README.md) +s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi + +APPNAME=example + +# include err() and new() functions and creates $dir + +cfg=$dir/conf_yang.xml +fspec=$dir/automode.cli +fyang=$dir/example.yang + +cat < $fyang +module example { + yang-version 1.1; + namespace "urn:example:clixon"; + prefix ex; + typedef uniontype { + type union { + type enumeration { + enum u1; + enum u2; + } + type string; + } + } + /* Generic config data */ + container tableleaf{ + list parleaf{ + description "leaf is union type"; + key name; + leaf name{ + type string; + } + leaf value{ + type uniontype; + } + } + } + container tablekey{ + list parkey{ + description "key is union type"; + key name; + leaf name{ + type uniontype; + } + leaf value{ + type string; + } + } + } +} +EOF + +AUTOCLI=$(autocli_config example kw-nokey false) + +# Use yang in example +cat < $cfg + + $cfg + ${YANG_INSTALLDIR} + $IETFRFC + $dir + /usr/local/lib/$APPNAME/backend + $APPNAME + /usr/local/lib/$APPNAME/cli + $dir + /usr/local/var/$APPNAME/$APPNAME.sock + /usr/local/var/$APPNAME/$APPNAME.pidfile + $dir + false + ${AUTOCLI} + +EOF + +cat < $fspec +CLICON_MODE="example"; +CLICON_PROMPT="%U@%H %W> "; +CLICON_PLUGIN="example_cli"; + +# Autocli syntax tree operations +set @datamodel, cli_auto_set(); +merge @datamodel, cli_auto_merge(); +create @datamodel, cli_auto_create(); +delete("Delete a configuration item") @datamodel, cli_auto_del(); +validate("Validate changes"), cli_validate(); +commit("Commit the changes"), cli_commit(); +quit("Quit"), cli_quit(); +show("Show a particular state of the system") + configuration("Show configuration"), cli_auto_show("datamodel", "candidate", "text", true, false); +EOF + +new "test params: -f $cfg" +if [ $BE -ne 0 ]; then + new "kill old backend" + sudo clixon_backend -z -f $cfg + if [ $? -ne 0 ]; then + err + fi + new "start backend -s init -f $cfg" + start_backend -s init -f $cfg +fi + +new "wait backend" +wait_backend + +new "cli set leaf union" +expectpart "$($clixon_cli -1 -f $cfg set tableleaf parleaf a value u1)" 0 "^$" + +new "cli set leaf union" +expectpart "$($clixon_cli -1 -f $cfg set tableleaf parleaf a value u1)" 0 "^$" +exit +new "cli query leaf union - basic" +expectpart "$(echo "set tableleaf parleaf a value ?" | $clixon_cli -f $cfg 2>/dev/null)" 0 u1 u2 + +new "cli query leaf union - count" +ret=$(echo "set tableleaf parleaf a value ?" | $clixon_cli -f $cfg 2>&1 2>/dev/null) +count=$(echo "$ret" | grep -c u1) +if [ $count -gt 1 ]; then + err "number of u1: 1" $count +fi + +new "cli set key union" +expectpart "$($clixon_cli -1 -f $cfg set tablekey parkey u1 value 42)" 0 "^$" + +new "cli query key union - basic" +expectpart "$(echo "set tablekey parkey ?" | $clixon_cli -f $cfg 2>/dev/null)" 0 u1 u2 + +new "cli query leaf union - count" +ret=$(echo "set tablekey parkey ?" | $clixon_cli -f $cfg 2>&1 2>/dev/null) +count=$(echo "$ret" | grep -c u1) +if [ $count -gt 1 ]; then + err "number of u1: 1" $count +fi + +if [ $BE -ne 0 ]; then + new "Kill backend" + # Check if premature kill + pid=$(pgrep -u root -f clixon_backend) + if [ -z "$pid" ]; then + err "backend already dead" + fi + # kill backend + stop_backend -f $cfg +fi + +rm -rf $dir + +unset count +unset ret + +new "endtest" +endtest