LockFileTestCase.test_context_manager()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nop 1
dl 0
loc 13
rs 9.95
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2014-2021 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: AGPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as
8
# published by the Free Software Foundation, either version 3 of the
9
# License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
import unittest
20
import shutil
21
import tempfile
22
23
from pathlib import Path, PosixPath
24
25
from unittest.mock import patch, MagicMock
26
from ospd_openvas.lock import LockFile
27
from .helper import assert_called_once, assert_called
28
29
30
class LockFileTestCase(unittest.TestCase):
31
    def setUp(self):
32
        self.temp_dir = Path(tempfile.mkdtemp())
33
34
    def tearDown(self):
35
        shutil.rmtree(str(self.temp_dir))
36
37
    def test_acquire_lock(self):
38
        lock_file_path = self.temp_dir / "test.lock"
39
40
        lock_file = LockFile(lock_file_path)
41
        lock_file._acquire_lock()  # pylint: disable = protected-access
42
43
        self.assertTrue(lock_file.has_lock())
44
        self.assertTrue(lock_file_path.exists())
45
        lock_file._release_lock()  # pylint: disable = protected-access
46
47
    @patch("ospd_openvas.lock.logger")
48
    def test_already_locked(self, mock_logger):
49
        lock_file_path = self.temp_dir / "test.lock"
50
51
        lock_file_aux = LockFile(lock_file_path)
52
        lock_file_aux._acquire_lock()  # pylint: disable = protected-access
53
        self.assertTrue(lock_file_aux.has_lock())
54
55
        lock_file = LockFile(lock_file_path)
56
        lock_file._acquire_lock()  # pylint: disable = protected-access
57
        self.assertFalse(lock_file.has_lock())
58
        assert_called(mock_logger.debug)
59
60
        lock_file_aux._release_lock()  # pylint: disable = protected-access
61
62
    def test_create_parent_dirs(self):
63
        lock_file_path = self.temp_dir / "foo" / "bar" / "test.lock"
64
65
        lock_file = LockFile(lock_file_path)
66
        lock_file._acquire_lock()  # pylint: disable = protected-access
67
68
        self.assertTrue(lock_file.has_lock())
69
70
        self.assertTrue(lock_file_path.exists())
71
        self.assertTrue(lock_file_path.parent.is_dir())
72
        self.assertTrue(lock_file_path.parent.parent.is_dir())
73
74
        lock_file._release_lock()  # pylint: disable = protected-access
75
76
    @patch("ospd_openvas.lock.logger")
77
    def test_create_paren_dirs_fail(self, mock_logger):
78
        lock_file_path = MagicMock(spec=Path).return_value
79
        parent = MagicMock(spec=PosixPath)
80
        lock_file_path.parent = parent
81
        parent.mkdir.side_effect = PermissionError
82
83
        lock_file = LockFile(lock_file_path)
84
85
        lock_file._acquire_lock()  # pylint: disable = protected-access
86
        self.assertFalse(lock_file.has_lock())
87
88
        assert_called_once(mock_logger.error)
89
90
    def test_context_manager(self):
91
        lock_file_path = self.temp_dir / "test.lock"
92
93
        lock_file = LockFile(lock_file_path)
94
95
        with lock_file:
96
            self.assertTrue(lock_file.has_lock())
97
            self.assertTrue(lock_file_path.is_file())
98
            lock_file._release_lock()  # pylint: disable = protected-access
99
100
        # The file is not removed
101
        self.assertFalse(lock_file.has_lock())
102
        self.assertTrue(lock_file_path.is_file())
103