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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
slapos
Commits
a1d7a046
Commit
a1d7a046
authored
Sep 27, 2016
by
Nicolas Wavrant
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test_free_port: monkey patching an object shouldn't impact other class/test/module/...
parent
a2b2ef7e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
4 deletions
+16
-4
setup.py
setup.py
+4
-1
slapos/test/recipe/test_free_port.py
slapos/test/recipe/test_free_port.py
+12
-3
No files found.
setup.py
View file @
a1d7a046
...
...
@@ -214,5 +214,8 @@ setup(name=name,
],
},
test_suite
=
'slapos.test'
,
tests_require
=
[
'jsonschema'
],
tests_require
=
[
'jsonschema'
,
'mock'
,
],
)
slapos/test/recipe/test_free_port.py
View file @
a1d7a046
import
socket
import
sys
import
unittest
from
mock
import
patch
from
slapos.recipe
import
free_port
class
SocketMock
():
...
...
@@ -14,11 +17,14 @@ class SocketMock():
bind
=
close
=
nothing_happen
import
sys
sys
.
modules
[
'socket'
].
socket
=
SocketMock
def
useMock
(
function
):
def
withMock
(
function
):
with
patch
(
'slapos.recipe.free_port.socket.socket'
,
new
=
SocketMock
):
return
function
return
withMock
class
FreePortTest
(
unittest
.
TestCase
):
def
afterSetu
p
(
self
):
def
setU
p
(
self
):
SocketMock
.
bind
=
SocketMock
.
close
=
SocketMock
.
nothing_happen
def
new_recipe
(
self
,
**
kw
):
...
...
@@ -48,10 +54,12 @@ class FreePortTest(unittest.TestCase):
options
.
update
(
kw
)
return
free_port
.
Recipe
(
buildout
=
buildout
,
name
=
'free_port'
,
options
=
options
)
@
useMock
def
test_ifNoBusyPortThenMinPortIsAlwaysReturned
(
self
):
recipe
=
self
.
new_recipe
(
minimum
=
2000
)
self
.
assertEqual
(
recipe
.
options
[
'port'
],
'2000'
)
@
useMock
def
test_iterateUntilFreePortIsFound
(
self
):
def
bindFailExceptOnPort2020
(
socket_instance
,
binding
):
ip
,
port
=
binding
...
...
@@ -61,6 +69,7 @@ class FreePortTest(unittest.TestCase):
recipe
=
self
.
new_recipe
(
minimum
=
2000
)
self
.
assertEqual
(
recipe
.
options
[
'port'
],
'2020'
)
@
useMock
def
test_returnsPort0IfNoPortIsFreeInRange
(
self
):
def
bindAlwaysFail
(
socket_instance
,
binding
):
raise
socket
.
error
()
...
...
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