Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: PEP8; r-strings in regex; some docstrings #1290

Merged
merged 1 commit into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions common/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +0,0 @@
# Back In Time
# Copyright (C) 2015-2022 Germar Reitze
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU 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.

# This main file is required to run all unit tests in the "test" folder
# without specifying each test file name in the command line.
# See also:
# https://stackoverflow.com/a/43733357
# https://docs.python.org/3/library/unittest.html#test-discovery

from test import *

if __name__ == '__main__':
pass
167 changes: 148 additions & 19 deletions common/test/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""This module offers some helpers and tools for unittesting.

Most of the conent are `unittest.TestCase` derived classed doing basic setup
for the testing environment. They are dealing with snapshot path's, SSH,
config files and things like set.
"""

import os
import sys
import unittest
Expand All @@ -33,12 +40,17 @@
tools.registerBackintimePath('qt', 'plugins')

TMP_FLOCK = NamedTemporaryFile()
PRIV_KEY_FILE = os.path.expanduser(os.path.join("~",".ssh","id_rsa"))
AUTHORIZED_KEYS_FILE = os.path.expanduser(os.path.join("~",".ssh","authorized_keys"))
PRIV_KEY_FILE = os.path.expanduser(
os.path.join("~", ".ssh", "id_rsa"))
AUTHORIZED_KEYS_FILE = os.path.expanduser(
os.path.join("~", ".ssh", "authorized_keys"))
DUMMY = 'dummy_test_process.sh'

if os.path.exists(PRIV_KEY_FILE + '.pub') and os.path.exists(AUTHORIZED_KEYS_FILE):
if os.path.exists(PRIV_KEY_FILE + '.pub') \
and os.path.exists(AUTHORIZED_KEYS_FILE):

with open(PRIV_KEY_FILE + '.pub', 'rb') as pub:

with open(AUTHORIZED_KEYS_FILE, 'rb') as auth:
KEY_IN_AUTH = pub.read() in auth.readlines()
else:
Expand All @@ -49,7 +61,7 @@
try:
with socket.create_connection(('localhost', '22'), 2.0) as s:
sshdPortAvailable = not bool(s.connect_ex(s.getpeername()))
except:
except ConnectionRefusedError:
sshdPortAvailable = False

LOCAL_SSH = all((tools.processExists('sshd'),
Expand All @@ -60,27 +72,54 @@
ON_TRAVIS = os.environ.get('TRAVIS', 'None').lower() == 'true'
ON_RTD = os.environ.get('READTHEDOCS', 'None').lower() == 'true'


class TestCase(unittest.TestCase):
"""Base class for Back In Time unittesting.

"""

def __init__(self, methodName):
"""Initialize.

Args:
methodName: Unkown.
"""

# note by buhtz: This is not recommended. Unittest module handle
# that itself. The default locale is "C". Need further investigation.
os.environ['LANGUAGE'] = 'en_US.UTF-8'
self.cfgFile = os.path.abspath(os.path.join(__file__, os.pardir, 'config'))

# config file path
self.cfgFile = os.path.abspath(
os.path.join(__file__, os.pardir, 'config'))

logger.APP_NAME = 'BIT_unittest'
logger.openlog()

super(TestCase, self).__init__(methodName)

def setUp(self):
"""
"""
logger.DEBUG = '-v' in sys.argv
self.run = False
self.sharePathObj = TemporaryDirectory()
self.sharePath = self.sharePathObj.name

def tearDown(self):
"""
"""
self.sharePathObj.cleanup()

def callback(self, func, *args):
"""
"""
func(*args)
self.run = True

# the next six assert methods are deprecated and can be replaced
# by using Pythons in-build "pathlib.Path"

def assertExists(self, *path):
full_path = os.path.join(*path)
if not os.path.exists(full_path):
Expand Down Expand Up @@ -111,7 +150,11 @@ def assertIsLink(self, *path):
if not os.path.islink(full_path):
self.fail('Not a symlink: {}'.format(full_path))


class TestCaseCfg(TestCase):
"""Base class setting up config file.
"""

def setUp(self):
super(TestCaseCfg, self).setUp()
self.cfg = config.Config(self.cfgFile, self.sharePath)
Expand All @@ -122,31 +165,60 @@ def setUp(self):

self.cfg.PLUGIN_MANAGER.load()


class TestCaseSnapshotPath(TestCaseCfg):
"""Base class for snapshot test cases.

It setup a temporary directory as snapshot paths for each test method.
"""

def setUp(self):
"""
"""
super(TestCaseSnapshotPath, self).setUp()
#use a new TemporaryDirectory for snapshotPath to avoid
#side effects on leftovers

# use a new TemporaryDirectory for snapshotPath to avoid
# side effects on leftovers
self.tmpDir = TemporaryDirectory()
self.cfg.dict['profile1.snapshots.path'] = self.tmpDir.name
self.snapshotPath = self.cfg.snapshotsFullPath()

def tearDown(self):
"""
"""
super(TestCaseSnapshotPath, self).tearDown()

self.tmpDir.cleanup()


class SnapshotsTestCase(TestCaseSnapshotPath):
"""Base class for snapshot testing unittest classes.

Unclear why this need to be separated from `TestCaseSnapshotPath`.
"""

def setUp(self):
"""
"""
super(SnapshotsTestCase, self).setUp()

os.makedirs(self.snapshotPath)
self.sn = snapshots.Snapshots(self.cfg)
#use a tmp-file for flock because test_flockExclusive would deadlock
#otherwise if a regular snapshot is running in background

# use a tmp-file for flock because test_flockExclusive would deadlock
# otherwise if a regular snapshot is running in background
self.sn.GLOBAL_FLOCK = TMP_FLOCK.name


class SnapshotsWithSidTestCase(SnapshotsTestCase):
"""
"""

def setUp(self):
"""
"""
super(SnapshotsWithSidTestCase, self).setUp()

self.sid = snapshots.SID('20151219-010324-123', self.cfg)

self.sid.makeDirs()
Expand All @@ -156,69 +228,126 @@ def setUp(self):
self.testFileFullPath = self.sid.pathBackup(self.testFile)

self.sid.makeDirs(self.testDir)
with open(self.sid.pathBackup(self.testFile), 'wt') as f:
with open(self.sid.pathBackup(self.testFile), 'wt'):
pass


class SSHTestCase(TestCaseCfg):
# running this test requires that user has public / private key pair created and ssh server running
"""Base class for test cases using the SSH features.

Running this test requires that user has public / private key pair
created and SSH server (at localhost) running.
"""

def setUp(self):
"""
"""
super(SSHTestCase, self).setUp()

logger.DEBUG = '-v' in sys.argv

self.cfg.setSnapshotsMode('ssh')
self.cfg.setSshHost('localhost')
self.cfg.setSshPrivateKeyFile(PRIV_KEY_FILE)

# use a TemporaryDirectory for remote snapshot path
self.tmpDir = TemporaryDirectory()
self.remotePath = os.path.join(self.tmpDir.name, 'foo')
self.remoteFullPath = os.path.join(self.remotePath, 'backintime', *self.cfg.hostUserProfile())
self.remoteFullPath = os.path.join(
self.remotePath, 'backintime', *self.cfg.hostUserProfile())
self.cfg.setSshSnapshotsPath(self.remotePath)
self.mount_kwargs = {}

def tearDown(self):
"""
"""
super(SSHTestCase, self).tearDown()
self.tmpDir.cleanup()


class SSHSnapshotTestCase(SSHTestCase):
"""
"""

def setUp(self):
"""
"""
super(SSHSnapshotTestCase, self).setUp()

self.snapshotPath = self.cfg.sshSnapshotsFullPath()
os.makedirs(self.snapshotPath)

self.sn = snapshots.Snapshots(self.cfg)
#use a tmp-file for flock because test_flockExclusive would deadlock
#otherwise if a regular snapshot is running in background

# use a tmp-file for flock because test_flockExclusive would deadlock
# otherwise if a regular snapshot is running in background
self.sn.GLOBAL_FLOCK = TMP_FLOCK.name


class SSHSnapshotsWithSidTestCase(SSHSnapshotTestCase):
"""
"""

def setUp(self):
"""
"""
super(SSHSnapshotsWithSidTestCase, self).setUp()

self.sid = snapshots.SID('20151219-010324-123', self.cfg)

self.remoteSIDBackupPath = os.path.join(self.snapshotPath, self.sid.sid, 'backup')
self.remoteSIDBackupPath = os.path.join(
self.snapshotPath, self.sid.sid, 'backup')

os.makedirs(self.remoteSIDBackupPath)

self.testDir = 'foo/bar'
self.testDirFullPath = os.path.join(self.remoteSIDBackupPath, self.testDir)
self.testDirFullPath = os.path.join(
self.remoteSIDBackupPath, self.testDir)
self.testFile = 'foo/bar/baz'
self.testFileFullPath = os.path.join(self.remoteSIDBackupPath, self.testFile)
self.testFileFullPath = os.path.join(
self.remoteSIDBackupPath, self.testFile)

os.makedirs(self.testDirFullPath)
with open(self.testFileFullPath, 'wt') as f:

with open(self.testFileFullPath, 'wt'):
pass


def create_test_files(path):
"""Create folders and files.

Args:
path: Destination folder.

That is the resulting folder structure ::

foo
└── bar
├── baz
├── file with spaces
└── test

"""
os.makedirs(os.path.join(path, 'foo', 'bar'))

with open(os.path.join(path, 'foo', 'bar', 'baz'), 'wt') as f:
f.write('foo')

with open(os.path.join(path, 'test'), 'wt') as f:
f.write('bar')

with open(os.path.join(path, 'file with spaces'), 'wt') as f:
f.write('asdf')


@contextmanager
def mockPermissions(path, mode = 0o000):
def mockPermissions(path, mode=0o000):
"""
"""

st = os.stat(path)
os.chmod(path, mode)
yield

# fix permissions so it can be removed
os.chmod(path, st.st_mode)
6 changes: 3 additions & 3 deletions common/test/test_backintime.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_local_snapshot_is_successful(self):

self.assertEqual(proc.returncode, 0, msg)

self.assertRegex(output.decode(), re.compile('''
self.assertRegex(output.decode(), re.compile(r'''
Back In Time
Version: \d+.\d+.\d+.*

Expand Down Expand Up @@ -113,7 +113,7 @@ def test_local_snapshot_is_successful(self):
.format(proc.returncode, error.decode(), output.decode())
self.assertEqual(proc.returncode, 0, msg)

self.assertRegex(output.decode(), re.compile('''
self.assertRegex(output.decode(), re.compile(r'''
Back In Time
Version: \d+.\d+.\d+.*

Expand Down Expand Up @@ -152,7 +152,7 @@ def test_local_snapshot_is_successful(self):
.format(proc.returncode, error.decode(), output.decode())
self.assertEqual(proc.returncode, 0, msg)

self.assertRegex(output.decode(), re.compile('''
self.assertRegex(output.decode(), re.compile(r'''
Back In Time
Version: \d+.\d+.\d+.*

Expand Down
Loading