Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 # This file is part of the Juju GUI, which lets users view and manage Juju | 1 # This file is part of the Juju GUI, which lets users view and manage Juju |
2 # environments within a graphical interface (https://launchpad.net/juju-gui). | 2 # environments within a graphical interface (https://launchpad.net/juju-gui). |
3 # Copyright (C) 2013 Canonical Ltd. | 3 # Copyright (C) 2013 Canonical Ltd. |
4 # | 4 # |
5 # This program is free software: you can redistribute it and/or modify it under | 5 # This program is free software: you can redistribute it and/or modify it under |
6 # the terms of the GNU Affero General Public License version 3, as published by | 6 # the terms of the GNU Affero General Public License version 3, as published by |
7 # the Free Software Foundation. | 7 # the Free Software Foundation. |
8 # | 8 # |
9 # This program is distributed in the hope that it will be useful, but WITHOUT | 9 # This program is distributed in the hope that it will be useful, but WITHOUT |
10 # ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | 10 # ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, |
11 # SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 # SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 # Affero General Public License for more details. | 12 # Affero General Public License for more details. |
13 # | 13 # |
14 # You should have received a copy of the GNU Affero General Public License | 14 # You should have received a copy of the GNU Affero General Public License |
15 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 15 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 |
17 """Tests for the deployment utility functions and objects.""" | 17 """Tests for the deployment utility functions and objects.""" |
18 | 18 |
19 import unittest | 19 import unittest |
20 | 20 |
21 from concurrent.futures import Future | 21 from concurrent.futures import Future |
22 import mock | 22 import mock |
23 from tornado import gen | 23 from tornado import gen |
24 from tornado.testing import( | 24 from tornado.testing import( |
25 AsyncTestCase, | 25 AsyncTestCase, |
26 ExpectLog, | 26 ExpectLog, |
27 gen_test, | 27 gen_test, |
28 LogTrapTestCase, | 28 LogTrapTestCase, |
29 ) | 29 ) |
30 import urllib | |
30 | 31 |
31 from guiserver import watchers | 32 from guiserver import watchers |
32 from guiserver.bundles import utils | 33 from guiserver.bundles import utils |
33 from guiserver.tests import helpers | 34 from guiserver.tests import helpers |
34 | 35 |
35 | 36 |
36 mock_time = mock.patch('time.time', mock.Mock(return_value=12345)) | 37 mock_time = mock.patch('time.time', mock.Mock(return_value=12345)) |
37 | 38 |
38 | 39 |
39 @mock_time | 40 @mock_time |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
440 return future | 441 return future |
441 return fetch | 442 return fetch |
442 | 443 |
443 | 444 |
444 class TestIncrementDeploymentCounter(LogTrapTestCase, AsyncTestCase): | 445 class TestIncrementDeploymentCounter(LogTrapTestCase, AsyncTestCase): |
445 | 446 |
446 @gen_test | 447 @gen_test |
447 def test_no_cw_url_returns_true(self): | 448 def test_no_cw_url_returns_true(self): |
448 bundle_id = '~bac/muletrain/wiki' | 449 bundle_id = '~bac/muletrain/wiki' |
449 mock_path = 'tornado.httpclient.AsyncHTTPClient.fetch' | 450 mock_path = 'tornado.httpclient.AsyncHTTPClient.fetch' |
450 with mock.patch(mock_path): | 451 with mock.patch(mock_path) as mock_fetch: |
frankban
2013/11/15 08:48:20
as mock_fetch...
bac
2013/11/15 12:45:10
Done.
| |
451 ok = yield utils.increment_deployment_counter(bundle_id, None) | 452 ok = yield utils.increment_deployment_counter(bundle_id, None) |
frankban
2013/11/15 08:48:20
self.assertFalse(mock_fetch.called)?
bac
2013/11/15 12:45:10
Nice addition.
| |
452 self.assertTrue(ok) | 453 self.assertFalse(ok) |
454 self.assertFalse(mock_fetch.called) | |
455 | |
456 @gen_test | |
457 def test_increment_nonstring_bundle_id(self): | |
458 bundle_id = 4 | |
459 cw_url = 'http://my.charmworld.example.com/' | |
460 mock_path = 'tornado.httpclient.AsyncHTTPClient.fetch' | |
461 with mock.patch(mock_path) as mock_fetch: | |
462 ok = yield utils.increment_deployment_counter(bundle_id, cw_url) | |
463 self.assertFalse(ok) | |
464 self.assertFalse(mock_fetch.called) | |
465 | |
466 @gen_test | |
467 def test_increment_nonstring_cwurl(self): | |
468 bundle_id = u'~bac/muletrain/wiki' | |
469 cw_url = 7 | |
470 mock_path = 'tornado.httpclient.AsyncHTTPClient.fetch' | |
471 with mock.patch(mock_path) as mock_fetch: | |
472 ok = yield utils.increment_deployment_counter(bundle_id, cw_url) | |
473 self.assertFalse(ok) | |
474 self.assertFalse(mock_fetch.called) | |
453 | 475 |
454 @gen_test | 476 @gen_test |
455 def test_increment_url_logged(self): | 477 def test_increment_url_logged(self): |
456 bundle_id = '~bac/muletrain/wiki' | 478 bundle_id = '~bac/muletrain/wiki' |
457 cw_url = 'http://my.charmworld.example.com/' | 479 cw_url = 'http://my.charmworld.example.com/' |
458 url = '{}api/3/bundle/{}/metric/deployments/increment'.format( | 480 url = u'{}api/3/bundle/{}/metric/deployments/increment'.format( |
459 cw_url, bundle_id) | 481 cw_url, bundle_id) |
460 expected = 'Incrementing bundle.+\n{}.'.format(url) | 482 expected = 'Incrementing bundle.+' |
461 called_with = [] | 483 called_with = [] |
462 mock_fetch = mock_fetch_factory(200, called_with) | 484 mock_fetch = mock_fetch_factory(200, called_with) |
463 with ExpectLog('', expected, required=True): | 485 with ExpectLog('', expected, required=True): |
464 mock_path = 'tornado.httpclient.AsyncHTTPClient.fetch' | 486 mock_path = 'tornado.httpclient.AsyncHTTPClient.fetch' |
465 with mock.patch(mock_path, mock_fetch): | 487 with mock.patch(mock_path, mock_fetch): |
466 ok = yield utils.increment_deployment_counter( | 488 ok = yield utils.increment_deployment_counter( |
467 bundle_id, cw_url) | 489 bundle_id, cw_url) |
468 self.assertTrue(ok) | 490 self.assertTrue(ok) |
469 called_args, called_kwargs = called_with[0] | 491 called_args, called_kwargs = called_with[0] |
470 self.assertEqual(url, called_args[0]) | 492 self.assertEqual(url, urllib.unquote(called_args[0])) |
471 self.assertEqual(dict(callback=None), called_kwargs) | 493 self.assertEqual(dict(callback=None), called_kwargs) |
472 | 494 |
473 @gen_test | 495 @gen_test |
474 def test_increment_errors(self): | 496 def test_increment_errors(self): |
475 bundle_id = '~bac/muletrain/wiki' | 497 bundle_id = '~bac/muletrain/wiki' |
476 cw_url = 'http://my.charmworld.example.com/' | 498 cw_url = 'http://my.charmworld.example.com/' |
477 mock_path = 'tornado.httpclient.AsyncHTTPClient.fetch' | 499 mock_path = 'tornado.httpclient.AsyncHTTPClient.fetch' |
478 mock_fetch = mock_fetch_factory(404) | 500 mock_fetch = mock_fetch_factory(404) |
479 with mock.patch(mock_path, mock_fetch): | 501 with mock.patch(mock_path, mock_fetch): |
480 ok = yield utils.increment_deployment_counter(bundle_id, cw_url) | 502 ok = yield utils.increment_deployment_counter(bundle_id, cw_url) |
481 self.assertFalse(ok) | 503 self.assertFalse(ok) |
LEFT | RIGHT |