#!/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"){ auto("Show expand") @datamodel, cli_show_auto("running", "xml"); candidate("Show configuration"), cli_show_auto_mode("candidate", "xml", false, false); running("Show configuration"), cli_show_auto_mode("running", "xml", false, 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 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 set key union again" 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 new "cli commit" expectpart "$($clixon_cli -1 -f $cfg commit)" 0 "^$" new "show candidate" expectpart "$($clixon_cli -1 -f $cfg show candidate)" 0 'au1' 'u142' new "show running" expectpart "$($clixon_cli -1 -f $cfg show running)" 0 'au1' 'u142' 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