Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
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
5
Merge Requests
5
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jérome Perrin
slapos
Commits
b4ef089e
Commit
b4ef089e
authored
Apr 10, 2018
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test f
parent
d9feb41c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
97 additions
and
50 deletions
+97
-50
software/proftpd/test/test.py
software/proftpd/test/test.py
+78
-43
software/proftpd/test/utils.py
software/proftpd/test/utils.py
+19
-7
No files found.
software/proftpd/test/test.py
View file @
b4ef089e
...
@@ -25,8 +25,13 @@
...
@@ -25,8 +25,13 @@
#
#
##############################################################################
##############################################################################
import
pysftp
import
os
import
os
import
shutil
import
urlparse
import
tempfile
import
pysftp
import
utils
import
utils
def
setUpModule
():
def
setUpModule
():
...
@@ -36,62 +41,75 @@ def tearDownModule():
...
@@ -36,62 +41,75 @@ def tearDownModule():
utils
.
tearDownModule
()
utils
.
tearDownModule
()
import
unittest
# for development: log a lot and install Ctrl+C handler
unittest
.
installHandler
()
if
1
:
import
logging
import
logging
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
import
unittest
unittest
.
installHandler
()
logging
.
basicConfig
(
level
=
logging
.
DEBUG
)
import
urlparse
import
pysftp
import
tempfile
software_url
=
os
.
path
.
abspath
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'..'
,
'software.cfg'
))
class
ProFTPdTestCase
(
utils
.
SlapOSInstanceTestCase
):
software_url_list
=
(
os
.
path
.
abspath
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'..'
,
'software.cfg'
)),
)
class
TestSFTPFeature
(
utils
.
SlapOSInstanceTestCas
e
):
def
_getConnection
(
self
,
username
=
None
,
password
=
None
,
hostname
=
Non
e
):
software_url_list
=
(
software_url
,
)
"""Returns a pysftp connection connected to the SFTP
def
test_simple_sftp_session
(
self
):
username and password can be specified and default to the ones from
# we will not verify host key
instance connection parameters.
another hostname can also be passed.
"""
# this will not verify host key
cnopts
=
pysftp
.
CnOpts
()
cnopts
=
pysftp
.
CnOpts
()
cnopts
.
hostkeys
=
None
cnopts
.
hostkeys
=
None
parameter_dict
=
self
.
computer_partition
.
getConnectionParameterDict
()
parameter_dict
=
self
.
computer_partition
.
getConnectionParameterDict
()
sftp_url
=
urlparse
.
urlparse
(
parameter_dict
[
'url'
])
sftp_url
=
urlparse
.
urlparse
(
parameter_dict
[
'url'
])
print
parameter_dict
with
pysftp
.
Connection
(
return
pysftp
.
Connection
(
sftp_url
.
hostname
,
hostname
or
sftp_url
.
hostname
,
port
=
sftp_url
.
port
,
port
=
sftp_url
.
port
,
cnopts
=
cnopts
,
cnopts
=
cnopts
,
username
=
parameter_dict
[
'user'
],
username
=
username
or
parameter_dict
[
'user'
],
# XXX
password
=
parameter_dict
[
'password'
])
as
sftp
:
password
=
password
or
parameter_dict
[
'password'
])
class
TestSFTPFeature
(
ProFTPdTestCase
):
"""Tests the features we expect in SFTP server.
"""
def
test_simple_sftp_session
(
self
):
with
self
.
_getConnection
()
as
sftp
:
# put a file
with
tempfile
.
NamedTemporaryFile
()
as
f
:
with
tempfile
.
NamedTemporaryFile
()
as
f
:
f
.
write
(
"Hello FTP !"
)
f
.
write
(
"Hello FTP !"
)
f
.
close
()
f
.
flush
()
sftp
.
put
(
f
.
name
)
sftp
.
put
(
f
.
name
,
remotepath
=
'testfile'
)
retrieved
=
sftp
.
get
(
f
.
name
)
self
.
assertEqual
(
retrieved
,
"Hello FTP :)"
)
print
self
.
id
()
# it's visible in listdir()
import
pdb
;
pdb
.
set_trace
()
self
.
assertEqual
([
'testfile'
],
sftp
.
listdir
())
# download it, it should have same content
tempdir
=
tempfile
.
mkdtemp
()
self
.
addCleanup
(
lambda
:
shutil
.
rmtree
(
tempdir
))
local_file
=
os
.
path
.
join
(
tempdir
,
'testfile'
)
retrieve_same_file
=
sftp
.
get
(
'testfile'
,
local_file
)
with
open
(
local_file
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
"Hello FTP !"
)
def
test_connect_on_ipv4
(
self
):
def
test_connect_on_ipv4
(
self
):
pass
self
.
assertEqual
(
[],
self
.
_getConnection
(
hostname
=
self
.
config
[
'ipv4_address'
]).
listdir
())
def
test_proftpd_does_not_listen_on_all_ip
(
self
):
def
test_proftpd_does_not_listen_on_all_ip
(
self
):
parameter_dict
=
self
.
computer_partition
.
getConnectionParameterDict
()
from
paramiko.ssh_exception
import
SSHException
sftp_url
=
urlparse
.
urlparse
(
parameter_dict
[
'url'
])
with
self
.
assertRaises
(
SSHException
):
cnopts
=
pysftp
.
CnOpts
()
self
.
_getConnection
(
hostname
=
'127.0.0.1'
)
cnopts
.
hostkeys
=
None
import
pdb
;
pdb
.
set_trace
()
with
self
.
assertRaises
(
AttributeError
):
pysftp
.
Connection
(
'0.0.0.0'
,
port
=
sftp_url
.
port
,
cnopts
=
cnopts
,
username
=
parameter_dict
[
'user'
],
password
=
parameter_dict
[
'password'
])
def
test_uploaded_file_not_visible_until_fully_uploaded
(
self
):
def
test_uploaded_file_not_visible_until_fully_uploaded
(
self
):
pass
pass
...
@@ -99,19 +117,36 @@ class TestSFTPFeature(utils.SlapOSInstanceTestCase):
...
@@ -99,19 +117,36 @@ class TestSFTPFeature(utils.SlapOSInstanceTestCase):
def
test_partial_upload_are_deleted
(
self
):
def
test_partial_upload_are_deleted
(
self
):
pass
pass
def
test_client_are_banned_after_5_wrong_passwords
(
self
):
pass
def
test_user_can_be_added
(
self
):
def
test_user_can_be_added
(
self
):
pass
pass
class
TestPortParameter
(
utils
.
SlapOSInstanceTestCase
):
class
TestBan
(
ProFTPdTestCase
):
software_url_list
=
(
software_url
,
)
def
test_client_are_banned_after_5_wrong_passwords
(
self
):
# Simulate 5 login errors
from
paramiko.ssh_exception
import
AuthenticationException
for
i
in
range
(
5
):
with
self
.
assertRaisesRegexp
(
AuthenticationException
,
'Authentication failed'
):
self
.
_getConnection
(
password
=
'wrong'
)
# after that, even with a valid password we cannot connect
from
paramiko.ssh_exception
import
SSHException
with
self
.
assertRaisesRegexp
(
SSHException
,
'Connection reset by peer'
):
self
.
_getConnection
()
# ban event is logged
with
open
(
os
.
path
.
join
(
self
.
computer_partition_root_path
,
'var'
,
'log'
,
'proftpd-ban.log'
))
as
ban_log_file
:
self
.
assertRegexpMatches
(
ban_log_file
.
readlines
()[
-
1
],
'login from host .* denied due to host bane'
)
class
TestPortParameter
(
ProFTPdTestCase
):
instance_parameter_dict
=
{
instance_parameter_dict
=
{
'port'
:
10022
'port'
:
10022
}
}
def
test_c
(
self
):
#
def test_c(self):
print
self
.
id
()
#
print self.id()
software/proftpd/test/utils.py
View file @
b4ef089e
...
@@ -38,7 +38,7 @@ global slapos_controler
...
@@ -38,7 +38,7 @@ global slapos_controler
global
config
global
config
def
setUpModule
():
def
setUpModule
():
#
XXX
read from environ
#
TODO
read from environ
# XXX beware of Error: Cannot open an HTTP server: socket.error reported AF_UNIX path too long
# XXX beware of Error: Cannot open an HTTP server: socket.error reported AF_UNIX path too long
working_directory
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'.slapos'
)
working_directory
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'.slapos'
)
working_directory
=
'/tmp/slapotest/'
working_directory
=
'/tmp/slapotest/'
...
@@ -84,9 +84,11 @@ class SlapOSInstanceTestCase(unittest.TestCase):
...
@@ -84,9 +84,11 @@ class SlapOSInstanceTestCase(unittest.TestCase):
software_url_list
=
(
NotImplemented
,
)
software_url_list
=
(
NotImplemented
,
)
instance_parameter_dict
=
{}
instance_parameter_dict
=
{}
# This is set to the path of the instance, to inspect created files.
instance_root_path
=
None
@
classmethod
@
classmethod
def
setUpClass
(
cls
):
def
setUpClass
(
cls
):
print
"Setting up"
,
cls
,
cls
.
software_url_list
slapproxy_log
=
os
.
path
.
join
(
config
[
'log_directory'
],
'slapproxy.log'
)
slapproxy_log
=
os
.
path
.
join
(
config
[
'log_directory'
],
'slapproxy.log'
)
logger
.
debug
(
'Configured slapproxy log to %r'
,
slapproxy_log
)
logger
.
debug
(
'Configured slapproxy log to %r'
,
slapproxy_log
)
...
@@ -96,26 +98,36 @@ class SlapOSInstanceTestCase(unittest.TestCase):
...
@@ -96,26 +98,36 @@ class SlapOSInstanceTestCase(unittest.TestCase):
reset_software
=
False
,
reset_software
=
False
,
software_path_list
=
cls
.
software_url_list
)
software_path_list
=
cls
.
software_url_list
)
print
"initialized"
process_manager
.
supervisord_pid_file
=
os
.
path
.
join
(
process_manager
.
supervisord_pid_file
=
os
.
path
.
join
(
\
slapos_controler
.
instance_root
,
'var'
,
'run'
,
'supervisord.pid'
)
slapos_controler
.
instance_root
,
'var'
,
'run'
,
'supervisord.pid'
)
software_status_dict
=
slapos_controler
.
runSoftwareRelease
(
config
,
environment
=
os
.
environ
)
software_status_dict
=
slapos_controler
.
runSoftwareRelease
(
config
,
environment
=
os
.
environ
)
# TODO: log more details in this case
assert
software_status_dict
[
'status_code'
]
==
0
assert
software_status_dict
[
'status_code'
]
==
0
instance_status_dict
=
slapos_controler
.
runComputerPartition
(
instance_status_dict
=
slapos_controler
.
runComputerPartition
(
config
,
config
,
cluster_configuration
=
cls
.
instance_parameter_dict
,
cluster_configuration
=
cls
.
instance_parameter_dict
,
environment
=
os
.
environ
)
environment
=
os
.
environ
)
assert
instance_status_dict
[
'status_code'
]
==
0
# TODO: log more details in this case
assert
software_status_dict
[
'status_code'
]
==
0
cls
.
slapos_controler
=
slapos_controler
cls
.
slapos_controler
=
slapos_controler
cls
.
config
=
config
cls
.
computer_partition
=
slapos_controler
.
slap
.
registerOpenOrder
().
request
(
cls
.
computer_partition
=
slapos_controler
.
slap
.
registerOpenOrder
().
request
(
cls
.
software_url_list
[
0
],
cls
.
software_url_list
[
0
],
partition_reference
=
'testing partition 0'
,
partition_reference
=
'testing partition 0'
,
partition_parameter_kw
=
cls
.
instance_parameter_dict
)
partition_parameter_kw
=
cls
.
instance_parameter_dict
)
cls
.
computer_partition_root_path
=
os
.
path
.
join
(
config
[
'working_directory'
],
'inst'
,
cls
.
computer_partition
.
getId
())
@
classmethod
@
classmethod
def
tearDownClass
(
cls
):
def
tearDownClass
(
cls
):
print
"Teardown"
,
cls
print
"Teardown"
,
cls
#for reference in cls.slapos_controler.instance_config:
# cls.slapos_controler.destroyInstance(reference)
cls
.
slapos_controler
.
process_manager
.
killPreviousRun
()
import
os
#os.waitpid(0, 0)
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