Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
slapos
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
106
Merge Requests
106
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
nexedi
slapos
Commits
9e68c19f
Commit
9e68c19f
authored
5 months ago
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
slapconfiguration:jsonschema: Validate defaults
parent
5b477bfe
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
8 deletions
+21
-8
slapos/recipe/slapconfiguration.py
slapos/recipe/slapconfiguration.py
+8
-4
slapos/test/recipe/test_slaposconfiguration.py
slapos/test/recipe/test_slaposconfiguration.py
+13
-4
No files found.
slapos/recipe/slapconfiguration.py
View file @
9e68c19f
...
...
@@ -345,18 +345,22 @@ class DefaultValidator(object):
for
error
in
self
.
validator
.
iter_errors
(
instance
):
invalid
=
True
yield
error
#
Apply collected defaults - only to valid instance
s
#
Stop there in case of validation error
s
if
invalid
:
return
for
instance
,
defaults
in
self
.
defaults
.
values
():
# Apply collected defaults
for
data
,
defaults
in
self
.
defaults
.
values
():
for
key
,
defaultdict
in
defaults
.
items
():
if
key
not
in
instance
:
if
key
not
in
data
:
it
=
iter
(
defaultdict
.
values
())
default
=
next
(
it
)
if
any
(
d
!=
default
for
d
in
it
):
raise
UserError
(
"Conflicting defaults for key %s: %r"
%
(
key
,
defaultlist
))
instance
[
key
]
=
default
data
[
key
]
=
default
# Validate the updated instance
for
error
in
self
.
validatorfor
(
self
.
schema
).
iter_errors
(
instance
):
yield
error
class
JsonSchema
(
Recipe
):
...
...
This diff is collapsed.
Click to expand it.
slapos/test/recipe/test_slaposconfiguration.py
View file @
9e68c19f
...
...
@@ -54,7 +54,7 @@ class SlapConfigurationTest(unittest.TestCase):
self
.
assertEqual
(
options
[
'address-list'
],
[
10
,
20
],
"All underscores should be replaced with -"
)
def
writeJsonSchema
(
self
,
serialisation
=
'json-in-xml'
):
def
writeJsonSchema
(
self
,
serialisation
=
'json-in-xml'
,
valid_defaults
=
True
):
self
.
software_json_file
=
os
.
path
.
join
(
self
.
software_root
,
'software.cfg.json'
)
software_schema
=
{
"name"
:
"Test"
,
...
...
@@ -82,7 +82,7 @@ class SlapConfigurationTest(unittest.TestCase):
"letter"
:
{
"type"
:
"string"
,
"enum"
:
[
"a"
,
"b"
,
"c"
],
"default"
:
"a"
"default"
:
"a"
if
valid_defaults
else
1
,
},
"number"
:
{
"type"
:
"integer"
,
...
...
@@ -136,7 +136,7 @@ class SlapConfigurationTest(unittest.TestCase):
},
"thing"
:
{
"type"
:
"string"
,
"default"
:
"hello"
,
"default"
:
"hello"
if
valid_defaults
else
1
,
},
},
"required"
:
[
"kind"
],
...
...
@@ -153,7 +153,7 @@ class SlapConfigurationTest(unittest.TestCase):
},
"thing"
:
{
"type"
:
"integer"
,
"default"
:
42
,
"default"
:
42
if
valid_defaults
else
"forty-two"
,
},
"required"
:
[
"kind"
],
}
...
...
@@ -286,6 +286,15 @@ class SlapConfigurationTest(unittest.TestCase):
self
.
receiveParameters
,
)
def
test_jsonschema_json_in_xml_invalid_defaults_json_input
(
self
):
self
.
writeJsonSchema
(
valid_defaults
=
False
)
parameters
=
{
"number"
:
1
}
with
self
.
patchSlap
(
parameters
,
True
):
self
.
assertRaises
(
slapconfiguration
.
UserError
,
self
.
receiveParameters
,
)
def
test_jsonschema_shared_1_valid_defaults
(
self
):
self
.
writeJsonSchema
()
parameters
=
{
"number"
:
1
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment