Completed
Push — master ( 422e90...9c87d7 )
by Juan José
17s queued 13s
created

LockFileTestCase.test_context_manager()   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nop 1
dl 0
loc 13
rs 9.9
c 0
b 0
f 0
1
# Copyright (C) 2020 Greenbone Networks GmbH
2
#
3
# SPDX-License-Identifier: GPL-2.0-or-later
4
#
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
import unittest
20
import shutil
21
import tempfile
22
23
from pathlib import Path
24
25
from ospd_openvas.lock import LockFile
26
27
28
class LockFileTestCase(unittest.TestCase):
29
    def setUp(self):
30
        self.temp_dir = Path(tempfile.mkdtemp())
31
32
    def tearDown(self):
33
        shutil.rmtree(str(self.temp_dir))
34
35
    def test_is_locked(self):
36
        lock_file_path = self.temp_dir / 'test.lock'
37
38
        lock_file = LockFile(lock_file_path)
39
40
        self.assertFalse(lock_file.is_locked())
41
42
        lock_file_path.touch()
43
44
        self.assertTrue(lock_file.is_locked())
45
46
    def test_acquire_lock(self):
47
        lock_file_path = self.temp_dir / 'test.lock'
48
49
        lock_file = LockFile(lock_file_path)
50
        lock_file.acquire_lock()
51
52
        self.assertTrue(lock_file.has_lock())
53
        self.assertTrue(lock_file.is_locked())
54
55
        self.assertTrue(lock_file_path.exists())
56
57
    def test_already_locked(self):
58
        lock_file_path = self.temp_dir / 'test.lock'
59
        lock_file_path.touch()
60
61
        lock_file = LockFile(lock_file_path)
62
        lock_file.acquire_lock()
63
64
        self.assertFalse(lock_file.has_lock())
65
        self.assertTrue(lock_file.is_locked())
66
67
        self.assertTrue(lock_file_path.exists())
68
69
    def test_create_parent_dirs(self):
70
        lock_file_path = self.temp_dir / 'foo' / 'bar' / 'test.lock'
71
72
        lock_file = LockFile(lock_file_path)
73
        lock_file.acquire_lock()
74
75
        self.assertTrue(lock_file.has_lock())
76
        self.assertTrue(lock_file.is_locked())
77
78
        self.assertTrue(lock_file_path.exists())
79
        self.assertTrue(lock_file_path.parent.is_dir())
80
        self.assertTrue(lock_file_path.parent.parent.is_dir())
81
82
    def test_context_manager(self):
83
        lock_file_path = self.temp_dir / 'test.lock'
84
85
        lock_file = LockFile(lock_file_path)
86
87
        with lock_file:
88
            self.assertTrue(lock_file.is_locked())
89
            self.assertTrue(lock_file.has_lock())
90
            self.assertTrue(lock_file_path.is_file())
91
92
        self.assertFalse(lock_file.is_locked())
93
        self.assertFalse(lock_file.has_lock())
94
        self.assertFalse(lock_file_path.is_file())
95
96
    def test_context_manager_lock_exists(self):
97
        lock_file_path = self.temp_dir / 'test.lock'
98
        lock_file_path.touch()
99
100
        lock_file = LockFile(lock_file_path)
101
102
        with lock_file:
103
            self.assertTrue(lock_file.is_locked())
104
            self.assertTrue(lock_file_path.is_file())
105
            self.assertFalse(lock_file.has_lock())
106
107
        self.assertTrue(lock_file.is_locked())
108
        self.assertFalse(lock_file.has_lock())
109
        self.assertTrue(lock_file_path.is_file())
110