Allow suites to dynamically register schemas so tests can receive parameters

Change-Id: Idbe99a35993d193cd97059feb980e61ff14c67ad
diff --git a/selftest/schema_test/_prep.py b/selftest/schema_test/_prep.py
new file mode 120000
index 0000000..9cea3fe
--- /dev/null
+++ b/selftest/schema_test/_prep.py
@@ -0,0 +1 @@
+../_prep.py
\ No newline at end of file
diff --git a/selftest/schema_test/schema_case_01.conf b/selftest/schema_test/schema_case_01.conf
new file mode 100644
index 0000000..dacf18a
--- /dev/null
+++ b/selftest/schema_test/schema_case_01.conf
@@ -0,0 +1,43 @@
+schema:
+  handover:
+    duration: 'duration'
+    threshold: 'uint'
+
+tests:
+   - foobar:
+       prefix:
+         handover:
+           duration: 3
+           threshold: 2
+   - foobar:
+       prefix:
+         handover:
+           duration: 22kkk
+   - foobar:
+       prefix:
+         handover:
+           duration: 22h
+   - foobar:
+       wrongprefix:
+         handover:
+           duration: 22h
+   - foobar:
+       wrongprefix:
+         handover:
+   - foobar:
+       prefix:
+         handover:
+           threshold: 1
+   - foobar:
+       prefix:
+         handover:
+           threshold: -2
+   - foobar:
+       prefix:
+         handover:
+         - threshold: 1
+   - foobar:
+       prefix:
+         handover:
+           threshold:
+           - 1
diff --git a/selftest/schema_test/schema_case_02.conf b/selftest/schema_test/schema_case_02.conf
new file mode 100644
index 0000000..ddc02df
--- /dev/null
+++ b/selftest/schema_test/schema_case_02.conf
@@ -0,0 +1,12 @@
+schema:
+  hey:
+    ho:
+      letsgo: ['wrongtype']
+
+tests:
+   - foobar:
+       prefix:
+         hey:
+           ho:
+             letsgo:
+             - nanana
diff --git a/selftest/schema_test/schema_case_03.conf b/selftest/schema_test/schema_case_03.conf
new file mode 100644
index 0000000..e06fa24
--- /dev/null
+++ b/selftest/schema_test/schema_case_03.conf
@@ -0,0 +1,12 @@
+schema:
+  hey:
+    ho:
+      letsgo: ['str', 'str']
+
+tests:
+   - foobar:
+       prefix:
+         hey:
+           ho:
+             letsgo:
+             - nanana
diff --git a/selftest/schema_test/schema_case_04.conf b/selftest/schema_test/schema_case_04.conf
new file mode 100644
index 0000000..4148310
--- /dev/null
+++ b/selftest/schema_test/schema_case_04.conf
@@ -0,0 +1,12 @@
+schema:
+  hey:
+    ho:
+      letsgo: []
+
+tests:
+   - foobar:
+       prefix:
+         hey:
+           ho:
+             letsgo:
+             - nanana
diff --git a/selftest/schema_test/schema_case_05.conf b/selftest/schema_test/schema_case_05.conf
new file mode 100644
index 0000000..ee3d5db
--- /dev/null
+++ b/selftest/schema_test/schema_case_05.conf
@@ -0,0 +1,44 @@
+schema:
+  hey:
+    ho:
+      letsgo: ['str']
+
+tests:
+   - foobar:
+       prefix:
+         hey:
+           ho:
+             letsgo:
+             - nanana
+   - foobar:
+       prefix:
+         hey:
+           ho:
+             letsgo: []
+   - foobar:
+       prefix:
+         hey:
+           ho:
+             letsgo:
+             - nanana
+             - nunu
+   - foobar:
+       prefix:
+         hey:
+           ho:
+             letsgo: nanana
+   - foobar:
+       prefix:
+         hey:
+           ho:
+             letsgo: ['nana', 'nana', 'nana']
+   - foobar:
+       prefix:
+         hey:
+           ho:
+             letsgo: ['nana', {}, 'nana']
+   - foobar:
+       prefix:
+         hey:
+           ho:
+             letsgo: ['nana', [], 'nana']
diff --git a/selftest/schema_test/schema_test.err b/selftest/schema_test/schema_test.err
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/selftest/schema_test/schema_test.err
diff --git a/selftest/schema_test/schema_test.ok b/selftest/schema_test/schema_test.ok
new file mode 100644
index 0000000..2c4cd6a
--- /dev/null
+++ b/selftest/schema_test/schema_test.ok
@@ -0,0 +1,63 @@
+==== Testing dynamically generated schemas ====
+schema_case_01.conf:
+{'foobar.prefix.handover.duration': 'duration',
+ 'foobar.prefix.handover.threshold': 'uint'}
+validating tests[0]
+Validation: OK
+validating tests[1]
+--- foobar.prefix.handover.duration: ERR: ValueError: Invalid duration value: '22kkk'
+Validation: Error
+validating tests[2]
+Validation: OK
+validating tests[3]
+--- -: ERR: ValueError: config item not known: 'foobar.wrongprefix.handover.duration'
+Validation: Error
+validating tests[4]
+--- -: ERR: ValueError: config item not known: 'foobar.wrongprefix.handover'
+Validation: Error
+validating tests[5]
+Validation: OK
+validating tests[6]
+--- foobar.prefix.handover.threshold: ERR: ValueError: Positive value expected instead of -2
+Validation: Error
+validating tests[7]
+--- -: ERR: ValueError: config item not known: 'foobar.prefix.handover[].threshold'
+Validation: Error
+validating tests[8]
+--- -: ERR: ValueError: config item is a list, should be 'uint': 'foobar.prefix.handover.threshold'
+Validation: Error
+----------------------
+schema_case_02.conf:
+{'foobar.prefix.hey.ho.letsgo[]': 'wrongtype'}
+validating tests[0]
+--- -: ERR: ValueError: unknown type 'wrongtype' at 'foobar.prefix.hey.ho.letsgo[]'
+Validation: Error
+----------------------
+schema_case_03.conf:
+--- -: ERR: AssertionError: 
+config2schema: Error
+----------------------
+schema_case_04.conf:
+--- -: ERR: AssertionError: 
+config2schema: Error
+----------------------
+schema_case_05.conf:
+{'foobar.prefix.hey.ho.letsgo[]': 'str'}
+validating tests[0]
+Validation: OK
+validating tests[1]
+Validation: OK
+validating tests[2]
+Validation: OK
+validating tests[3]
+--- -: ERR: ValueError: config item not known: 'foobar.prefix.hey.ho.letsgo'
+Validation: Error
+validating tests[4]
+Validation: OK
+validating tests[5]
+--- -: ERR: ValueError: config item is dict but should be a leaf node of type 'str': 'foobar.prefix.hey.ho.letsgo[]'
+Validation: Error
+validating tests[6]
+--- -: ERR: ValueError: config item is a list, should be 'str': 'foobar.prefix.hey.ho.letsgo[]'
+Validation: Error
+----------------------
diff --git a/selftest/schema_test/schema_test.py b/selftest/schema_test/schema_test.py
new file mode 100755
index 0000000..3cf2799
--- /dev/null
+++ b/selftest/schema_test/schema_test.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python3
+
+import _prep
+
+import sys
+import os
+import io
+import pprint
+import copy
+
+from osmo_gsm_tester.core import config, log, schema
+
+def val(which, test_schema):
+    try:
+        schema.validate(which, test_schema)
+        print('Validation: OK')
+    except ValueError:
+        log.log_exn()
+        print('Validation: Error')
+
+def get_case_list(dir):
+    li = []
+    for f in os.listdir(dir):
+        if f.startswith('schema_case'):
+            li.append(f)
+    return sorted(li)
+
+print('==== Testing dynamically generated schemas ====')
+for f in get_case_list(_prep.script_dir):
+    print('%s:' % f)
+    example_config = os.path.join(_prep.script_dir, f)
+    cfg = config.read(example_config)
+    try:
+        schema_def = schema.config_to_schema_def(cfg['schema'], 'foobar.prefix.')
+    except AssertionError:
+        schema_def = None
+        log.log_exn()
+        print('config2schema: Error')
+
+    if schema_def is not None:
+        pprint.pprint(schema_def)
+        i = 0
+        for t in cfg['tests']:
+            print('validating tests[%d]' % i)
+            val(t, schema_def)
+            i += 1
+    print('----------------------')
+
+
+
+
+
+# vim: expandtab tabstop=4 shiftwidth=4