Change in osmo-gsm-tester[master]: schema: Allow objects registering their own schema types

2020-05-22 Thread pespin
pespin has posted comments on this change. ( 
https://gerrit.osmocom.org/c/osmo-gsm-tester/+/18388 )

Change subject: schema: Allow objects registering their own schema types
..


Patch Set 1: Code-Review+2


--
To view, visit https://gerrit.osmocom.org/c/osmo-gsm-tester/+/18388
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-Change-Id: I998c8674a55531909bfeac420064c3f238cea126
Gerrit-Change-Number: 18388
Gerrit-PatchSet: 1
Gerrit-Owner: pespin 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: pespin 
Gerrit-Comment-Date: Fri, 22 May 2020 12:55:42 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


Change in osmo-gsm-tester[master]: schema: Allow objects registering their own schema types

2020-05-22 Thread pespin
pespin has submitted this change. ( 
https://gerrit.osmocom.org/c/osmo-gsm-tester/+/18388 )

Change subject: schema: Allow objects registering their own schema types
..

schema: Allow objects registering their own schema types

Change-Id: I998c8674a55531909bfeac420064c3f238cea126
---
A selftest/schema_test/schema_case_06.conf
M selftest/schema_test/schema_test.ok
M selftest/schema_test/schema_test.py
M src/osmo_gsm_tester/core/schema.py
4 files changed, 90 insertions(+), 17 deletions(-)

Approvals:
  Jenkins Builder: Verified
  pespin: Looks good to me, approved



diff --git a/selftest/schema_test/schema_case_06.conf 
b/selftest/schema_test/schema_case_06.conf
new file mode 100644
index 000..ea7f45f
--- /dev/null
+++ b/selftest/schema_test/schema_case_06.conf
@@ -0,0 +1,29 @@
+schema:
+  handover:
+threshold: 'uint'
+myvar: 'test_type'
+anothervar: 'another_type'
+
+tests:
+   - foobar:
+   prefix:
+ handover:
+   myvar: 'valid_value1'
+   anothervar: 'unique_val_ok'
+   threshold: 2
+   - foobar:
+   prefix:
+ handover:
+   myvar: 'valid_value2'
+   - foobar:
+   prefix:
+ handover:
+   threshold: 0
+   - foobar:
+   prefix:
+ handover:
+ myvar: 'invalid_val'
+   - foobar:
+   prefix:
+ handover:
+ anothervar: 'another_invalid_val'
diff --git a/selftest/schema_test/schema_test.ok 
b/selftest/schema_test/schema_test.ok
index 2c4cd6a..846caae 100644
--- a/selftest/schema_test/schema_test.ok
+++ b/selftest/schema_test/schema_test.ok
@@ -61,3 +61,20 @@
 --- -: ERR: ValueError: config item is a list, should be 'str': 
'foobar.prefix.hey.ho.letsgo[]'
 Validation: Error
 --
+schema_case_06.conf:
+{'foobar.prefix.handover.anothervar': 'another_type',
+ 'foobar.prefix.handover.myvar': 'test_type',
+ 'foobar.prefix.handover.threshold': 'uint'}
+validating tests[0]
+Validation: OK
+validating tests[1]
+Validation: OK
+validating tests[2]
+Validation: OK
+validating tests[3]
+--- foobar.prefix.handover.myvar: ERR: ValueError: Invalid value 'invalid_val' 
for schema type 'test_type' (validator: test_validator)
+Validation: Error
+validating tests[4]
+--- foobar.prefix.handover.anothervar: ERR: ValueError: Invalid value 
'another_invalid_val' for schema type 'another_type' (validator: )
+Validation: Error
+--
diff --git a/selftest/schema_test/schema_test.py 
b/selftest/schema_test/schema_test.py
index 3cf2799..bffa601 100755
--- a/selftest/schema_test/schema_test.py
+++ b/selftest/schema_test/schema_test.py
@@ -25,6 +25,13 @@
 li.append(f)
 return sorted(li)

+def test_validator(val):
+return val in ('valid_value1', 'valid_value2')
+
+schema.register_schema_types({'test_type': test_validator,
+  'another_type': lambda val: val == 
'unique_val_ok'})
+
+
 print(' Testing dynamically generated schemas ')
 for f in get_case_list(_prep.script_dir):
 print('%s:' % f)
diff --git a/src/osmo_gsm_tester/core/schema.py 
b/src/osmo_gsm_tester/core/schema.py
index 9055c5b..70b4c8c 100644
--- a/src/osmo_gsm_tester/core/schema.py
+++ b/src/osmo_gsm_tester/core/schema.py
@@ -36,12 +36,12 @@
 break;
 if not regex.fullmatch(val):
 break;
-return
+return True
 raise ValueError('Invalid %s: %r' % (name, val))

 def band(val):
 if val in ('GSM-900', 'GSM-1800', 'GSM-1900'):
-return
+return True
 raise ValueError('Unknown GSM band: %r' % val)

 def ipv4(val):
@@ -49,27 +49,30 @@
 els = [int(el) for el in val.split('.')]
 if not all([el >= 0 and el <= 255 for el in els]):
 raise ValueError('Invalid IPv4 address: %r' % val)
+return True

 def hwaddr(val):
-match_re('hardware address', HWADDR_RE, val)
+return match_re('hardware address', HWADDR_RE, val)

 def imsi(val):
-match_re('IMSI', IMSI_RE, val)
+return match_re('IMSI', IMSI_RE, val)

 def ki(val):
-match_re('KI', KI_RE, val)
+return match_re('KI', KI_RE, val)

 def msisdn(val):
-match_re('MSISDN', MSISDN_RE, val)
+return match_re('MSISDN', MSISDN_RE, val)

 def auth_algo(val):
 if val not in util.ENUM_OSMO_AUTH_ALGO:
 raise ValueError('Unknown Authentication Algorithm: %r' % val)
+return True

 def uint(val):
 n = int(val)
 if n < 0:
 raise ValueError('Positive value expected instead of %d' % n)
+return True

 def uint8(val):
 n = int(val)
@@ -77,6 +80,7 @@
 raise ValueError('Positive value expected instead of %d' % n)
 if n > 255: # 2^8 - 1
 raise ValueError('Value %d too big, max value is 255' % n)
+return True

 def uint16(val):
 n = int(val)
@@ -84,57 +88,64 @@
 raise ValueError('Positive value expected instead of %d' % n)
 if n > 65535: # 2^16 - 1
 raise 

Change in osmo-gsm-tester[master]: schema: Allow objects registering their own schema types

2020-05-21 Thread pespin
pespin has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmo-gsm-tester/+/18388 )


Change subject: schema: Allow objects registering their own schema types
..

schema: Allow objects registering their own schema types

Change-Id: I998c8674a55531909bfeac420064c3f238cea126
---
A selftest/schema_test/schema_case_06.conf
M selftest/schema_test/schema_test.ok
M selftest/schema_test/schema_test.py
M src/osmo_gsm_tester/core/schema.py
4 files changed, 90 insertions(+), 17 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester 
refs/changes/88/18388/1

diff --git a/selftest/schema_test/schema_case_06.conf 
b/selftest/schema_test/schema_case_06.conf
new file mode 100644
index 000..ea7f45f
--- /dev/null
+++ b/selftest/schema_test/schema_case_06.conf
@@ -0,0 +1,29 @@
+schema:
+  handover:
+threshold: 'uint'
+myvar: 'test_type'
+anothervar: 'another_type'
+
+tests:
+   - foobar:
+   prefix:
+ handover:
+   myvar: 'valid_value1'
+   anothervar: 'unique_val_ok'
+   threshold: 2
+   - foobar:
+   prefix:
+ handover:
+   myvar: 'valid_value2'
+   - foobar:
+   prefix:
+ handover:
+   threshold: 0
+   - foobar:
+   prefix:
+ handover:
+ myvar: 'invalid_val'
+   - foobar:
+   prefix:
+ handover:
+ anothervar: 'another_invalid_val'
diff --git a/selftest/schema_test/schema_test.ok 
b/selftest/schema_test/schema_test.ok
index 2c4cd6a..846caae 100644
--- a/selftest/schema_test/schema_test.ok
+++ b/selftest/schema_test/schema_test.ok
@@ -61,3 +61,20 @@
 --- -: ERR: ValueError: config item is a list, should be 'str': 
'foobar.prefix.hey.ho.letsgo[]'
 Validation: Error
 --
+schema_case_06.conf:
+{'foobar.prefix.handover.anothervar': 'another_type',
+ 'foobar.prefix.handover.myvar': 'test_type',
+ 'foobar.prefix.handover.threshold': 'uint'}
+validating tests[0]
+Validation: OK
+validating tests[1]
+Validation: OK
+validating tests[2]
+Validation: OK
+validating tests[3]
+--- foobar.prefix.handover.myvar: ERR: ValueError: Invalid value 'invalid_val' 
for schema type 'test_type' (validator: test_validator)
+Validation: Error
+validating tests[4]
+--- foobar.prefix.handover.anothervar: ERR: ValueError: Invalid value 
'another_invalid_val' for schema type 'another_type' (validator: )
+Validation: Error
+--
diff --git a/selftest/schema_test/schema_test.py 
b/selftest/schema_test/schema_test.py
index 3cf2799..bffa601 100755
--- a/selftest/schema_test/schema_test.py
+++ b/selftest/schema_test/schema_test.py
@@ -25,6 +25,13 @@
 li.append(f)
 return sorted(li)

+def test_validator(val):
+return val in ('valid_value1', 'valid_value2')
+
+schema.register_schema_types({'test_type': test_validator,
+  'another_type': lambda val: val == 
'unique_val_ok'})
+
+
 print(' Testing dynamically generated schemas ')
 for f in get_case_list(_prep.script_dir):
 print('%s:' % f)
diff --git a/src/osmo_gsm_tester/core/schema.py 
b/src/osmo_gsm_tester/core/schema.py
index 9055c5b..70b4c8c 100644
--- a/src/osmo_gsm_tester/core/schema.py
+++ b/src/osmo_gsm_tester/core/schema.py
@@ -36,12 +36,12 @@
 break;
 if not regex.fullmatch(val):
 break;
-return
+return True
 raise ValueError('Invalid %s: %r' % (name, val))

 def band(val):
 if val in ('GSM-900', 'GSM-1800', 'GSM-1900'):
-return
+return True
 raise ValueError('Unknown GSM band: %r' % val)

 def ipv4(val):
@@ -49,27 +49,30 @@
 els = [int(el) for el in val.split('.')]
 if not all([el >= 0 and el <= 255 for el in els]):
 raise ValueError('Invalid IPv4 address: %r' % val)
+return True

 def hwaddr(val):
-match_re('hardware address', HWADDR_RE, val)
+return match_re('hardware address', HWADDR_RE, val)

 def imsi(val):
-match_re('IMSI', IMSI_RE, val)
+return match_re('IMSI', IMSI_RE, val)

 def ki(val):
-match_re('KI', KI_RE, val)
+return match_re('KI', KI_RE, val)

 def msisdn(val):
-match_re('MSISDN', MSISDN_RE, val)
+return match_re('MSISDN', MSISDN_RE, val)

 def auth_algo(val):
 if val not in util.ENUM_OSMO_AUTH_ALGO:
 raise ValueError('Unknown Authentication Algorithm: %r' % val)
+return True

 def uint(val):
 n = int(val)
 if n < 0:
 raise ValueError('Positive value expected instead of %d' % n)
+return True

 def uint8(val):
 n = int(val)
@@ -77,6 +80,7 @@
 raise ValueError('Positive value expected instead of %d' % n)
 if n > 255: # 2^8 - 1
 raise ValueError('Value %d too big, max value is 255' % n)
+return True

 def uint16(val):
 n = int(val)
@@ -84,57 +88,64 @@
 raise ValueError('Positive value expected instead of %d' % n)
 if n > 65535: # 2^16 - 1