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
Eric Zheng
slapos.core
Commits
196b6d83
Commit
196b6d83
authored
Aug 08, 2011
by
Łukasz Nowak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cover more cases.
Additionally add required "persistence" to test.
parent
1fa7fbd6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
92 additions
and
21 deletions
+92
-21
slapos/tests/slapformat.py
slapos/tests/slapformat.py
+92
-21
No files found.
slapos/tests/slapformat.py
View file @
196b6d83
...
@@ -7,6 +7,9 @@ import grp
...
@@ -7,6 +7,9 @@ import grp
import
os
import
os
import
pwd
import
pwd
USER_LIST
=
[]
CALL_AND_READ_LIST
=
[]
class
FakeConfig
:
class
FakeConfig
:
pass
pass
...
@@ -18,10 +21,12 @@ class TestLoggerHandler(logging.Handler):
...
@@ -18,10 +21,12 @@ class TestLoggerHandler(logging.Handler):
def
emit
(
self
,
record
):
def
emit
(
self
,
record
):
self
.
bucket
.
append
(
record
.
msg
)
self
.
bucket
.
append
(
record
.
msg
)
call_and_read_list
=
[]
def
fakeCallAndRead
(
argument_list
,
raise_on_error
=
True
):
def
fakeCallAndRead
(
argument_list
,
raise_on_error
=
True
):
global
call_and_read_list
if
'useradd'
in
argument_list
:
call_and_read_list
.
append
(
argument_list
)
global
USER_LIST
USER_LIST
.
append
(
argument_list
[
-
1
])
global
CALL_AND_READ_LIST
CALL_AND_READ_LIST
.
append
(
argument_list
)
return
0
,
'UP'
return
0
,
'UP'
class
LoggableWrapper
:
class
LoggableWrapper
:
...
@@ -44,7 +49,8 @@ class GrpMock:
...
@@ -44,7 +49,8 @@ class GrpMock:
class
PwdMock
:
class
PwdMock
:
@
classmethod
@
classmethod
def
getpwnam
(
self
,
name
):
def
getpwnam
(
self
,
name
):
if
name
in
[
'testuser'
,
'slapsoft'
]:
global
USER_LIST
if
name
in
USER_LIST
:
class
result
:
class
result
:
pw_uid
=
0
pw_uid
=
0
pw_gid
=
0
pw_gid
=
0
...
@@ -102,8 +108,11 @@ class SlapformatMixin(unittest.TestCase):
...
@@ -102,8 +108,11 @@ class SlapformatMixin(unittest.TestCase):
config
.
logger
=
logger
config
.
logger
=
logger
self
.
partition
=
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
self
.
partition
=
slapos
.
format
.
Partition
(
'partition'
,
'/part_path'
,
slapos
.
format
.
User
(
'testuser'
),
[],
None
)
slapos
.
format
.
User
(
'testuser'
),
[],
None
)
global
call_and_read_list
global
CALL_AND_READ_LIST
call_and_read_list
=
[]
global
USER_LIST
CALL_AND_READ_LIST
=
[]
USER_LIST
=
[
'testuser'
]
self
.
real_callAndRead
=
slapos
.
format
.
callAndRead
self
.
real_callAndRead
=
slapos
.
format
.
callAndRead
slapos
.
format
.
callAndRead
=
fakeCallAndRead
slapos
.
format
.
callAndRead
=
fakeCallAndRead
self
.
patchOs
(
logger
)
self
.
patchOs
(
logger
)
...
@@ -114,11 +123,12 @@ class SlapformatMixin(unittest.TestCase):
...
@@ -114,11 +123,12 @@ class SlapformatMixin(unittest.TestCase):
self
.
restoreOs
()
self
.
restoreOs
()
self
.
restoreGrp
()
self
.
restoreGrp
()
self
.
restorePwd
()
self
.
restorePwd
()
global
call_and_read_list
global
CALL_AND_READ_LIST
call_and_read_list
=
[]
global
USER_LIST
CALL_AND_READ_LIST
=
[]
USER_LIST
=
[
'testuser'
]
slapos
.
format
.
callAndRead
=
self
.
real_callAndRead
slapos
.
format
.
callAndRead
=
self
.
real_callAndRead
class
TestComputer
(
SlapformatMixin
):
class
TestComputer
(
SlapformatMixin
):
def
test_getAddress_empty_computer
(
self
):
def
test_getAddress_empty_computer
(
self
):
computer
=
slapos
.
format
.
Computer
(
'computer'
)
computer
=
slapos
.
format
.
Computer
(
'computer'
)
...
@@ -127,6 +137,7 @@ class TestComputer(SlapformatMixin):
...
@@ -127,6 +137,7 @@ class TestComputer(SlapformatMixin):
def
test_construct_empty
(
self
):
def
test_construct_empty
(
self
):
computer
=
slapos
.
format
.
Computer
(
'computer'
)
computer
=
slapos
.
format
.
Computer
(
'computer'
)
computer
.
construct
()
computer
.
construct
()
raise
NotImplementedError
def
test_construct_empty_prepared
(
self
):
def
test_construct_empty_prepared
(
self
):
computer
=
slapos
.
format
.
Computer
(
'computer'
,
computer
=
slapos
.
format
.
Computer
(
'computer'
,
...
@@ -140,6 +151,66 @@ class TestComputer(SlapformatMixin):
...
@@ -140,6 +151,66 @@ class TestComputer(SlapformatMixin):
"chown('/software_root', 0, 0)"
,
"chown('/software_root', 0, 0)"
,
"chmod('/software_root', 493)"
],
"chmod('/software_root', 493)"
],
self
.
test_result
.
bucket
)
self
.
test_result
.
bucket
)
global
CALL_AND_READ_LIST
self
.
assertEqual
([
[
'ip'
,
'addr'
,
'list'
,
'bridge'
],
[
'groupadd'
,
'slapsoft'
],
[
'useradd'
,
'-d'
,
'/software_root'
,
'-g'
,
'slapsoft'
,
'-s'
,
'/bin/false'
,
'slapsoft'
]],
CALL_AND_READ_LIST
)
def
test_construct_empty_prepared_no_alter_user
(
self
):
computer
=
slapos
.
format
.
Computer
(
'computer'
,
bridge
=
slapos
.
format
.
Bridge
(
'bridge'
,
'127.0.0.1/16'
,
'eth0'
))
computer
.
instance_root
=
'/instance_root'
computer
.
software_root
=
'/software_root'
computer
.
construct
(
alter_user
=
False
)
self
.
assertEqual
([
"makedirs('/instance_root', 493)"
,
"makedirs('/software_root', 493)"
,
"chmod('/software_root', 493)"
],
self
.
test_result
.
bucket
)
global
CALL_AND_READ_LIST
self
.
assertEqual
([
[
'ip'
,
'addr'
,
'list'
,
'bridge'
],],
CALL_AND_READ_LIST
)
def
test_construct_empty_prepared_no_alter_network
(
self
):
computer
=
slapos
.
format
.
Computer
(
'computer'
,
bridge
=
slapos
.
format
.
Bridge
(
'bridge'
,
'127.0.0.1/16'
,
'eth0'
))
computer
.
instance_root
=
'/instance_root'
computer
.
software_root
=
'/software_root'
computer
.
construct
(
alter_network
=
False
)
self
.
assertEqual
([
"makedirs('/instance_root', 493)"
,
"makedirs('/software_root', 493)"
,
"chown('/software_root', 0, 0)"
,
"chmod('/software_root', 493)"
],
self
.
test_result
.
bucket
)
global
CALL_AND_READ_LIST
self
.
assertEqual
([
[
'ip'
,
'addr'
,
'list'
,
'bridge'
],
[
'groupadd'
,
'slapsoft'
],
[
'useradd'
,
'-d'
,
'/software_root'
,
'-g'
,
'slapsoft'
,
'-s'
,
'/bin/false'
,
'slapsoft'
]],
CALL_AND_READ_LIST
)
def
test_construct_empty_prepared_no_alter_network_user
(
self
):
computer
=
slapos
.
format
.
Computer
(
'computer'
,
bridge
=
slapos
.
format
.
Bridge
(
'bridge'
,
'127.0.0.1/16'
,
'eth0'
))
computer
.
instance_root
=
'/instance_root'
computer
.
software_root
=
'/software_root'
computer
.
construct
(
alter_network
=
False
,
alter_user
=
False
)
self
.
assertEqual
([
"makedirs('/instance_root', 493)"
,
"makedirs('/software_root', 493)"
,
"chmod('/software_root', 493)"
],
self
.
test_result
.
bucket
)
global
CALL_AND_READ_LIST
self
.
assertEqual
([
[
'ip'
,
'addr'
,
'list'
,
'bridge'
],
],
CALL_AND_READ_LIST
)
class
TestPartition
(
SlapformatMixin
):
class
TestPartition
(
SlapformatMixin
):
...
@@ -166,7 +237,7 @@ class TestPartition(SlapformatMixin):
...
@@ -166,7 +237,7 @@ class TestPartition(SlapformatMixin):
class
TestUser
(
SlapformatMixin
):
class
TestUser
(
SlapformatMixin
):
def
test_create
(
self
):
def
test_create
(
self
):
global
call_and_read_list
global
CALL_AND_READ_LIST
user
=
slapos
.
format
.
User
(
'doesnotexistsyet'
)
user
=
slapos
.
format
.
User
(
'doesnotexistsyet'
)
user
.
setPath
(
'/doesnotexistsyet'
)
user
.
setPath
(
'/doesnotexistsyet'
)
user
.
create
()
user
.
create
()
...
@@ -177,10 +248,10 @@ class TestUser(SlapformatMixin):
...
@@ -177,10 +248,10 @@ class TestUser(SlapformatMixin):
[
'useradd'
,
'-d'
,
'/doesnotexistsyet'
,
'-g'
,
'doesnotexistsyet'
,
'-s'
,
[
'useradd'
,
'-d'
,
'/doesnotexistsyet'
,
'-g'
,
'doesnotexistsyet'
,
'-s'
,
'/bin/false'
,
'doesnotexistsyet'
]
'/bin/false'
,
'doesnotexistsyet'
]
],
],
call_and_read_list
)
CALL_AND_READ_LIST
)
def
test_create_additional_groups
(
self
):
def
test_create_additional_groups
(
self
):
global
call_and_read_list
global
CALL_AND_READ_LIST
user
=
slapos
.
format
.
User
(
'doesnotexistsyet'
,
[
'additionalgroup1'
,
user
=
slapos
.
format
.
User
(
'doesnotexistsyet'
,
[
'additionalgroup1'
,
'additionalgroup2'
])
'additionalgroup2'
])
user
.
setPath
(
'/doesnotexistsyet'
)
user
.
setPath
(
'/doesnotexistsyet'
)
...
@@ -193,11 +264,11 @@ class TestUser(SlapformatMixin):
...
@@ -193,11 +264,11 @@ class TestUser(SlapformatMixin):
'/bin/false'
,
'-G'
,
'additionalgroup1,additionalgroup2'
,
'/bin/false'
,
'-G'
,
'additionalgroup1,additionalgroup2'
,
'doesnotexistsyet'
]
'doesnotexistsyet'
]
],
],
call_and_read_list
)
CALL_AND_READ_LIST
)
def
test_create_group_exists
(
self
):
def
test_create_group_exists
(
self
):
pwd
.
getpwnam
=
self
.
raisingKeyError
pwd
.
getpwnam
=
self
.
raisingKeyError
global
call_and_read_list
global
CALL_AND_READ_LIST
user
=
slapos
.
format
.
User
(
'testuser'
)
user
=
slapos
.
format
.
User
(
'testuser'
)
user
.
setPath
(
'/testuser'
)
user
.
setPath
(
'/testuser'
)
...
@@ -208,11 +279,11 @@ class TestUser(SlapformatMixin):
...
@@ -208,11 +279,11 @@ class TestUser(SlapformatMixin):
[
'useradd'
,
'-d'
,
'/testuser'
,
'-g'
,
'testuser'
,
'-s'
,
'/bin/false'
,
[
'useradd'
,
'-d'
,
'/testuser'
,
'-g'
,
'testuser'
,
'-s'
,
'/bin/false'
,
'testuser'
]
'testuser'
]
],
],
call_and_read_list
)
CALL_AND_READ_LIST
)
def
test_create_user_exists_additional_groups
(
self
):
def
test_create_user_exists_additional_groups
(
self
):
grp
.
getgrnam
=
self
.
raisingKeyError
grp
.
getgrnam
=
self
.
raisingKeyError
global
call_and_read_list
global
CALL_AND_READ_LIST
user
=
slapos
.
format
.
User
(
'testuser'
,
[
'additionalgroup1'
,
user
=
slapos
.
format
.
User
(
'testuser'
,
[
'additionalgroup1'
,
'additionalgroup2'
])
'additionalgroup2'
])
user
.
setPath
(
'/testuser'
)
user
.
setPath
(
'/testuser'
)
...
@@ -224,11 +295,11 @@ class TestUser(SlapformatMixin):
...
@@ -224,11 +295,11 @@ class TestUser(SlapformatMixin):
[
'usermod'
,
'-d'
,
'/testuser'
,
'-g'
,
'testuser'
,
'-s'
,
'/bin/false'
,
[
'usermod'
,
'-d'
,
'/testuser'
,
'-g'
,
'testuser'
,
'-s'
,
'/bin/false'
,
'-G'
,
'additionalgroup1,additionalgroup2'
,
'testuser'
]
'-G'
,
'additionalgroup1,additionalgroup2'
,
'testuser'
]
],
],
call_and_read_list
)
CALL_AND_READ_LIST
)
def
test_create_user_exists
(
self
):
def
test_create_user_exists
(
self
):
grp
.
getgrnam
=
self
.
raisingKeyError
grp
.
getgrnam
=
self
.
raisingKeyError
global
call_and_read_list
global
CALL_AND_READ_LIST
user
=
slapos
.
format
.
User
(
'testuser'
)
user
=
slapos
.
format
.
User
(
'testuser'
)
user
.
setPath
(
'/testuser'
)
user
.
setPath
(
'/testuser'
)
user
.
create
()
user
.
create
()
...
@@ -239,10 +310,10 @@ class TestUser(SlapformatMixin):
...
@@ -239,10 +310,10 @@ class TestUser(SlapformatMixin):
[
'usermod'
,
'-d'
,
'/testuser'
,
'-g'
,
'testuser'
,
'-s'
,
'/bin/false'
,
[
'usermod'
,
'-d'
,
'/testuser'
,
'-g'
,
'testuser'
,
'-s'
,
'/bin/false'
,
'testuser'
]
'testuser'
]
],
],
call_and_read_list
)
CALL_AND_READ_LIST
)
def
test_create_user_group_exists
(
self
):
def
test_create_user_group_exists
(
self
):
global
call_and_read_list
global
CALL_AND_READ_LIST
user
=
slapos
.
format
.
User
(
'testuser'
)
user
=
slapos
.
format
.
User
(
'testuser'
)
user
.
setPath
(
'/testuser'
)
user
.
setPath
(
'/testuser'
)
user
.
create
()
user
.
create
()
...
@@ -252,7 +323,7 @@ class TestUser(SlapformatMixin):
...
@@ -252,7 +323,7 @@ class TestUser(SlapformatMixin):
[
'usermod'
,
'-d'
,
'/testuser'
,
'-g'
,
'testuser'
,
'-s'
,
'/bin/false'
,
[
'usermod'
,
'-d'
,
'/testuser'
,
'-g'
,
'testuser'
,
'-s'
,
'/bin/false'
,
'testuser'
]
'testuser'
]
],
],
call_and_read_list
)
CALL_AND_READ_LIST
)
def
test_isAvailable
(
self
):
def
test_isAvailable
(
self
):
user
=
slapos
.
format
.
User
(
'testuser'
)
user
=
slapos
.
format
.
User
(
'testuser'
)
...
...
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