~abentley/juju-ci-tools/client-from-config-4

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
from __future__ import print_function

from datetime import timedelta
import json
import os
from time import time
from unittest import TestCase

from mock import (
    call,
    patch,
)

from jujuci import Credentials
from schedule_hetero_control import (
    build_jobs,
    calculate_jobs,
    get_args,
    get_candidate_info,
    get_releases,
    )
from utility import temp_dir
from test_utility import write_config


__metaclass__ = type


class TestScheduleHeteroControl(TestCase):

    def test_get_args_credentials(self):
        args, credentials = get_args(['root', '--user', 'jrandom',
                                      '--password', 'password'])
        self.assertEqual(args.user, 'jrandom')
        self.assertEqual(args.password, 'password')
        self.assertEqual(credentials, Credentials('jrandom', 'password'))

    def test_get_releases(self):
        with temp_dir() as root:
            old_juju_dir = os.path.join(root, 'old-juju')
            supported_juju = os.path.join(old_juju_dir, '1.24.5')
            devel_juju = os.path.join(old_juju_dir, '1.25-alpha1')
            test_juju = os.path.join(old_juju_dir, '1.24.5~')
            for path in (old_juju_dir, supported_juju, devel_juju, test_juju):
                os.mkdir(path)
            found = get_releases(root)
            self.assertEqual(['1.24.5'], list(found))


class TestBuildJobs(TestCase):

    def test_build_jobs_credentials(self):
        credentials = Credentials('jrandom', 'password1')
        with temp_dir() as root:
            write_config(root, 'compatibility-control', 'asdf')
            with patch('schedule_hetero_control.Jenkins',
                       autospec=True) as jenkins_mock:
                build_jobs(credentials, root, [])
            jenkins_mock.assert_called_once_with(
                'http://localhost:8080', 'jrandom', 'password1')

    def test_build_jobs(self):
        credentials = Credentials('jrandom', 'password1')
        jobs = []
        for os_str in ('ubuntu', 'osx', 'windows'):
            jobs.append({
                'old_version': "1.18.4",
                'candidate': "1.24.5",
                'new_to_old': 'true',
                'candidate_path': "1.24.5",
                'client_os': os_str,
                'revision_build': "2999"
            })
        calls = [
            call('compatibility-control',
                 {'candidate_path': '1.24.5', 'candidate': '1.24.5',
                  'new_to_old': 'true', 'revision_build': '2999',
                  'old_version': '1.18.4', 'client_os': 'ubuntu'},
                 token='asdf'),
            call('compatibility-control-osx',
                 {'candidate_path': '1.24.5', 'candidate': '1.24.5',
                  'new_to_old': 'true', 'revision_build': '2999',
                  'old_version': '1.18.4', 'client_os': 'osx'}, token='asdf'),
            call('compatibility-control-windows',
                 {'candidate_path': '1.24.5', 'candidate': '1.24.5',
                  'new_to_old': 'true', 'revision_build': '2999',
                  'old_version': '1.18.4', 'client_os': 'windows'},
                 token='asdf')]
        with temp_dir() as root:
            write_config(root, 'compatibility-control', 'asdf')
            with patch('schedule_hetero_control.Jenkins',
                       autospec=True) as jenkins_mock:
                build_jobs(credentials, root, jobs)
            jenkins_mock.assert_called_once_with(
                'http://localhost:8080', 'jrandom', 'password1')
            self.assertEqual(
                jenkins_mock.return_value.build_job.call_args_list, calls)


class TestGetCandidateInfo(TestCase):
    def test_get_candidate_info(self):
        with temp_dir() as dir_path:
            make_build_var_file(dir_path, version='1.24.3')
            version, revision = get_candidate_info(dir_path)
        self.assertEqual(version, '1.24.3')
        self.assertEqual(revision, '2870')


class CalculateJobs(TestCase):
    def test_calculate_jobs(self):
        with temp_dir() as root:
            release_path = os.path.join(root, 'old-juju', '1.20.11')
            os.makedirs(release_path)
            candidate_path = os.path.join(root, 'candidate', '1.24')
            os.makedirs(candidate_path)
            make_build_var_file(candidate_path, version='1.24.3')
            jobs = list(calculate_jobs(root))
        expected = self.make_jobs('1.24.3', '1.20.11', '1.24')
        self.assertItemsEqual(jobs, expected)

    def test_calculate_jobs_schedule_all(self):
        with temp_dir() as root:
            release_path = os.path.join(root, 'old-juju', '1.20.11')
            os.makedirs(release_path)
            candidate_path = os.path.join(root, 'candidate', '1.24')
            os.makedirs(candidate_path)
            make_build_var_file(candidate_path, '1.24.3')
            candidate_path_2 = os.path.join(root, 'candidate', '1.23')
            os.makedirs(candidate_path_2)
            buildvars_path = make_build_var_file(candidate_path_2, '1.23.3')
            a_week_ago = time() - timedelta(days=7, seconds=1).total_seconds()
            os.utime(buildvars_path, (time(), a_week_ago))
            jobs = list(calculate_jobs(root, schedule_all=False))
            jobs_schedule_all = list(calculate_jobs(root, schedule_all=True))
        expected = self.make_jobs('1.24.3', '1.20.11', '1.24')
        expected.extend(self.make_jobs('1.23.3', '1.20.11', '1.23'))
        self.assertItemsEqual(jobs, expected[:6])
        self.assertItemsEqual(jobs_schedule_all, expected)

    def test_calculate_jobs_ignore_1_26(self):
        with temp_dir() as root:
            release_path = os.path.join(root, 'old-juju', '1.20.11')
            os.makedirs(release_path)
            candidate_path = os.path.join(root, 'candidate', '1.24')
            os.makedirs(candidate_path)
            make_build_var_file(candidate_path, '1.24.3')
            candidate_path_2 = os.path.join(root, 'candidate', '1.26')
            os.makedirs(candidate_path_2)
            make_build_var_file(candidate_path_2, '1.26-aloha1')
            jobs = list(calculate_jobs(root))
        expected = self.make_jobs('1.24.3', '1.20.11', '1.24')
        self.assertItemsEqual(jobs, expected)

    def test_calculate_jobs_osx(self):
        with temp_dir() as root:
            release_path = os.path.join(root, 'old-juju', '1.20.11')
            os.makedirs(release_path)
            candidate_path = os.path.join(root, 'candidate', '1.24.4')
            os.makedirs(candidate_path)
            make_build_var_file(candidate_path, '1.24.4')
            jobs = list(calculate_jobs(root, schedule_all=False))
        expected = self.make_jobs('1.24.4', '1.20.11')
        self.assertItemsEqual(jobs, expected)

    def test_calculate_jobs_candidate_v2(self):
        with temp_dir() as root:
            release_path = os.path.join(root, 'old-juju', '1.20.11')
            os.makedirs(release_path)
            release_path = os.path.join(root, 'old-juju', '2.0.0')
            os.makedirs(release_path)
            candidate_path_2 = os.path.join(root, 'candidate', '2.0.1')
            os.makedirs(candidate_path_2)
            make_build_var_file(candidate_path_2, '2.0.1')
            jobs = list(calculate_jobs(root))
        expected = self.make_jobs('2.0.1',  '2.0.0')
        self.assertItemsEqual(jobs, expected)

    def test_calculate_jobs_candiade_v1_and_v2(self):
        with temp_dir() as root:
            release_path = os.path.join(root, 'old-juju', '1.20.11')
            os.makedirs(release_path)
            release_path = os.path.join(root, 'old-juju', '2.0.0')
            os.makedirs(release_path)
            candidate_path = os.path.join(root, 'candidate', '1.24.3')
            os.makedirs(candidate_path)
            make_build_var_file(candidate_path, '1.24.3')
            candidate_path_2 = os.path.join(root, 'candidate', '2.0.1')
            os.makedirs(candidate_path_2)
            make_build_var_file(candidate_path_2, '2.0.1')
            jobs = list(calculate_jobs(root))
        expected = self.make_jobs('2.0.1',  '2.0.0')
        expected.extend(self.make_jobs('1.24.3', '1.20.11'))
        self.assertItemsEqual(jobs, expected)

    def make_jobs(self, candidate, old_version, candidate_path=None):
        jobs = []
        for client_os in ('ubuntu', 'osx', 'windows'):
            for new_to_old in ('false', 'true'):
                jobs.append({
                    'candidate': candidate,
                    'candidate_path': candidate_path or candidate,
                    'client_os': client_os,
                    'new_to_old': new_to_old,
                    'old_version': old_version,
                    'revision_build': '2870'})
        return jobs


def make_build_var_file(dir_path, version, revision_build="2870"):
    build_vars = {"version": version, "revision_build": revision_build}
    file_path = os.path.join(dir_path, 'buildvars.json')
    with open(file_path, 'w') as json_file:
        json.dump(build_vars, json_file)
    return file_path