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

« back to all changes in this revision

Viewing changes to tests/test_schedule_reliability_tests.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 argparse import Namespace
2
 
from contextlib import contextmanager
3
 
from unittest import TestCase
4
 
 
5
 
from mock import (
6
 
    call,
7
 
    patch,
8
 
    )
9
 
 
10
 
from industrial_test import (
11
 
    FULL,
12
 
    suites,
13
 
    UPGRADE,
14
 
    )
15
 
from jujuci import Credentials
16
 
from schedule_reliability_tests import (
17
 
    build_job,
18
 
    parse_args,
19
 
    main,
20
 
    )
21
 
from test_utility import (
22
 
    make_candidate_dir,
23
 
    write_config,
24
 
    )
25
 
from tests import parse_error
26
 
from utility import temp_dir
27
 
 
28
 
 
29
 
class TestParseArgs(TestCase):
30
 
 
31
 
    def test_parse_args(self):
32
 
        args, credentials = parse_args([
33
 
            'foo', '--user', 'jrandom', '--password', 'password1'])
34
 
        self.assertEqual(
35
 
            args,
36
 
            Namespace(root_dir='foo', suite=[], jobs=None, user='jrandom',
37
 
                      password='password1'))
38
 
        self.assertEqual(credentials,
39
 
                         Credentials(user='jrandom', password='password1'))
40
 
 
41
 
    def test_parse_args_suite_supports_known_suites(self):
42
 
        for suite in suites.keys():
43
 
            self.assertEqual(
44
 
                parse_args([
45
 
                    'foo', '--suite', suite, '--user', 'u', '--password',
46
 
                    'p'])[0],
47
 
                Namespace(root_dir='foo', suite=[suite], jobs=None,
48
 
                          user='u', password='p'))
49
 
 
50
 
    def test_parse_args_bad_suite(self):
51
 
        with parse_error(self) as stderr:
52
 
            parse_args(['foo', '--suite', 'foo'])
53
 
        self.assertRegexpMatches(stderr.getvalue(),
54
 
                                 ".*invalid choice: 'foo'.*")
55
 
 
56
 
    def test_parse_args_multi_suite(self):
57
 
        args = parse_args(['foo', '--suite', FULL, '--suite', UPGRADE,
58
 
                           '--user=u', '--password=p'])[0]
59
 
        self.assertEqual(args.suite, [FULL, UPGRADE])
60
 
 
61
 
    def test_parse_jobs(self):
62
 
        self.assertEqual(
63
 
            parse_args(['foo', 'bar', '--user', 'jrandom', '--password',
64
 
                        'password1'])[0],
65
 
            Namespace(root_dir='foo', suite=[], jobs=['bar'], user='jrandom',
66
 
                      password='password1'))
67
 
        self.assertEqual(
68
 
            parse_args(['foo', 'bar', 'baz', '--user', 'jrandom',
69
 
                        '--password', 'password1'])[0],
70
 
            Namespace(root_dir='foo', suite=[], jobs=['bar', 'baz'],
71
 
                      user='jrandom', password='password1'))
72
 
 
73
 
 
74
 
class TestBuildJob(TestCase):
75
 
 
76
 
    def test_build_job(self):
77
 
        jenkins_cxt = patch('schedule_reliability_tests.Jenkins')
78
 
        with jenkins_cxt as jenkins_mock, temp_dir() as root:
79
 
            write_config(root, 'foo', 'quxxx')
80
 
            build_job(Credentials('jrandom', 'password1'), root, 'foo',
81
 
                      [('bar', 1), ('baz', 2)], ['qux'])
82
 
        jenkins_mock.assert_called_once_with(
83
 
            'http://localhost:8080', 'jrandom', 'password1')
84
 
        calls = jenkins_mock.return_value.build_job.mock_calls
85
 
        expected = [
86
 
            call('foo', {
87
 
                'suite': 'qux',
88
 
                'attempts': '10',
89
 
                'new_juju_dir': 'bar',
90
 
                'revision_build': '1',
91
 
                }, token='quxxx'),
92
 
            call('foo', {
93
 
                'suite': 'qux',
94
 
                'attempts': '10',
95
 
                'new_juju_dir': 'baz',
96
 
                'revision_build': '2'
97
 
                }, token='quxxx'),
98
 
            ]
99
 
        self.assertEqual(calls, expected)
100
 
 
101
 
    def test_build_job_multi_suite(self):
102
 
        jenkins_cxt = patch('schedule_reliability_tests.Jenkins')
103
 
        with jenkins_cxt as jenkins_mock, temp_dir() as root:
104
 
            write_config(root, 'foo', 'bar')
105
 
            build_job(Credentials('jrandom', 'password1'), root, 'foo',
106
 
                      [('baz', 1)], ['qux', 'quxx'])
107
 
        jenkins_mock.return_value.build_job.assert_called_once_with(
108
 
            'foo', {
109
 
                'suite': 'qux,quxx',
110
 
                'attempts': '10',
111
 
                'new_juju_dir': 'baz',
112
 
                'revision_build': '1',
113
 
                }, token='bar')
114
 
 
115
 
 
116
 
class TestMain(TestCase):
117
 
 
118
 
    @contextmanager
119
 
    def build_job_context(self):
120
 
        with temp_dir() as root:
121
 
            write_config(root, 'foo', 'quxxx')
122
 
            yield root
123
 
 
124
 
    def run_main(self, root):
125
 
        with patch('jenkins.Jenkins.build_job') as build_job_mock:
126
 
            main([root, 'foo', '--user', 'bar', '--password', 'baz'])
127
 
        return build_job_mock
128
 
 
129
 
    def test_selects_newest_candidate(self):
130
 
        with self.build_job_context() as root:
131
 
            path_1234 = make_candidate_dir(
132
 
                root, '1234', 'mybranch', '1234')
133
 
            make_candidate_dir(root, '1233', 'mybranch', '1233')
134
 
            build_job_mock = self.run_main(root)
135
 
        build_job_mock.assert_called_once_with('foo', {
136
 
            'new_juju_dir': path_1234,
137
 
            'attempts': '10',
138
 
            'suite': 'full',
139
 
            'revision_build': '1234',
140
 
            }, token='quxxx')
141
 
 
142
 
    def test_limit_3(self):
143
 
        # Even though it's only testing the latest, it should only test 3,
144
 
        # even if there are more than 3 latest.
145
 
        with self.build_job_context() as root:
146
 
            make_candidate_dir(
147
 
                root, 'branch1', 'mybranch1')
148
 
            make_candidate_dir(
149
 
                root, 'branch2', 'mybranch2')
150
 
            make_candidate_dir(
151
 
                root, 'branch3', 'mybranch3')
152
 
            make_candidate_dir(
153
 
                root, 'branch4', 'mybranch4')
154
 
            build_job_mock = self.run_main(root)
155
 
        self.assertEqual(build_job_mock.call_count, 3)