#!/usr/bin/env bash # INSTANCE-ID tests # Magic line must be first in script (see README.md) s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi : ${clixon_util_path:=clixon_util_path -D $DBG} # Number of list/leaf-list entries : ${nr:=100} # Number of tests to generate XML for max=9 # How many times to run ech test (go through some random numbers) rep=10 # XML file (alt provide it in stdin after xpath) for (( i=1; i<$max; i++ )); do eval xml$i=$dir/xml$i.xml done ydir=$dir/yang if [ ! -d $ydir ]; then mkdir $ydir fi # Instance-id PATH binary search in ordered-by system lists cat < $ydir/moda.yang module moda{ namespace "urn:example:a"; prefix a; container x1{ description "list with single string key"; list y{ ordered-by system; key k1; leaf k1{ type string; } leaf z{ type string; } } } container x2{ description "list with single int key"; list y{ ordered-by system; key k1; leaf k1{ type uint32; } leaf z{ type string; } } } container x3{ description "list with double string keys"; list y{ ordered-by system; key "k1 k2"; leaf k1{ type string; } leaf k2{ type string; } leaf z{ type string; } } } container x4{ description "leaf-list with int key"; leaf-list y{ type string; } } list x5{ ordered-by system; description "Direct under root"; key "k1"; leaf k1{ type string; } leaf z{ type string; } } augment "/b:x6/b:yy" { list y{ ordered-by system; key "k1 k2"; leaf k1{ type string; } leaf k2{ type string; } leaf-list z{ type string; } } } container x7{ description "Single list, ordered by user"; list y{ ordered-by user; key k1; leaf k1{ type string; } leaf z{ type string; } } } container x8{ description "Single list state data"; config false; list y{ key k1; leaf k1{ type string; } leaf z{ type string; } } } } EOF # This is for augment usecase cat < $ydir/modb.yang module modb{ namespace "urn:example:b"; prefix b; container x6{ description "deep tree and augment"; list yy{ ordered-by system; key "kk1 kk2"; leaf kk1{ type string; } leaf kk2{ type string; } leaf-list zz{ type string; } } } } EOF # This make it non-deterministic, some numbers may not work,... rnd=$(( ( RANDOM % $nr ) )) # Single string key new "generate list with $nr single string key to $xml1" echo -n '' > $xml1 for (( i=0; i<$nr; i++ )); do echo -n "a$ifoo$i" >> $xml1 done echo -n '' >> $xml1 for (( ii=0; ii<$rep; ii++ )); do new "instance-id single string key k1=a$rnd" expectpart "$($clixon_util_path -f $xml1 -y $ydir -p /a:x1/a:y[a:k1=\"a$rnd\"])" 0 "^0: a$rndfoo$rnd$" new "instance-id single string key /x1" expectpart "$($clixon_util_path -f $xml1 -y $ydir -p /a:x1)" 0 "0: a0foo0a1foo1" # Assume at least two elements new "instance-id position specific position 5" # expectpart "$($clixon_util_path -f $xml1 -y $ydir -p /a:x1/a:y[5])" 0 "0: a13foo13" # sort alphanumerivc wrong 1,10,2 new "instance-id single string key omit key" expectpart "$($clixon_util_path -f $xml1 -y $ydir -p /a:x1/a:y)" 0 '^0: a0foo0 1: a0foo0' # Fails and error handling new "instance-id single string search non-index" expectpart "$($clixon_util_path -f $xml1 -y $ydir -p /a:x1/a:y[a:z=\"foo$rnd\"] )" 0 "a$rndfoo$rnd$" new "instance-id single string search non-index (two variables, index first)" expectpart "$($clixon_util_path -f $xml1 -y $ydir -p /a:x1/a:y[a:k1=\"a$rnd\"][a:z=\"foo$rnd\"] )" 0 "a$rndfoo$rnd$" new "instance-id single string search non-index (two variables, index last)" expectpart "$($clixon_util_path -f $xml1 -y $ydir -p /a:x1/a:y[a:z=\"foo$rnd\"][a:k1=\"a$rnd\"] )" 0 "a$rndfoo$rnd$" new "instance-id single string wrong module, notfound" expectpart "$($clixon_util_path -f $xml1 -y $ydir -p /xxx:x1/a:y[a:k1=\"a$rnd\"] 2> /dev/null)" 255 '^$' new "instance-id single string no module, notfound" expectpart "$($clixon_util_path -f $xml1 -y $ydir -p /x1/a:y[a:k1=\"a$rnd\"] 2> /dev/null)" 255 '^$' new "instance-id single string no sub-prefixes, notfound" expectpart "$($clixon_util_path -f $xml1 -y $ydir -p /a:x1/y[k1=\"a$rnd\"] 2> /dev/null)" 255 '^$' new "instance-id single string two keys, notfound" expectpart "$($clixon_util_path -f $xml1 -y $ydir -p /a:x1/a:y[a:k1=a$rnd][a:k2=a$rnd] 2> /dev/null)" 255 '^$' done # Single int key new "generate list with $nr single int key to $xml2" echo -n '' > $xml2 for (( i=0; i<$nr; i++ )); do echo -n "$ifoo$i" >> $xml2 done echo -n '' >> $xml2 for (( ii=0; ii<$rep; ii++ )); do new "instance-id single int key k1=$rnd" expectpart "$($clixon_util_path -f $xml2 -y $ydir -p /a:x2/a:y[a:k1=\"$rnd\"])" 0 "^0: $rndfoo$rnd$" done # Double string key new "generate list with $nr double string keys to $xml3 (two k2 entries per k1 key)" echo -n '' > $xml3 for (( i=0; i<$nr; i++ )); do echo -n "a$ia$ifoo$i" >> $xml3 echo -n "a$ib$ifoob$i" >> $xml3 done # Add two rules with empty k2 string echo -n "a0foo0" >> $xml3 echo -n "a1foo1" >> $xml3 echo -n '' >> $xml3 for (( ii=0; ii<$rep; ii++ )); do new "instance-id double string key k1=a$rnd k2=b$rnd" expectpart "$($clixon_util_path -f $xml3 -y $ydir -p /a:x3/a:y[k1=\"a$rnd\"][k2=\"b$rnd\"])" 0 "0: a$rndb$rndfoob$rnd" new "instance-id double string key k1=a$rnd, - empty k2 string" expectpart "$($clixon_util_path -f $xml3 -y $ydir -p /a:x3/a:y[k1=\"a1\"][k2=\"\"])" 0 "0: a1foo1" new "instance-id double string key k1=a$rnd, - no k2 string - three matches" expecteq "$($clixon_util_path -f $xml3 -y $ydir -p /a:x3/a:y[k1=\"a1\"])" 0 "0: a1foo1 1: a1a1foo1 2: a1b1foob1" new "instance-id double string specific position 5" expectpart "$($clixon_util_path -f $xml3 -y $ydir -p /a:x3/a:y[5])" 0 "0: a1b1foob1" # sort alphanumerivc wrong 1,10,2 done # Leaf-list new "generate leaf-list int keys to $xml4" echo -n '' > $xml4 for (( i=0; i<$nr; i++ )); do echo -n "a$i" >> $xml4 done echo -n '' >> $xml4 for (( ii=0; ii<$rep; ii++ )); do new "instance-id leaf-list k1=a$rnd" expectpart "$($clixon_util_path -f $xml4 -y $ydir -p /a:x4/a:y[.=\"a$rnd\"])" 0 "^0: a$rnd$" done # Single string key direct under root new "generate list with $nr single string key to $xml5" echo -n '' > $xml5 for (( i=0; i<$nr; i++ )); do echo -n "a$ifoo$i" >> $xml5 done for (( ii=0; ii<$rep; ii++ )); do new "instance-id direct under root single string key k1=a$rnd" expectpart "$($clixon_util_path -f $xml5 -y $ydir -p /a:x5[k1=\"a$rnd\"])" 0 "^0: a$rndfoo$rnd$" done # Depth and augment # Deep augmented xml path new "generate deep list with augment" echo -n '' > $xml6 for (( i=0; i<$nr; i++ )); do echo -n "b$ib$ifoo$i" >> $xml6 for (( j=0; j<3; j++ )); do echo -n "a$ja$jfoo$j" >> $xml6 done echo -n "" >> $xml6 done echo -n '' >> $xml6 for (( ii=0; ii<$rep; ii++ )); do new "instance-id double string key b$rnd,b$rnd in mod b" expectpart "$($clixon_util_path -f $xml6 -y $ydir -p /b:x6/b:yy[kk1=\"b$rnd\"][kk2=\"b$rnd\"])" 0 "0: b$rndb$rndfoo$rnda0a0foo0a1a1foo1a2a2foo2" new "instance-id double string key a$rnd,b$rnd in modb + augmented in moda" expectpart "$($clixon_util_path -f $xml6 -y $ydir -p /b:x6/b:yy[kk1=\"b$rnd\"][kk2=\"b$rnd\"]/a:y[k1=\"a1\"][k2=\"a1\"]/a:z[.=\"foo1\"])" 0 "0: foo1" done # Single list ordered by user new "generate list with $nr single string key to $xml7" echo -n '' > $xml7 for (( i=0; i<$nr; i++ )); do echo -n "a$ifoo$i" >> $xml7 done echo -n '' >> $xml7 for (( ii=0; ii<$rep; ii++ )); do new "instance-id single string key k1=a$rnd ordered by user" expectpart "$($clixon_util_path -f $xml7 -y $ydir -p /a:x7/a:y[a:k1=\"a$rnd\"])" 0 "^0: a$rndfoo$rnd$" done # Single list state data (non-config) new "generate list with $nr single string key to $xml8" echo -n '' > $xml8 for (( i=0; i<$nr; i++ )); do echo -n "a$ifoo$i" >> $xml8 done echo -n '' >> $xml8 for (( ii=0; ii<$rep; ii++ )); do rnd=$(( ( RANDOM % $nr ) )) new "instance-id single string key k1=a$rnd ordered by user" expectpart "$($clixon_util_path -f $xml8 -y $ydir -p /a:x8/a:y[a:k1=\"a$rnd\"])" 0 "^0: a$rndfoo$rnd$" done rm -rf $dir unset nr unset clixon_util_path # for other script reusing it