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
zhifan huang
slapos
Commits
3248a533
Commit
3248a533
authored
Oct 12, 2021
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
recipe/promise_plugin: Fix tests
parent
5a565f05
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
81 deletions
+103
-81
slapos/recipe/promise_plugin.py
slapos/recipe/promise_plugin.py
+1
-1
slapos/test/recipe/test_plugin.py
slapos/test/recipe/test_plugin.py
+102
-80
No files found.
slapos/recipe/promise_plugin.py
View file @
3248a533
...
@@ -91,7 +91,7 @@ class Recipe(GenericBaseRecipe):
...
@@ -91,7 +91,7 @@ class Recipe(GenericBaseRecipe):
if
klass
==
'RunPromise'
:
if
klass
==
'RunPromise'
:
klass
=
None
klass
=
None
elif
klass
and
not
isPythonName
(
klass
):
elif
klass
and
not
isPythonName
(
klass
):
raise
UserError
(
"%r is not valid class name"
%
klass
)
raise
UserError
(
"%r is not
a
valid class name"
%
klass
)
if
bool
(
module
)
==
bool
(
filepath
):
if
bool
(
module
)
==
bool
(
filepath
):
raise
UserError
(
"Either 'module' or 'file' is required but not both"
)
raise
UserError
(
"Either 'module' or 'file' is required but not both"
)
...
...
slapos/test/recipe/test_plugin.py
View file @
3248a533
import
os
,
shutil
,
tempfile
,
unittest
import
os
,
shutil
,
tempfile
,
unittest
from
slapos.recipe
import
promise_plugin
from
slapos.recipe
import
promise_plugin
from
slapos.test.utils
import
makeRecipe
from
slapos.test.utils
import
makeRecipe
from
zc.buildout
import
UserError
from
pprint
import
pformat
from
pprint
import
pformat
import
stat
,
json
import
stat
,
json
import
six
import
six
...
@@ -10,7 +11,7 @@ class TestPromisePlugin(unittest.TestCase):
...
@@ -10,7 +11,7 @@ class TestPromisePlugin(unittest.TestCase):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
tmp
=
tempfile
.
mkdtemp
()
self
.
tmp
=
tempfile
.
mkdtemp
()
self
.
output
=
os
.
path
.
join
(
self
.
tmp
,
'output.py'
)
self
.
output
=
os
.
path
.
join
(
self
.
tmp
,
'output.py'
)
self
.
options
=
options
=
{
self
.
options
=
{
'output'
:
self
.
output
,
'output'
:
self
.
output
,
'eggs'
:
'slapos.cookbook'
'eggs'
:
'slapos.cookbook'
}
}
...
@@ -18,9 +19,16 @@ class TestPromisePlugin(unittest.TestCase):
...
@@ -18,9 +19,16 @@ class TestPromisePlugin(unittest.TestCase):
def
tearDown
(
self
):
def
tearDown
(
self
):
shutil
.
rmtree
(
self
.
tmp
)
shutil
.
rmtree
(
self
.
tmp
)
def
test_parameters
(
self
):
def
makeRecipe
(
self
):
self
.
options
[
'mode'
]
=
'0644'
return
makeRecipe
(
self
.
options
[
'import'
]
=
'slapos.promise.plugin.check_site_available'
promise_plugin
.
Recipe
,
options
=
self
.
options
,
name
=
'plugin'
)
def
installRecipe
(
self
):
self
.
makeRecipe
().
install
()
def
setConfig
(
self
):
self
.
options
[
'config-param1'
]
=
"YY^@12"
self
.
options
[
'config-param1'
]
=
"YY^@12"
self
.
options
[
'config-param2'
]
=
"23'91'"
self
.
options
[
'config-param2'
]
=
"23'91'"
self
.
options
[
'config-param3'
]
=
None
self
.
options
[
'config-param3'
]
=
None
...
@@ -28,88 +36,102 @@ class TestPromisePlugin(unittest.TestCase):
...
@@ -28,88 +36,102 @@ class TestPromisePlugin(unittest.TestCase):
in multi line
in multi line
123444
123444
"""
"""
recipe
=
makeRecipe
(
promise_plugin
.
Recipe
,
options
=
self
.
options
,
name
=
'plugin'
)
recipe
.
install
()
def
assertOutput
(
self
,
*
expect
):
self
.
assertTrue
(
os
.
path
.
exists
(
self
.
output
))
self
.
assertTrue
(
os
.
path
.
exists
(
self
.
output
))
with
open
(
self
.
output
,
'r'
)
as
f
:
with
open
(
self
.
output
,
'r'
)
as
f
:
content
=
f
.
read
()
content
=
f
.
read
()
self
.
assertIn
(
"from slapos.promise.plugin.check_site_available import RunPromise"
,
content
)
for
s
in
expect
:
self
.
assertEqual
(
stat
.
S_IMODE
(
os
.
stat
(
self
.
output
).
st_mode
),
int
(
'644'
,
8
))
self
.
assertIn
(
s
,
content
)
expected_dict
=
dict
(
def
assertConfig
(
self
):
param1
=
self
.
options
[
'config-param1'
],
items
=
self
.
options
.
items
()
param2
=
self
.
options
[
'config-param2'
],
expect
=
{
k
[
7
:]
:
v
for
k
,
v
in
items
if
k
.
startswith
(
'config-'
)}
param3
=
self
.
options
[
'config-param3'
],
self
.
assertOutput
(
"extra_config_dict = %s"
%
pformat
(
expect
,
indent
=
2
))
param4
=
self
.
options
[
'config-param4'
],
)
def
assertEmptyConfig
(
self
):
self
.
assertIn
(
'extra_config_dict = %s'
%
pformat
(
expected_dict
,
indent
=
2
),
content
)
self
.
assertOutput
(
"extra_config_dict = %s"
%
(
'{}'
if
six
.
PY3
else
'{ }'
))
def
test_no_module_set
(
self
):
def
test_module
(
self
):
recipe
=
makeRecipe
(
self
.
options
[
'module'
]
=
'slapos.promise.plugin.check_site_available'
promise_plugin
.
Recipe
,
self
.
installRecipe
()
options
=
self
.
options
,
self
.
assertOutput
(
"from %s import RunPromise"
%
self
.
options
[
'module'
])
name
=
'plugin'
)
self
.
assertEmptyConfig
()
with
self
.
assertRaises
(
KeyError
):
recipe
.
install
()
def
test_file
(
self
):
self
.
options
[
'file'
]
=
__file__
def
test_default
(
self
):
self
.
installRecipe
()
self
.
options
[
'import'
]
=
'slapos.promise.plugin.check_site_available'
self
.
assertOutput
(
"exec(_(%r))"
%
self
.
options
[
'file'
])
recipe
=
makeRecipe
(
self
.
assertEmptyConfig
()
promise_plugin
.
Recipe
,
options
=
self
.
options
,
def
test_module_and_parameters
(
self
):
name
=
'plugin'
)
self
.
options
[
'module'
]
=
'slapos.promise.plugin.check_site_available'
recipe
.
install
()
self
.
setConfig
()
self
.
installRecipe
()
self
.
assertTrue
(
os
.
path
.
exists
(
self
.
output
))
self
.
assertOutput
(
"from %s import RunPromise"
%
self
.
options
[
'module'
])
self
.
assertConfig
()
def
test_file_and_parameters
(
self
):
self
.
options
[
'file'
]
=
__file__
self
.
setConfig
()
self
.
installRecipe
()
self
.
assertOutput
(
"exec(_(%r))"
%
self
.
options
[
'file'
])
self
.
assertConfig
()
def
test_mode
(
self
):
self
.
options
[
'mode'
]
=
'0644'
self
.
options
[
'module'
]
=
'slapos.promise.plugin.check_site_available'
self
.
installRecipe
()
self
.
assertEqual
(
stat
.
S_IMODE
(
os
.
stat
(
self
.
output
).
st_mode
),
int
(
'644'
,
8
))
self
.
assertEqual
(
stat
.
S_IMODE
(
os
.
stat
(
self
.
output
).
st_mode
),
int
(
'644'
,
8
))
with
open
(
self
.
output
)
as
f
:
content
=
f
.
read
()
self
.
assertIn
(
"from slapos.promise.plugin.check_site_available import RunPromise"
,
content
)
self
.
assertIn
(
'extra_config_dict = %s'
%
(
'{}'
if
six
.
PY3
else
'{ }'
),
content
)
def
test_module_and_class
(
self
):
self
.
options
[
'module'
]
=
m
=
'slapos.promise.plugin.check_site_available'
self
.
options
[
'class'
]
=
'MyPromise'
self
.
installRecipe
()
self
.
assertOutput
(
"from %s import MyPromise as RunPromise"
%
m
)
def
test_file_and_class
(
self
):
self
.
options
[
'file'
]
=
__file__
self
.
options
[
'class'
]
=
'MyPromise'
self
.
installRecipe
()
self
.
assertOutput
(
"exec(_(%r))
\
n
\
n
RunPromise = MyPromise"
%
__file__
)
def
test_no_module_or_file
(
self
):
with
self
.
assertRaises
(
UserError
)
as
p
:
self
.
makeRecipe
()
msg
=
str
(
p
.
exception
)
self
.
assertEqual
(
msg
,
"Either 'module' or 'file' is required but not both"
)
def
test_module_and_file
(
self
):
self
.
options
[
'module'
]
=
'slapos.promise.plugin.check_site_available'
self
.
options
[
'file'
]
=
__file__
self
.
test_no_module_or_file
()
def
test_bad_parameters
(
self
):
def
test_bad_parameters
(
self
):
self
.
options
[
'import'
]
=
'slapos.promise.plugin.check_site_available'
self
.
options
[
'module'
]
=
'slapos.promise.plugin.check_site_available'
self
.
options
[
'config-param1; print "toto"'
]
=
"""#xxxx"
\
n
import os; os.stat(f)"""
self
.
options
.
update
((
self
.
options
[
'config-param2
\
n
@domething'
]
=
'"#$$*PPP
\
n
\
n
p = 2*5; print "result is %s" % p'
(
'config-param1; print "toto"'
,
'#xxxx"
\
n
import os; os.stat(f)'
),
recipe
=
makeRecipe
(
(
'config-param2
\
n
@domething'
,
'"#$$*PPP
\
n
p = 2*5; print "result=%s" % p'
)
promise_plugin
.
Recipe
,
))
options
=
self
.
options
,
self
.
installRecipe
()
name
=
'plugin'
)
self
.
assertOutput
(
recipe
.
install
()
r"""'param1; print "toto"': '#xxxx"\nimport os; os.stat(f)',"""
,
r"""'param2\n@domething': '"#$$*PPP\np = 2*5; print "result=%s" % p'"""
self
.
assertTrue
(
os
.
path
.
exists
(
self
.
output
))
)
with
open
(
self
.
output
)
as
f
:
content
=
f
.
read
()
expected_param1
=
r"""'param1; print "toto"': '#xxxx"\nimport os; os.stat(f)',"""
expected_param2
=
r"""'param2\n@domething': '"#$$*PPP\n\n p = 2*5; print "result is %s" % p'"""
self
.
assertIn
(
expected_param1
,
content
)
self
.
assertIn
(
expected_param2
,
content
)
def
test_bad_module_path
(
self
):
self
.
options
[
'import'
]
=
'slapos.promise.plugin.check_site_available; print "toto"'
recipe
=
makeRecipe
(
promise_plugin
.
Recipe
,
options
=
self
.
options
,
name
=
'plugin'
)
with
self
.
assertRaises
(
ValueError
)
as
p
:
recipe
.
install
()
self
.
assertEqual
(
str
(
p
.
exception
),
"Import path %r is not a valid"
%
self
.
options
[
'import'
])
def
test_bad_content
(
self
):
self
.
options
[
'content'
]
=
'from slapos.plugin.check_site_available import toto; print "toto"'
recipe
=
makeRecipe
(
promise_plugin
.
Recipe
,
options
=
self
.
options
,
name
=
'plugin'
)
with
self
.
assertRaises
(
ValueError
)
as
p
:
recipe
.
install
()
self
.
assertEqual
(
str
(
p
.
exception
),
"Promise content %r is not valid"
%
self
.
options
[
'content'
])
def
test_bad_module
(
self
):
self
.
options
[
'module'
]
=
'slapos.promise.plugin.check_site_available; print "toto"'
with
self
.
assertRaises
(
UserError
)
as
p
:
self
.
makeRecipe
()
self
.
assertEqual
(
str
(
p
.
exception
),
"%r is not a valid module name"
%
self
.
options
[
'module'
])
def
test_bad_file
(
self
):
self
.
options
[
'file'
]
=
'print "toto"'
self
.
installRecipe
()
self
.
assertOutput
(
r"""exec(_('print "toto"'))"""
)
def
test_bad_class
(
self
):
self
.
options
[
'class'
]
=
'MyPromise; print "toto"'
with
self
.
assertRaises
(
UserError
)
as
p
:
self
.
makeRecipe
()
self
.
assertEqual
(
str
(
p
.
exception
),
"%r is not a valid class name"
%
self
.
options
[
'class'
])
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