Skip to content

Commit

Permalink
Merge pull request #944 from bareos/dev/joergs/master/systemtests-python
Browse files Browse the repository at this point in the history
Systemtest python-bareos: split tests in separate files
  • Loading branch information
joergsteffens committed Sep 30, 2021
2 parents a3c364f + fa4f5c7 commit 4d163c0
Show file tree
Hide file tree
Showing 30 changed files with 3,034 additions and 2,449 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -82,6 +82,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https:
- docs: BareosSecurityIssues add remark about new systemd service (non forking) logged information into systemd-journal [PR #927]

### Changed
- systemtest python-bareos: split tests in separate files [PR #944]
- core: systemd service: change daemon type from forking to simple and start daemons in foreground [PR #824]
- systemtests: define variable BackupDirectory globally [PR #780]
- systemtests: run all systemstests with ``set -o pipefail`` [PR #780]
Expand Down Expand Up @@ -262,4 +263,5 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https:
[PR #936]: https://github.com/bareos/bareos/pull/936
[PR #938]: https://github.com/bareos/bareos/pull/938
[PR #942]: https://github.com/bareos/bareos/pull/942
[PR #944]: https://github.com/bareos/bareos/pull/944
[unreleased]: https://github.com/bareos/bareos/tree/master
60 changes: 52 additions & 8 deletions systemtests/cmake/BareosSystemtestFunctions.cmake
Expand Up @@ -27,6 +27,9 @@ macro(create_systemtests_directory)

configurefilestosystemtest("systemtests" "scripts" "functions" @ONLY "")
configurefilestosystemtest("systemtests" "scripts" "cleanup" @ONLY "")
configurefilestosystemtest(
"systemtests" "scripts" "run_python_unittests.sh" @ONLY ""
)
configurefilestosystemtest("systemtests" "scripts" "setup" @ONLY "")
configurefilestosystemtest("systemtests" "scripts" "start_bareos.sh" @ONLY "")
configurefilestosystemtest("systemtests" "scripts" "start_minio.sh" @ONLY "")
Expand Down Expand Up @@ -433,6 +436,7 @@ macro(prepare_test_python)
"${CMAKE_BINARY_DIR}/core/src/plugins/filed/python/${python_module_name}modules:"
"${CMAKE_BINARY_DIR}/core/src/plugins/stored/python/${python_module_name}modules:"
"${CMAKE_BINARY_DIR}/core/src/plugins/dird/python/${python_module_name}modules:"
"${CMAKE_SOURCE_DIR}/systemtests/python-modules:"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/${TEST_NAME}/python-modules"
)
endif()
Expand Down Expand Up @@ -505,18 +509,30 @@ function(add_disabled_systemtest PREFIX TEST_NAME)
endfunction()

function(add_systemtest name file)
cmake_parse_arguments(PARSE_ARGV 2 ARG "" "WORKING_DIRECTORY" "")
cmake_parse_arguments(PARSE_ARGV 2 ARG "PYTHON" "WORKING_DIRECTORY" "")
message(STATUS " * ${name}")

if(ARG_WORKING_DIRECTORY)
set(directory "${ARG_WORKING_DIRECTORY}")
else()
get_filename_component(directory ${file} DIRECTORY)
endif()
add_test(
NAME ${name}
COMMAND ${file}
WORKING_DIRECTORY ${directory}
)

if(ARG_PYTHON)
get_filename_component(filename_without_ext ${file} NAME_WE)
add_test(
NAME ${name}
COMMAND ${PROJECT_BINARY_DIR}/scripts/run_python_unittests.sh
${filename_without_ext}
WORKING_DIRECTORY ${directory}
)
else()
add_test(
NAME ${name}
COMMAND ${file}
WORKING_DIRECTORY ${directory}
)
endif()
set_tests_properties(
${name} PROPERTIES TIMEOUT "${SYSTEMTEST_TIMEOUT}" COST 1.0
SKIP_RETURN_CODE 77
Expand Down Expand Up @@ -566,6 +582,25 @@ function(add_systemtest_from_directory tests_basedir prefix test_subdir)
)
endforeach()

# add all Python unittests named "test_*.py*" as tests.
file(
GLOB all_tests
LIST_DIRECTORIES false
RELATIVE "${test_dir}"
CONFIGURE_DEPENDS "${test_dir}/test_*.py"
)
foreach(testfilename ${all_tests})
string(REPLACE ".py" "" test0 ${testfilename})
string(REPLACE "test_" "" test ${test0})
add_systemtest(${test_basename}:${test} ${test_dir}/${testfilename} PYTHON)
set_tests_properties(
"${test_basename}:${test}"
PROPERTIES FIXTURES_REQUIRED "${test_basename}-fixture"
# use RESOURCE_LOCK to run tests sequential
RESOURCE_LOCK "${test_basename}-lock"
)
endforeach()

if(NOT EXISTS ${test_dir}/test-cleanup)
create_symlink(
"${PROJECT_BINARY_DIR}/scripts/cleanup" "${test_dir}/test-cleanup"
Expand All @@ -581,8 +616,17 @@ function(add_systemtest_from_directory tests_basedir prefix test_subdir)
endfunction()

macro(create_systemtest prefix test_subdir)
# Parameter: * prefix STRING * test_subdir STRING * DISABLED option * COMMENT
# "..." (optional) macro, not function, to be able to update BASEPORT.
# cmake-format: off
#
# Parameter:
# * prefix STRING
# * test_subdir STRING
# * DISABLED option
# * COMMENT "..." (optional)
#
# Made as a macro, not as a function to be able to update BASEPORT.
#
# cmake-format: on
cmake_parse_arguments(ARG "DISABLED" "COMMENT" "" ${ARGN})
set(test_basename "${prefix}${test_subdir}")
if(ARG_DISABLED)
Expand Down
25 changes: 25 additions & 0 deletions systemtests/python-modules/bareos_unittest/__init__.py
@@ -0,0 +1,25 @@
# BAREOS - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2021-2021 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

"""
`bareos_unittest` module.
"""

from bareos_unittest.base import Base
from bareos_unittest.json import Json
175 changes: 175 additions & 0 deletions systemtests/python-modules/bareos_unittest/base.py
@@ -0,0 +1,175 @@
#!/usr/bin/env python
#
# BAREOS - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2019-2021 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

# -*- coding: utf-8 -*-

import logging
import os
import re
import subprocess
from time import sleep
import unittest

import bareos.bsock
from bareos.bsock.constants import Constants
from bareos.bsock.protocolmessages import ProtocolMessages
from bareos.bsock.protocolversions import ProtocolVersions
from bareos.bsock.lowlevel import LowLevel
import bareos.exceptions


class Base(unittest.TestCase):

config_directory = "/etc/bareos"

director_name = u"bareos-dir"

director_address = "localhost"
director_port = 9101
director_root_password = "secret"
director_extra_options = {}
client = "bareos-fd"

filedaemon_address = "localhost"
filedaemon_port = 9102
filedaemon_director_password = u"secret"

dbcheck_binary = "dbcheck"

# restorefile = '/usr/sbin/bconsole'
# path to store logging files
backup_directory = "tmp/data/"
debug = False
logpath = "{}/PythonBareosTest.log".format(os.getcwd())

@classmethod
def setUpClass(cls):
# Configure the logger, for information about the timings set it to INFO
cls.get_env()

logging.basicConfig(
filename=cls.logpath,
format="%(levelname)s %(module)s.%(funcName)s: %(message)s",
level=logging.INFO,
)
logger = logging.getLogger()
if cls.debug:
logger.setLevel(logging.DEBUG)

# assertRegexpMatches has been renamed
# to assertRegex in Python 3.2
# and is deprecated now.
# This prevents a deprecation warning.
if hasattr(cls, "assertRegexpMatches") and not hasattr(cls, "assertRegex"):
cls.assertRegex = cls.assertRegexpMatches
logger.debug("setUpClass")

def setUp(self):
logger = logging.getLogger()
logger.debug("setUp")

def tearDown(self):
logger = logging.getLogger()
logger.debug("tearDown\n\n\n")

def get_name_of_test(self):
return self.id().split(".", 1)[1]

def get_operator_username(self, tls=None):
if tls is None:
if bareos.bsock.DirectorConsole.is_tls_psk_available():
tls = True
else:
tls = False
if tls:
return u"admin-tls"
else:
return u"admin-notls"

def get_operator_password(self, username=None):
return bareos.bsock.Password(u"secret")

@staticmethod
def append_to_file(filename, data):
with open(filename, "a") as writer:
writer.write(data)
writer.flush()

def run_dbcheck(self):
logger = logging.getLogger()
cmd = [self.dbcheck_binary, "-c", self.config_directory, "-vvv", "-b", "-f"]
logger.debug("calling: {}".format(" ".join(cmd)))
output = subprocess.check_output(cmd, stderr=subprocess.PIPE)
logger.debug("result: {}".format(output))

@classmethod
def get_env(cls):
"""
Get attributes as environment variables,
if not available or set use defaults.
"""

config_directory = os.environ.get("confdir")
if config_directory:
cls.config_directory = config_directory

director_root_password = os.environ.get("dir_password")
if director_root_password:
cls.director_root_password = director_root_password

director_port = os.environ.get("BAREOS_DIRECTOR_PORT")
if director_port:
cls.director_port = director_port

filedaemon_director_password = os.environ.get("fd_password")
if filedaemon_director_password:
cls.filedaemon_director_password = filedaemon_director_password

filedaemon_port = os.environ.get("BAREOS_FD_PORT")
if filedaemon_port:
cls.filedaemon_port = filedaemon_port

tls_version_str = os.environ.get("PYTHON_BAREOS_TLS_VERSION")
if tls_version_str is not None:
tls_version_parser = bareos.bsock.TlsVersionParser()
tls_version = tls_version_parser.get_protocol_version_from_string(
tls_version_str
)
if tls_version is not None:
cls.director_extra_options["tls_version"] = tls_version
else:
print(
"Environment variable PYTHON_BAREOS_TLS_VERSION has invalid value ({}). Valid values: {}".format(
tls_version_str,
", ".join(tls_version_parser.get_protocol_versions()),
)
)

backup_directory = os.environ.get("BackupDirectory")
if backup_directory:
cls.backup_directory = backup_directory

dbcheck_binary = os.environ.get("BAREOS_DBCHECK_BINARY")
if dbcheck_binary:
cls.dbcheck_binary = dbcheck_binary

if os.environ.get("REGRESS_DEBUG"):
cls.debug = True

0 comments on commit 4d163c0

Please sign in to comment.