~opnfv-team/charms/trusty/odl-controller/Be

« back to all changes in this revision

Viewing changes to tests/charmhelpers/contrib/amulet/deployment.py

  • Committer: James Page
  • Date: 2015-11-12 11:25:54 UTC
  • mfrom: (10.1.2 odl-controller)
  • Revision ID: james.page@ubuntu.com-20151112112554-xbh2zetd8zzldl0z
Add unit tests, functional tests, Makefiles and tox configuration

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
5
# charm-helpers is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3 as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# charm-helpers is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
import amulet
 
18
import os
 
19
import six
 
20
 
 
21
 
 
22
class AmuletDeployment(object):
 
23
    """Amulet deployment.
 
24
 
 
25
       This class provides generic Amulet deployment and test runner
 
26
       methods.
 
27
       """
 
28
 
 
29
    def __init__(self, series=None):
 
30
        """Initialize the deployment environment."""
 
31
        self.series = None
 
32
 
 
33
        if series:
 
34
            self.series = series
 
35
            self.d = amulet.Deployment(series=self.series)
 
36
        else:
 
37
            self.d = amulet.Deployment()
 
38
 
 
39
    def _add_services(self, this_service, other_services):
 
40
        """Add services.
 
41
 
 
42
           Add services to the deployment where this_service is the local charm
 
43
           that we're testing and other_services are the other services that
 
44
           are being used in the local amulet tests.
 
45
           """
 
46
        if this_service['name'] != os.path.basename(os.getcwd()):
 
47
            s = this_service['name']
 
48
            msg = "The charm's root directory name needs to be {}".format(s)
 
49
            amulet.raise_status(amulet.FAIL, msg=msg)
 
50
 
 
51
        if 'units' not in this_service:
 
52
            this_service['units'] = 1
 
53
 
 
54
        self.d.add(this_service['name'], units=this_service['units'],
 
55
                   constraints=this_service.get('constraints'))
 
56
 
 
57
        for svc in other_services:
 
58
            if 'location' in svc:
 
59
                branch_location = svc['location']
 
60
            elif self.series:
 
61
                branch_location = 'cs:{}/{}'.format(self.series, svc['name']),
 
62
            else:
 
63
                branch_location = None
 
64
 
 
65
            if 'units' not in svc:
 
66
                svc['units'] = 1
 
67
 
 
68
            self.d.add(svc['name'], charm=branch_location, units=svc['units'],
 
69
                       constraints=svc.get('constraints'))
 
70
 
 
71
    def _add_relations(self, relations):
 
72
        """Add all of the relations for the services."""
 
73
        for k, v in six.iteritems(relations):
 
74
            self.d.relate(k, v)
 
75
 
 
76
    def _configure_services(self, configs):
 
77
        """Configure all of the services."""
 
78
        for service, config in six.iteritems(configs):
 
79
            self.d.configure(service, config)
 
80
 
 
81
    def _deploy(self):
 
82
        """Deploy environment and wait for all hooks to finish executing."""
 
83
        try:
 
84
            self.d.setup(timeout=900)
 
85
            self.d.sentry.wait(timeout=900)
 
86
        except amulet.helpers.TimeoutError:
 
87
            amulet.raise_status(amulet.FAIL, msg="Deployment timed out")
 
88
        except Exception:
 
89
            raise
 
90
 
 
91
    def run_tests(self):
 
92
        """Run all of the methods that are prefixed with 'test_'."""
 
93
        for test in dir(self):
 
94
            if test.startswith('test_'):
 
95
                getattr(self, test)()