~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to tests/test_schedule_hetero_control.py

  • Committer: Curtis Hovey
  • Date: 2014-08-01 12:44:38 UTC
  • Revision ID: curtis@canonical.com-20140801124438-l48516pldkzh7g5n
Do not show all the files in the tarball because it distracts from the test output.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from __future__ import print_function
2
 
 
3
 
from datetime import timedelta
4
 
import json
5
 
import os
6
 
from time import time
7
 
from unittest import TestCase
8
 
 
9
 
from mock import (
10
 
    call,
11
 
    patch,
12
 
)
13
 
 
14
 
from jujuci import Credentials
15
 
from schedule_hetero_control import (
16
 
    build_jobs,
17
 
    calculate_jobs,
18
 
    get_args,
19
 
    get_candidate_info,
20
 
    get_releases,
21
 
    )
22
 
from utility import temp_dir
23
 
from test_utility import write_config
24
 
 
25
 
 
26
 
__metaclass__ = type
27
 
 
28
 
 
29
 
class TestScheduleHeteroControl(TestCase):
30
 
 
31
 
    def test_get_args_credentials(self):
32
 
        args, credentials = get_args(['root', '--user', 'jrandom',
33
 
                                      '--password', 'password'])
34
 
        self.assertEqual(args.user, 'jrandom')
35
 
        self.assertEqual(args.password, 'password')
36
 
        self.assertEqual(credentials, Credentials('jrandom', 'password'))
37
 
 
38
 
    def test_get_releases(self):
39
 
        with temp_dir() as root:
40
 
            old_juju_dir = os.path.join(root, 'old-juju')
41
 
            supported_juju = os.path.join(old_juju_dir, '1.24.5')
42
 
            devel_juju = os.path.join(old_juju_dir, '1.25-alpha1')
43
 
            test_juju = os.path.join(old_juju_dir, '1.24.5~')
44
 
            for path in (old_juju_dir, supported_juju, devel_juju, test_juju):
45
 
                os.mkdir(path)
46
 
            found = get_releases(root)
47
 
            self.assertEqual(['1.24.5'], list(found))
48
 
 
49
 
 
50
 
class TestBuildJobs(TestCase):
51
 
 
52
 
    def test_build_jobs_credentials(self):
53
 
        credentials = Credentials('jrandom', 'password1')
54
 
        with temp_dir() as root:
55
 
            write_config(root, 'compatibility-control', 'asdf')
56
 
            with patch('schedule_hetero_control.Jenkins',
57
 
                       autospec=True) as jenkins_mock:
58
 
                build_jobs(credentials, root, [])
59
 
            jenkins_mock.assert_called_once_with(
60
 
                'http://localhost:8080', 'jrandom', 'password1')
61
 
 
62
 
    def test_build_jobs(self):
63
 
        credentials = Credentials('jrandom', 'password1')
64
 
        jobs = []
65
 
        for os_str in ('ubuntu', 'osx', 'windows'):
66
 
            jobs.append({
67
 
                'old_version': "1.18.4",
68
 
                'candidate': "1.24.5",
69
 
                'new_to_old': 'true',
70
 
                'candidate_path': "1.24.5",
71
 
                'client_os': os_str,
72
 
                'revision_build': "2999"
73
 
            })
74
 
        calls = [
75
 
            call('compatibility-control',
76
 
                 {'candidate_path': '1.24.5', 'candidate': '1.24.5',
77
 
                  'new_to_old': 'true', 'revision_build': '2999',
78
 
                  'old_version': '1.18.4', 'client_os': 'ubuntu'},
79
 
                 token='asdf'),
80
 
            call('compatibility-control-osx',
81
 
                 {'candidate_path': '1.24.5', 'candidate': '1.24.5',
82
 
                  'new_to_old': 'true', 'revision_build': '2999',
83
 
                  'old_version': '1.18.4', 'client_os': 'osx'}, token='asdf'),
84
 
            call('compatibility-control-windows',
85
 
                 {'candidate_path': '1.24.5', 'candidate': '1.24.5',
86
 
                  'new_to_old': 'true', 'revision_build': '2999',
87
 
                  'old_version': '1.18.4', 'client_os': 'windows'},
88
 
                 token='asdf')]
89
 
        with temp_dir() as root:
90
 
            write_config(root, 'compatibility-control', 'asdf')
91
 
            with patch('schedule_hetero_control.Jenkins',
92
 
                       autospec=True) as jenkins_mock:
93
 
                build_jobs(credentials, root, jobs)
94
 
            jenkins_mock.assert_called_once_with(
95
 
                'http://localhost:8080', 'jrandom', 'password1')
96
 
            self.assertEqual(
97
 
                jenkins_mock.return_value.build_job.call_args_list, calls)
98
 
 
99
 
 
100
 
class TestGetCandidateInfo(TestCase):
101
 
    def test_get_candidate_info(self):
102
 
        with temp_dir() as dir_path:
103
 
            make_build_var_file(dir_path, version='1.24.3')
104
 
            version, revision = get_candidate_info(dir_path)
105
 
        self.assertEqual(version, '1.24.3')
106
 
        self.assertEqual(revision, '2870')
107
 
 
108
 
 
109
 
class CalculateJobs(TestCase):
110
 
    def test_calculate_jobs(self):
111
 
        with temp_dir() as root:
112
 
            release_path = os.path.join(root, 'old-juju', '1.20.11')
113
 
            os.makedirs(release_path)
114
 
            candidate_path = os.path.join(root, 'candidate', '1.24')
115
 
            os.makedirs(candidate_path)
116
 
            make_build_var_file(candidate_path, version='1.24.3')
117
 
            jobs = list(calculate_jobs(root))
118
 
        expected = self.make_jobs('1.24.3', '1.20.11', '1.24')
119
 
        self.assertItemsEqual(jobs, expected)
120
 
 
121
 
    def test_calculate_jobs_schedule_all(self):
122
 
        with temp_dir() as root:
123
 
            release_path = os.path.join(root, 'old-juju', '1.20.11')
124
 
            os.makedirs(release_path)
125
 
            candidate_path = os.path.join(root, 'candidate', '1.24')
126
 
            os.makedirs(candidate_path)
127
 
            make_build_var_file(candidate_path, '1.24.3')
128
 
            candidate_path_2 = os.path.join(root, 'candidate', '1.23')
129
 
            os.makedirs(candidate_path_2)
130
 
            buildvars_path = make_build_var_file(candidate_path_2, '1.23.3')
131
 
            a_week_ago = time() - timedelta(days=7, seconds=1).total_seconds()
132
 
            os.utime(buildvars_path, (time(), a_week_ago))
133
 
            jobs = list(calculate_jobs(root, schedule_all=False))
134
 
            jobs_schedule_all = list(calculate_jobs(root, schedule_all=True))
135
 
        expected = self.make_jobs('1.24.3', '1.20.11', '1.24')
136
 
        expected.extend(self.make_jobs('1.23.3', '1.20.11', '1.23'))
137
 
        self.assertItemsEqual(jobs, expected[:6])
138
 
        self.assertItemsEqual(jobs_schedule_all, expected)
139
 
 
140
 
    def test_calculate_jobs_ignore_1_26(self):
141
 
        with temp_dir() as root:
142
 
            release_path = os.path.join(root, 'old-juju', '1.20.11')
143
 
            os.makedirs(release_path)
144
 
            candidate_path = os.path.join(root, 'candidate', '1.24')
145
 
            os.makedirs(candidate_path)
146
 
            make_build_var_file(candidate_path, '1.24.3')
147
 
            candidate_path_2 = os.path.join(root, 'candidate', '1.26')
148
 
            os.makedirs(candidate_path_2)
149
 
            make_build_var_file(candidate_path_2, '1.26-aloha1')
150
 
            jobs = list(calculate_jobs(root))
151
 
        expected = self.make_jobs('1.24.3', '1.20.11', '1.24')
152
 
        self.assertItemsEqual(jobs, expected)
153
 
 
154
 
    def test_calculate_jobs_osx(self):
155
 
        with temp_dir() as root:
156
 
            release_path = os.path.join(root, 'old-juju', '1.20.11')
157
 
            os.makedirs(release_path)
158
 
            candidate_path = os.path.join(root, 'candidate', '1.24.4')
159
 
            os.makedirs(candidate_path)
160
 
            make_build_var_file(candidate_path, '1.24.4')
161
 
            jobs = list(calculate_jobs(root, schedule_all=False))
162
 
        expected = self.make_jobs('1.24.4', '1.20.11')
163
 
        self.assertItemsEqual(jobs, expected)
164
 
 
165
 
    def test_calculate_jobs_candidate_v2(self):
166
 
        with temp_dir() as root:
167
 
            release_path = os.path.join(root, 'old-juju', '1.20.11')
168
 
            os.makedirs(release_path)
169
 
            release_path = os.path.join(root, 'old-juju', '2.0.0')
170
 
            os.makedirs(release_path)
171
 
            candidate_path_2 = os.path.join(root, 'candidate', '2.0.1')
172
 
            os.makedirs(candidate_path_2)
173
 
            make_build_var_file(candidate_path_2, '2.0.1')
174
 
            jobs = list(calculate_jobs(root))
175
 
        expected = self.make_jobs('2.0.1',  '2.0.0')
176
 
        self.assertItemsEqual(jobs, expected)
177
 
 
178
 
    def test_calculate_jobs_candiade_v1_and_v2(self):
179
 
        with temp_dir() as root:
180
 
            release_path = os.path.join(root, 'old-juju', '1.20.11')
181
 
            os.makedirs(release_path)
182
 
            release_path = os.path.join(root, 'old-juju', '2.0.0')
183
 
            os.makedirs(release_path)
184
 
            candidate_path = os.path.join(root, 'candidate', '1.24.3')
185
 
            os.makedirs(candidate_path)
186
 
            make_build_var_file(candidate_path, '1.24.3')
187
 
            candidate_path_2 = os.path.join(root, 'candidate', '2.0.1')
188
 
            os.makedirs(candidate_path_2)
189
 
            make_build_var_file(candidate_path_2, '2.0.1')
190
 
            jobs = list(calculate_jobs(root))
191
 
        expected = self.make_jobs('2.0.1',  '2.0.0')
192
 
        expected.extend(self.make_jobs('1.24.3', '1.20.11'))
193
 
        self.assertItemsEqual(jobs, expected)
194
 
 
195
 
    def make_jobs(self, candidate, old_version, candidate_path=None):
196
 
        jobs = []
197
 
        for client_os in ('ubuntu', 'osx', 'windows'):
198
 
            for new_to_old in ('false', 'true'):
199
 
                jobs.append({
200
 
                    'candidate': candidate,
201
 
                    'candidate_path': candidate_path or candidate,
202
 
                    'client_os': client_os,
203
 
                    'new_to_old': new_to_old,
204
 
                    'old_version': old_version,
205
 
                    'revision_build': '2870'})
206
 
        return jobs
207
 
 
208
 
 
209
 
def make_build_var_file(dir_path, version, revision_build="2870"):
210
 
    build_vars = {"version": version, "revision_build": revision_build}
211
 
    file_path = os.path.join(dir_path, 'buildvars.json')
212
 
    with open(file_path, 'w') as json_file:
213
 
        json.dump(build_vars, json_file)
214
 
    return file_path