Initial commit
[ta/config-manager.git] / cmframework / test / cmflagfile_test.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import unittest
16 import mock
17 from mock import call
18 from mock import mock_open
19 import json
20
21 from cmframework.utils.cmflagfile import CMFlagFile
22 from cmframework.apis.cmerror import CMError
23
24
25 class CMFlagFileTest(unittest.TestCase):
26     @mock.patch('cmframework.utils.cmflagfile.os')
27     @mock.patch('cmframework.utils.cmflagfile.logging')
28     def test_is_set_for_non_existing_file(self, mock_logging, mock_os):
29         mock_os.path = mock.MagicMock()
30         mock_os.path.exists = mock.MagicMock()
31         mock_os.path.exists.return_value = False
32
33         flagfile = CMFlagFile('foo')
34         self.assertFalse(flagfile)
35
36     @mock.patch('cmframework.utils.cmflagfile.os')
37     @mock.patch('cmframework.utils.cmflagfile.logging')
38     def test_is_set_for_existing_file(self, mock_logging, mock_os):
39         mock_os.path = mock.MagicMock()
40         mock_os.path.exists = mock.MagicMock()
41         mock_os.path.exists.return_value = True
42
43         flagfile = CMFlagFile('foo')
44         self.assertTrue(flagfile)
45
46     @mock.patch('cmframework.utils.cmflagfile.open', new_callable=mock_open)
47     @mock.patch('cmframework.utils.cmflagfile.os')
48     @mock.patch('cmframework.utils.cmflagfile.logging')
49     def test_set_for_nonexisting_file(self, mock_logging, mock_os, mock_file):
50         mock_os.path = mock.MagicMock()
51         mock_os.path.exists = mock.MagicMock()
52         mock_os.path.exists.return_value = False
53
54         flagfile = CMFlagFile('foo')
55         self.assertFalse(flagfile)
56
57         flagfile.set()
58
59         mock_file.assert_called_with('/mnt/config-manager/foo', 'w')
60         mock_file.return_value.write.assert_called_once()
61
62     @mock.patch('cmframework.utils.cmflagfile.open', new_callable=mock_open)
63     @mock.patch('cmframework.utils.cmflagfile.os')
64     @mock.patch('cmframework.utils.cmflagfile.logging')
65     def test_set_for_existing_file(self, mock_logging, mock_os, mock_file):
66         mock_os.path = mock.MagicMock()
67         mock_os.path.exists = mock.MagicMock()
68         mock_os.path.exists.return_value = True
69
70         flagfile = CMFlagFile('foo')
71         self.assertTrue(flagfile)
72
73         flagfile.set()
74
75         mock_file.assert_not_called()
76
77     @mock.patch('cmframework.utils.cmflagfile.open', new_callable=mock_open)
78     @mock.patch('cmframework.utils.cmflagfile.os')
79     @mock.patch('cmframework.utils.cmflagfile.logging')
80     def test_set_io_failure(self, mock_logging, mock_os, mock_file):
81         mock_os.path = mock.MagicMock()
82         mock_os.path.exists = mock.MagicMock()
83         mock_os.path.exists.return_value = False
84
85         mock_file.return_value.write.side_effect = IOError()
86
87         flagfile = CMFlagFile('foo')
88         self.assertFalse(flagfile)
89
90         with self.assertRaises(CMError) as context:
91             flagfile.set()
92
93     @mock.patch('cmframework.utils.cmflagfile.os')
94     @mock.patch('cmframework.utils.cmflagfile.logging')
95     def test_unset_for_existing_file(self, mock_logging, mock_os):
96         mock_os.path = mock.MagicMock()
97         mock_os.path.exists = mock.MagicMock()
98         mock_os.path.exists.return_value = True
99
100         flagfile = CMFlagFile('foo')
101         self.assertTrue(flagfile)
102
103         flagfile.unset()
104
105         mock_os.remove.assert_called_once_with('/mnt/config-manager/foo')
106
107     @mock.patch('cmframework.utils.cmflagfile.os')
108     @mock.patch('cmframework.utils.cmflagfile.logging')
109     def test_unset_for_nonexisting_file(self, mock_logging, mock_os):
110         mock_os.path = mock.MagicMock()
111         mock_os.path.exists = mock.MagicMock()
112         mock_os.path.exists.return_value = False
113
114         flagfile = CMFlagFile('foo')
115         self.assertFalse(flagfile)
116
117         flagfile.unset()
118
119         mock_os.remove.assert_not_called()
120
121     @mock.patch('cmframework.utils.cmflagfile.os')
122     @mock.patch('cmframework.utils.cmflagfile.logging')
123     def test_unset_io_failure(self, mock_logging, mock_os):
124         mock_os.path = mock.MagicMock()
125         mock_os.path.exists = mock.MagicMock()
126         mock_os.path.exists.return_value = True
127
128         flagfile = CMFlagFile('foo')
129         self.assertTrue(flagfile)
130
131         mock_os.remove.side_effect = IOError()
132
133         with self.assertRaises(CMError) as context:
134             flagfile.unset()
135
136 if __name__ == '__main__':
137     unittest.main()