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

« back to all changes in this revision

Viewing changes to tests/test_assess_resources.py

  • Committer: Aaron Bentley
  • Date: 2015-01-19 15:32:33 UTC
  • mto: This revision was merged to the branch mainline in revision 804.
  • Revision ID: aaron.bentley@canonical.com-20150119153233-jjcvikwiw1dx2lak
Print error on missing environment.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import logging
2
 
from argparse import Namespace
3
 
from mock import Mock, patch, call
4
 
import StringIO
5
 
 
6
 
from assess_resources import (
7
 
    assess_resources,
8
 
    large_assess,
9
 
    parse_args,
10
 
    push_resource,
11
 
    main,
12
 
    verify_status,
13
 
)
14
 
from tests import (
15
 
    parse_error,
16
 
    TestCase,
17
 
)
18
 
from utility import JujuAssertionError
19
 
 
20
 
 
21
 
class TestParseArgs(TestCase):
22
 
 
23
 
    def test_common_args(self):
24
 
        args = parse_args(
25
 
            ["an-env",
26
 
             "/bin/juju",
27
 
             "/tmp/logs",
28
 
             "an-env-mod"])
29
 
        self.assertEqual("an-env", args.env)
30
 
        self.assertEqual("/bin/juju", args.juju_bin)
31
 
        self.assertEqual("/tmp/logs", args.logs)
32
 
        self.assertEqual("an-env-mod", args.temp_env_name)
33
 
        self.assertEqual(False, args.debug)
34
 
 
35
 
    def test_help(self):
36
 
        fake_stdout = StringIO.StringIO()
37
 
        with parse_error(self) as fake_stderr:
38
 
            with patch("sys.stdout", fake_stdout):
39
 
                parse_args(["--help"])
40
 
        self.assertEqual("", fake_stderr.getvalue())
41
 
        self.assertNotIn("TODO", fake_stdout.getvalue())
42
 
 
43
 
 
44
 
class TestMain(TestCase):
45
 
 
46
 
    def test_main(self):
47
 
        argv = [
48
 
            "an-env",
49
 
            "/bin/juju",
50
 
            "/tmp/logs",
51
 
            "an-env-mod",
52
 
            "--verbose",
53
 
            ]
54
 
        client = Mock(spec=["is_jes_enabled"])
55
 
        with patch("assess_resources.configure_logging",
56
 
                   autospec=True) as mock_cl:
57
 
            with patch("assess_resources.BootstrapManager.booted_context",
58
 
                       autospec=True) as mock_bc:
59
 
                    with patch("deploy_stack.client_from_config",
60
 
                               return_value=client) as mock_c:
61
 
                        with patch("assess_resources.assess_resources",
62
 
                                   autospec=True) as mock_assess:
63
 
                            main(argv)
64
 
        mock_cl.assert_called_once_with(logging.DEBUG)
65
 
        mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False,
66
 
                                       soft_deadline=None)
67
 
        self.assertEqual(mock_bc.call_count, 1)
68
 
        mock_assess.assert_called_once_with(client, make_args())
69
 
 
70
 
 
71
 
class TestAssessResources(TestCase):
72
 
 
73
 
    def test_verify_status(self):
74
 
        verify_status(
75
 
            make_resource_list(), 'dummy-resource/foo', 'foo', '1234', 27)
76
 
 
77
 
    def test_verify_status_serviceid(self):
78
 
        verify_status(make_resource_list('serviceid'), 'dummy-resource/foo',
79
 
                      'foo', '1234', 27)
80
 
 
81
 
    def test_verify_status_exception(self):
82
 
        status = make_resource_list()
83
 
        status['resources'][0]['expected']['origin'] = 'charmstore'
84
 
        with self.assertRaisesRegexp(
85
 
                JujuAssertionError, 'Unexpected resource list values'):
86
 
            verify_status(status, 'dummy-resource/foo', 'foo', '1234', 27)
87
 
 
88
 
    def test_verify_status_unit_exception(self):
89
 
        status = make_resource_list()
90
 
        status['resources'][0]['unit']['origin'] = 'charmstore'
91
 
        with self.assertRaisesRegexp(
92
 
                JujuAssertionError, 'Unexpected unit resource list values'):
93
 
            verify_status(status, 'dummy-resource/foo', 'foo', '1234', 27)
94
 
 
95
 
    def test_verify_status_no_resoruce_id_exception(self):
96
 
        with self.assertRaisesRegexp(
97
 
                JujuAssertionError, 'Resource id not found.'):
98
 
            verify_status(make_resource_list(), 'NO_ID', 'foo', '1234', 27)
99
 
 
100
 
    def test_push_resource(self):
101
 
        mock_client = Mock(
102
 
            spec=["deploy", "wait_for_started", "list_resources",
103
 
                  "wait_for_resource", "show_status"])
104
 
        mock_client.version = '2.0.0'
105
 
        mock_client.list_resources.return_value = make_resource_list()
106
 
        push_resource(mock_client, 'foo', '1234', 27, 1800, 1800)
107
 
        mock_client.deploy.assert_called_once_with(
108
 
            'dummy-resource', resource='foo=dummy-resource/foo.txt')
109
 
        mock_client.wait_for_started.assert_called_once_with(timeout=1800)
110
 
        mock_client.show_status.assert_called_once_with()
111
 
 
112
 
    def test_push_resource_attach(self):
113
 
        mock_client = Mock(
114
 
            spec=["attach", "wait_for_started", "list_resources",
115
 
                  "wait_for_resource", "show_status"])
116
 
        mock_client.version = '2.0.0'
117
 
        mock_client.list_resources.return_value = make_resource_list()
118
 
        push_resource(mock_client, 'foo', '1234', 27, 1800, 1800, deploy=False)
119
 
        mock_client.attach.assert_called_once_with(
120
 
            'dummy-resource', resource='foo=dummy-resource/foo.txt')
121
 
        mock_client.wait_for_started.assert_called_once_with(timeout=1800)
122
 
 
123
 
    def test_assess_resources(self):
124
 
        fake_file = FakeFile()
125
 
        args = make_args()
126
 
        with patch("assess_resources.push_resource", autospec=True) as mock_p:
127
 
            with patch('assess_resources.NamedTemporaryFile',
128
 
                       autospec=True) as mock_ntf:
129
 
                mock_ntf.return_value.__enter__.return_value = fake_file
130
 
                assess_resources(None, args)
131
 
        calls = [
132
 
            call(
133
 
                None, 'foo',
134
 
                '4ddc48627c6404e538bb0957632ef68618c0839649d9ad9e41ad94472c158'
135
 
                '9f4b7f9d830df6c4b209d7eb1b4b5522c4d', 27, 1800, 1800),
136
 
            call(
137
 
                None, 'bar',
138
 
                'ffbf43d68a6960de63908bb05c14a026abeda136119d3797431bdd7b469c1'
139
 
                'f027e57a28aeec0df01a792e9e70aad2d6b', 17, 1800, 1800,
140
 
                deploy=False),
141
 
            call(
142
 
                None, 'bar',
143
 
                '2a3821585efcccff1562efea4514dd860cd536441954e182a7649910e21f6'
144
 
                'a179a015677a68a351a11d3d2f277e551e4', 27, 1800, 1800,
145
 
                deploy=False, resource_file='baz.txt'),
146
 
            call(
147
 
                None, 'bar',
148
 
                '3164673a8ac27576ab5fc06b9adc4ce0aca5bd3025384b1cf2128a8795e74'
149
 
                '7c431e882785a0bf8dc70b42995db388575', 1024 * 1024, 1800, 1800,
150
 
                deploy=False, resource_file='/tmp/fake'),
151
 
        ]
152
 
        self.assertEqual(mock_p.mock_calls, calls)
153
 
 
154
 
    def test_assess_resources_large_test(self):
155
 
        fake_file = FakeFile()
156
 
        args = make_args()
157
 
        args.large_test_enabled = True
158
 
        with patch("assess_resources.push_resource", autospec=True) as mock_p:
159
 
            with patch('assess_resources.fill_dummy_file',
160
 
                       autospec=True) as mock_fdf:
161
 
                with patch('assess_resources.NamedTemporaryFile',
162
 
                           autospec=True) as mock_ntf:
163
 
                    mock_ntf.return_value.__enter__.return_value = fake_file
164
 
                    with patch('assess_resources.large_assess',
165
 
                               autospec=True) as mock_lt:
166
 
                        assess_resources(None, args)
167
 
        calls = [
168
 
            call(
169
 
                None, 'foo',
170
 
                '4ddc48627c6404e538bb0957632ef68618c0839649d9ad9e41ad94472c158'
171
 
                '9f4b7f9d830df6c4b209d7eb1b4b5522c4d', 27, 1800, 1800),
172
 
            call(
173
 
                None, 'bar',
174
 
                'ffbf43d68a6960de63908bb05c14a026abeda136119d3797431bdd7b469c1'
175
 
                'f027e57a28aeec0df01a792e9e70aad2d6b', 17, 1800, 1800,
176
 
                deploy=False),
177
 
            call(
178
 
                None, 'bar',
179
 
                '2a3821585efcccff1562efea4514dd860cd536441954e182a7649910e21f6'
180
 
                'a179a015677a68a351a11d3d2f277e551e4', 27, 1800, 1800,
181
 
                deploy=False, resource_file='baz.txt'),
182
 
            call(None, 'bar',
183
 
                 '3164673a8ac27576ab5fc06b9adc4ce0aca5bd3025384b1cf2128a8795e7'
184
 
                 '47c431e882785a0bf8dc70b42995db388575', 1024 * 1024, 1800,
185
 
                 1800, deploy=False, resource_file='/tmp/fake')]
186
 
        self.assertEqual(mock_p.mock_calls, calls)
187
 
        mock_fdf.assert_called_once_with('/tmp/fake', size=1024 * 1024)
188
 
        mock_lt.assert_called_once_with(None, 1800, 1800)
189
 
 
190
 
    def test_large_tests(self):
191
 
        fake_file = FakeFile()
192
 
        with patch("assess_resources.push_resource", autospec=True) as mock_pr:
193
 
            with patch('assess_resources.NamedTemporaryFile',
194
 
                       autospec=True) as mock_ntf:
195
 
                mock_ntf.return_value.__enter__.return_value = fake_file
196
 
                with patch('assess_resources.fill_dummy_file',
197
 
                           autospec=True):
198
 
                    large_assess(None, 1800, 1800)
199
 
        calls = [
200
 
            call(
201
 
                None, 'bar',
202
 
                'd7c014629d74ae132cc9f88e3ec2f31652f40a7a1fcc52c54b04d6c0d0891'
203
 
                '69bcd55958d1277b4cdf6262f21c712d0a7', 1024 * 1024 * 10, 1800,
204
 
                1800, deploy=False, resource_file='/tmp/fake'),
205
 
            call(
206
 
                None, 'bar',
207
 
                'c11e93892b66de781e4d0883efe10482f8d1642f3b6574ba2ee0da6f8db03'
208
 
                'f53c0eadfb5e5e0463574c113024ded369e', 1024 * 1024 * 100, 1800,
209
 
                1800, deploy=False, resource_file='/tmp/fake'),
210
 
            call(
211
 
                None, 'bar',
212
 
                '77db39eca74c6205e31a7701e488a1df4b9b38a527a6084bdbb6843fd430a'
213
 
                '0b51047378ee0255e633b32c0dda3cf43ab', 1024 * 1024 * 200, 1800,
214
 
                1800, deploy=False, resource_file='/tmp/fake')]
215
 
        self.assertEqual(mock_pr.mock_calls, calls)
216
 
 
217
 
 
218
 
def make_args():
219
 
    return Namespace(
220
 
        agent_stream=None, agent_timeout=1800, agent_url=None,
221
 
        bootstrap_host=None, debug=False, env='an-env', juju_bin='/bin/juju',
222
 
        keep_env=False, large_test_enabled=False, logs='/tmp/logs', machine=[],
223
 
        region=None, resource_timeout=1800, series=None,
224
 
        temp_env_name='an-env-mod', upload_tools=False, verbose=10,
225
 
        deadline=None,)
226
 
 
227
 
 
228
 
def make_resource_list(service_app_id='applicationId'):
229
 
    return {'resources': [{
230
 
        'expected': {
231
 
            'origin': 'upload', 'used': True, 'description': 'foo resource.',
232
 
            'username': 'admin@local', 'resourceid': 'dummy-resource/foo',
233
 
            'name': 'foo', service_app_id: 'dummy-resource', 'size': 27,
234
 
            'fingerprint': '1234', 'type': 'file', 'path': 'foo.txt'},
235
 
        'unit': {
236
 
            'origin': 'upload', 'username': 'admin@local', 'used': True,
237
 
            'name': 'foo', 'resourceid': 'dummy-resource/foo',
238
 
            service_app_id: 'dummy-resource', 'fingerprint': '1234',
239
 
            'path': 'foo.txt', 'size': 27, 'type': 'file',
240
 
            'description': 'foo resource.'}}]}
241
 
 
242
 
 
243
 
class FakeFile:
244
 
    name = '/tmp/fake'