Permalink
Browse files

Validation: Add MSR test for tutorial

We disable tests that do not work on macOS, since they require the
`simpleini` plugin.
  • Loading branch information...
sanssecours committed Oct 7, 2017
1 parent 4f339bf commit c9f091e195eb7d84a3c8ee0388c06263047e9e7a
Showing with 62 additions and 37 deletions.
  1. +61 −37 doc/tutorials/validation.md
  2. +1 −0 tests/shell/shell_recorder/tutorial_wrapper/CMakeLists.txt
@@ -43,7 +43,7 @@ described in this tutorial, e.g.:
The most direct way to validate keys is
```sh
kdb mount validation.dump user/tutorial/together dump validation
sudo kdb mount validation.dump user/tutorial/together dump validation
kdb vset user/tutorial/together/test 123 "[1-9][0-9]*" "Not a number"
kdb set user/tutorial/together/test abc
# STDERR: The command kdb set failed while accessing the key database .*⏎
@@ -74,6 +74,10 @@ kdb setmeta user/tutorial/together/test check/validation/match LINE
kdb setmeta user/tutorial/together/test check/validation/message "Not a number"
kdb set user/tutorial/together/test 123
#> Set string to "123"
# Undo modifications
kdb rm -r user/tutorial/together
sudo kdb umount user/tutorial/together
```
The approach is not limited to validation via regular expressions, but
@@ -109,7 +113,7 @@ to all other namespaces. This plugin is called `spec` and needs to be mounted
globally (will be added by default with `kdb global-mount`):
```sh
kdb global-mount
sudo kdb global-mount
```
Then we can write metadata to `spec` and see it for every cascading key:
@@ -129,6 +133,10 @@ kdb setmeta "spec/tutorial/spec/_" new meta
kdb lsmeta /tutorial/spec/test
#> hello
#> new
# Remove metadata from command above
kdb rm -r spec/tutorial/spec
kdb rm -r user/tutorial/spec
```
So let us combine this functionality with validation plugins.
@@ -159,7 +167,7 @@ Nonetheless we were able to set this key, because the validation plugin was not
active for this key.
On that behalf we have to make sure that the validation plugin is loaded for
this key with:
```sh
```
kdb mount tutorial.dump user/tutorial dump validation
```
This [mounts](/doc/tutorials/mount.md) the backend `tutorial.dump` to the mountpoint
@@ -178,6 +186,12 @@ This problem can be addressed by recognizing that the location of the
configuration files and the plugins that must be loaded is part of the
_schema_ of our configuration and therefore should be stored in the spec namespace.
```sh
# Undo modifications
kdb rm -r spec/tutorial/spec/test
kdb rm -r user/tutorial/spec/test
```
### Specfiles
We call the files, that contain a complete schema for configuration
below a specific path in form of metadata, _Specfiles_.
@@ -191,17 +205,17 @@ Let us create an example _Specfile_ in the dump format, which supports metadata
(altough the specfile is stored in the dump format, we can still create it using
the human readable [ni format](/src/plugins/ni/README.md) by using `kdb import`):
```sh
kdb mount tutorial.dump spec/tutorial dump
cat << HERE | kdb import spec/tutorial ni
[]
mountpoint = tutorial.dump
infos/plugins = dump validation
[/links/_]
check/validation = https?://.*\..*
check/validation/match = LINE
check/validation/message = not a valid URL
description = A link to some website
sudo kdb mount tutorial.dump spec/tutorial dump
cat << HERE | kdb import spec/tutorial ni \
[] \
mountpoint = tutorial.dump \
infos/plugins = dump validation \
\
[/links/_] \
check/validation = https?://.*\..* \
check/validation/match = LINE \
check/validation/message = not a valid URL \
description = A link to some website \
HERE
kdb lsmeta spec/tutorial
#> infos/plugins
@@ -219,8 +233,8 @@ loads the validation plugin.
```sh
kdb set /tutorial/links/url "invalid url"
#> Using name user/tutorial/links/elektra
# STDERR: .*Sorry, the error .#42. occurred ;(
#> Using name user/tutorial/links/url
# STDERR: .*Sorry, the error.*#42.*occurred.*
# Description: Key Value failed to validate⏎
# .*Reason: not a valid URL.*
# RET: 5
@@ -229,9 +243,9 @@ kdb set /tutorial/links/url "invalid url"
Note that the backend `tutorial.dump` is mounted for all namespaces:
```sh
kdb file user/tutorial
# STDOUT-REGEX: /home/.*/\.config/tutorial\.dump
# STDOUT-REGEX: /(home|Users)/.*/\.config/tutorial\.dump
kdb file system/tutorial
#> /etc/kdb/tutorial.dump
# STDOUT-REGEX: .*/tutorial\.dump
kdb file dir/tutorial
# STDOUT-REGEX: /.*/\.dir/tutorial\.dump
```
@@ -255,17 +269,27 @@ Another way is to trigger errors with the [error plugin](/src/plugins/error):
```sh
kdb setmeta /tutorial/spec/should_not_be_here trigger/error 10
#> Using keyname spec/tutorial/spec/should_not_be_here
kdb spec-mount /tutorial/spec
kdb spec-mount /tutorial
kdb set /tutorial/spec/should_not_be_here abc
#> Using name user/tutorial/spec/should_not_be_here
# STDERR: .*Error .#10. occurred!.*
# RET: 5
# STDERR: .*error.*10.*occurred.*
kdb get /tutorial/spec/should_not_be_here
#> Did not find key
# RET: 1
# STDERR: Did not find key
```
If we want to reject every optional key (and only want to allow required keys)
we can use the plugin `required` as further discussed below.
Before we look further let us undo the modifications to the key database.
```sh
kdb rm -r spec/tutorial
kdb rm -r system/tutorial
kdb umount spec/tutorial
kdb umount /tutorial
```
## Customized Schemas
@@ -276,13 +300,13 @@ We can write a plugin that parses that format and transform the content to key-v
For example, let us assume we have enum validations in the file `schema.txt`:
```sh
cat > "$PWD/schema.txt" << HERE
%: notation TBD ? graph text semi
%: tool-support* TBD ? none compiler ide
%: applied-to TBD ? none small real-world
mountpoint file.txt
plugins required
```
cat > "$PWD/schema.txt" << HERE \
%: notation TBD ? graph text semi \
%: tool-support* TBD ? none compiler ide \
%: applied-to TBD ? none small real-world \
mountpoint file.txt \
plugins required \
HERE
```
@@ -297,7 +321,7 @@ So we want to transform above syntax to:
Lucky, we already have a plugin which allows us to so:
```sh
```
kdb mount "$PWD/schema.txt" spec/tutorial/schema simplespeclang keyword/enum=%:,keyword/assign=TBD
kdb spec-mount /tutorial/schema
```
@@ -309,23 +333,23 @@ schema using `spec-mount`.
Now we have enforced that the 3 configuration options `notation tool-support* applied-to`
need to be present (and no other). For example we can import:
```sh
kdb import -s validate -c "format=% : %" /tutorial/schema simpleini << HERE
notation : graph
tool-support : ? none
applied-to : small
HERE
```
kdb import -s validate -c "format=% : %" /tutorial/schema simpleini << HERE \
notation : graph \
tool-support : ? none \
applied-to : small \
HERE \
```
Or (afterwards) setting individual values:
```sh
```
kdb set /tutorial/schema/applied-to smal # fails, not a valid enum
```
Or (in `visudo` fashion):
```sh
```
kdb editor -s validate /tutorial/schema simpleini
```
@@ -16,6 +16,7 @@ function (add_plugin_shell_test PLUGIN)
endfunction ()
add_s_test (tutorial_cascading "${CMAKE_SOURCE_DIR}/doc/tutorials/cascading.md")
add_s_test (tutorial_validation "${CMAKE_SOURCE_DIR}/doc/tutorials/validation.md")
add_s_test (kdb-complete "${CMAKE_SOURCE_DIR}/doc/help/kdb-complete.md")
add_s_test (kdb-global-umount "${CMAKE_SOURCE_DIR}/doc/help/kdb-global-umount.md")
add_s_test (kdb-ls "${CMAKE_SOURCE_DIR}/doc/help/kdb-ls.md")

0 comments on commit c9f091e

Please sign in to comment.