test: add test_python.py for testing python-mscp

Dockerfiles also adapt themselvs for testing python-mscp bindings.
This commit is contained in:
Ryo Nakamura
2023-03-12 20:37:57 +09:00
parent 597a7a8cba
commit e2da5811ce
8 changed files with 142 additions and 7 deletions

View File

@@ -21,10 +21,11 @@ RUN cd ${mscpdir} \
&& cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake \
-DBUILD_STATIC=ON -DBUILD_CONAN=ON \
-DBUILD_CONAN=ON -DBUILD_STATIC=ON \
&& make \
&& cp mscp /usr/bin/ \
&& cp mscp /mscp/build/mscp_0.0.6-alpine-3.17-x86_64.static
&& cp mscp /mscp/build/mscp_$(cat ${mscpdir}/VERSION)-alpine-3.17-x86_64.static \
&& cp mscp /mscp/build/mscp.linux.x86.static
# copy mscp to PKG FILE NAME because this build doesn't use CPACK
@@ -33,3 +34,12 @@ RUN ssh-keygen -A
RUN mkdir /var/run/sshd \
&& ssh-keygen -f /root/.ssh/id_rsa -N "" \
&& mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
# install mscp python module
RUN cd ${mscpdir} \
&& python3 setup.py install --user
# Need Fix: A trick putting libmscp.so to python mscp module dir does not work on alpine,
# so install libmscp.
RUN cd ${mscpdir}/build \
&& make install

View File

@@ -11,7 +11,7 @@ RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|
# install pytest, sshd for test, and rpm-build
RUN set -ex && yum -y update && yum -y install \
python3 python3-pip openssh openssh-server openssh-clients rpm-build
python3 python3-pip python3-devel openssh openssh-server openssh-clients rpm-build
RUN python3 -m pip install pytest
@@ -34,3 +34,6 @@ RUN cd ${mscpdir} \
&& cpack -G RPM CPackConfig.cmake \
&& rpm -iv *.rpm
# install mscp python module
RUN cd ${mscpdir} \
&& python3 setup.py install --user

View File

@@ -6,7 +6,7 @@ COPY . ${mscpdir}
# install pytest, sshd for test, and rpm-build
RUN set -ex && yum -y install \
python3 python3-pip openssh openssh-server openssh-clients rpm-build
python3 python3-pip python3-devel openssh openssh-server openssh-clients rpm-build
RUN python3 -m pip install pytest
@@ -29,3 +29,7 @@ RUN cd ${mscpdir} \
&& cpack -G RPM CPackConfig.cmake \
&& rpm -iv *.rpm
# install mscp python module
RUN cd ${mscpdir} \
&& python3 setup.py install --user

View File

@@ -10,7 +10,7 @@ RUN set -ex && apt-get update && apt-get install -y --no-install-recommends \
# install pytest, and sshd for test
RUN apt-get install -y --no-install-recommends \
python3 python3-pip openssh-server
python3 python3-pip python3-dev openssh-server
RUN python3 -m pip install pytest
@@ -33,3 +33,7 @@ RUN cd ${mscpdir} \
&& make \
&& cpack -G DEB CPackConfig.cmake \
&& dpkg -i *.deb
# install mscp python module
RUN cd ${mscpdir} \
&& python3 setup.py install --user

View File

@@ -10,7 +10,7 @@ RUN set -ex && apt-get update && apt-get install -y --no-install-recommends \
# install pytest, and sshd for test
RUN apt-get install -y --no-install-recommends \
python3 python3-pip openssh-server
python3 python3-pip python3-dev openssh-server
RUN python3 -m pip install pytest
@@ -33,3 +33,8 @@ RUN cd ${mscpdir} \
&& make \
&& cpack -G DEB CPackConfig.cmake \
&& dpkg -i *.deb
# install mscp python module
RUN cd ${mscpdir} \
&& python3 setup.py install --user

View File

@@ -13,5 +13,7 @@ if [ ! -e /var/run/sshd.pid ]; then
/usr/sbin/sshd
fi
ssh-keyscan localhost >> ${HOME}/.ssh/known_hosts
# Run test
python3 -m pytest ../test -v

View File

@@ -1,6 +1,9 @@
"""
test_e2e.py: End-to-End test for mscp executable.
"""
import pytest
import hashlib
import os
from subprocess import check_call, CalledProcessError, PIPE

104
test/test_python.py Normal file
View File

@@ -0,0 +1,104 @@
"""
test_python.py: Testing libmscp through the mscp python binding.
"""
import pytest
import mscp
import os
from util import File, check_same_md5sum
def test_create_and_release():
m = mscp.mscp("localhost", mscp.LOCAL2REMOTE)
m.cleanup()
""" copy test """
remote = "localhost"
remote_prefix = "{}/".format(os.getcwd()) # use current dir
param_remote_prefix_and_direction = [
("", remote_prefix, mscp.LOCAL2REMOTE), (remote_prefix, "", mscp.REMOTE2LOCAL)
]
param_single_copy = [
(File("src", size = 64), File("dst")),
(File("src", size = 4096 * 1), File("dst")),
(File("src", size = 128 * 1024 * 1024), File("dst")),
]
@pytest.mark.parametrize("src_prefix, dst_prefix, direction",
param_remote_prefix_and_direction)
@pytest.mark.parametrize("src, dst", param_single_copy)
def test_single_copy(src_prefix, dst_prefix, direction, src, dst):
src.make()
m = mscp.mscp(remote, direction)
m.copy(src_prefix + src.path, dst_prefix + dst.path)
assert check_same_md5sum(src, dst)
src.cleanup()
dst.cleanup()
param_double_copy = [
(File("src1", size = 1024 * 1024), File("src2", size = 1024 * 1024),
File("dst/src1"), File("dst/src2")
)
]
@pytest.mark.parametrize("src_prefix, dst_prefix, direction",
param_remote_prefix_and_direction)
@pytest.mark.parametrize("s1, s2, d1, d2", param_double_copy)
def test_double_copy(src_prefix, dst_prefix, direction, s1, s2, d1, d2):
s1.make()
s2.make()
mscp.mscp(remote, direction).copy([src_prefix + s1.path, src_prefix + s2.path],
dst_prefix + "dst")
assert check_same_md5sum(s1, d1)
assert check_same_md5sum(s2, d2)
s1.cleanup()
s2.cleanup()
d1.cleanup()
d2.cleanup()
param_single_copy = [
(File("src", size = 1024 * 1024 * 4), File("dst")),
]
param_kwargs = [
{ "nr_threads": 6 },
{ "nr_ahead": 64 },
{ "min_chunk_sz": 1 * 1024 * 1024 },
{ "max_chunk_sz": 64 * 1024 * 1024 },
{ "coremask": "0x0f" },
{ "severity": mscp.SEVERITY_NONE },
{ "cipher": "aes128-gcm@openssh.com" },
{ "compress": "yes" },
{ "no_hostkey_check": True },
{ "enable_nagle": True },
]
@pytest.mark.parametrize("src_prefix, dst_prefix, direction",
param_remote_prefix_and_direction)
@pytest.mark.parametrize("src, dst", param_single_copy)
@pytest.mark.parametrize("kw", param_kwargs)
def test_kwargs(src_prefix, dst_prefix, direction, src, dst, kw):
src.make()
m = mscp.mscp(remote, direction, **kw)
m.copy(src_prefix + src.path, dst_prefix + dst.path)
assert check_same_md5sum(src, dst)
src.cleanup()
dst.cleanup()
def test_login_failed():
m = mscp.mscp("asdfasdf@" + remote, mscp.LOCAL2REMOTE)
with pytest.raises(RuntimeError) as e:
m.connect()
m = mscp.mscp(remote, mscp.LOCAL2REMOTE, login_name = "asdfadsf")
with pytest.raises(RuntimeError) as e:
m.connect()
m = mscp.mscp(remote, mscp.LOCAL2REMOTE, port = "65534")
with pytest.raises(RuntimeError) as e:
m.connect()