Browse Source

openstack: adapt to the format of host addresses

keep-around/376b5d711996188e5bf832b585d7def3e9c6aa7b
Loïc Dachary 1 year ago
committed by some
parent
commit
376b5d7119
Signed by: dachary GPG Key ID: 992D23B392F9E4F2
  1. 19
      enough/common/openstack.py
  2. 15
      tests/enough/common/test_openstack.py

19
enough/common/openstack.py

@ -1,3 +1,4 @@
import ast
import base64
import datetime
import json
@ -446,16 +447,20 @@ class OpenStack(OpenStackBase):
def server_connected_to_network(self, server, network):
return self.server_ip_in_network(server, network)
def server_ip_in_network(self, server, network):
info = self.o.port.list('--server', server, '--network', network,
def server_port_list(self, server, network):
return self.o.port.list('--server', server, '--network', network,
'--format=value', '-c', 'Fixed IP Addresses')
info = info.strip()
def server_ip_in_network(self, server, network):
info = self.server_port_list(server, network).strip()
if info == '':
return None
pattern = r"ip_address='([0-9.]*?)'"
m = re.search(pattern, info)
assert m, f're.search({pattern}, {info})'
return m.group(1)
pattern = r"^[0-9.]+$"
found = None
for subnet in ast.literal_eval(info):
if re.match(pattern, subnet['ip_address']):
found = subnet['ip_address']
return found
def backup_date(self):
return datetime.datetime.today().strftime('%Y-%m-%d')

15
tests/enough/common/test_openstack.py

@ -96,6 +96,21 @@ def test_create_test_subdomain_with_bind(tmpdir, mocker, requests_mock):
#
# OpenStack
#
def test_server_ip_in_network_ok(mocker):
ip = '1.2.3.4'
mocker.patch('enough.common.openstack.OpenStack.server_port_list',
return_value="[{'ip_address': '" + ip + "'}, {'ip_address': '1:2:3:4:5'}]")
o = OpenStack(settings.CONFIG_DIR)
assert o.server_ip_in_network('server', 'network') == ip
def test_server_ip_in_network_none(mocker):
mocker.patch('enough.common.openstack.OpenStack.server_port_list',
return_value='[]')
o = OpenStack(settings.CONFIG_DIR)
assert o.server_ip_in_network('server', 'network') is None
@pytest.mark.skipif('SKIP_OPENSTACK_INTEGRATION_TESTS' in os.environ,
reason='skip integration test')
def test_network(openstack_name):

Loading…
Cancel
Save