Debian Codegraph @

oslo_upgradecheck/tests/test_upgradecheck.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 """
14 test_upgradecheck
15 ----------------------------------
16
17 Tests for `upgradecheck` module.
18 """
19
20 import os.path
21 import subprocess
22 import sys
23 from unittest import mock
24
25 from oslo_config import cfg # type: ignore
26 from oslotest import base # type: ignore
27
28 from oslo_upgradecheck import upgradecheck
29
30
31 class TestUpgradeCheckResult(base.BaseTestCase):
32 def test_details(self):
33 result = upgradecheck.Result(upgradecheck.Code.SUCCESS, 'test details')
34 self.assertEqual(0, result.code)
35 self.assertEqual('test details', result.details)
36
37
38 class TestCommands(upgradecheck.UpgradeCommands):
39 def success(self):
40 return upgradecheck.Result(
41 upgradecheck.Code.SUCCESS, 'Always succeeds'
42 )
43
44 def warning(self):
45 return upgradecheck.Result(upgradecheck.Code.WARNING, 'Always warns')
46
47 def failure(self):
48 return upgradecheck.Result(upgradecheck.Code.FAILURE, 'Always fails')
49
50 _upgrade_checks = (
51 ('always succeeds', success),
52 ('always warns', warning),
53 ('always fails', failure),
54 )
55
56
57 class SuccessCommands(upgradecheck.UpgradeCommands):
58 def success(self):
59 return upgradecheck.Result(
60 upgradecheck.Code.SUCCESS, 'Always succeeds'
61 )
62
63 _upgrade_checks = (('always succeeds', success),)
64
65
66 class TestUpgradeCommands(base.BaseTestCase):
67 def test_get_details(self):
68 result = upgradecheck.Result(upgradecheck.Code.SUCCESS, '*' * 70)
69 upgrade_commands = upgradecheck.UpgradeCommands()
70 details = upgrade_commands._get_details(result)
71 wrapped = '*' * 60 + '\n ' + '*' * 10
72 self.assertEqual(wrapped, details)
73
74 def test_check(self):
75 inst = TestCommands()
76 result = inst.check()
77 self.assertEqual(upgradecheck.Code.FAILURE, result)
78
79
80 class TestMain(base.BaseTestCase):
81 def _run_test(self, upgrade_command, expected):
82 conf = cfg.ConfigOpts()
83 result = upgradecheck.main(
84 conf=conf,
85 project='oslo-upgradecheck-test',
86 upgrade_command=upgrade_command,
87 argv=['upgrade', 'check'],
88 )
89 self.assertEqual(expected, result)
90
91 def test_main(self):
92 inst = TestCommands()
93 self._run_test(inst, upgradecheck.Code.FAILURE)
94
95 def test_main_exception(self):
96 raises = mock.Mock()
97 raises.check.side_effect = Exception('test exception')
98 self._run_test(raises, 255)
99
100 def test_main_success(self):
101 inst = SuccessCommands()
102 self._run_test(inst, 0)
103
104
105 class TestExampleFile(base.BaseTestCase):
106 def test_example_main(self):
107 path = os.path.join(
108 os.path.dirname(os.path.abspath(__file__)),
109 '../../doc/source/main.py',
110 )
111 # The example includes both a passing and failing test, which means the
112 # overall result is failure.
113 self.assertEqual(
114 upgradecheck.Code.FAILURE,
115 subprocess.call([sys.executable, path, 'upgrade', 'check']),
116 )