Duplicate utils code in kuryr-lib and kuryr-libnetwork

Since commit[1] had renamed kuryr utils to kuryr-libnetwork utils,
but there are some duplicate codes between lib and libnetwork repo.

[1] https://review.openstack.org/#/c/337350

Change-Id: I3dbf737b2a3e23b91a756cc5ff39ff7bbf5a818c
Closes-Bug: #1618012
This commit is contained in:
Dongcan Ye 2016-08-30 20:59:48 +08:00
parent 20456034fe
commit bf1fc58e5d
14 changed files with 105 additions and 220 deletions

View File

@ -26,6 +26,7 @@ from oslo_utils import excutils
from kuryr.lib import binding
from kuryr.lib import exceptions
from kuryr.lib import utils as lib_utils
from kuryr.lib._i18n import _LE, _LI, _LW
from kuryr_libnetwork import app
from kuryr_libnetwork.common import config
@ -100,12 +101,12 @@ def get_neutron_client():
neutron_uri = cfg.CONF.neutron_client.neutron_uri
if username and password:
# Authenticate with password crentials
neutron_client = utils.get_neutron_client(
neutron_client = lib_utils.get_neutron_client(
url=neutron_uri, username=username, tenant_name=tenant_name,
password=password, auth_url=auth_uri,
ca_cert=ca_cert, insecure=insecure)
else:
neutron_client = utils.get_neutron_client_simple(
neutron_client = lib_utils.get_neutron_client_simple(
url=neutron_uri, auth_url=auth_uri, token=auth_token)
return neutron_client
@ -245,7 +246,7 @@ def _create_port(endpoint_id, neutron_network_id, interface_mac, fixed_ips):
'network_id': neutron_network_id,
'device_owner': const.DEVICE_OWNER,
'device_id': endpoint_id,
'binding:host_id': utils.get_hostname(),
'binding:host_id': lib_utils.get_hostname(),
'fixed_ips': fixed_ips
}
if interface_mac:
@ -320,7 +321,8 @@ def _create_or_update_port(neutron_network_id, endpoint_id,
filtered_ports = app.neutron.list_ports(fixed_ips=fixed_ips)
num_port = len(filtered_ports.get('ports', []))
if not num_port:
fixed_ips = utils.get_dict_format_fixed_ips_from_kv_format(fixed_ips)
fixed_ips = (
lib_utils.get_dict_format_fixed_ips_from_kv_format(fixed_ips))
response_port = _create_port(endpoint_id, neutron_network_id,
interface_mac, fixed_ips)
elif num_port == 1:
@ -1210,7 +1212,7 @@ def ipam_request_pool():
cidr = netaddr.IPNetwork(requested_pool)
subnet_cidr = _get_subnet_cidr_using_cidr(cidr)
if not pool_name:
pool_name = utils.get_neutron_subnetpool_name(subnet_cidr)
pool_name = lib_utils.get_neutron_subnetpool_name(subnet_cidr)
pools = _get_subnetpools_by_attrs(name=pool_name)
if len(pools):
raise exceptions.KuryrException(
@ -1343,7 +1345,7 @@ def ipam_request_address():
'name': 'kuryr-unbound-port',
'admin_state_up': True,
'network_id': neutron_network_id,
'binding:host_id': utils.get_hostname(),
'binding:host_id': lib_utils.get_hostname(),
}
fixed_ips = port['fixed_ips'] = []
fixed_ip = {'subnet_id': subnet['id']}
@ -1366,7 +1368,7 @@ def ipam_request_address():
if not host and vif_type == 'unbound':
updated_port = {
'admin_state_up': True,
'binding:host_id': utils.get_hostname(),
'binding:host_id': lib_utils.get_hostname(),
}
created_port_resp = app.neutron.update_port(
existing_port['id'],

View File

@ -10,8 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
from kuryr.lib import utils as lib_utils
from kuryr.tests.fullstack import kuryr_base
from kuryr import utils
from kuryr_libnetwork import utils
class ContainerTest(kuryr_base.KuryrBaseTest):
@ -34,7 +36,7 @@ class ContainerTest(kuryr_base.KuryrBaseTest):
}
]
}
net_name = utils.get_random_string(8)
net_name = lib_utils.get_random_string(8)
res = self.docker_client.create_network(name=net_name,
driver='kuryr',
ipam=fake_ipam)
@ -54,7 +56,7 @@ class ContainerTest(kuryr_base.KuryrBaseTest):
def test_connect_disconnect_container(self):
# Test if support connect/disconnect operations
container_name = utils.get_random_string(8)
container_name = lib_utils.get_random_string(8)
container = self.docker_client.create_container(
image='busybox:1',
command='/bin/sleep 600',

View File

@ -11,8 +11,10 @@
# under the License.
from kuryr.lib import utils as lib_utils
from kuryr.tests.fullstack import kuryr_base
from kuryr import utils
from kuryr_libnetwork import utils
class NetworkTest(kuryr_base.KuryrBaseTest):
@ -39,7 +41,7 @@ class NetworkTest(kuryr_base.KuryrBaseTest):
}
]
}
net_name = utils.get_random_string(8)
net_name = lib_utils.get_random_string(8)
res = self.docker_client.create_network(name=net_name, driver='kuryr',
ipam=fake_ipam)
net_id = res['Id']
@ -58,7 +60,7 @@ class NetworkTest(kuryr_base.KuryrBaseTest):
docker driver, It tests that it was created correctly, but
not added to Neutron
"""
net_name = utils.get_random_string(8)
net_name = lib_utils.get_random_string(8)
res = self.docker_client.create_network(name=net_name)
net_id = res['Id']
network = self.neutron_client.list_networks(
@ -101,7 +103,7 @@ class NetworkTest(kuryr_base.KuryrBaseTest):
}
]
}
net_name = utils.get_random_string(8)
net_name = lib_utils.get_random_string(8)
res = self.docker_client.create_network(name=net_name, driver='kuryr',
ipam=fake_ipam_1)
net_id1 = res['Id']

View File

@ -15,6 +15,7 @@ import uuid
import ddt
from oslo_serialization import jsonutils
from kuryr.lib import utils as lib_utils
from kuryr_libnetwork import app
from kuryr_libnetwork.common import constants
from kuryr_libnetwork.tests.unit import base
@ -39,8 +40,8 @@ class TestExternalConnectivityKuryr(base.TestKuryrBase):
@ddt.unpack
def test_network_driver_program_external_connectivity(self, existing_sg,
num_ports):
fake_docker_net_id = utils.get_hash()
fake_docker_endpoint_id = utils.get_hash()
fake_docker_net_id = lib_utils.get_hash()
fake_docker_endpoint_id = lib_utils.get_hash()
fake_neutron_net_id = str(uuid.uuid4())
fake_neutron_port_id = str(uuid.uuid4())
@ -66,7 +67,7 @@ class TestExternalConnectivityKuryr(base.TestKuryrBase):
'description': 'Docker exposed ports created by Kuryr.'
}
self.mox.StubOutWithMock(app.neutron, 'create_security_group')
fake_neutron_sec_group_id = utils.get_hash()
fake_neutron_sec_group_id = lib_utils.get_hash()
fake_neutron_sec_group_response = {'security_group':
{'id': fake_neutron_sec_group_id}}
app.neutron.create_security_group({'security_group':
@ -120,12 +121,12 @@ class TestExternalConnectivityKuryr(base.TestKuryrBase):
@ddt.data((False), (True))
def test_network_driver_revoke_external_connectivity(self, existing_sg):
fake_docker_net_id = utils.get_hash()
fake_docker_endpoint_id = utils.get_hash()
fake_docker_net_id = lib_utils.get_hash()
fake_docker_endpoint_id = lib_utils.get_hash()
fake_neutron_net_id = str(uuid.uuid4())
fake_neutron_port_id = str(uuid.uuid4())
fake_neutron_sec_group_id = utils.get_hash()
fake_neutron_sec_group_id = lib_utils.get_hash()
self.mox.StubOutWithMock(app.neutron, 'list_ports')
neutron_port_name = utils.get_neutron_port_name(
fake_docker_endpoint_id)

View File

@ -14,9 +14,9 @@ import ddt
from neutronclient.common import exceptions
from oslo_serialization import jsonutils
from kuryr.lib import utils as lib_utils
from kuryr_libnetwork import app
from kuryr_libnetwork.tests.unit import base
from kuryr_libnetwork import utils
@ddt.ddt
@ -43,7 +43,7 @@ class TestIpamRequestPoolFailures(base.TestKuryrFailures):
@ddt.data(exceptions.Unauthorized, exceptions.Forbidden,
exceptions.NotFound)
def test_request_pool_create_failures(self, GivenException):
pool_name = utils.get_neutron_subnetpool_name("10.0.0.0/16")
pool_name = lib_utils.get_neutron_subnetpool_name("10.0.0.0/16")
new_subnetpool = {
'name': pool_name,
'default_prefixlen': 16,
@ -81,7 +81,7 @@ class TestIpamRequestPoolFailures(base.TestKuryrFailures):
def test_request_pool_list_subnetpool_failure(self):
self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
pool_name = utils.get_neutron_subnetpool_name("10.0.0.0/16")
pool_name = lib_utils.get_neutron_subnetpool_name("10.0.0.0/16")
fake_name = pool_name
ex = exceptions.Unauthorized
app.neutron.list_subnetpools(name=fake_name).AndRaise(ex)

View File

@ -19,6 +19,7 @@ from werkzeug import exceptions as w_exceptions
from kuryr.lib import binding
from kuryr.lib import exceptions
from kuryr.lib import utils as lib_utils
from kuryr_libnetwork import app
from kuryr_libnetwork.common import constants as const
from kuryr_libnetwork.tests.unit import base
@ -65,9 +66,9 @@ class TestKuryrJoinFailures(base.TestKuryrFailures):
@ddt.data(exceptions.VethCreationFailure,
processutils.ProcessExecutionError)
def test_join_veth_failures(self, GivenException):
fake_docker_network_id = utils.get_hash()
fake_docker_endpoint_id = utils.get_hash()
fake_container_id = utils.get_hash()
fake_docker_network_id = lib_utils.get_hash()
fake_docker_endpoint_id = lib_utils.get_hash()
fake_container_id = lib_utils.get_hash()
fake_neutron_network_id = str(uuid.uuid4())
self._mock_out_network(fake_neutron_network_id, fake_docker_network_id)
@ -110,9 +111,9 @@ class TestKuryrJoinFailures(base.TestKuryrFailures):
self.assertIn(fake_message, decoded_json['Err'])
def test_join_bad_request(self):
fake_docker_network_id = utils.get_hash()
fake_docker_network_id = lib_utils.get_hash()
invalid_docker_endpoint_id = 'id-should-be-hexdigits'
fake_container_id = utils.get_hash()
fake_container_id = lib_utils.get_hash()
response = self._invoke_join_request(
fake_docker_network_id, invalid_docker_endpoint_id,

View File

@ -15,6 +15,7 @@ import uuid
import ddt
from oslo_serialization import jsonutils
from kuryr.lib import utils as lib_utils
from kuryr_libnetwork import app
from kuryr_libnetwork.common import config
from kuryr_libnetwork.common import constants
@ -57,7 +58,7 @@ class TestKuryr(base.TestKuryrBase):
self.assertEqual(expected, decoded_json)
def test_network_driver_create_network(self):
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
self.mox.StubOutWithMock(app.neutron, "create_network")
fake_request = {
"network": {
@ -145,7 +146,7 @@ class TestKuryr(base.TestKuryrBase):
self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)
def test_network_driver_create_network_with_net_name_option(self):
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
fake_neutron_net_id = "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
self.mox.StubOutWithMock(app.neutron, "list_networks")
fake_neutron_net_name = 'my_network_name'
@ -235,7 +236,7 @@ class TestKuryr(base.TestKuryrBase):
self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)
def test_network_driver_create_network_with_netid_option(self):
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
fake_neutron_net_id = "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
self.mox.StubOutWithMock(app.neutron, "list_networks")
fake_existing_networks_response = {
@ -331,7 +332,7 @@ class TestKuryr(base.TestKuryrBase):
fake_kuryr_subnetpool_id, name=fake_name)
app.neutron.list_subnetpools(name=fake_name).AndReturn(
{'subnetpools': kuryr_subnetpools['subnetpools']})
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
self.mox.StubOutWithMock(app.neutron, "create_network")
fake_request = {
"network": {
@ -425,7 +426,7 @@ class TestKuryr(base.TestKuryrBase):
self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)
def test_network_driver_create_network_wo_gw(self):
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
self.mox.StubOutWithMock(app.neutron, "create_network")
fake_request = {
"network": {
@ -511,7 +512,7 @@ class TestKuryr(base.TestKuryrBase):
self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)
def test_network_driver_create_network_with_network_id_not_exist(self):
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
self.mox.StubOutWithMock(app.neutron, "list_networks")
fake_neutron_net_id = str(uuid.uuid4())
@ -550,7 +551,7 @@ class TestKuryr(base.TestKuryrBase):
self.assertEqual({'Err': err_message}, decoded_json)
def test_network_driver_create_network_with_network_name_not_exist(self):
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
self.mox.StubOutWithMock(app.neutron, "list_networks")
fake_neutron_network_name = "fake_network"
@ -589,7 +590,7 @@ class TestKuryr(base.TestKuryrBase):
self.assertEqual({'Err': err_message}, decoded_json)
def test_network_driver_delete_network(self):
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
fake_neutron_net_id = str(uuid.uuid4())
self._mock_out_network(fake_neutron_net_id, docker_network_id,
check_existing=True)
@ -612,8 +613,8 @@ class TestKuryr(base.TestKuryrBase):
self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)
def test_network_driver_delete_network_with_subnets(self):
docker_network_id = utils.get_hash()
docker_endpoint_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
docker_endpoint_id = lib_utils.get_hash()
fake_neutron_net_id = str(uuid.uuid4())
self._mock_out_network(fake_neutron_net_id, docker_network_id,
@ -662,8 +663,8 @@ class TestKuryr(base.TestKuryrBase):
self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)
def test_network_driver_create_endpoint(self):
docker_network_id = utils.get_hash()
docker_endpoint_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
docker_endpoint_id = lib_utils.get_hash()
fake_neutron_net_id = str(uuid.uuid4())
self._mock_out_network(fake_neutron_net_id, docker_network_id)
@ -743,8 +744,8 @@ class TestKuryr(base.TestKuryrBase):
self.assertEqual(expected, decoded_json)
def test_network_driver_delete_endpoint(self):
docker_network_id = utils.get_hash()
docker_endpoint_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
docker_endpoint_id = lib_utils.get_hash()
data = {
'NetworkID': docker_network_id,
'EndpointID': docker_endpoint_id,
@ -764,9 +765,9 @@ class TestKuryr(base.TestKuryrBase):
self.mox.StubOutWithMock(app, "vif_plug_is_fatal")
app.vif_plug_is_fatal = True
fake_docker_net_id = utils.get_hash()
fake_docker_endpoint_id = utils.get_hash()
fake_container_id = utils.get_hash()
fake_docker_net_id = lib_utils.get_hash()
fake_docker_endpoint_id = lib_utils.get_hash()
fake_container_id = lib_utils.get_hash()
fake_neutron_net_id = str(uuid.uuid4())
self._mock_out_network(fake_neutron_net_id, fake_docker_net_id)
@ -837,8 +838,8 @@ class TestKuryr(base.TestKuryrBase):
self.assertEqual(expected_response, decoded_json)
def test_network_driver_leave(self):
fake_docker_net_id = utils.get_hash()
fake_docker_endpoint_id = utils.get_hash()
fake_docker_net_id = lib_utils.get_hash()
fake_docker_endpoint_id = lib_utils.get_hash()
fake_neutron_net_id = str(uuid.uuid4())
self._mock_out_network(fake_neutron_net_id, fake_docker_net_id)

View File

@ -16,6 +16,7 @@ import ddt
from neutronclient.common import exceptions
from oslo_serialization import jsonutils
from kuryr.lib import utils as lib_utils
from kuryr_libnetwork import app
from kuryr_libnetwork.common import constants
from kuryr_libnetwork.tests.unit import base
@ -88,7 +89,7 @@ class TestKuryrEndpointFailures(base.TestKuryrFailures):
'port': {
'name': utils.get_neutron_port_name(docker_endpoint_id),
'admin_state_up': True,
"binding:host_id": utils.get_hostname(),
"binding:host_id": lib_utils.get_hostname(),
'device_owner': constants.DEVICE_OWNER,
'device_id': docker_endpoint_id,
'fixed_ips': [{
@ -110,7 +111,7 @@ class TestKuryrEndpointFailures(base.TestKuryrFailures):
"name": utils.get_neutron_port_name(docker_endpoint_id),
"allowed_address_pairs": [],
"admin_state_up": True,
"binding:host_id": utils.get_hostname(),
"binding:host_id": lib_utils.get_hostname(),
"network_id": neutron_network_id,
"tenant_id": "d6700c0c9ffa4f1cb322cd4a1f3906fa",
"device_owner": constants.DEVICE_OWNER,
@ -170,8 +171,8 @@ class TestKuryrEndpointCreateFailures(TestKuryrEndpointFailures):
@ddt.data(exceptions.Unauthorized, exceptions.Forbidden,
exceptions.NotFound, exceptions.ServiceUnavailable)
def test_create_endpoint_port_failures(self, GivenException):
fake_docker_network_id = utils.get_hash()
fake_docker_endpoint_id = utils.get_hash()
fake_docker_network_id = lib_utils.get_hash()
fake_docker_endpoint_id = lib_utils.get_hash()
fake_neutron_network_id = str(uuid.uuid4())
fake_neutron_subnet_v4_id = str(uuid.uuid4())
fake_neutron_subnet_v6_id = str(uuid.uuid4())
@ -212,7 +213,7 @@ class TestKuryrEndpointCreateFailures(TestKuryrEndpointFailures):
self.assertEqual({'Err': GivenException.message}, decoded_json)
def test_create_endpoint_bad_request(self):
fake_docker_network_id = utils.get_hash()
fake_docker_network_id = lib_utils.get_hash()
invalid_docker_endpoint_id = 'id-should-be-hexdigits'
response = self._invoke_create_request(
@ -238,7 +239,7 @@ class TestKuryrEndpointDeleteFailures(TestKuryrEndpointFailures):
return response
def test_delete_endpoint_bad_request(self):
fake_docker_network_id = utils.get_hash()
fake_docker_network_id = lib_utils.get_hash()
invalid_docker_endpoint_id = 'id-should-be-hexdigits'
response = self._invoke_delete_request(

View File

@ -15,6 +15,7 @@ import uuid
import ddt
from oslo_serialization import jsonutils
from kuryr.lib import utils as lib_utils
from kuryr_libnetwork import app
from kuryr_libnetwork.common import constants as const
from kuryr_libnetwork.tests.unit import base
@ -25,7 +26,7 @@ from kuryr_libnetwork import utils
class TestKuryrNetworkPreExisting(base.TestKuryrBase):
def _ids(self):
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
fake_neutron_net_id = "4e8e5957-649f-477b-9e5b-f1f75b21c03c"
fake_response = {
'networks':

View File

@ -14,11 +14,11 @@ import ddt
from oslo_serialization import jsonutils
import uuid
from kuryr.lib import utils as lib_utils
from kuryr_libnetwork.common import config
from kuryr_libnetwork.common import constants as const
from kuryr_libnetwork.controllers import app
from kuryr_libnetwork.tests.unit import base
from kuryr_libnetwork import utils
FAKE_IP4_CIDR = '10.0.0.0/16'
@ -55,7 +55,7 @@ class TestKuryrIpam(base.TestKuryrBase):
self.assertEqual(expected, decoded_json)
def test_ipam_driver_request_pool_with_user_pool(self):
pool_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
pool_name = lib_utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
new_subnetpool = {
'name': pool_name,
'default_prefixlen': 16,
@ -178,7 +178,7 @@ class TestKuryrIpam(base.TestKuryrBase):
# faking list_subnetpools
self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
fake_kuryr_subnetpool_id = str(uuid.uuid4())
fake_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
fake_name = lib_utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
kuryr_subnetpools = self._get_fake_v4_subnetpools(
fake_kuryr_subnetpool_id, prefixes=[FAKE_IP4_CIDR],
name=fake_name)
@ -186,7 +186,7 @@ class TestKuryrIpam(base.TestKuryrBase):
kuryr_subnetpools)
# faking list_subnets
docker_endpoint_id = utils.get_hash()
docker_endpoint_id = lib_utils.get_hash()
neutron_network_id = str(uuid.uuid4())
subnet_v4_id = str(uuid.uuid4())
fake_v4_subnet = self._get_fake_v4_subnet(
@ -213,7 +213,7 @@ class TestKuryrIpam(base.TestKuryrBase):
'name': 'kuryr-unbound-port',
'admin_state_up': True,
'network_id': neutron_network_id,
'binding:host_id': utils.get_hostname(),
'binding:host_id': lib_utils.get_hostname(),
}
fixed_ips = port_request['fixed_ips'] = []
fixed_ip = {'subnet_id': subnet_v4_id}
@ -243,7 +243,7 @@ class TestKuryrIpam(base.TestKuryrBase):
# faking list_subnetpools
self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
fake_kuryr_subnetpool_id = str(uuid.uuid4())
fake_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
fake_name = lib_utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
kuryr_subnetpools = self._get_fake_v4_subnetpools(
fake_kuryr_subnetpool_id, prefixes=[FAKE_IP4_CIDR],
name=fake_name)
@ -279,7 +279,7 @@ class TestKuryrIpam(base.TestKuryrBase):
# faking list_subnetpools
self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
fake_kuryr_subnetpool_id = str(uuid.uuid4())
fake_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
fake_name = lib_utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
kuryr_subnetpools = self._get_fake_v4_subnetpools(
fake_kuryr_subnetpool_id, prefixes=[FAKE_IP4_CIDR],
name=fake_name)
@ -287,7 +287,7 @@ class TestKuryrIpam(base.TestKuryrBase):
kuryr_subnetpools)
# faking list_subnets
docker_endpoint_id = utils.get_hash()
docker_endpoint_id = lib_utils.get_hash()
neutron_network_id = str(uuid.uuid4())
subnet_v4_id = str(uuid.uuid4())
fake_v4_subnet = self._get_fake_v4_subnet(
@ -328,7 +328,7 @@ class TestKuryrIpam(base.TestKuryrBase):
if existing_port:
update_port = {
'admin_state_up': True,
'binding:host_id': utils.get_hostname(),
'binding:host_id': lib_utils.get_hostname(),
}
self.mox.StubOutWithMock(app.neutron, 'update_port')
app.neutron.update_port(fake_neutron_port_id,
@ -339,7 +339,7 @@ class TestKuryrIpam(base.TestKuryrBase):
'name': 'kuryr-unbound-port',
'admin_state_up': True,
'network_id': neutron_network_id,
'binding:host_id': utils.get_hostname(),
'binding:host_id': lib_utils.get_hostname(),
}
fixed_ips = port_request['fixed_ips'] = []
fixed_ip = {'subnet_id': subnet_v4_id,
@ -373,7 +373,7 @@ class TestKuryrIpam(base.TestKuryrBase):
fake_kuryr_subnetpool_id = str(uuid.uuid4())
fake_kuryr_subnetpool_id2 = str(uuid.uuid4())
fake_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
fake_name = lib_utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
kuryr_subnetpools = self._get_fake_v4_subnetpools(
fake_kuryr_subnetpool_id, prefixes=[FAKE_IP4_CIDR],
name=fake_name)
@ -381,7 +381,7 @@ class TestKuryrIpam(base.TestKuryrBase):
kuryr_subnetpools)
# faking list_subnets
docker_endpoint_id = utils.get_hash()
docker_endpoint_id = lib_utils.get_hash()
neutron_network_id = str(uuid.uuid4())
neutron_network_id2 = str(uuid.uuid4())
@ -419,7 +419,7 @@ class TestKuryrIpam(base.TestKuryrBase):
'name': 'kuryr-unbound-port',
'admin_state_up': True,
'network_id': neutron_network_id,
'binding:host_id': utils.get_hostname(),
'binding:host_id': lib_utils.get_hostname(),
}
port_request['fixed_ips'] = []
fixed_ip = {'subnet_id': neutron_subnet_v4_id}
@ -448,7 +448,7 @@ class TestKuryrIpam(base.TestKuryrBase):
# faking list_subnetpools
self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
fake_kuryr_subnetpool_id = str(uuid.uuid4())
fake_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
fake_name = lib_utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
kuryr_subnetpools = self._get_fake_v4_subnetpools(
fake_kuryr_subnetpool_id, prefixes=[FAKE_IP4_CIDR],
name=fake_name)
@ -456,7 +456,7 @@ class TestKuryrIpam(base.TestKuryrBase):
kuryr_subnetpools)
# faking list_subnets
docker_endpoint_id = utils.get_hash()
docker_endpoint_id = lib_utils.get_hash()
neutron_network_id = str(uuid.uuid4())
subnet_v4_id = str(uuid.uuid4())
fake_v4_subnet = self._get_fake_v4_subnet(
@ -496,7 +496,7 @@ class TestKuryrIpam(base.TestKuryrBase):
# faking list_subnetpools
self.mox.StubOutWithMock(app.neutron, 'list_subnetpools')
fake_kuryr_subnetpool_id = str(uuid.uuid4())
fake_name = utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
fake_name = lib_utils.get_neutron_subnetpool_name(FAKE_IP4_CIDR)
kuryr_subnetpools = self._get_fake_v4_subnetpools(
fake_kuryr_subnetpool_id, prefixes=[FAKE_IP4_CIDR],
name=fake_name)
@ -504,7 +504,7 @@ class TestKuryrIpam(base.TestKuryrBase):
kuryr_subnetpools)
# faking list_subnets
docker_endpoint_id = utils.get_hash()
docker_endpoint_id = lib_utils.get_hash()
neutron_network_id = str(uuid.uuid4())
subnet_v4_id = str(uuid.uuid4())
fake_v4_subnet = self._get_fake_v4_subnet(
@ -556,8 +556,8 @@ class TestKuryrIpam(base.TestKuryrBase):
fake_ip4 = '10.0.0.5'
# faking list_subnets
docker_network_id = utils.get_hash()
docker_endpoint_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
docker_endpoint_id = lib_utils.get_hash()
neutron_network_id = docker_network_id = str(uuid.uuid4())
subnet_v4_id = str(uuid.uuid4())
fake_v4_subnet = self._get_fake_v4_subnet(

View File

@ -15,6 +15,7 @@ from ddt import ddt
from neutronclient.common import exceptions
from oslo_serialization import jsonutils
from kuryr.lib import utils as lib_utils
from kuryr_libnetwork import app
from kuryr_libnetwork.common import constants as const
from kuryr_libnetwork.tests.unit import base
@ -62,7 +63,7 @@ class TestKuryrNetworkCreateFailures(base.TestKuryrFailures):
return response
def test_create_network_unauthorized(self):
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
self._create_network_with_exception(
docker_network_id, exceptions.Unauthorized())
@ -122,8 +123,8 @@ class TestKuryrNetworkDeleteFailures(base.TestKuryrFailures):
subnet_v4_id = "9436e561-47bf-436a-b1f1-fe23a926e031"
subnet_v6_id = "64dd4a98-3d7a-4bfd-acf4-91137a8d2f51"
docker_network_id = utils.get_hash()
docker_endpoint_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
docker_endpoint_id = lib_utils.get_hash()
fake_v4_subnet = self._get_fake_v4_subnet(
docker_network_id, docker_endpoint_id, subnet_v4_id)
@ -181,8 +182,8 @@ class TestKuryrNetworkDeleteFailures(base.TestKuryrFailures):
subnet_v4_id = "9436e561-47bf-436a-b1f1-fe23a926e031"
subnet_v6_id = "64dd4a98-3d7a-4bfd-acf4-91137a8d2f51"
docker_network_id = utils.get_hash()
docker_endpoint_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
docker_endpoint_id = lib_utils.get_hash()
fake_v4_subnet = self._get_fake_v4_subnet(
docker_network_id, docker_endpoint_id, subnet_v4_id)
@ -219,7 +220,7 @@ class TestKuryrNetworkDeleteFailures(base.TestKuryrFailures):
@data(exceptions.Unauthorized, exceptions.NotFound, exceptions.Conflict)
def test_delete_network_failures(self, GivenException):
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
self._delete_network_with_exception(
docker_network_id, GivenException())
@ -244,7 +245,7 @@ class TestKuryrNetworkDeleteFailures(base.TestKuryrFailures):
@data(exceptions.Unauthorized, exceptions.NotFound, exceptions.Conflict)
def test_delete_network_with_subnet_deletion_failures(self,
GivenException):
docker_network_id = utils.get_hash()
docker_network_id = lib_utils.get_hash()
self._delete_network_with_subnet_exception(
docker_network_id, GivenException())

View File

@ -19,6 +19,7 @@ from werkzeug import exceptions as w_exceptions
from kuryr.lib import binding
from kuryr.lib import exceptions
from kuryr.lib import utils as lib_utils
from kuryr_libnetwork import app
from kuryr_libnetwork.common import constants as const
from kuryr_libnetwork.tests.unit import base
@ -56,8 +57,8 @@ class TestKuryrLeaveFailures(base.TestKuryrFailures):
@ddt.data(exceptions.VethDeletionFailure,
processutils.ProcessExecutionError)
def test_leave_unbinding_failure(self, GivenException):
fake_docker_network_id = utils.get_hash()
fake_docker_endpoint_id = utils.get_hash()
fake_docker_network_id = lib_utils.get_hash()
fake_docker_endpoint_id = lib_utils.get_hash()
fake_neutron_network_id = str(uuid.uuid4())
self._mock_out_network(fake_neutron_network_id, fake_docker_network_id)
@ -90,7 +91,7 @@ class TestKuryrLeaveFailures(base.TestKuryrFailures):
self.assertIn(fake_message, decoded_json['Err'])
def test_leave_bad_request(self):
fake_docker_network_id = utils.get_hash()
fake_docker_network_id = lib_utils.get_hash()
invalid_docker_endpoint_id = 'id-should-be-hexdigits'
response = self._invoke_leave_request(

View File

@ -10,12 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import uuid
import ddt
from oslo_config import cfg
from kuryr_libnetwork.common import constants as const
from kuryr.lib import utils as lib_utils
from kuryr_libnetwork.tests.unit import base
from kuryr_libnetwork import utils
@ -24,54 +22,16 @@ from kuryr_libnetwork import utils
class TestKuryrUtils(base.TestKuryrBase):
"""Unit tests for utilities."""
@ddt.data(utils.get_hash(), '51c75a2515d4' '51c75a')
@ddt.data(lib_utils.get_hash(), '51c75a2515d4' '51c75a')
def test_get_sandbox_key(self, fake_container_id):
sandbox_key = utils.get_sandbox_key(fake_container_id)
expected = '/'.join([utils.DOCKER_NETNS_BASE, fake_container_id[:12]])
expected = '/'.join([lib_utils.DOCKER_NETNS_BASE,
fake_container_id[:12]])
self.assertEqual(expected, sandbox_key)
def test_get_port_name(self):
fake_docker_endpoint_id = utils.get_hash()
fake_docker_endpoint_id = lib_utils.get_hash()
generated_neutron_port_name = utils.get_neutron_port_name(
fake_docker_endpoint_id)
self.assertIn(utils.PORT_POSTFIX, generated_neutron_port_name)
self.assertIn(lib_utils.PORT_POSTFIX, generated_neutron_port_name)
self.assertIn(fake_docker_endpoint_id, generated_neutron_port_name)
def test_get_veth_pair_names(self):
fake_neutron_port_id = str(uuid.uuid4())
generated_ifname, generated_peer = utils.get_veth_pair_names(
fake_neutron_port_id)
namelen = const.NIC_NAME_LEN
ifname_postlen = namelen - len(const.VETH_PREFIX)
peer_postlen = namelen - len(const.CONTAINER_VETH_PREFIX)
self.assertEqual(namelen, len(generated_ifname))
self.assertEqual(namelen, len(generated_peer))
self.assertIn(const.VETH_PREFIX, generated_ifname)
self.assertIn(const.CONTAINER_VETH_PREFIX, generated_peer)
self.assertIn(fake_neutron_port_id[:ifname_postlen], generated_ifname)
self.assertIn(fake_neutron_port_id[:peer_postlen], generated_peer)
def test_get_subnetpool_name(self):
fake_subnet_cidr = "10.0.0.0/16"
generated_neutron_subnetpool_name = utils.get_neutron_subnetpool_name(
fake_subnet_cidr)
name_prefix = cfg.CONF.subnetpool_name_prefix
self.assertIn(name_prefix, generated_neutron_subnetpool_name)
self.assertIn(fake_subnet_cidr, generated_neutron_subnetpool_name)
def test_get_dict_format_fixed_ips_from_kv_format(self):
fake_fixed_ips_kv_format = \
['subnet_id=5083bda8-1b7c-4625-97f3-1d4c33bfeea8',
'ip_address=192.168.1.2',
'subnet_id=6607a230-f3eb-4937-b09f-9dd659211139',
'ip_address=fdfa:8456:1afa:0:f816:3eff:fe67:885e']
expected_dict_form = \
[{'subnet_id': '5083bda8-1b7c-4625-97f3-1d4c33bfeea8',
'ip_address': '192.168.1.2'},
{'subnet_id': '6607a230-f3eb-4937-b09f-9dd659211139',
'ip_address': 'fdfa:8456:1afa:0:f816:3eff:fe67:885e'}]
fixed_ips = utils.get_dict_format_fixed_ips_from_kv_format(
fake_fixed_ips_kv_format)
self.assertEqual(expected_dict_form, fixed_ips)

View File

@ -10,10 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import hashlib
import os
import random
import socket
import sys
import traceback
@ -21,37 +18,18 @@ import flask
import jsonschema
from neutronclient.common import exceptions as n_exceptions
from neutronclient.neutron import client
from neutronclient.v2_0 import client as client_v2
from oslo_concurrency import processutils
from oslo_config import cfg
from werkzeug import exceptions as w_exceptions
from kuryr.lib._i18n import _LE
from kuryr.lib import exceptions
from kuryr.lib import utils as lib_utils
from kuryr_libnetwork.common import constants as const
DOCKER_NETNS_BASE = '/var/run/docker/netns'
PORT_POSTFIX = 'port'
SG_POSTFIX = 'exposed_ports'
def get_neutron_client_simple(url, auth_url, token):
auths = auth_url.rsplit('/', 1)
version = auths[1][1:]
return client.Client(version, endpoint_url=url, token=token)
def get_neutron_client(url, username, tenant_name, password,
auth_url, ca_cert, insecure, timeout=30):
return client_v2.Client(endpoint_url=url, timeout=timeout,
username=username, tenant_name=tenant_name,
password=password, auth_url=auth_url,
ca_cert=ca_cert, insecure=insecure)
# Return all errors as JSON. From http://flask.pocoo.org/snippets/83/
def make_json_app(import_name, **kwargs):
"""Creates a JSON-oriented Flask app.
@ -100,7 +78,7 @@ def get_sandbox_key(container_id):
:param container_id: the ID of the Docker container as string
:returns: the constructed sandbox key as string
"""
return os.path.join(DOCKER_NETNS_BASE, container_id[:12])
return os.path.join(lib_utils.DOCKER_NETNS_BASE, container_id[:12])
def get_neutron_port_name(docker_endpoint_id):
@ -109,30 +87,7 @@ def get_neutron_port_name(docker_endpoint_id):
:param docker_endpoint_id: the EndpointID
:returns: the Neutron port name formatted appropriately
"""
return '-'.join([docker_endpoint_id, PORT_POSTFIX])
def get_veth_pair_names(port_id):
ifname = const.VETH_PREFIX + port_id
ifname = ifname[:const.NIC_NAME_LEN]
peer_name = const.CONTAINER_VETH_PREFIX + port_id
peer_name = peer_name[:const.NIC_NAME_LEN]
return ifname, peer_name
def get_hostname():
"""Returns the host name."""
return socket.gethostname()
def get_neutron_subnetpool_name(subnet_cidr):
"""Returns a Neutron subnetpool name.
:param subnet_cidr: The subnetpool allocation cidr
:returns: the Neutron subnetpool_name name formatted appropriately
"""
name_prefix = cfg.CONF.subnetpool_name_prefix
return '-'.join([name_prefix, subnet_cidr])
return '-'.join([docker_endpoint_id, lib_utils.PORT_POSTFIX])
def get_sg_expose_name(port_id):
@ -144,35 +99,6 @@ def get_sg_expose_name(port_id):
return '-'.join([port_id, SG_POSTFIX])
def get_dict_format_fixed_ips_from_kv_format(fixed_ips):
"""Returns fixed_ips in dict format.
:param fixed_ips: Format that neutron client expects for list_ports ex,
['subnet_id=5083bda8-1b7c-4625-97f3-1d4c33bfeea8',
'ip_address=192.168.1.2']
:returns: normal dict form,
[{'subnet_id': '5083bda8-1b7c-4625-97f3-1d4c33bfeea8',
'ip_address': '192.168.1.2'}]
"""
new_fixed_ips = []
for fixed_ip in fixed_ips:
if 'subnet_id' == fixed_ip.split('=')[0]:
subnet_id = fixed_ip.split('=')[1]
else:
ip = fixed_ip.split('=')[1]
new_fixed_ips.append({'subnet_id': subnet_id,
'ip_address': ip})
return new_fixed_ips
def getrandbits(bit_size=256):
return str(random.getrandbits(bit_size)).encode('utf-8')
def get_hash(bit_size=256):
return hashlib.sha256(getrandbits(bit_size=bit_size)).hexdigest()
def create_net_tags(tag):
tags = []
tags.append(const.NEUTRON_ID_LH_OPTION + ':' + tag[:32])
@ -191,17 +117,3 @@ def make_net_name(netid, tags=True):
if tags:
return const.NET_NAME_PREFIX + netid[:8]
return netid
def string_mappings(mapping_list):
"""Make a string out of the mapping list"""
details = ''
if mapping_list:
details = '"' + str(mapping_list) + '"'
return details
def get_random_string(length):
"""Get a random hex string of the specified length."""
return "{0:0{1}x}".format(random.getrandbits(length * 4), length)