1
# Copyright 2014 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Helpers for architectures in testing."""
6
from __future__ import (
16
'make_usable_architecture',
17
'patch_usable_architectures',
20
from random import randint
22
from maasserver import forms
23
from maasserver.testing.factory import factory
26
def make_arch(with_subarch=True, arch_name=None, subarch_name=None):
27
"""Generate an arbitrary architecture name.
29
:param with_subarch: Should the architecture include a slash and a
30
sub-architecture name? Defaults to `True`.
33
arch_name = factory.make_name('arch')
35
if subarch_name is None:
36
subarch_name = factory.make_name('sub')
37
return '%s/%s' % (arch_name, subarch_name)
42
def patch_usable_architectures(testcase, architectures=None):
43
"""Set a fixed list of usable architecture names.
45
A usable architecture is one for which boot images are available.
47
:param testcase: A `TestCase` whose `patch` this function can use.
48
:param architectures: Optional list of architecture names. If omitted,
49
defaults to a list (which may be empty) of random architecture names.
51
if architectures is None:
53
"%s/%s" % (factory.make_name('arch'), factory.make_name('sub'))
54
for _ in range(randint(0, 2))
56
patch = testcase.patch(forms, 'list_all_usable_architectures')
57
patch.return_value = architectures
60
def make_usable_architecture(
61
testcase, with_subarch=True, arch_name=None, subarch_name=None):
62
"""Return arbitrary architecture name, and make it "usable."
64
A usable architecture is one for which boot images are available.
66
:param testcase: A `TestCase` whose `patch` this function can pass to
67
`patch_usable_architectures`.
68
:param with_subarch: Should the architecture include a slash and a
69
sub-architecture name? Defaults to `True`.
70
:param arch_name: The architecture name. Useful in cases where
71
we need to test that not supplying an arch works correctly.
72
:param subarch_name: The subarchitecture name. Useful in cases where
73
we need to test that not supplying a subarch works correctly.
76
with_subarch=with_subarch, arch_name=arch_name,
77
subarch_name=subarch_name)
78
patch_usable_architectures(testcase, [arch])