~vila/uci-engine/enable-nova-and-swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
# Ubuntu CI Engine
# Copyright 2014 Canonical Ltd.

# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from cStringIO import StringIO
import mock
import os
import sys
import unittest
import yaml

from ucitests import fixtures


HERE = os.path.abspath(os.path.dirname(__file__))
# update.py isn't in our pythonpath
sys.path.append(os.path.join(HERE, '..', 'juju-deployer'))
import update

from bzrlib import branchbuilder
from bzrlib.transport import memory


class TestOptionParsing(unittest.TestCase):

    def setUp(self):
        super(TestOptionParsing, self).setUp()
        self.out = StringIO()
        self.err = StringIO()

    def parse_args(self, args):
        ns = update.UpdateArgParser().parse_args(args, self.out, self.err)
        return ns

    def test_default_values(self):
        ns = self.parse_args([])
        self.assertFalse(ns.assert_pinned)
        self.assertFalse(ns.check)

    def test_assert_pinned(self):
        ns = self.parse_args(['--assert-pinned'])
        self.assertTrue(ns.assert_pinned)

    def test_check(self):
        ns = self.parse_args(['--check'])
        self.assertTrue(ns.check)


class TestGetDeployerConfigs(unittest.TestCase):

    def setUp(self):
        super(TestGetDeployerConfigs, self).setUp()
        fixtures.set_uniq_cwd(self)

    def test_get_deployer_no_configs(self):
        configs = list(update.get_deployer_configs('.'))
        self.assertEqual(configs, [])

    def test_get_deployer_two_configs(self):
        for x in ('foo.yaml', 'bar.yaml.tmpl'):
            with open(x, 'w') as fp:
                fp.write('nothing: here')
        self.assertEqual(sorted(['./bar.yaml.tmpl', './foo.yaml']),
                         sorted(update.get_deployer_configs('.')))


class TestUpdate(unittest.TestCase):

    def setUp(self):
        super(TestUpdate, self).setUp()
        fixtures.set_uniq_cwd(self)
        fixtures.override_env(self, 'BZR_EMAIL', 'foo@example.com')
        fixtures.override_env(self, 'BZR_HOME', self.uniq_dir)
        # We need a real branch with at least 2 revisions (so we can check out
        # of date references)
        builder = branchbuilder.BranchBuilder(
            memory.MemoryTransport("memory:///"))
        builder.start_series()
        builder.build_snapshot(
            'rev-id', None,
            [('add', ('', 'root-id', 'directory', '')),
             ('add', ('filename', 'f-id', 'file', 'content\n'))])
        builder.build_snapshot('rev2-id', ['rev-id'],
                               [('modify', ('f-id', 'new-content\n'))])
        builder.finish_series()
        # get_branches_and_revnos won't be able to use a in-memory branch,
        # let's create one on disk.
        tree = builder.get_branch().create_checkout('the_branch')
        self.branch = tree.branch

    def get_branch_url(self):
        return self.branch.user_url

    def get_service_yaml(self, revno=None):
        """Returns an from a yaml string for an arbitrary service.

        This defines just enough data for the tests: the branch a service is
        based on.

        :param revno: An optional revno to pin the branch at.
        """
        branch_url = self.get_branch_url()
        spec = '{}'.format(branch_url)
        if revno is not None:
            spec = spec + '@{}'.format(revno)
        return {'ci-eng': {'services': {'gunicorn': {'branch': spec}}}}

    def test_ensure_all_branches_are_pinned(self):
        yaml_string = self.get_service_yaml(1)
        path = 'foo.yaml'
        with open(path, 'w') as fp:
            fp.write(yaml.safe_dump(yaml_string))
        self.assertTrue(update.ensure_all_branches_are_pinned([path]))

    def test_ensure_some_branches_are_not_pinned(self):
        yaml_string = self.get_service_yaml()
        path = 'foo.yaml'
        with open(path, 'w') as fp:
            fp.write(yaml.safe_dump(yaml_string))
        self.assertFalse(update.ensure_all_branches_are_pinned([path]))

    def test_get_branches_and_revnos(self):
        branch_url = self.get_branch_url()
        current = self.branch.revno()
        y = self.get_service_yaml(current - 1)
        path = 'foo.yaml'
        with open(path, 'w') as fp:
            fp.write(yaml.safe_dump(y))

        with mock.patch('update.log.warning') as warn:
            actual = update.get_branches_and_revnos(
                [path], True)
            msg = '{} is out of date (currently {}, latest {})'
            msg = msg.format(branch_url, current - 1, current)
            warn.assert_called_with(msg)
        self.assertEqual(actual, {branch_url: current})

    def test_set_branches_and_revnos_on_naked_branches(self):
        branch_url = self.get_branch_url()
        y = self.get_service_yaml()
        path = 'foo.yaml'
        with open(path, 'w') as fp:
            fp.write(yaml.safe_dump(y))
        update.set_branches_and_revnos([path], {branch_url: 1})
        with open(path) as fp:
            y = yaml.safe_load(fp.read())

        actual_branch = y['ci-eng']['services']['gunicorn']['branch']
        self.assertEqual(actual_branch, '{}@1'.format(branch_url))

    def test_set_branches_and_revnos_on_pinned_branches(self):
        branch_url = self.get_branch_url()
        y = self.get_service_yaml(1)
        path = 'foo.yaml'
        with open(path, 'w') as fp:
            fp.write(yaml.safe_dump(y))
        update.set_branches_and_revnos([path], {branch_url: 2})
        with open(path) as fp:
            y = yaml.safe_load(fp.read())

        actual_branch = y['ci-eng']['services']['gunicorn']['branch']
        self.assertEqual(actual_branch, '{}@2'.format(branch_url))


if __name__ == '__main__':
    unittest.main()