Refactor port creation code

Introduce a method 'create_port' in base test class.
This allows subclass reuse the port creation logic.

Change-Id: Ib5ed02b765f22f9231d1d380152ab80478412351
This commit is contained in:
Hongbin Lu 2018-12-26 19:13:28 +00:00
parent a3ed016af4
commit 2e74efcdaa
3 changed files with 18 additions and 7 deletions

View File

@ -37,7 +37,6 @@ class TestCapsule(base.BaseZunTest):
def setup_clients(cls):
super(TestCapsule, cls).setup_clients()
cls.images_client = cls.os_primary.images_client
cls.ports_client = cls.os_primary.ports_client
cls.sgs_client = cls.os_primary.sgs_client
@classmethod

View File

@ -47,7 +47,6 @@ class TestContainer(base.BaseZunTest):
def setup_clients(cls):
super(TestContainer, cls).setup_clients()
cls.images_client = cls.os_primary.images_client
cls.ports_client = cls.os_primary.ports_client
cls.sgs_client = cls.os_primary.sgs_client
cls.vol_client = cls.os_primary.vol_client
@ -169,15 +168,17 @@ class TestContainer(base.BaseZunTest):
networks = self.networks_client.list_networks()['networks']
for network in networks:
if network['project_id'] == project_id:
network_id = network['id']
tenant_network = network
break
else:
self.fail('Cannot find network in tenant.')
port = self.ports_client.create_port(
network_id=network_id, name='testport')['port']
self.addCleanup(self.ports_client.delete_port, port['id'])
port = self.create_port(tenant_network)
port_address = port['fixed_ips'][0]['ip_address']
port_subnet = port['fixed_ips'][0]['subnet_id']
_, model = self._run_container(nets=[{'port': 'testport'}])
_, model = self._run_container(nets=[{'port': port['id']}])
network_id = tenant_network['id']
self.assertEqual(port['id'], model.addresses[network_id][0]['port'])
self.assertEqual(port_address, model.addresses[network_id][0]['addr'])
self.assertEqual(port_subnet,

View File

@ -53,6 +53,7 @@ class BaseZunTest(api_version_utils.BaseMicroversionTest,
cls.subnets_client = cls.os_primary.subnets_client
cls.docker_client = clients.DockerClient()
cls.container_client = cls.os_primary.container_client
cls.ports_client = cls.os_primary.ports_client
@classmethod
def setup_credentials(cls):
@ -131,3 +132,13 @@ class BaseZunTest(api_version_utils.BaseMicroversionTest,
subnet = client.create_subnet(**kwargs)['subnet']
self.addCleanup(client.delete_subnet, subnet['id'])
return subnet
def create_port(self, network, client=None, **values):
kwargs = {'name': data_utils.rand_name('test-port'),
'network_id': network['id']}
if values:
kwargs.update(values)
client = client or self.ports_client
port = client.create_port(**kwargs)['port']
self.addCleanup(client.delete_port, port['id'])
return port