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
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Xavier Thompson
slapos
Commits
d288911a
Commit
d288911a
authored
Sep 12, 2024
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
slapconfiguration:jsonschema: Validate defaults
parent
c7db2923
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 @
d288911a
...
...
@@ -335,18 +335,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
):
...
...
slapos/test/recipe/test_slaposconfiguration.py
View file @
d288911a
...
...
@@ -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
}
...
...
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