1 # Copyright (c) 2015 IBM Corp.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
18 from neutron.common import constants
19 from neutron.plugins.common import utils
20 from neutron.tests import base
22 LONG_NAME1 = "A_REALLY_LONG_INTERFACE_NAME1"
23 LONG_NAME2 = "A_REALLY_LONG_INTERFACE_NAME2"
25 MOCKED_HASH = "mockedhash"
28 class MockSHA(object):
33 class TestUtils(base.BaseTestCase):
35 @mock.patch.object(hashlib, 'sha1', return_value=MockSHA())
36 def test_get_interface_name(self, mock_sha1):
38 prefix_long = "long_prefix"
39 prefix_exceeds_max_dev_len = "much_too_long_prefix"
40 hash_used = MOCKED_HASH[0:6]
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,
53 # len(prefix) > max_device_len
54 self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME,
55 prefix=prefix_exceeds_max_dev_len)
57 def test_get_interface_uniqueness(self):
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)
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,
69 self.assertEqual(12, len(utils.get_interface_name(LONG_NAME1,