~maas-committers/maas/trunk

« back to all changes in this revision

Viewing changes to src/maasserver/testing/factory.py

[r=ltrager][bug=1558635][author=allenap] When adding devices via the Web UI, cope with MAC addresses of different forms and cases.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
from datetime import timedelta
12
12
import hashlib
13
13
from io import BytesIO
 
14
from itertools import (
 
15
    chain,
 
16
    repeat,
 
17
)
14
18
import logging
15
19
import random
16
20
import time
127
131
# src/maasserver/tests/data/test_rsa{0, 1, 2, 3, 4}.pub
128
132
MAX_PUBLIC_KEYS = 5
129
133
 
130
 
 
 
134
# Used to save a node without worrying about a valid transition.
131
135
ALL_NODE_STATES = list(map_enum(NODE_STATUS).values())
132
136
 
 
137
# Maximum PID available on this machine.
 
138
with open("/proc/sys/kernel/pid_max") as fd:
 
139
    PID_MAX = int(fd.read())
133
140
 
134
141
# Use `undefined` instead of `None` for default factory arguments when `None`
135
142
# is a reasonable value for the argument.
281
288
        region.save()
282
289
        return region
283
290
 
 
291
    # PIDs for use with make_RegionControllerProcess. Note that the simpler
 
292
    # cycle(range(...)) is not used because it gradually consumes all memory.
 
293
    _rcp_pids = chain.from_iterable(repeat(range(PID_MAX)))
 
294
 
284
295
    def make_RegionControllerProcess(
285
296
            self, region=None, pid=None, updated=None):
286
297
        if region is None:
287
298
            region = self.make_RegionController()
288
299
        if pid is None:
289
 
            pid = random.randint(1, 10000)
 
300
            pid = next(self._rcp_pids)
290
301
        process = RegionControllerProcess(
291
302
            region=region, pid=pid, updated=updated)
292
303
        process.save()
293
304
        return process
294
305
 
 
306
    # Ports for use with make_RegionControllerProcessEndpoint. Note that the
 
307
    # simpler cycle(range(...)) is not used because it gradually consumes all
 
308
    # memory.
 
309
    _rcpe_ports = chain.from_iterable(repeat(range(2**16)))
 
310
 
295
311
    def make_RegionControllerProcessEndpoint(
296
312
            self, process=None, address=None, port=None):
297
313
        if process is None:
299
315
        if address is None:
300
316
            address = self.make_ip_address()
301
317
        if port is None:
302
 
            port = random.randint(1, 10000)
 
318
            port = next(self._rcpe_ports)
303
319
        endpoint = RegionControllerProcessEndpoint(
304
320
            process=process, address=address, port=port)
305
321
        endpoint.save()