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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from mock import mock_open
21 from cmframework.utils.cmflagfile import CMFlagFile
22 from cmframework.apis.cmerror import CMError
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
33 flagfile = CMFlagFile('foo')
34 self.assertFalse(flagfile)
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
43 flagfile = CMFlagFile('foo')
44 self.assertTrue(flagfile)
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
54 flagfile = CMFlagFile('foo')
55 self.assertFalse(flagfile)
59 mock_file.assert_called_with('/mnt/config-manager/foo', 'w')
60 mock_file.return_value.write.assert_called_once()
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
70 flagfile = CMFlagFile('foo')
71 self.assertTrue(flagfile)
75 mock_file.assert_not_called()
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
85 mock_file.return_value.write.side_effect = IOError()
87 flagfile = CMFlagFile('foo')
88 self.assertFalse(flagfile)
90 with self.assertRaises(CMError) as context:
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
100 flagfile = CMFlagFile('foo')
101 self.assertTrue(flagfile)
105 mock_os.remove.assert_called_once_with('/mnt/config-manager/foo')
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
114 flagfile = CMFlagFile('foo')
115 self.assertFalse(flagfile)
119 mock_os.remove.assert_not_called()
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
128 flagfile = CMFlagFile('foo')
129 self.assertTrue(flagfile)
131 mock_os.remove.side_effect = IOError()
133 with self.assertRaises(CMError) as context:
136 if __name__ == '__main__':