Passed
Pull Request — master (#257)
by Juan José
01:25
created

tests.test_lock.LockFileTestCase.test_is_locked()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2014-2020 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
import fcntl
23
24
from pathlib import Path
25
26
from unittest.mock import patch, MagicMock
27
from ospd_openvas.lock import LockFile
28
from .helper import assert_called_once
29
30
31
class LockFileTestCase(unittest.TestCase):
32
    def setUp(self):
33
        self.temp_dir = Path(tempfile.mkdtemp())
34
35
    def tearDown(self):
36
        shutil.rmtree(str(self.temp_dir))
37
38
    def test_acquire_lock(self):
39
        lock_file_path = self.temp_dir / 'test.lock'
40
41
        lock_file = LockFile(lock_file_path)
42
        lock_file._acquire_lock()
43
44
        self.assertTrue(lock_file.has_lock())
45
        self.assertTrue(lock_file_path.exists())
46
        lock_file._release_lock()
47
48
    @patch('ospd_openvas.lock.logger')
49
    def test_already_locked(self, mock_logger):
50
        lock_file_path = self.temp_dir / 'test.lock'
51
52
        lock_file_aux = LockFile(lock_file_path)
53
        lock_file_aux._acquire_lock()
54
        self.assertTrue(lock_file_aux.has_lock())
55
56
        lock_file = LockFile(lock_file_path)
57
        lock_file._acquire_lock()
58
        self.assertFalse(lock_file.has_lock())
59
        assert_called_once(mock_logger.error)
60
61
        lock_file_aux._release_lock()
62
63
    def test_create_parent_dirs(self):
64
        lock_file_path = self.temp_dir / 'foo' / 'bar' / 'test.lock'
65
66
        lock_file = LockFile(lock_file_path)
67
        lock_file._acquire_lock()
68
69
        self.assertTrue(lock_file.has_lock())
70
71
        self.assertTrue(lock_file_path.exists())
72
        self.assertTrue(lock_file_path.parent.is_dir())
73
        self.assertTrue(lock_file_path.parent.parent.is_dir())
74
75
        lock_file._release_lock()
76
77
    @patch('ospd_openvas.lock.logger')
78
    def test_create_paren_dirs_fail(self, mock_logger):
79
        lock_file_path = Path('/root/lock/file/test.lock')
80
        lock_file = LockFile(lock_file_path)
81
82
        lock_file._acquire_lock()
83
        self.assertFalse(lock_file.has_lock())
84
85
        assert_called_once(mock_logger.error)
86
87
    def test_context_manager(self):
88
        lock_file_path = self.temp_dir / 'test.lock'
89
90
        lock_file = LockFile(lock_file_path)
91
92
        with lock_file:
93
            self.assertTrue(lock_file.has_lock())
94
            self.assertTrue(lock_file_path.is_file())
95
            lock_file._release_lock()
96
97
        # The file is not removed
98
        self.assertFalse(lock_file.has_lock())
99
        self.assertTrue(lock_file_path.is_file())
100