~lutostag/ubuntu/trusty/maas/1.5.2+packagefix

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2014-03-28 10:43:53 UTC
  • mto: This revision was merged to the branch mainline in revision 57.
  • Revision ID: package-import@ubuntu.com-20140328104353-ekpolg0pm5xnvq2s
Tags: upstream-1.5+bzr2204
ImportĀ upstreamĀ versionĀ 1.5+bzr2204

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Helpers for architectures in testing."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
str = None
 
13
 
 
14
__metaclass__ = type
 
15
__all__ = [
 
16
    'make_usable_architecture',
 
17
    'patch_usable_architectures',
 
18
    ]
 
19
 
 
20
from random import randint
 
21
 
 
22
from maasserver import forms
 
23
from maasserver.testing.factory import factory
 
24
 
 
25
 
 
26
def make_arch(with_subarch=True, arch_name=None, subarch_name=None):
 
27
    """Generate an arbitrary architecture name.
 
28
 
 
29
    :param with_subarch: Should the architecture include a slash and a
 
30
        sub-architecture name?  Defaults to `True`.
 
31
    """
 
32
    if arch_name is None:
 
33
        arch_name = factory.make_name('arch')
 
34
    if with_subarch:
 
35
        if subarch_name is None:
 
36
            subarch_name = factory.make_name('sub')
 
37
        return '%s/%s' % (arch_name, subarch_name)
 
38
    else:
 
39
        return arch_name
 
40
 
 
41
 
 
42
def patch_usable_architectures(testcase, architectures=None):
 
43
    """Set a fixed list of usable architecture names.
 
44
 
 
45
    A usable architecture is one for which boot images are available.
 
46
 
 
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.
 
50
    """
 
51
    if architectures is None:
 
52
        architectures = [
 
53
            "%s/%s" % (factory.make_name('arch'), factory.make_name('sub'))
 
54
            for _ in range(randint(0, 2))
 
55
            ]
 
56
    patch = testcase.patch(forms, 'list_all_usable_architectures')
 
57
    patch.return_value = architectures
 
58
 
 
59
 
 
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."
 
63
 
 
64
    A usable architecture is one for which boot images are available.
 
65
 
 
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.
 
74
    """
 
75
    arch = make_arch(
 
76
        with_subarch=with_subarch, arch_name=arch_name,
 
77
        subarch_name=subarch_name)
 
78
    patch_usable_architectures(testcase, [arch])
 
79
    return arch