~jelmer/byoci/brz-debian

« back to all changes in this revision

Viewing changes to byoci/tests/features.py

  • Committer: Vincent Ladeuil
  • Date: 2018-02-16 18:34:49 UTC
  • mfrom: (156.1.19 split)
  • Revision ID: v.ladeuil+lp@free.fr-20180216183449-jb0l1xbvdcrv4xzw
The three main components can now be setup independently

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# This file is part of Online Services Jenkaas.
 
1
# This file is part of Build Your Own CI
2
2
#
 
3
# Copyright 2017, 2018 Vincent Ladeuil
3
4
# Copyright 2016, 2017 Canonical Ltd.
4
5
#
5
6
# This program is free software: you can redistribute it and/or modify it under
14
15
# You should have received a copy of the GNU General Public License along with
15
16
# this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
 
17
 
from __future__ import unicode_literals
18
 
 
19
18
import os
20
19
 
21
 
from olstests import features
22
 
from olsvms import subprocesses
23
 
from olsvms.tests import features as vms_features
 
20
from byot import features
24
21
 
25
22
HERE = os.path.abspath(os.path.dirname(__file__))
26
23
 
27
 
# Useful shortcuts
28
 
requires = features.requires
29
 
ssh_feature = vms_features.ssh_feature
30
 
 
31
 
 
32
 
ssh_agent_feature = features.ExecutableFeature('ssh-agent')
33
 
 
34
 
 
35
 
# FIXME: Should be backported to olstests -- vila 2017-01-04
36
 
def test_requires(test, feature):
37
 
    """Skip a test if a feature is not available."""
38
 
    if not feature.available():
39
 
        reason = '{} is not available'.format(feature.feature_name())
40
 
        test.skipTest(reason)
41
 
 
42
 
 
43
 
class QaStagingToken(features.Feature):
44
 
    """Credentials for qastaging launchpad."""
45
 
 
46
 
    def __init__(self):
47
 
        super(QaStagingToken, self).__init__()
48
 
        # FIXME: This is not available in production -- vila 2016-11-25
49
 
        # FIXME: This is a config option one way or the other. The /intent/ is
50
 
        # for the dev to control whether he want to test against qastaging or
51
 
        # not by providing (or not) a valid credentials file -- vila 2017-11-29
52
 
        self.path = os.path.join(HERE, '..', '..', '..', 'my-secrets',
53
 
                                 'launchpad', 'qastaging')
54
 
 
55
 
    def _probe(self):
56
 
        # We only test that the credentials file exists here, not its validity.
57
 
        return os.path.exists(self.path)
58
 
 
59
 
    def feature_name(self):
60
 
        return 'An OAuth token in {} for qastaging launchpad'.format(self.path)
61
 
 
62
 
 
63
 
qastaging_token = QaStagingToken()
64
 
 
65
 
 
66
 
class BzrIdentity(features.Feature):
67
 
    """A feature capturing the current bzr whoami."""
68
 
 
69
 
    def __init__(self):
70
 
        super(BzrIdentity, self).__init__()
71
 
        self.identify = None
72
 
 
73
 
    def _probe(self):
74
 
        try:
75
 
            _, out, _ = subprocesses.run(['bzr', 'whoami'])
76
 
            self.identity = out.strip()
77
 
            return True
78
 
        except subprocesses.errors.CommandError:
79
 
            return False
80
 
 
81
 
    def feature_name(self):
82
 
        return 'An existing bazaar whoami'
83
 
 
84
 
 
85
 
bzr_identity = BzrIdentity()
86
 
 
87
 
 
88
 
class GitIdentity(features.Feature):
89
 
    """A feature capturing the current git user email."""
90
 
 
91
 
    def __init__(self):
92
 
        super(GitIdentity, self).__init__()
93
 
        self.name = None
94
 
        self.email = None
95
 
 
96
 
    def _probe(self):
97
 
        try:
98
 
            _, out, _ = subprocesses.run(['git', 'config', 'user.name'])
99
 
            self.name = out.strip()
100
 
            _, out, _ = subprocesses.run(['git', 'config', 'user.email'])
101
 
            self.email = out.strip()
102
 
            return True
103
 
        except subprocesses.errors.CommandError:
104
 
            return False
105
 
 
106
 
    def feature_name(self):
107
 
        return 'An existing git user name and email'
108
 
 
109
 
 
110
 
git_identity = GitIdentity()
111
 
 
112
 
 
113
 
class LaunchpadIdentity(features.Feature):
114
 
    """A feature capturing the current bzr launchpad login."""
115
 
 
116
 
    def __init__(self):
117
 
        super(LaunchpadIdentity, self).__init__()
118
 
        self.identify = None
119
 
 
120
 
    def _probe(self):
121
 
        try:
122
 
            _, out, _ = subprocesses.run(['bzr', 'launchpad-login'])
123
 
            self.identity = out.strip()
124
 
            return True
125
 
        except subprocesses.errors.CommandError:
126
 
            return False
127
 
 
128
 
    def feature_name(self):
129
 
        return 'An existing launchpad login'
130
 
 
131
 
 
132
 
launchpad_identity = LaunchpadIdentity()
 
24
lxd_client_feature = features.ExecutableFeature('lxc')
 
25
make_feature = features.ExecutableFeature('make')
133
26
 
134
27
 
135
28
# Features relying on an environment property should be checked before any test
136
29
# isolation is setup. Once this is done, they can be freely used to decorate
137
30
# tests [classes] or during setUp().
138
31
 
139
 
qastaging_token.available()
140
 
bzr_identity.available()
141
 
git_identity.available()
142
 
launchpad_identity.available()
 
32
lxd_client_feature.available()
 
33
make_feature.available()
 
34
 
 
35
 
 
36
# Useful shortcuts to export but not use internally
 
37
ExecutableFeature = features.ExecutableFeature
 
38
Feature = features.Feature
 
39
requires = features.requires
 
40
test_requires = features.test_requires