| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 |
# you may not use this file except in compliance with the License. |
| 3 |
# You may obtain 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, |
| 9 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 10 |
# implied. |
| 11 |
# See the License for the specific language governing permissions and |
| 12 |
# limitations under the License. |
| 13 |
|
| 14 |
import itertools |
| 15 |
import random |
| 16 |
import socket |
| 17 |
from unittest import mock |
| 18 |
|
| 19 |
from neutron_lib import constants |
| 20 |
from neutron_lib.tests import _base as base |
| 21 |
from neutron_lib.utils import net |
| 22 |
|
| 23 |
|
| 24 |
class TestGetHostname(base.BaseTestCase): |
| 25 |
|
| 26 |
@mock.patch.object(socket, 'gethostname', |
| 27 |
return_value='fake-host-name') |
| 28 |
def test_get_hostname(self, mock_gethostname): |
| 29 |
self.assertEqual('fake-host-name', |
| 30 |
net.get_hostname()) |
| 31 |
mock_gethostname.assert_called_once_with() |
| 32 |
|
| 33 |
|
| 34 |
class TestGetRandomMac(base.BaseTestCase): |
| 35 |
|
| 36 |
@mock.patch.object(random, 'getrandbits', return_value=0xa2) |
| 37 |
def test_first_4_octets_unchanged(self, mock_rnd): |
| 38 |
mac = net.get_random_mac(['aa', 'bb', '00', 'dd', 'ee', 'ff']) |
| 39 |
self.assertEqual('aa:bb:00:dd:a2:a2', mac) |
| 40 |
mock_rnd.assert_called_with(8) |
| 41 |
|
| 42 |
@mock.patch.object(random, 'getrandbits', return_value=0xa2) |
| 43 |
def test_first_4th_octet_generated(self, mock_rnd): |
| 44 |
mac = net.get_random_mac(['aa', 'bb', 'cc', '00', 'ee', 'ff']) |
| 45 |
self.assertEqual('aa:bb:cc:a2:a2:a2', mac) |
| 46 |
mock_rnd.assert_called_with(8) |
| 47 |
|
| 48 |
|
| 49 |
class TestRandomMacGenerator(base.BaseTestCase): |
| 50 |
|
| 51 |
def test_all_macs_generated(self): |
| 52 |
mac = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff'] |
| 53 |
generator = itertools.islice(net.random_mac_generator(mac), 70000) |
| 54 |
self.assertEqual(2**16, len(list(generator))) |
| 55 |
|
| 56 |
@mock.patch.object(random, 'getrandbits', return_value=0xa2) |
| 57 |
def test_first_generated_mac(self, mock_rnd): |
| 58 |
mac = ['aa', 'bb', 'cc', '00', 'ee', 'ff'] |
| 59 |
generator = itertools.islice(net.random_mac_generator(mac), 1) |
| 60 |
self.assertEqual(['aa:bb:cc:a2:a2:a2'], list(generator)) |
| 61 |
mock_rnd.assert_called_with(8) |
| 62 |
|
| 63 |
@mock.patch.object(random, 'getrandbits', return_value=0xa2) |
| 64 |
def test_respected_early_zeroes_generated_mac(self, mock_rnd): |
| 65 |
mac1 = ['00', 'bb', 'cc', '00', 'ee', 'ff'] |
| 66 |
|
| 67 |
generator = itertools.islice(net.random_mac_generator(mac1), 1) |
| 68 |
self.assertEqual(['00:bb:cc:a2:a2:a2'], list(generator)) |
| 69 |
|
| 70 |
mac2 = ['aa', '00', 'cc', '00', 'ee', 'ff'] |
| 71 |
generator = itertools.islice(net.random_mac_generator(mac2), 1) |
| 72 |
self.assertEqual(['aa:00:cc:a2:a2:a2'], list(generator)) |
| 73 |
|
| 74 |
mac3 = ['aa', 'bb', '00', '00', 'ee', 'ff'] |
| 75 |
generator = itertools.islice(net.random_mac_generator(mac3), 1) |
| 76 |
self.assertEqual(['aa:bb:00:a2:a2:a2'], list(generator)) |
| 77 |
mock_rnd.assert_called_with(8) |
| 78 |
|
| 79 |
@mock.patch.object(random, 'getrandbits', return_value=0xa2) |
| 80 |
def test_short_supplied_mac(self, mock_rnd): |
| 81 |
mac_base = '12:34:56:78' |
| 82 |
mac = mac_base.split(':') |
| 83 |
generator = itertools.islice(net.random_mac_generator(mac), 1) |
| 84 |
self.assertEqual(['12:34:56:78:a2:a2'], list(generator)) |
| 85 |
mock_rnd.assert_called_with(8) |
| 86 |
|
| 87 |
|
| 88 |
class TestPortDeviceOwner(base.BaseTestCase): |
| 89 |
|
| 90 |
def test_is_port_trusted(self): |
| 91 |
self.assertTrue(net.is_port_trusted( |
| 92 |
{'device_owner': |
| 93 |
constants.DEVICE_OWNER_NETWORK_PREFIX + 'dev'})) |
| 94 |
|
| 95 |
def test_is_port_not_trusted(self): |
| 96 |
self.assertFalse(net.is_port_trusted( |
| 97 |
{'device_owner': constants.DEVICE_OWNER_COMPUTE_PREFIX + 'dev'})) |