Debian Codegraph @

neutron_lib/tests/unit/utils/test_file.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10 # implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 import os.path
15 import stat
16
17 from neutron_lib.tests import _base as base
18 from neutron_lib.utils import file
19
20
21 class TestReplaceFile(base.BaseTestCase):
22
23 def setUp(self):
24 super().setUp()
25 temp_dir = self.get_default_temp_dir().path
26 self.file_name = os.path.join(temp_dir, "new_file")
27 self.data = "data to copy"
28
29 def _verify_result(self, file_mode):
30 self.assertTrue(os.path.exists(self.file_name))
31 with open(self.file_name, encoding='utf-8') as f:
32 content = f.read()
33 self.assertEqual(self.data, content)
34 mode = os.stat(self.file_name).st_mode
35 self.assertEqual(file_mode, stat.S_IMODE(mode))
36
37 def test_replace_file_default_mode(self):
38 file_mode = 0o644
39 file.replace_file(self.file_name, self.data)
40 self._verify_result(file_mode)
41
42 def test_replace_file_custom_mode(self):
43 file_mode = 0o722
44 file.replace_file(self.file_name, self.data, file_mode)
45 self._verify_result(file_mode)
46
47 def test_replace_file_custom_mode_twice(self):
48 file_mode = 0o722
49 file.replace_file(self.file_name, self.data, file_mode)
50 self.data = "new data to copy"
51 file_mode = 0o777
52 file.replace_file(self.file_name, self.data, file_mode)
53 self._verify_result(file_mode)