Files
deb-python-neutron-lib/neutron_lib/tests/unit/utils/test_net.py
Trevor McCasland ee0f5b2ab2 Move get_random_mac into neutron-lib
get_random_mac is used by networking-ovn, tap-as-a-service,
neutron-vpnaas, and vmware-nsx, so we should move it into the
neutron-lib repository.

Change-Id: I391300e6f1d04b2413084bccdbacb038071c4509
2016-11-13 00:21:34 +00:00

49 lines
1.6 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import socket
import mock
from neutron_lib.tests import _base as base
from neutron_lib.utils import net
class TestGetHostname(base.BaseTestCase):
@mock.patch.object(socket, 'gethostname',
return_value='fake-host-name')
def test_get_hostname(self, mock_gethostname):
self.assertEqual('fake-host-name',
net.get_hostname())
mock_gethostname.assert_called_once_with()
class TestGetRandomMac(base.BaseTestCase):
def _test_get_random_mac(self, base_mac, startswith_len):
random_mac = net.get_random_mac(base_mac.split(':'))
self.assertEqual(base_mac[:startswith_len],
random_mac[:startswith_len])
self.assertEqual(len(base_mac), len(random_mac))
for i in random_mac.split(':'):
int(i, 16)
def test_get_random_mac_with_three_octets(self):
base_mac = 'fa:16:3e:00:00:00'
self._test_get_random_mac(base_mac, 9)
def test_get_random_mac_with_four_octets(self):
base_mac = 'fa:16:3e:fe:00:00'
self._test_get_random_mac(base_mac, 12)