Debian Codegraph @

oslo_upgradecheck/tests/test_common_checks.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # 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, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import fixtures
14 import os.path
15 import tempfile
16 import yaml
17
18 from oslo_config import cfg # type: ignore
19 from oslo_config import fixture as config
20 from oslo_policy import opts as policy_opts # type: ignore
21 from oslo_serialization import jsonutils # type: ignore
22 from oslotest import base # type: ignore
23
24 from oslo_upgradecheck import common_checks
25 from oslo_upgradecheck import upgradecheck
26
27
28 class TestUpgradeCheckPolicyJSON(base.BaseTestCase):
29 def setUp(self):
30 super().setUp()
31 conf_fixture = self.useFixture(config.Config())
32 conf_fixture.load_raw_values()
33 self.conf = conf_fixture.conf
34 self.conf.register_opts(
35 policy_opts._options, group=policy_opts._option_group
36 )
37
38 self.cmd = upgradecheck.UpgradeCommands()
39 self.cmd._upgrade_checks = (
40 (
41 'Policy File JSON to YAML Migration',
42 (common_checks.check_policy_json, {'conf': self.conf}),
43 ),
44 )
45
46 self.data = {'rule_admin': 'True', 'rule_admin2': 'is_admin:True'}
47 self.temp_dir = self.useFixture(fixtures.TempDir())
48 fd, self.json_file = tempfile.mkstemp(dir=self.temp_dir.path)
49 fd, self.yaml_file = tempfile.mkstemp(dir=self.temp_dir.path)
50 with open(self.json_file, 'w') as fh:
51 jsonutils.dump(self.data, fh)
52 with open(self.yaml_file, 'w') as fh:
53 yaml.dump(self.data, fh)
54
55 original_search_dirs = cfg._search_dirs
56
57 def fake_search_dirs(dirs, name):
58 dirs.append(self.temp_dir.path)
59 return original_search_dirs(dirs, name)
60
61 mock_search_dir = self.useFixture(
62 fixtures.MockPatch('oslo_config.cfg._search_dirs')
63 ).mock
64 mock_search_dir.side_effect = fake_search_dirs
65
66 def test_policy_json_file_fail_upgrade(self):
67 # Test with policy json file full path set in config.
68 self.conf.set_override(
69 'policy_file', self.json_file, group="oslo_policy"
70 )
71 self.assertEqual(upgradecheck.Code.FAILURE, self.cmd.check())
72
73 def test_policy_yaml_file_pass_upgrade(self):
74 # Test with full policy yaml file path set in config.
75 self.conf.set_override(
76 'policy_file', self.yaml_file, group="oslo_policy"
77 )
78 self.assertEqual(upgradecheck.Code.SUCCESS, self.cmd.check())
79
80 def test_no_policy_file_pass_upgrade(self):
81 # Test with no policy file exist, means use policy from code.
82 self.conf.set_override(
83 'policy_file', 'non_exist_file', group="oslo_policy"
84 )
85 self.assertEqual(upgradecheck.Code.SUCCESS, self.cmd.check())
86
87 def test_default_policy_yaml_file_pass_upgrade(self):
88 self.conf.set_override(
89 'policy_file', 'policy.yaml', group="oslo_policy"
90 )
91 tmpfilename = os.path.join(self.temp_dir.path, 'policy.yaml')
92 with open(tmpfilename, 'w') as fh:
93 yaml.dump(self.data, fh)
94 self.assertEqual(upgradecheck.Code.SUCCESS, self.cmd.check())
95
96 def test_old_default_policy_json_file_fail_upgrade(self):
97 self.conf.set_override(
98 'policy_file', 'policy.json', group="oslo_policy"
99 )
100 tmpfilename = os.path.join(self.temp_dir.path, 'policy.json')
101 with open(tmpfilename, 'w') as fh:
102 jsonutils.dump(self.data, fh)
103 self.assertEqual(upgradecheck.Code.FAILURE, self.cmd.check())