Skip to content

Commit 35fbfc0

Browse files
committed
Use new auto assign logic for ip addresses
We no longer want users to pick ip addresses but just specify the network and then they get automatically an ip address. The exception is manually setting an ip address in which case we can use the adminapi to get an ip address and lock it so others do not use it.
1 parent 46955f3 commit 35fbfc0

2 files changed

Lines changed: 7 additions & 52 deletions

File tree

tests/conftest.py

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
"""igvm - Shared functions
22
3-
Copyright (c) 2020 InnoGames GmbH
3+
Copyright (c) 2025 InnoGames GmbH
44
"""
55

66
from datetime import datetime, timezone
7-
from math import ceil, log
87
from re import match
9-
from time import sleep
108
from shlex import quote
9+
from time import sleep
1110
from typing import Optional
1211

1312
import boto3
1413
from adminapi.dataset import Query
1514
from adminapi.exceptions import DatasetError
16-
from adminapi.filters import Any, Not, Regexp
15+
from adminapi.filters import Any, Regexp
1716
from botocore.exceptions import ClientError
1817
from libvirt import VIR_DOMAIN_RUNNING
1918

20-
from igvm.exceptions import IGVMTestError
2119
from igvm.hypervisor import Hypervisor
2220
from igvm.settings import HYPERVISOR_ATTRIBUTES, AWS_RETURN_CODES
2321
from tests import (
2422
IGVM_LOCKED_TIMEOUT,
2523
JENKINS_EXECUTOR,
26-
PYTEST_XDIST_WORKER,
27-
PYTEST_XDIST_WORKER_COUNT,
2824
VM_HOSTNAME_PATTERN,
29-
VM_NET,
3025
)
3126

3227

@@ -70,14 +65,6 @@ def clean_all(route_network, datacenter_type, vm_hostname=None):
7065
# Remove all connected Serveradmin objects.
7166
clean_serveradmin({'hostname': Regexp(pattern)})
7267

73-
# Try to remove VMs with the same IP in any case because we use custom
74-
# logic to assign them and we want to avoid IP address conflicts.
75-
# Index 1 is usually used for the test's subject VM,
76-
# 2 might be used for testing IP change.
77-
for ip_attr in ('ipv4', 'ipv6'):
78-
ips = [get_next_address(VM_NET, i, ip_attr) for i in [1, 2]]
79-
clean_serveradmin({ip_attr: Any(*ips)})
80-
8168

8269
def clean_hv(hv, pattern):
8370
# We never know what happened on the HV, so always refresh
@@ -207,30 +194,3 @@ def _wait_for_state_reached(ec2_client, instance_id: str, state: str,
207194
if not any(error in str(e) for error in
208195
['InvalidInstanceID', 'IncorrectInstanceState']):
209196
raise
210-
211-
212-
def get_next_address(vm_net, index, ip_attr):
213-
non_vm_hosts = list(Query({
214-
'project_network': vm_net,
215-
'servertype': Not('vm'),
216-
}, [ip_attr]))
217-
offset = 1 if len(non_vm_hosts) > 0 else 0
218-
subnet_levels = ceil(log(PYTEST_XDIST_WORKER_COUNT + offset, 2))
219-
project_network = Query({'hostname': vm_net}, [ip_attr]).get()
220-
try:
221-
subnets = list(project_network[ip_attr].subnets(subnet_levels))
222-
except ValueError:
223-
raise IGVMTestError(
224-
'Can\'t split {} into enough subnets '
225-
'for {} parallel tests'.format(
226-
vm_net, PYTEST_XDIST_WORKER_COUNT,
227-
)
228-
)
229-
if len(non_vm_hosts) > subnets[0].num_addresses:
230-
raise IGVMTestError(
231-
'Can\'t split {} into enough subnets '
232-
'for {} parallel tests'.format(
233-
vm_net, PYTEST_XDIST_WORKER_COUNT,
234-
)
235-
)
236-
return subnets[PYTEST_XDIST_WORKER + 1][index]

tests/test_integration.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
from tests.conftest import (
5959
clean_all,
6060
cmd,
61-
get_next_address,
6261
)
6362

6463
basicConfig(level=INFO)
@@ -114,7 +113,7 @@ def setUp(self):
114113
self.vm_obj['environment'] = 'testing'
115114
self.vm_obj['hostname'] = VM_HOSTNAME
116115
self.vm_obj['hypervisor'] = None
117-
self.vm_obj['intern_ip'] = get_next_address(VM_NET, 1, self.ip_attr)
116+
self.vm_obj['project_network'] = VM_NET
118117
self.vm_obj['memory'] = 2048
119118
self.vm_obj['no_monitoring'] = True
120119
self.vm_obj['num_cpu'] = 2
@@ -688,15 +687,11 @@ def test_reject_out_of_sync_serveradmin(self):
688687
vm_migrate(VM_HOSTNAME)
689688

690689
def test_new_address(self):
691-
# We don't have a way to ask for new IP address from Serveradmin
692-
# and lock it for us. The method below will usually work fine.
693-
# When it starts failing, we must develop retry method.
694-
new_address = get_next_address(VM_NET, 2, 'ipv4')
695-
690+
new_address = Query({'hostname': VM_NET}, ['intern_ip']).get_free_ip_addr(lock=True)
696691
change_address(VM_HOSTNAME, new_address, offline=True)
697692

698-
obj = Query({'hostname': VM_HOSTNAME}, ['intern_ip']).get()
699-
self.assertEqual(obj['intern_ip'], new_address)
693+
obj = Query({'hostname': VM_HOSTNAME}, ['ipv4']).get()
694+
self.assertEqual(obj['ipv4'], new_address)
700695
with _get_vm(VM_HOSTNAME) as vm:
701696
vm.run(cmd('ip a | grep {}', new_address))
702697
self.check_vm_present()

0 commit comments

Comments
 (0)