Debian Codegraph @

reno/tests/test_utils.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # 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, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 from unittest import mock
16
17 from reno.tests import base
18 from reno import utils
19
20
21 class TestGetRandomString(base.TestCase):
22
23 @mock.patch('random.randrange')
24 @mock.patch('os.urandom')
25 def test_no_urandom(self, urandom, randrange):
26 urandom.side_effect = Exception('cannot use this')
27 randrange.return_value = ord('a')
28 actual = utils.get_random_string()
29 expected = '61' * 8 # hex for ord('a')
30 self.assertIsInstance(actual, str)
31 self.assertEqual(expected, actual)
32
33 @mock.patch('random.randrange')
34 @mock.patch('os.urandom')
35 def test_with_urandom(self, urandom, randrange):
36 urandom.return_value = b'\x62' * 8
37 randrange.return_value = ord('a')
38 actual = utils.get_random_string()
39 expected = '62' * 8 # hex for ord('b')
40 self.assertIsInstance(actual, str)
41 self.assertEqual(expected, actual)