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

« back to all changes in this revision

Viewing changes to tests/test_run_download_juju.py

  • Committer: Aaron Bentley
  • Date: 2015-07-27 19:19:56 UTC
  • mto: This revision was merged to the branch mainline in revision 1048.
  • Revision ID: aaron.bentley@canonical.com-20150727191956-uz4gdxuv28qhvv94
Use per-client JUJU_HOME.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from argparse import Namespace
2
 
import os
3
 
from tempfile import NamedTemporaryFile
4
 
 
5
 
from mock import patch, call
6
 
 
7
 
from run_download_juju import (
8
 
    create_workspace_yaml,
9
 
    get_revisions,
10
 
    parse_args,
11
 
    main
12
 
)
13
 
from jujupy import ensure_dir
14
 
from tests import (
15
 
    TestCase,
16
 
)
17
 
from test_schedule_hetero_control import make_build_var_file
18
 
from tests import parse_error
19
 
from utility import temp_dir
20
 
 
21
 
 
22
 
class TestGetCandidateInfo(TestCase):
23
 
 
24
 
    def test_get_candidate_info(self):
25
 
        with temp_dir() as dir_path:
26
 
            candidate_dir = os.path.join(dir_path, 'candidate')
27
 
            ensure_dir(candidate_dir)
28
 
            for ver, rev in [['1.24.3', '2870'], ['1.24.5', '2999']]:
29
 
                ver_dir = os.path.join(candidate_dir, ver)
30
 
                ensure_dir(ver_dir)
31
 
                make_build_var_file(ver_dir, version=ver, revision_build=rev)
32
 
            rev = get_revisions(dir_path)
33
 
        self.assertEqual(sorted(rev), ['2870', '2999'])
34
 
 
35
 
    def test_create_workspace_yaml(self):
36
 
        with NamedTemporaryFile() as temp_file:
37
 
            create_workspace_yaml("/my/home", "/script/path", temp_file)
38
 
            with open(temp_file.name) as yaml_file:
39
 
                content = yaml_file.read()
40
 
        self.assertEqual(content, self.expected())
41
 
 
42
 
    def test_create_workspace_yaml_with_rev(self):
43
 
        with NamedTemporaryFile() as temp_file:
44
 
            create_workspace_yaml(
45
 
                "/my/home", "/script/path", temp_file, ['2870', '2999'])
46
 
            with open(temp_file.name) as yaml_file:
47
 
                content = yaml_file.read()
48
 
        self.assertEqual(content, self.expected('-c 2870 2999'))
49
 
 
50
 
    def test_parse_args_default(self):
51
 
        os.environ['JUJU_HOME'] = '/juju/home'
52
 
        args = parse_args([])
53
 
        expected_args = Namespace(
54
 
            osx_host='jenkins@osx-slave.vapour.ws',
55
 
            win_host='Administrator@win-slave.vapour.ws',
56
 
            juju_home='/juju/home')
57
 
        self.assertEqual(args, expected_args)
58
 
 
59
 
    def test_parse_args(self):
60
 
        args = parse_args(['-o', 'j@my-osx-host', '-w', 'j@my-win-host',
61
 
                           '-j' '/juju/home'])
62
 
        expected_args = Namespace(
63
 
            osx_host='j@my-osx-host', win_host='j@my-win-host',
64
 
            juju_home='/juju/home')
65
 
        self.assertEqual(args, expected_args)
66
 
 
67
 
    def test_parse_args_juju_home_unset(self):
68
 
        with parse_error(self) as stderr:
69
 
            parse_args([])
70
 
        self.assertRegexpMatches(stderr.getvalue(), 'Invalid JUJU_HOME value')
71
 
 
72
 
    def test_main(self):
73
 
        with temp_dir() as home:
74
 
            os.environ['HOME'] = home
75
 
            juju_home = os.path.join(home, 'juju')
76
 
            os.mkdir(juju_home)
77
 
            os.environ['JUJU_HOME'] = juju_home
78
 
            temp_file = NamedTemporaryFile(delete=False)
79
 
            with patch('run_download_juju.NamedTemporaryFile', autospec=True,
80
 
                       return_value=temp_file) as ntf_mock:
81
 
                with patch('subprocess.check_output') as sco_mock:
82
 
                    main([])
83
 
            calls = [
84
 
                call(['workspace-run', temp_file.name,
85
 
                      'Administrator@win-slave.vapour.ws']),
86
 
                call(['workspace-run', temp_file.name,
87
 
                      'jenkins@osx-slave.vapour.ws'])]
88
 
        self.assertEqual(sco_mock.call_args_list, calls)
89
 
        ntf_mock.assert_called_once_with()
90
 
 
91
 
    def test_main_expected_arguments(self):
92
 
        os.environ['HOME'] = '/home/fake-juju-user'
93
 
        with temp_dir() as juju_home:
94
 
            os.environ['JUJU_HOME'] = juju_home
95
 
            temp_file = NamedTemporaryFile(delete=False)
96
 
            with patch('run_download_juju.get_revisions',
97
 
                       autospec=True) as gr_mock:
98
 
                with patch('run_download_juju.create_workspace_yaml',
99
 
                           autospec=True) as cwy_mock:
100
 
                    with patch('run_download_juju.NamedTemporaryFile',
101
 
                               autospec=True,
102
 
                               return_value=temp_file) as ntf_mock:
103
 
                        with patch('subprocess.check_output') as sco_mock:
104
 
                            main([])
105
 
            sco_calls = [
106
 
                call(['workspace-run', temp_file.name,
107
 
                      'Administrator@win-slave.vapour.ws']),
108
 
                call(['workspace-run', temp_file.name,
109
 
                      'jenkins@osx-slave.vapour.ws'])]
110
 
            cwy_calls = [
111
 
                call(juju_home,
112
 
                     ('C:\\\\Users\\\\Administrator\\\\juju-ci-tools'
113
 
                      '\\\\download_juju.py'),
114
 
                     temp_file, gr_mock.return_value),
115
 
                call(juju_home, '$HOME/juju-ci-tools/download_juju.py',
116
 
                     temp_file, gr_mock.return_value)]
117
 
        self.assertEqual(sco_mock.call_args_list, sco_calls)
118
 
        ntf_mock.assert_called_once_with()
119
 
        gr_mock.assert_called_once_with(os.environ['HOME'])
120
 
        self.assertEqual(cwy_mock.call_args_list, cwy_calls)
121
 
 
122
 
    def expected(self, rev="''"):
123
 
        return ("command: [python, /script/path, cloud-city, -r, -v, {}]\n"
124
 
                "install:\n"
125
 
                "  cloud-city: [/my/home/ec2rc]\n".format(rev))