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

« back to all changes in this revision

Viewing changes to tests/test_schedule_reliability_tests.py

  • Committer: Aaron Bentley
  • Date: 2014-02-24 17:18:29 UTC
  • mto: This revision was merged to the branch mainline in revision 252.
  • Revision ID: aaron.bentley@canonical.com-20140224171829-sz644yhoygu7m9dm
Use tags to identify and shut down instances.

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