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
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
Nicolas Wavrant
slapos.core
Commits
6c493099
Commit
6c493099
authored
Aug 08, 2011
by
Łukasz Nowak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mock required classes in test.
parent
7d480307
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
53 deletions
+94
-53
slapos/tests/slapformat.py
slapos/tests/slapformat.py
+94
-53
No files found.
slapos/tests/slapformat.py
View file @
6c493099
...
...
@@ -2,6 +2,11 @@ import logging
import
slapos.format
import
unittest
# for mocking
import
grp
import
os
import
pwd
class
FakeConfig
:
pass
...
...
@@ -18,18 +23,76 @@ def fakeCallAndRead(argument_list, raise_on_error=True):
global
call_and_read_list
call_and_read_list
.
append
(
argument_list
)
def
raising_KeyError
(
name
):
raise
KeyError
class
LoggableWrapper
:
def
__init__
(
self
,
logger
,
name
):
self
.
__logger
=
logger
self
.
__name
=
name
def
__call__
(
self
,
*
args
,
**
kwargs
):
arg_list
=
[
repr
(
x
)
for
x
in
args
]
+
[
'%s=%r'
%
(
x
,
y
)
for
x
,
y
in
kwargs
.
iteritems
()]
self
.
__logger
.
debug
(
'%s(%s)'
%
(
self
.
__name
,
', '
.
join
(
arg_list
)))
class
GrpMock
:
@
classmethod
def
getgrnam
(
self
,
name
):
if
name
==
'testuser'
:
return
True
raise
KeyError
class
PwdMock
:
@
classmethod
def
getpwnam
(
self
,
name
):
if
name
==
'testuser'
:
class
result
:
pw_uid
=
0
pw_gid
=
0
return
result
raise
KeyError
class
SlapformatMixin
(
unittest
.
TestCase
):
def
returning_True
(
name
):
return
True
class
FakeClass
:
pass
@
classmethod
def
raisingKeyError
(
self
,
*
args
,
**
kwargs
):
raise
KeyError
def
patchPwd
(
self
):
self
.
saved_pwd
=
dict
()
for
fake
in
vars
(
PwdMock
):
self
.
saved_pwd
[
fake
]
=
getattr
(
pwd
,
fake
,
None
)
setattr
(
pwd
,
fake
,
getattr
(
PwdMock
,
fake
))
def
restorePwd
(
self
):
for
name
,
original_value
in
self
.
saved_pwd
.
items
():
setattr
(
pwd
,
name
,
original_value
)
del
self
.
saved_pwd
def
patchGrp
(
self
):
self
.
saved_grp
=
dict
()
for
fake
in
vars
(
GrpMock
):
self
.
saved_grp
[
fake
]
=
getattr
(
grp
,
fake
,
None
)
setattr
(
grp
,
fake
,
getattr
(
GrpMock
,
fake
))
def
restoreGrp
(
self
):
for
name
,
original_value
in
self
.
saved_grp
.
items
():
setattr
(
grp
,
name
,
original_value
)
del
self
.
saved_grp
def
patchOs
(
self
,
logger
):
self
.
saved_os
=
dict
()
for
fake
in
[
'mkdir'
,
'chown'
,
'chmod'
,
'makedirs'
]:
self
.
saved_os
[
fake
]
=
getattr
(
os
,
fake
,
None
)
f
=
LoggableWrapper
(
logger
,
fake
)
setattr
(
os
,
fake
,
f
)
def
restoreOs
(
self
):
for
name
,
original_value
in
self
.
saved_os
.
items
():
setattr
(
os
,
name
,
original_value
)
del
self
.
saved_os
class
SlapformatMixin
(
unittest
.
TestCase
):
def
setUp
(
self
):
config
=
FakeConfig
()
config
.
dry_run
=
Tru
e
config
.
dry_run
=
Fals
e
config
.
verbose
=
True
logger
=
logging
.
getLogger
(
'testcatch'
)
logger
.
setLevel
(
logging
.
DEBUG
)
...
...
@@ -37,14 +100,19 @@ class SlapformatMixin(unittest.TestCase):
logger
.
addHandler
(
self
.
test_result
)
config
.
logger
=
logger
self
.
partition
=
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'root'
),
[],
None
)
self
.
partition
.
_os
=
slapos
.
format
.
OS
(
config
)
slapos
.
format
.
User
(
'testuser'
),
[],
None
)
global
call_and_read_list
call_and_read_list
=
[]
self
.
real_callAndRead
=
slapos
.
format
.
callAndRead
slapos
.
format
.
callAndRead
=
fakeCallAndRead
self
.
patchOs
(
logger
)
self
.
patchGrp
()
self
.
patchPwd
()
def
tearDown
(
self
):
self
.
restoreOs
()
self
.
restoreGrp
()
self
.
restorePwd
()
global
call_and_read_list
call_and_read_list
=
[]
slapos
.
format
.
callAndRead
=
self
.
real_callAndRead
...
...
@@ -85,48 +153,39 @@ class TestPartition(SlapformatMixin):
class
TestUser
(
SlapformatMixin
):
def
test_create
(
self
):
global
call_and_read_list
user
=
slapos
.
format
.
User
(
'testuser'
)
user
.
_getpwnam
=
raising_KeyError
grp
=
FakeClass
()
grp
.
getgrnam
=
raising_KeyError
user
.
_grp
=
grp
user
.
setPath
(
'/testuser'
)
user
=
slapos
.
format
.
User
(
'doesnotexistsyet'
)
user
.
setPath
(
'/doesnotexistsyet'
)
user
.
create
()
self
.
assertEqual
(
[
[
'groupadd'
,
'
testuser
'
],
[
'useradd'
,
'-d'
,
'/
testuser'
,
'-g'
,
'testuser'
,
'-s'
,
'/bin/false
'
,
'
testuser
'
]
[
'groupadd'
,
'
doesnotexistsyet
'
],
[
'useradd'
,
'-d'
,
'/
doesnotexistsyet'
,
'-g'
,
'doesnotexistsyet'
,
'-s
'
,
'
/bin/false'
,
'doesnotexistsyet
'
]
],
call_and_read_list
)
def
test_create_additional_groups
(
self
):
global
call_and_read_list
user
=
slapos
.
format
.
User
(
'
testuser
'
,
[
'additionalgroup1'
,
user
=
slapos
.
format
.
User
(
'
doesnotexistsyet
'
,
[
'additionalgroup1'
,
'additionalgroup2'
])
user
.
_getpwnam
=
raising_KeyError
grp
=
FakeClass
()
grp
.
getgrnam
=
raising_KeyError
user
.
_grp
=
grp
user
.
setPath
(
'/testuser'
)
user
.
setPath
(
'/doesnotexistsyet'
)
user
.
create
()
self
.
assertEqual
(
[
[
'groupadd'
,
'testuser'
],
[
'useradd'
,
'-d'
,
'/testuser'
,
'-g'
,
'testuser'
,
'-s'
,
'/bin/false'
,
'-G'
,
'additionalgroup1,additionalgroup2'
,
'testuser'
]
[
'groupadd'
,
'doesnotexistsyet'
],
[
'useradd'
,
'-d'
,
'/doesnotexistsyet'
,
'-g'
,
'doesnotexistsyet'
,
'-s'
,
'/bin/false'
,
'-G'
,
'additionalgroup1,additionalgroup2'
,
'doesnotexistsyet'
]
],
call_and_read_list
)
def
test_create_group_exists
(
self
):
pwd
.
getpwnam
=
self
.
raisingKeyError
global
call_and_read_list
user
=
slapos
.
format
.
User
(
'testuser'
)
user
.
_getpwnam
=
raising_KeyError
grp
=
FakeClass
()
grp
.
getgrnam
=
returning_True
user
.
_grp
=
grp
user
.
setPath
(
'/testuser'
)
user
.
create
()
...
...
@@ -138,13 +197,10 @@ class TestUser(SlapformatMixin):
call_and_read_list
)
def
test_create_user_exists_additional_groups
(
self
):
grp
.
getgrnam
=
self
.
raisingKeyError
global
call_and_read_list
user
=
slapos
.
format
.
User
(
'testuser'
,
[
'additionalgroup1'
,
'additionalgroup2'
])
user
.
_getpwnam
=
returning_True
grp
=
FakeClass
()
grp
.
getgrnam
=
raising_KeyError
user
.
_grp
=
grp
user
.
setPath
(
'/testuser'
)
user
.
create
()
...
...
@@ -157,12 +213,9 @@ class TestUser(SlapformatMixin):
call_and_read_list
)
def
test_create_user_exists
(
self
):
grp
.
getgrnam
=
self
.
raisingKeyError
global
call_and_read_list
user
=
slapos
.
format
.
User
(
'testuser'
)
user
.
_getpwnam
=
returning_True
grp
=
FakeClass
()
grp
.
getgrnam
=
raising_KeyError
user
.
_grp
=
grp
user
.
setPath
(
'/testuser'
)
user
.
create
()
...
...
@@ -177,10 +230,6 @@ class TestUser(SlapformatMixin):
def
test_create_user_group_exists
(
self
):
global
call_and_read_list
user
=
slapos
.
format
.
User
(
'testuser'
)
user
.
_getpwnam
=
returning_True
grp
=
FakeClass
()
grp
.
getgrnam
=
returning_True
user
.
_grp
=
grp
user
.
setPath
(
'/testuser'
)
user
.
create
()
...
...
@@ -193,18 +242,10 @@ class TestUser(SlapformatMixin):
def
test_isAvailable
(
self
):
user
=
slapos
.
format
.
User
(
'testuser'
)
user
.
_getpwnam
=
returning_True
grp
=
FakeClass
()
grp
.
getgrnam
=
returning_True
user
.
_grp
=
grp
self
.
assertTrue
(
user
.
isAvailable
())
def
test_isAvailable_notAvailable
(
self
):
user
=
slapos
.
format
.
User
(
'testuser'
)
user
.
_getpwnam
=
raising_KeyError
grp
=
FakeClass
()
grp
.
getgrnam
=
raising_KeyError
user
.
_grp
=
grp
user
=
slapos
.
format
.
User
(
'doesnotexistsyet'
)
self
.
assertFalse
(
user
.
isAvailable
())
if
__name__
==
'__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