#!/usr/bin/env bash # Test of clixon-clispec.yang and cli_clispec.[ch] # 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 fin=$dir/in fstate=$dir/state.xml fyang=$dir/clixon-example.yang fyang2=$dir/clixon-example2.yang cat < $fyang module clixon-example { yang-version 1.1; namespace "urn:example:clixon"; prefix ex; /* Generic config data */ container table{ list parameter{ key name; leaf name{ type string; } leaf value{ type string; } leaf stat{ description "Inline state data for example application"; config false; type int32; } } } } EOF cat < $fyang2 module clixon-example2 { yang-version 1.1; namespace "urn:example:clixon2"; prefix ex2; /* Alt config data */ container table2{ list parameter{ key name; leaf name{ type string; } leaf value{ type string; } leaf stat{ description "Inline state data for example application"; config false; type int32; } } } } EOF cat < $fspec CLICON_MODE="example"; CLICON_PROMPT="%U@%H %W> "; CLICON_PLUGIN="example_cli"; # Autocli syntax tree operations edit @datamodelshow, cli_auto_edit("basemodel"); up, cli_auto_up("basemodel"); top, cli_auto_top("basemodel"); 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);{ xml("Show configuration as XML"), cli_auto_show("datamodel", "candidate", "xml", false, false); cli("Show configuration as CLI commands"), cli_auto_show("datamodel", "candidate", "cli", false, false, "set "); netconf("Show configuration as netconf edit-config operation"), cli_auto_show("datamodel", "candidate", "netconf", false, false); text("Show configuration as text"), cli_auto_show("datamodel", "candidate", "text", false, false); json("Show configuration as JSON"), cli_auto_show("datamodel", "candidate", "json", false, false); } state("Show configuration and state"), cli_auto_show("datamodel", "running", "xml", false, true); } EOF cat < $dir/startup_db <${DATASTORE_TOP}> a 42
EOF # Add inline state cat < $fstate a 99
EOF # Make a new variant of clixon config file # Arg 1: autocli-spec function runconfig() { AUTOCLI="$1" # Use yang in example cat < $cfg $cfg ietf-netconf:startup /usr/local/share/clixon $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 } new "backend config" runconfig "" 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 startup -f $cfg -- -sS $fstate" start_backend -s startup -f $cfg -- -sS $fstate fi new "wait backend" wait_backend AUTOCLI=$(cat < false EOF ) new "disable autocli" runconfig "$AUTOCLI" new "set table expected fail" expectpart "$(echo "set table" | $clixon_cli -f $cfg 2>&1)" 255 "CLIgen tree 'datamodel' not found" AUTOCLI=$(cat < true EOF ) new "enable autocli" runconfig "$AUTOCLI" new "set table OK" expectpart "$(echo "set table" | $clixon_cli -f $cfg 2>&1)" 0 "" AUTOCLI=$(cat < false include example enable Include the example module for autocli generation clixon-example EOF ) new "exclude example2 using default" runconfig "$AUTOCLI" new "set table" expectpart "$(echo "set table" | $clixon_cli -f $cfg 2>&1)" 0 "" new "set table2 expect fail" expectpart "$(echo "set table2" | $clixon_cli -f $cfg 2>&1)" 0 "CLI syntax error" "Unknown command" 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 new "endtest" endtest