#!/usr/bin/env bash # Datastore tests: # - XML and JSON # - save and load config files # Pretty and not # 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 fyang=$dir/clixon-example.yang fclispec=$dir/clispec.cli # Use yang in example cat < $cfg $cfg ${YANG_INSTALLDIR} $IETFRFC $fyang /usr/local/lib/$APPNAME/backend $APPNAME /usr/local/lib/$APPNAME/cli $dir /usr/local/var/run/$APPNAME.sock /usr/local/var/run/$APPNAME.pidfile $dir EOF 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; } container two{ leaf a{ type string; } } } } } EOF cat < $fclispec 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(); discard("Discard edits (rollback 0)"), discard_changes(); load("Load configuration from XML file") ("Filename (local filename)"){ xml("Replace candidate with file containing XML"), load_config_file("","filename", "replace", "xml"); json("Replace candidate with file containing JSON"), load_config_file("","filename", "replace", "json"); merge("Merge file with existent candidate"), load_config_file("filename", "merge"); } save("Save candidate configuration to XML file") ("Filename (local filename)"){ xml("Save configuration as XML"), save_config_file("candidate","filename", "xml"); json("Save configuration as JSON"), save_config_file("candidate","filename", "json"); } show("Show a particular state of the system") configuration("Show configuration"), cli_show_auto_mode("candidate", "xml", false, false); quit("Quit"), cli_quit(); EOF # Restconf test routine with arguments: # 1. format: xml/json # 2. pretty: false/true - pretty-printed XMLDB function testrun() { format=$1 pretty=$2 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 -o CLICON_XMLDB_FORMAT=$format -o CLICON_XMLDB_PRETTY=$pretty" start_backend -s init -f $cfg -o CLICON_XMLDB_FORMAT=$format -o CLICON_XMLDB_PRETTY=$pretty fi new "wait backend" wait_backend new "cli configure parameter a" expectpart "$($clixon_cli -1 -f $cfg set table parameter a value 42)" 0 "^$" new "cli show config xml" expectpart "$($clixon_cli -1 -f $cfg show config)" 0 "^a42
$" new "Check xmldb $format format" # permission kludges sudo chmod 666 $dir/candidate_db if [ "$format" = xml ]; then if [ "$pretty" = false ]; then cat < $dir/expect <${DATASTORE_TOP}>a42
EOF else cat < $dir/expect <${DATASTORE_TOP}> a 42
EOF fi else if [ "$pretty" = false ]; then cat < $dir/expect {"$DATASTORE_TOP":{"clixon-example:table":{"parameter":[{"name":"a","value":"42"}]}}} EOF else cat < $dir/expect { "${DATASTORE_TOP}": { "clixon-example:table": { "parameter": [ { "name": "a", "value": "42" } ] } } } EOF fi fi # -w ignore white space ret=$(diff -w $dir/candidate_db $dir/expect) if [ $? -ne 0 ]; then err "$(cat $dir/expect)" "$(cat $dir/candidate_db)" fi new "save config file" expectpart "$($clixon_cli -1 -f $cfg save $dir/myconfig $format)" 0 "^$" new "discard" expectpart "$($clixon_cli -1 -f $cfg discard)" 0 "^$" new "load config file" expectpart "$($clixon_cli -1 -f $cfg load $dir/myconfig $format)" 0 "^$" new "cli show config xml" expectpart "$($clixon_cli -1 -f $cfg show config)" 0 "^a42
$" 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 } new "test params: -f $cfg" for format in xml json; do for pretty in false true; do new "test db $format pretty=$pretty" testrun xml false done done # Negative test, load yang-invalid xml 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 -o CLICON_XMLDB_FORMAT=$format -o CLICON_XMLDB_PRETTY=$pretty" start_backend -s init -f $cfg -o CLICON_XMLDB_FORMAT=$format -o CLICON_XMLDB_PRETTY=$pretty fi new "wait backend" wait_backend # Wrong: two toplevels cat < $dir/myconfig <${DATASTORE_TOP}> a 42
b 99
EOF new "load invalid file: 2 top-level containers, expect fail" expectpart "$($clixon_cli -1 -f $cfg load $dir/myconfig xml 2>&1)" 0 "Editing configuration: protocol operation-failed : too-many-elements : /rpc/edit-config/config/table" # Wrong: two toplevels cat < $dir/myconfig <${DATASTORE_TOP}> a 42 1 2
EOF # XXX This is invalid but not detected at load will be checked with validate new "load invalid file: 2 inner containers, expect fail" expectpart "$($clixon_cli -1 -f $cfg load $dir/myconfig xml 2>&1)" 0 "" new "Validate expect fail" expectpart "$($clixon_cli -1 -f $cfg validate 2>&1)" 255 "too-many-elements" 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