Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.core
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Rafael Monnerat
slapos.core
Commits
d78a0e6c
Commit
d78a0e6c
authored
Mar 06, 2024
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test_interface: test that Manager classes implements their interfaces
parent
d07e3ccf
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
9 deletions
+31
-9
slapos/tests/test_interface.py
slapos/tests/test_interface.py
+31
-9
No files found.
slapos/tests/test_interface.py
View file @
d78a0e6c
...
...
@@ -25,11 +25,15 @@
#
##############################################################################
import
unittest
import
os.path
import
glob
from
zope.interface.verify
import
verifyClass
import
zope.interface
from
six
import
class_types
from
slapos
import
slap
import
slapos.slap
import
slapos.manager
def
getImplementationAssertionMethod
(
klass
,
interface
):
...
...
@@ -45,22 +49,29 @@ def getDeclarationAssertionMethod(klass):
self
.
fail
(
'%s class does not respect its interface(s).'
%
klass
.
__name__
)
return
testMethod
def
generateTestMethodListOnClass
(
klass
,
module
):
tested_classes
=
set
()
# to test aliases only once
def
generateTestMethodListOnClass
(
klass
,
module
,
ignore_classes_without_interfaces
=
False
):
"""Generate test method on klass"""
for
class_id
in
dir
(
module
):
implementing_class
=
getattr
(
module
,
class_id
)
if
not
isinstance
(
implementing_class
,
class_types
):
continue
if
implementing_class
in
tested_classes
:
continue
tested_classes
.
add
(
implementing_class
)
# add methods to assert that publicly available classes are defining
# interfaces
method_name
=
'test_%s_declares_interface'
%
(
class_id
,)
# interfaces, except for some internal modules (slapos.manager).
if
not
ignore_classes_without_interfaces
:
method_name
=
'test_%s.%s_declares_interface'
%
(
module
.
__name__
,
class_id
)
setattr
(
klass
,
method_name
,
getDeclarationAssertionMethod
(
implementing_class
))
for
interface
in
list
(
zope
.
interface
.
implementedBy
(
implementing_class
)):
# for each interface which class declares add a method which verify
# implementation
method_name
=
'test_%s_implements_%s'
%
(
class_id
,
method_name
=
'test_%s.%s_implements_%s'
%
(
module
.
__name__
,
class_id
,
interface
.
__identifier__
)
setattr
(
klass
,
method_name
,
getImplementationAssertionMethod
(
implementing_class
,
interface
))
...
...
@@ -74,7 +85,18 @@ class TestInterface(unittest.TestCase):
"""
# add methods to test class
generateTestMethodListOnClass
(
TestInterface
,
slap
)
generateTestMethodListOnClass
(
TestInterface
,
slapos
.
slap
)
# add manager classes by introspecting the modules
for
module_name
in
glob
.
glob
(
os
.
path
.
join
(
os
.
path
.
dirname
(
slapos
.
manager
.
__file__
),
'*.py'
)):
module_name
=
os
.
path
.
splitext
(
os
.
path
.
basename
(
module_name
))[
0
]
__import__
(
'slapos.manager.'
+
module_name
)
manager_module
=
getattr
(
slapos
.
manager
,
module_name
)
generateTestMethodListOnClass
(
TestInterface
,
manager_module
,
ignore_classes_without_interfaces
=
True
)
if
__name__
==
'__main__'
:
unittest
.
main
()
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