Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / tests / unit / plugins / common / test_utils.py
1 # Copyright (c) 2015 IBM Corp.
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 import hashlib
16 import mock
17
18 from neutron.common import constants
19 from neutron.plugins.common import utils
20 from neutron.tests import base
21
22 LONG_NAME1 = "A_REALLY_LONG_INTERFACE_NAME1"
23 LONG_NAME2 = "A_REALLY_LONG_INTERFACE_NAME2"
24 SHORT_NAME = "SHORT"
25 MOCKED_HASH = "mockedhash"
26
27
28 class MockSHA(object):
29     def hexdigest(self):
30         return MOCKED_HASH
31
32
33 class TestUtils(base.BaseTestCase):
34
35     @mock.patch.object(hashlib, 'sha1', return_value=MockSHA())
36     def test_get_interface_name(self, mock_sha1):
37         prefix = "pre-"
38         prefix_long = "long_prefix"
39         prefix_exceeds_max_dev_len = "much_too_long_prefix"
40         hash_used = MOCKED_HASH[0:6]
41
42         self.assertEqual("A_REALLY_" + hash_used,
43                          utils.get_interface_name(LONG_NAME1))
44         self.assertEqual("SHORT",
45                          utils.get_interface_name(SHORT_NAME))
46         self.assertEqual("pre-A_REA" + hash_used,
47                          utils.get_interface_name(LONG_NAME1, prefix=prefix))
48         self.assertEqual("pre-SHORT",
49                          utils.get_interface_name(SHORT_NAME, prefix=prefix))
50         # len(prefix) > max_device_len - len(hash_used)
51         self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME,
52                           prefix_long)
53         # len(prefix) > max_device_len
54         self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME,
55                           prefix=prefix_exceeds_max_dev_len)
56
57     def test_get_interface_uniqueness(self):
58         prefix = "prefix-"
59         if_prefix1 = utils.get_interface_name(LONG_NAME1, prefix=prefix)
60         if_prefix2 = utils.get_interface_name(LONG_NAME2, prefix=prefix)
61         self.assertNotEqual(if_prefix1, if_prefix2)
62
63     @mock.patch.object(hashlib, 'sha1', return_value=MockSHA())
64     def test_get_interface_max_len(self, mock_sha1):
65         self.assertEqual(constants.DEVICE_NAME_MAX_LEN,
66                          len(utils.get_interface_name(LONG_NAME1)))
67         self.assertEqual(10, len(utils.get_interface_name(LONG_NAME1,
68                                                           max_len=10)))
69         self.assertEqual(12, len(utils.get_interface_name(LONG_NAME1,
70                                                           prefix="pre-",
71                                                           max_len=12)))