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
Boxiang Sun
slapos.core
Commits
35d7014b
Commit
35d7014b
authored
Jul 15, 2022
by
Thomas Gambier
🚴🏼
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
register: fix slapconfig in python3
Also add test for 'slapos register'
parent
e82e8361
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
104 additions
and
13 deletions
+104
-13
slapos/cli/register.py
slapos/cli/register.py
+1
-1
slapos/tests/test_register.py
slapos/tests/test_register.py
+103
-12
No files found.
slapos/cli/register.py
View file @
35d7014b
...
...
@@ -282,7 +282,7 @@ def slapconfig(conf):
cfg
=
re
.
sub
(
'
\
n
\
\
s*%s
\
\
s*=.*'
%
key
,
'
\
n
%s = %s'
%
(
key
,
value
),
cfg
)
if
not
dry_run
:
with
open
(
config_path
,
'w'
)
as
fout
:
with
open
(
config_path
,
'w
b
'
)
as
fout
:
fout
.
write
(
cfg
.
encode
(
'utf8'
))
conf
.
logger
.
info
(
'SlapOS configuration written to %s'
,
config_path
)
...
...
slapos/tests/test_register.py
View file @
35d7014b
...
...
@@ -25,8 +25,20 @@
#
##############################################################################
import
httmock
import
mock
import
os
import
random
import
shutil
import
string
import
tempfile
import
unittest
import
slapos.cli.register
from
argparse
import
Namespace
from
six.moves.urllib
import
parse
import
slapos.slap
from
slapos.cli.register
import
RegisterCommand
,
RegisterConfig
,
fetch_configuration_template
,
do_register
from
slapos.cli.entry
import
SlapOSApp
class
TestRegister
(
unittest
.
TestCase
):
""" Tests for slapos.cli.register
...
...
@@ -34,10 +46,55 @@ class TestRegister(unittest.TestCase):
XXX There is a lack of tests for register, so include more.
"""
def
setUp
(
self
):
self
.
slap
=
slapos
.
slap
.
slap
()
self
.
app
=
SlapOSApp
()
self
.
temp_dir
=
tempfile
.
mkdtemp
()
self
.
computer_id
=
'COMP-%s'
%
random
.
randrange
(
10000
)
self
.
certificate
=
'CN=%s/emailAddress=admin@vifib.org'
%
self
.
computer_id
self
.
key
=
'key_test_%s'
%
''
.
join
(
random
.
choice
(
string
.
ascii_lowercase
)
for
i
in
range
(
64
))
self
.
default_args
=
[
'test-computer'
,
'--token'
,
'token-test'
]
def
tearDown
(
self
):
if
os
.
path
.
exists
(
self
.
temp_dir
):
shutil
.
rmtree
(
self
.
temp_dir
)
def
request_handler
(
self
,
url
,
req
):
"""
Define _callback.
Will register global sequence of message, sequence by partition
and error and error message by partition
"""
#self.sequence.append(url.path)
if
req
.
method
==
'GET'
:
qs
=
parse
.
parse_qs
(
url
.
query
)
else
:
qs
=
parse
.
parse_qs
(
req
.
body
)
print
(
"DEBUG THOMAS"
)
print
(
url
)
print
(
req
)
if
url
.
path
==
'/Person_requestComputer'
:
return
{
'status_code'
:
200
,
'content'
:
{
'certificate'
:
self
.
certificate
,
'key'
:
self
.
key
}
}
def
getConf
(
self
,
args
):
cmd
=
RegisterCommand
(
self
.
app
,
Namespace
())
cmd_parser
=
cmd
.
get_parser
(
"slapos node register for test"
)
parsed_args
=
cmd_parser
.
parse_args
(
args
)
conf
=
RegisterConfig
(
logger
=
self
.
app
.
log
)
conf
.
setConfig
(
parsed_args
)
return
conf
def
test_fetch_configuration
(
self
):
""" Simple test to Fetch the configuration template
"""
template
=
slapos
.
cli
.
register
.
fetch_configuration_template
()
template
=
fetch_configuration_template
()
self
.
assertNotEqual
(
""
,
template
)
for
entry
in
[
'computer_id'
,
...
...
@@ -50,3 +107,37 @@ class TestRegister(unittest.TestCase):
'partition_amount'
,
'create_tap'
]:
self
.
assertTrue
(
entry
in
template
,
"%s is not in template (%s)"
%
(
entry
,
template
))
def
test_default_token_rejected
(
self
):
""" Make sure that the token for test if rejected by default
"""
conf
=
self
.
getConf
(
self
.
default_args
)
with
self
.
assertRaises
(
SystemExit
)
as
cm
:
do_register
(
conf
)
self
.
assertEqual
(
cm
.
exception
.
code
,
1
)
def
test_write_configuration
(
self
):
""" Simple test to see if we can write the configuration file
"""
conf
=
self
.
getConf
(
self
.
default_args
)
# we manually set the parameters below because we mock the function COMPConfig
# indeed, we don't want to put the config file in '/etc/opt/slapos'
conf
.
slapos_configuration
=
self
.
temp_dir
conf
.
computer_id
=
self
.
computer_id
conf
.
certificate
=
self
.
certificate
conf
.
key
=
self
.
key
with
httmock
.
HTTMock
(
self
.
request_handler
),
\
mock
.
patch
.
object
(
RegisterConfig
,
'COMPConfig'
)
as
COMPConfigMock
,
\
mock
.
patch
(
'slapos.cli.register.save_former_config'
)
as
save_former_config_mock
:
return_code
=
do_register
(
conf
)
COMPConfigMock
.
assert_called_with
(
slapos_configuration
=
'/etc/opt/slapos/'
,
computer_id
=
self
.
computer_id
,
certificate
=
self
.
certificate
,
key
=
self
.
key
)
save_former_config_mock
.
assert_called
()
self
.
assertEquals
(
0
,
return_code
)
self
.
assertTrue
(
os
.
path
.
exists
(
'%s/slapos.cfg'
%
self
.
temp_dir
))
config_content
=
open
(
'%s/slapos.cfg'
%
self
.
temp_dir
).
read
()
self
.
assertIn
(
'computer_id = %s'
%
self
.
computer_id
,
config_content
)
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