From 42efb59a754eb86cc4ca7b32fe859f46ba77eb30 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Thu, 21 Jul 2022 08:07:48 +0200 Subject: [PATCH] Example main -v/-V issues breaking previous commit tests --- example/main/example_backend.c | 36 +++++++++++++++++++++++++++-- example/main/example_backend_nacm.c | 10 ++++++-- test/test_restconf_err.sh | 2 +- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/example/main/example_backend.c b/example/main/example_backend.c index a78d97b1..e78f1001 100644 --- a/example/main/example_backend.c +++ b/example/main/example_backend.c @@ -43,7 +43,7 @@ * -u enable upgrade function - auto-upgrade testing * -U general-purpose upgrade * -t enable transaction logging (call syslog for every transaction) - * -v Failing validate and commit if is present (synthetic error) + * -V Failing validate and commit if is present (synthetic error) */ #include #include @@ -67,7 +67,7 @@ #include /* Command line options to be passed to getopt(3) */ -#define BACKEND_EXAMPLE_OPTS "a:rsS:x:iuUtv:" +#define BACKEND_EXAMPLE_OPTS "a:rsS:x:iuUtV:" /*! Yang action * Start backend with -- -a @@ -132,6 +132,19 @@ static int _general_upgrade = 0; */ static int _transaction_log = 0; +/*! Variable to trigger validation/commit errors (synthetic errors) for tests + * XPath to trigger validation error, ie if the XPath matches, then validate fails + * This is to make tests where a transaction fails midway and aborts/reverts the transaction. + * Start backend with -- -V + * Note that the second backend plugin has a corresponding -v to do the same thing + */ +static char *_validate_fail_xpath = NULL; + +/*! Sub state variable to fail on validate/commit (not configured) + * Obscure, but a way to first trigger a validation error, next time to trigger a commit error + */ +static int _validate_fail_toggle = 0; /* fail at validate and commit */ + /* forward */ static int example_stream_timer_setup(clicon_handle h); @@ -151,6 +164,14 @@ main_validate(clicon_handle h, { if (_transaction_log) transaction_log(h, td, LOG_NOTICE, __FUNCTION__); + if (_validate_fail_xpath){ + if (_validate_fail_toggle==0 && + xpath_first(transaction_target(td), NULL, "%s", _validate_fail_xpath)){ + _validate_fail_toggle = 1; /* toggle if triggered */ + clicon_err(OE_XML, 0, "User error"); + return -1; /* induce fail */ + } + } return 0; } @@ -177,6 +198,14 @@ main_commit(clicon_handle h, if (_transaction_log) transaction_log(h, td, LOG_NOTICE, __FUNCTION__); + if (_validate_fail_xpath){ + if (_validate_fail_toggle==1 && + xpath_first(transaction_target(td), NULL, "%s", _validate_fail_xpath)){ + _validate_fail_toggle = 0; /* toggle if triggered */ + clicon_err(OE_XML, 0, "User error"); + return -1; /* induce fail */ + } + } /* Create namespace context for xpath */ if ((nsc = xml_nsctx_init(NULL, "urn:ietf:params:xml:ns:yang:ietf-interfaces")) == NULL) @@ -1309,6 +1338,9 @@ clixon_plugin_init(clicon_handle h) case 't': /* transaction log */ _transaction_log = 1; break; + case 'V': /* validate fail */ + _validate_fail_xpath = optarg; + break; } if (_state_file){ diff --git a/example/main/example_backend_nacm.c b/example/main/example_backend_nacm.c index b61c16d3..9001474a 100644 --- a/example/main/example_backend_nacm.c +++ b/example/main/example_backend_nacm.c @@ -67,11 +67,17 @@ */ static int _transaction_log = 0; -/*! Variable to control transaction logging (for debug) - * XPath to trigger validation error +/*! Variable to trigger validation/commit errors (synthetic errors) for tests + * XPath to trigger validation error, ie if the XPath matches, then validate fails + * This is to make tests where a transaction fails midway and aborts/reverts the transaction. * Start backend with -- -v + * Note that the first backend plugin has a corresponding -V to do the same thing */ static char *_validate_fail_xpath = NULL; + +/*! Sub state variable to fail on validate/commit, + * Obscure, but a way to first trigger a validation error, next time to trigger a commit error + */ static int _validate_fail_toggle = 0; /* fail at validate and commit */ int diff --git a/test/test_restconf_err.sh b/test/test_restconf_err.sh index ef242012..1a3b6705 100755 --- a/test/test_restconf_err.sh +++ b/test/test_restconf_err.sh @@ -186,7 +186,7 @@ if [ $BE -ne 0 ]; then fi sudo pkill -f clixon_backend # to be sure new "start backend -s init -f $cfg -- -sS $fstate -v /table/parameter[name=\"4242\"]" - start_backend -s init -f $cfg -- -sS $fstate -v /table/parameter[name="4242"] + start_backend -s init -f $cfg -- -sS $fstate -V "/table/parameter[name='4242']" fi new "wait backend"