~juju-qa/charms/trusty/juju-reports/trunk

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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import ConfigParser
from contextlib import contextmanager
from mock import patch
import os
import shutil
import sys
from tempfile import mkdtemp
from textwrap import dedent
from unittest import TestCase

HOOKS_DIR = os.path.join(os.path.dirname(__file__), '../hooks')
sys.path.insert(0, HOOKS_DIR)
from common import (
    call_bzr,
    ensure_deleted,
    finish_update_source,
    get_last_revision,
    HOME,
    INI,
    install_cronjob,
    install_ini,
    make,
    PROJECT_DIR,
    update_ini,
    update_source,
)


@contextmanager
def temp_dir(parent=None):
    directory = mkdtemp(dir=parent)
    try:
        yield directory
    finally:
        shutil.rmtree(directory)


class Contexter():

    def __init__(self, return_value):
        self.return_value = return_value

    @contextmanager
    def __call__(self, *args):
        yield self.return_value


def write_file(contents, file_name, parent=None):
    if parent is not None:
        file_name = os.path.join(parent, file_name)
    with open(file_name, 'w') as out_file:
        out_file.write(contents)
    return file_name


class CommonTestCase(TestCase):

    def test_update_ini(self):
        with temp_dir() as base_dir:
            ini_path = os.path.join(base_dir, 'foo.ini')
            with open(ini_path, 'w') as f:
                f.write('[app:main]\n')
            with update_ini(base_dir, ini_path) as config:
                config.set('app:main', 'bar', 'baz')
            with open(ini_path) as f:
                content = f.read()
            self.assertEqual('[app:main]\nbar = baz\n\n', content)

    def test_install_ini(self):
        config = ConfigParser.ConfigParser()
        config.add_section('app:main')
        contexter = Contexter(config)
        with patch('common.update_ini', autospec=True,
                   side_effect=contexter) as mock:
            ini = install_ini(
                'mongo_url', 'gh_token', 'cidata_url', 'token',
                'error_email', 'error_email_from',
                'cloud_health_failure_threshold', 'current_conditions_list',
                'jujuci_url', 'jujuci_username', 'jujuci_password',
                'source_path')
        self.assertEqual(2, mock.call_count)
        mock.assert_any_call('source_path', INI)
        mock.assert_any_call('source_path', 'test.ini')
        self.assertIs(config, ini)
        self.assertEqual('mongo_url', ini.get('app:main', 'mongo.url'))
        self.assertEqual('gh_token', ini.get('app:main', 'gh_token'))
        self.assertEqual('jujuci_url', ini.get('app:main', 'jujuci.url'))
        self.assertEqual(
            'jujuci_username', ini.get('app:main', 'jujuci.username'))
        self.assertEqual(
            'jujuci_password', ini.get('app:main', 'jujuci.password'))
        self.assertEqual('cidata_url', ini.get('app:main', 'cidata.url'))
        self.assertEqual(
            'token', ini.get('app:main', 'charm_bundle_test_token'))
        self.assertEqual(HOME, ini.get('app:main', 'HOME'))
        self.assertEqual('error_email', ini.get('app:main', 'error_email'))
        self.assertEqual(
            'error_email_from', ini.get('app:main', 'error_email_from'))

    def test_ensure_deleted(self):
        with temp_dir() as file_dir:
            file_path = write_file('', 'file', file_dir)
            self.assertTrue(os.path.exists(file_path))
            ensure_deleted(file_path)
            self.assertFalse(os.path.exists(file_path))
            # No error if the file does not exist.
            ensure_deleted(file_path)

    def test_make(self):
        with temp_dir() as make_dir:
            make_path = os.path.join(make_dir, 'Makefile')
            with open(make_path, 'w') as makefile:
                makefile.write(dedent("""\
                    foo:
                    \techo foo
                    bar:
                    \techo bar > bar
                    """))
            make('bar', make_dir)
            with open(os.path.join(make_dir, 'bar')) as bar_file:
                self.assertEqual(bar_file.read(), 'bar\n')

    def make_source_branch(self, branch_path):
        call_bzr(['whoami', 'jrandom@example.com'])
        call_bzr(['init', branch_path])
        foo_path = os.path.join(branch_path, 'foo')
        write_file('foo', foo_path)
        makefile_path = write_file(dedent("""\
            distclean:
            \techo 'bar'> distclean
            """), 'Makefile', branch_path)
        call_bzr(['add', foo_path, makefile_path])
        call_bzr(['commit', foo_path, makefile_path, '-m', 'added foo'])

    @contextmanager
    def update_source_context(self, workspace):
        project_dir = os.path.join(workspace, 'project-dir')
        fake_home = os.path.join(workspace, 'fake-home')
        revision_info_file = os.path.join(workspace, 'revision-info')
        os.mkdir(fake_home)

        def missing_raise(path, user, group):
            os.stat(path)

        with patch('common.HOME', fake_home):
            with patch('common.PROJECT_DIR', project_dir):
                with patch('common.REVISION_INFO_FILE', revision_info_file):
                    with patch('common.PROJECT_DIR', project_dir):
                        with patch('common.getpwnam'):
                            with patch('os.chown', side_effect=missing_raise):
                                yield project_dir

    def test_update_source_in_place(self):
        with temp_dir() as workspace:
            source_branch = os.path.join(workspace, 'source')
            with patch.dict(os.environ, {'BZR_HOME': workspace}):
                self.make_source_branch(source_branch)
                with self.update_source_context(workspace) as project_dir:
                    source_path = update_source(source_branch, 1,
                                                in_place=True)
                self.assertEqual(source_path, project_dir)
                self.assertTrue(os.path.exists(os.path.join(
                    project_dir, 'foo')))
                self.assertFalse(os.path.islink(project_dir))

    def test_update_source_to_in_place(self):
        with temp_dir() as workspace:
            source_branch = os.path.join(workspace, 'source')
            with patch.dict(os.environ, {'BZR_HOME': workspace}):
                self.make_source_branch(source_branch)
                with self.update_source_context(workspace) as project_dir:
                    out_branch = update_source(source_branch, 1,
                                               in_place=False)
                    self.assertNotEqual(out_branch, project_dir)
                    finish_update_source(out_branch, source_branch, 1)
                    self.assertTrue(os.path.exists(os.path.join(
                        project_dir, 'foo')))
                    self.assertTrue(os.path.islink(project_dir))
                    out_branch = update_source(source_branch, 1, in_place=True)
                    self.assertEqual(out_branch, project_dir)
                    self.assertTrue(os.path.exists(os.path.join(
                        project_dir, 'foo')))
                    self.assertFalse(os.path.islink(project_dir))
                    with open(os.path.join(project_dir, 'distclean')) as out:
                        self.assertEqual(out.read(), 'bar\n')

    def test_finish_update_source_in_place(self):
        with temp_dir() as workspace:
            revision_info = os.path.join(workspace, 'revision-info')
            with patch('common.REVISION_INFO_FILE', revision_info):
                self.assertEqual(get_last_revision(), (None, None))
                with patch('os.symlink', side_effect=Exception):
                    finish_update_source(PROJECT_DIR, 'foo', 'bar')
                self.assertEqual(get_last_revision(), ('foo', 'bar'))

    def test_finish_update_source_not_in_place(self):
        with temp_dir() as workspace:
            with self.update_source_context(workspace) as project_dir:
                self.assertEqual(get_last_revision(), (None, None))
                finish_update_source(workspace, 'foo', 'bar')
                self.assertEqual(get_last_revision(), ('foo', 'bar'))
                self.assertEqual(os.readlink(project_dir), workspace)

    def test_install_cronjob(self):
        with patch('common.update_cronjob', autospec=True) as mock_up:
            with patch('common.ensure_dir', autospec=True) as mock_ed:
                install_cronjob('9', 'admin@ci.testing')
        mock_ed.assert_called_once_with('/home/ubuntu/locks')
        self.assertEqual(1, mock_up.call_count)
        cronstring = mock_up.call_args[0][0]
        self.assertRegexpMatches(
            cronstring,
            r'(?m)^MAILTO=admin@ci\.testing$'
        )
        self.assertRegexpMatches(
            cronstring,
            r'(?m)^\*/9(?: \*){4} ubuntu'
            r' flock -n /home/ubuntu/locks/get_s3_ci_results.lock'
            r' get-s3-ci-results --email-logs --log-file'
            r' /home/ubuntu/logs/get-s3-ci-results.log$'
        )